Search code examples
xmlxsddtd

cvc-elt.1: Cannot find the declaration of element 'data'


These are my simple XSD and XML files, I keep getting cvc-elt.1 for the "data" node.

Here is the XML

<?xml version="1.0" encoding="UTF-8" ?>
<data xmlns="http://www.w3schools.com" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="https://www.w3schools.com/xml {FULL_PATH}/car_designer.xsd">
    <car_designer id="1" designer_name="A C Bertelli"/>
    <car_designer id="2" designer_name="Adam Ty Dean Smith"/>
</data>

Here is the XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="https://www.w3schools.com"
           xmlns="https://www.w3schools.com"
           elementFormDefault="qualified">
    <xs:element name="data">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="car_designer" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:attribute name="id" type="xs:int"></xs:attribute>
                        <xs:attribute name="designer_name" type="xs:string"></xs:attribute>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Solution

  • The problem is that the default namespace in the XML file is http://www.w3schools.com, but the targetNamespace in the schema is https://www.w3schools.com.

    Notice the difference between http and https in the uri. If you change the namespace in the XML to https (xmlns="https://www.w3schools.com"), it should work.