Search code examples
xsdxsd-validation

how to set the order of elements with attributes in XSD


is it possible to define the order of elements?

example I have the following XML file

<input>
   <action no="1" type="xyz"/>
   <action no="2" type="abc"/>
   <action no="3" type="ddd"/>
</input>

An XSD check should fail when

  • attribute no does not start at 1
  • the attributes are not ordered 1, 2, 3 ...
  • there are numbers missing like 1, 3, 4

Thats my current XSD

<complexType name="action">
    <attribute name="no" use="required">
        <simpleType>
            <restriction base="int">
                <minInclusive value="1"></minInclusive>
                <maxInclusive value="99"></maxInclusive>
                <whiteSpace value="collapse"></whiteSpace>
            </restriction>
        </simpleType>
    </attribute>
    <attribute name="type" type="string"></attribute>
</complexType>

<complexType name="input">
    <sequence>
        <element name="action" type="tns:action"></element>
    </sequence>
</complexType>

Solution

  • In XSD 1.1 you could use an assertion:

     <xs:element name="input">
        <xs:complexType>
          <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="action"/>
          </xs:sequence>
          <xs:assert id="sequence-of-no-values" test="every $i in 1 to count(action) satisfies $i eq action[$i]/@no"/>
        </xs:complexType>
      </xs:element>