Search code examples
xmlxsdxsd-validationxml-validation

XSD problem - cannot find the declaration of root element


I've got a problem validating my XML file against my XML Schema. Everything works perfectly except it shows that it cannot find the declaration of countries root element.

Here is part of my XML file:

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

<countries xmlns="https://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="kraje.xsd">
    <!-- I don't paste everything that's inside the countries element 
         since it's not causing errors -->
</countries>

Here is part of my XSD file:

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="countries" type="countriesType"/>

    <xs:complexType name="countriesType">
        <xs:sequence>
            <xs:element ref="country"/>
        </xs:sequence>
    </xs:complexType>

</xs:schema>

Solution

  • XML

    Since your XML is in a https://www.w3schools.com namespace, change

    xsi:schemaLocation="kraje.xsd"
    

    to

    xsi:schemaLocation="https://www.w3schools.com kraje.xsd"
    

    XSD

    Add a matching targetNamespace,

    targetNamespace="https://www.w3schools.com"
    

    to the xs:schema root element.

    See also