Reputation: 31333
What would be the xsd definition to support the following xml?
<xml>
<RequiredElementA></RequiredElementA>
<RequiredElementB></RequiredElementB>
<RequiredElementC></RequiredElementC>
<OptionalElementD></OptionalElementD>
<OptionalElementE></OptionalElementE>
</xml>
I want the first 3 to be required, but the last two are optional. Order doesn't matter (but order yes/no not a requirement).
Upvotes: 1
Views: 239
Reputation: 25054
If the order of children conveys no information, it's usually simplest to fix an order.
<xs:complexType name="fixedOrder">
<xs:sequence>
<xs:element ref="RequiredElementA"/>
<xs:element ref="RequiredElementB"/>
<xs:element ref="RequiredElementC"/>
<xs:element ref="RequiredElementD" minOccurs="0"/>
<xs:element ref="RequiredElementE" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
If the order carries information (e.g. you are recording the user's rank ordering of A, B, C, with options to include D and E), you can make the order unconstrained.
<xs:complexType name="variableOrder">
<xs:all>
<xs:element ref="RequiredElementA"/>
<xs:element ref="RequiredElementB"/>
<xs:element ref="RequiredElementC"/>
<xs:element ref="RequiredElementD" minOccurs="0"/>
<xs:element ref="RequiredElementE" minOccurs="0"/>
</xs:all>
</xs:complexType>
Some people want unconstrained order even when it conveys no information; this makes it possible for data sources not to have to look up the prescribed order, at the cost of making validation somewhat more complex.
Upvotes: 3