Saturday, May 23, 2009

XSD element order

Recently I faced the problem that I had to create an XSD file to validate an XML file. The problem was that a requirement was that the elements could be in any order.
First I had something like:

<xs:sequence>
<xs:element name="customer" />
<xs:element name="order" />
</xs:sequence>

which validates correctly:

<root>
<customer />
<customer />
<customer />
<order />
<order />
<order />
</root>

but I needed to validate something like:

<root>
<order />
<customer />
<customer />
<order />
<customer />
<order />
</root>

After some research I finally found the solution.
instead of sequence You have to use choice with the min and max occurs attribute set:

<xs:sequence>
</xs:sequence>

is replaced with

<xs:choice minOccurs="0" maxOccurs="unbounded">
</xs:choice>

No comments: