Search code examples
xmlxsdxsd-validationxml-validation

XSD: Error: Element 'attribute' is invalid, misplaced, or occurs too often


I get the following error on my XSD document:

Element 'attribute' is invalid, misplaced, or occurs too often.

and I have no idea why.

Here is my XSD file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="bookcollection">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" maxOccurs="unbounded">
          <xs:complexType mixed="true">
            <xs:sequence>
              <xs:element name="name" type="xs:string"/>
              <xs:element name="image" type="xs:anyURI"/>
              <xs:element name="quantity" type="xs:string"/>
              <xs:element name="price">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:decimal"/>
                    <xs:attribute name="currency" default="EUR"
                                  maxOccurs="unbounded"/> 
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:enumeration value="EUR"/>
                        <xs:enumeration value="DOLLARS"/>
                        <xs:enumeration value="ISK"/>
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element name="shipping" type="xs:string"/>  
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Solution

  • To correct that error, move the xs:attribute element within the xs:extension, not after it as it currently appears.

    The next error that will arise can be corrected by moving xs:simpleType within xs:attribute, not after it as it currently appears.

    Altogether, the following XSD has both corrections applied and has no further errors:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="bookcollection">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="book" maxOccurs="unbounded">
              <xs:complexType mixed="true">
                <xs:sequence>
                  <xs:element name="name" type="xs:string"/>
                  <xs:element name="image" type="xs:anyURI"/>
                  <xs:element name="quantity" type="xs:string"/>
                  <xs:element name="price">
                    <xs:complexType>
                      <xs:simpleContent>
                        <xs:extension base="xs:decimal">
                          <xs:attribute name="currency" default="EUR"> 
                            <xs:simpleType>
                              <xs:restriction base="xs:string">
                                <xs:enumeration value="EUR"/>
                                <xs:enumeration value="DOLLARS"/>
                                <xs:enumeration value="ISK"/>
                              </xs:restriction>
                            </xs:simpleType>
                          </xs:attribute>
                        </xs:extension>
                      </xs:simpleContent>
                    </xs:complexType>
                  </xs:element>
                  <xs:element name="shipping" type="xs:string"/>  
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    One final note, unless you truly want to allow text between the children elements of book, remove mixed="true" from its xs:complexType declaration.