The XSD below will validate according to these rules:
- Allows
Child1
toChild5
elements in any order, all optional, repeating any number of times. - Allows only one
Child6
element which, if present, must be positioned after all the other elements.
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Child1" />
<xs:element name="Child2" />
<xs:element name="Child3" />
<xs:element name="Child4" />
<xs:element name="Child5" />
</xs:choice>
<xs:element name="Child6" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
To allow Child6
to also appear anywhere, you can use xs:all
but you'll need a XSD 1.1 compatible parser since XSD 1.0 parsers won't allow elements in xsd:all
to have unbounded occurrences:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
<xs:element name="data">
<xs:complexType>
<xs:all>
<xs:element name="Child1" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="Child2" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="Child3" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="Child4" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="Child5" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="Child6" minOccurs="0" maxOccurs="1"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
You can achieve something in between with XSD 1.0, but you'll have to decide if it is worth writing a more complex and less reusable schema to allow for the flexibility you want in placing your elements.