XML Schema Validation : Cannot find the declaration of element
Asked Answered
P

4

36

I am still a bit new to XML Schema etc. and have been working to develop some XML, Schema and a Stylesheet (XSLT). I have made reasonable progress, but then realized that my Schema had stopped working, so I have taken it back to a simpler non-descript example.

Here is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="Test.Namespace"  
      schemaLocation="http://myNameSpace.com Test1.xsd">
    <element1 id="001">
        <element2 id="001.1">
             <element3 id="001.1" />
        </element2>
    </element1>
</Root>

I have written a Schema that is here:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="Test.Namespace"
            elementFormDefault="qualified">
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="element1" maxOccurs="unbounded" type="element1Type"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="element1Type">
        <xsd:sequence>
            <xsd:element name="element2" maxOccurs="unbounded" type="element2Type"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>

    <xsd:complexType name="element2Type">
        <xsd:sequence>
            <xsd:element name="item" type="element3Type"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>

    <xsd:complexType name="element3Type">
         <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>        
 </xsd:schema>

The Schema is representative of the structure of my real XML.

Now, when I try to validate my XML, I get this error:

cvc-elt.1: Cannot find the declaration of element 'Root'. [7]

This I think is to do with a namespace issue on the Root element, but I am really not sure.

Can someone suggest what I am doing wrong please.

Phosphoprotein answered 26/3, 2013 at 16:15 Comment(0)
R
26

cvc-elt.1: Cannot find the declaration of element 'Root'. [7]

Your schemaLocation attribute on the root element should be xsi:schemaLocation, and you need to fix it to use the right namespace.

You should probably change the targetNamespace of the schema and the xmlns of the document to http://myNameSpace.com (since namespaces are supposed to be valid URIs, which Test.Namespace isn't, though urn:Test.Namespace would be ok). Once you do that it should find the schema. The point is that all three of the schema's target namespace, the document's namespace, and the namespace for which you're giving the schema location must be the same.

(though it still won't validate as your <element2> contains an <element3> in the document where the schema expects item)

Radiosurgery answered 27/3, 2013 at 9:11 Comment(2)
I have corrected the following as you suggested and now it works The clarification of the 'Three' Namespaces was what I needed.Phosphoprotein
What is the meaning of noNameSpaceSchemaLocation ? When I copied the input.xml from Amazon Documentation, it had noNameSpaceSchemaLocation but it showed error. After reading this comment, I changed it to schemaLocation and now it does not show any error. But what was noNameSpaceSchemaLocation?Fayola
P
33

Thanks to everyone above, but this is now fixed. For the benefit of others the most significant error was in aligning the three namespaces as suggested by Ian.

For completeness, here is the corrected XML and XSD

Here is the XML, with the typos corrected (sorry for any confusion caused by tardiness)

<?xml version="1.0" encoding="UTF-8"?>

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="urn:Test.Namespace"  
      xsi:schemaLocation="urn:Test.Namespace Test1.xsd">
    <element1 id="001">
        <element2 id="001.1">
            <element3 id="001.1" />
        </element2>
    </element1>
</Root>

and, here is the Schema

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:Test.Namespace"
            xmlns="urn:Test.Namespace"
            elementFormDefault="qualified">
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="element1" maxOccurs="unbounded" type="element1Type"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
       
    <xsd:complexType name="element1Type">
        <xsd:sequence>
            <xsd:element name="element2" maxOccurs="unbounded" type="element2Type"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>
       
    <xsd:complexType name="element2Type">
        <xsd:sequence>
            <xsd:element name="element3" type="element3Type"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>

    <xsd:complexType name="element3Type">
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>        
</xsd:schema>

Thanks again to everyone, I hope this is of use to somebody else in the future.

Phosphoprotein answered 27/3, 2013 at 10:14 Comment(2)
If I may suggest a couple of improvements to your answer: you should un-accept my answer, wait a day I think, and then accept yours since technically speaking yours contains all the fixes; I would also fix the NID part of you URN which is invalid (remove the . which is invalid character for a NID) and also add the mandatory NSS part, separated by yet another :. The schemaLocation is just a hint, XSD aware validators are free to ignore it, if not for security reasons - which is why it should never be a reason why an XML is invalid or not.Stanhope
This helped me lot and I also learned namespace issue easily. Thank you.Anaglyph
R
26

cvc-elt.1: Cannot find the declaration of element 'Root'. [7]

Your schemaLocation attribute on the root element should be xsi:schemaLocation, and you need to fix it to use the right namespace.

You should probably change the targetNamespace of the schema and the xmlns of the document to http://myNameSpace.com (since namespaces are supposed to be valid URIs, which Test.Namespace isn't, though urn:Test.Namespace would be ok). Once you do that it should find the schema. The point is that all three of the schema's target namespace, the document's namespace, and the namespace for which you're giving the schema location must be the same.

(though it still won't validate as your <element2> contains an <element3> in the document where the schema expects item)

Radiosurgery answered 27/3, 2013 at 9:11 Comment(2)
I have corrected the following as you suggested and now it works The clarification of the 'Three' Namespaces was what I needed.Phosphoprotein
What is the meaning of noNameSpaceSchemaLocation ? When I copied the input.xml from Amazon Documentation, it had noNameSpaceSchemaLocation but it showed error. After reading this comment, I changed it to schemaLocation and now it does not show any error. But what was noNameSpaceSchemaLocation?Fayola
S
1

The targetNamespace of your XML Schema does not match the namespace of the Root element (dot in Test.Namespace vs. comma in Test,Namespace)

Once you make the above agree, you have to consider that your element2 has an attribute order that is not in your XSD.

Stanhope answered 27/3, 2013 at 1:8 Comment(1)
Thanks for this, and spotting two obvious mistakes. I have corrected these; 'code' <?xml version="1.0" encoding="UTF-8"?> <Root xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns="Test.Namespace" schemaLocation="myNameSpace.com Test1.xsd" > <element1 id="001"> <element2 id="001.1"> <element3 id="001.1" /> </element2> </element1> </Root> 'code' (Sorry, I don't seem to have inserted the code correctly). The problem is still there...Phosphoprotein
C
0

My mistake was to have more than one root node, which had been added by vscode/codium when I clicked on "Quick Fix":

enter image description here

and chose to show the XML schema in the second line (which is wrong, as this led to two start nodes):

enter image description here

and hovering over "Feed" (in your case "Root") showed me the error.

enter image description here

In the end, the schema validation code of xml against xsd showed that adding the additional second line is wrong XML. Instead, I should not mind the three gray dots/the quick fix in the first screenshot, the XML was right from the start.

Coaxial answered 25/3, 2022 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.