I wanted to be able to allow complex type to contain any element in any order.

I wanted to basically say: A farm can have any number of <pig> and <sheep>, and you can have them in any order too.

<xs:complexType name="farm">
  <xs:all minOccurs="0" maxOccurs="unbounded">
    <xs:element name="pig" type="PigType" maxOccurs="unbounded" minOccurs="0" />
    <xs:element name="sheep" type="SheepType" maxOccurs="unbounded" minOccurs="0" />
  </xs:all>
<xs:complexType>

But this stupid visual studio schema editor kept on saying that you CANNOT combine maxOccurs=”unbounded” with xs:all.

Reason:

The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once

So what to do? Again the only 3 options are:

ALL: The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once

CHOICE: The <choice> indicator specifies that either one child element or another can occur

SEQUENCE: The <sequence> indicator specifies that the child elements must appear in a specific order:

No, no and NO!!! NONE of these options suit what I want to say.

WHY won’t it let me just say THE ELEMENTS CAN OCCUR IN ANY NUMBER IN ANY ORDER? Shouldn’t there be another option here…

So the answer is to use xs:choice minOccurs=”0″ maxOccurs=”unbounded” on the farm:

<xs:complexType name="farm">
  <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="pig" type="PigType" />
    <xs:element name="sheep" type="SheepType" />
  </xs:choice>
</xs:complexType>

Reference: This post, and same post here as well.

Post a Comment