Search code examples
xmlxsdxsd-validationxml-validation

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


I'm following a basic book learning XML. Following the book exactly and editing the XML slightly gave me this error:

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

XML:

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

<games
  xmlns="http://tempuri.org/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://tempuri.org game.xsd">
  <game>
    <title>Red Dead Redeption Two</title>
    <platform>Xbox One</platform>
    <desc>Red Dead Redeption Two is a western action adventure game</desc>
    <img>Cover.png</img>
  </game>
</games>

XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
targetNamespace="http://tempuri.org"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema games.xsd">

  <xs:element name="games">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="game" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="title" type="xs:string"/>
              <xs:element name="platform" type="xs:string"/>
              <xs:element name="desc" type="xs:string"/>
              <xs:element name="img" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Solution

  • XSD

    Change

    xmlns:xs="http://www.w3.org/2001/XMLSchema games.xsd"
    

    to

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

    because the latter is the actual definition of the XMLSchema namespace, and the former appears to be a misguided attempt at following the form of a schemaLocation attribute.

    Delete the following:

    xmlns="http://tempuri.org/XMLSchema.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
    

    because the default namespace of the XSD should not be http://tempuri.org/XMLSchema.xsd and because the mstns namespace prefix declaration is not needed.

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema
        targetNamespace="http://tempuri.org"
        elementFormDefault="qualified"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
      <xs:element name="games">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="game" maxOccurs="unbounded">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="title" type="xs:string"/>
                  <xs:element name="platform" type="xs:string"/>
                  <xs:element name="desc" type="xs:string"/>
                  <xs:element name="img" type="xs:string"/>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    XML

    Change

    xmlns="http://tempuri.org/"
    

    to be

    xmlns="http://tempuri.org"
    

    to match both the targetNamespace in the XSD and the schemaLocation in the XML.

    <?xml version="1.0" encoding="utf-8"?>
    <games
      xmlns="http://tempuri.org"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://tempuri.org games.xsd">
      <game>
        <title>Red Dead Redeption Two</title>
        <platform>Xbox One</platform>
        <desc>Red Dead Redeption Two is a western action adventure game</desc>
        <img>Cover.png</img>
      </game>
    </games>
    

    After making the above changes, your XML will validate against your XSD successfully.