Given an XML layout like this, I'm trying to create a XSD schema to validate it.
<RootNode>
<ChildA />
<ChildC />
<ChildB />
<ChildB />
<ChildA />
</RootNode>
The requirements are as follows:
- ChildA, ChildB and ChildC may occur in any order. (
<xs:sequence>
unsuitable)
- ChildA is mandatory but may occur multiple times.
- ChildB is optional and may occur multiple times.
- ChildC is optional and may occur once only.
The technique I usually use to create an unordered list of nodes is to use a <xs:choice maxOccurs="unbounded">
with each possible node in the list, however, I am unable to create the minOccurs="1"
constraint on ChildA and the maxOccurs="1"
contraint on ChildC. (The # of occurances of the choice takes precedence over those of the elements here).
<xs:element name="RootNode">
<xs:complexType>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="ChildA" minOccurs="1"/>
<xs:element name="ChildB" />
<xs:element name="ChildC" maxOccurs="1"/>
</xs:choice>
</xs:complexType>
</xs:element>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…