Схема XML - не удается найти объявление элемента
XML-файл:
<?xml version="1.0" encoding="UTF-8"?>
<rootItem xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="schema.xsd">
<product category="software" type="individual" currentlyOffered="Y">
<tid>725</tid>
<tname>MS Office</tname>
<reviewDate>2017-12-05</reviewDate>
<note staffID="ee21kfj">Need to specify Windows version</note>
<note staffID="ef23mls">Is there a price update?</note>
</product>
<product category="hardware" type="individual" currentlyOffered="TBC">
<tid>511</tid>
<tname>Mouse</tname>
<reviewDate>2016-09-26</reviewDate>
<note staffID="fh26eij">Need to ensure ... minors</note>
<note staffID="mm25por">Need to check ... this</note>
</product>
</rootItem>
Схема XML:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple elements -->
<xs:element name="tid" type="xs:positiveInteger"/>
<xs:element name="tname" type="xs:string"/>
<xs:element name="reviewDate" type="xs:date"/>
<xs:element name="note" type="xs:string"/>
<!-- definition of attributes -->
<xs:attribute name="category" type="xs:string"/>
<xs:attribute name="type" type="xs:string"/>
<xs:attribute name="currentlyOffered" type="xs:string"/>
<xs:attribute name="staffID">
<xs:simpleType>
<!-- Some Rules -->
</xs:simpleType>
</xs:attribute>
<!-- definition of complex elements -->
<xs:element name="product">
<xs:complexType>
<xs:sequence>
<!-- Some Rules -->
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="rootItem">
<xs:complexType>
<!-- Some Rules -->
</xs:complexType>
</xs:element>
</xs:schema>
И ответ я получаю:
"Cannot find the declaration of element 'rootItem'.
Есть идеи?
1 ответ
Ваш экземпляр xml использует пространство имен по умолчанию xmlns="http://www.w3schools.com"
который не определен в вашей схеме. Я не знаю, какая часть ваших данных может быть изменена, но вы должны либо удалить пространство имен в XML, либо добавить его как <xs:element name="rootItem" xmlns="http://www.w3schools.com">
в вашей схеме.
(Посмотрите на комментарий Майкла Кея, где упоминается правильное действие для исправления файла схемы)
Добавить целевое пространство имен в схему:
<xs:schema targetNamespace="http://www.w3schools.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">