Search code examples
xmlxsdxsd-validationxml-validation

Element 'all' Is Invalid, Misplaced, Or Occurs Too Often


I am trying to allow the following XML pattern:

<Locales>
    <Locale Language="FR">
        <Name>La Jetée</Name>
    </Locale>
    <Locale Language="EN">
        <Name>The Jetty</Name>
    </Locale>
</Locales>

Here is the XSD I currently have, but it is giving an error about the attributes. When I remove the attributes it validates

<xs:element name="Locales" minOccurs="0">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Locale" maxOccurs="unbounded" minOccurs="1">
                <xs:complexType>
                    <xs:attribute name="Language" use="optional"/>
                    <xs:all>
                        <xs:element name="Name" type="xs:string" minOccurs="0"/>
                    </xs:all>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

The error I get is

Element 'all' Is Invalid, Misplaced, Or Occurs Too Often.


Solution

  • Your XSD is fine except that you have to move xs:all before xs:attribute; it may not appear after xs:attribute, thus the error.

    Here is your XSD fragment with the above change applied:

      <xs:element name="Locales">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Locale" maxOccurs="unbounded" minOccurs="1">
              <xs:complexType>
                <xs:all>
                  <xs:element name="Name" type="xs:string" minOccurs="0"/>
                </xs:all>
                <xs:attribute name="Language" use="optional"/>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    

    It will successfully validate your XML. Note that it also removes minOccurs="0" because occurrence constraints may not appear on top-level elements.