XML Schema - cannot find the declaration of element

6.7k Views Asked by At

XML File:

<?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 Schema:

<?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>

And the response I get is:

"Cannot find the declaration of element 'rootItem'.

Any ideas?

2

There are 2 best solutions below

0
On

Add target namespace to schema:

<xs:schema targetNamespace="http://www.w3schools.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
1
On

your xml instance uses the default namespace xmlns="http://www.w3schools.com" which is not defined in your schema. I don't know which part of your data is changeable, but you either have to remove the namespace in the XML or add it as <xs:element name="rootItem" xmlns="http://www.w3schools.com"> in your schema.

(Have a look at the comment of Michael Kay where the right action to correct the schema file is mentioned)