XSD schema: define an unordered sequence

publish on

When defining a sequence, the element are ordered following their declaration order. But it might be useful to have an unordered sequence in a XSD schema.

The following schema will define an unordered sequence of A, B or C element:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="SEQUENCE" type="sequenceType"/>
 <xs:complexType name="sequenceType">
  <xs:sequence>
  <xs:choice maxOccurs="unbounded" minOccurs="1">
    <xs:element name="A" type="xs:string"/>
    <xs:element name="B" type="xs:string"/>
    <xs:element name="C" type="xs:string"/>
  </xs:choice>
  </xs:sequence>
 </xs:complexType>
</xs:schema>

The trick here is to use a choice tag in the sequence so that one or more elements are allowed in the sequence.
All those sequences are validating against the above schema:

<SEQUENCE>
<A/>
</SEQUENCE>

<SEQUENCE>
<A/>
<C/>
<B/>
</SEQUENCE>

<SEQUENCE>
<A/>
<A/>
<C/>
</SEQUENCE>

comments powered by Disqus