Reputation: 401
I want to define a Schema for an XML where I can define an arrow by defining its head, fletching or both of them. How can I create a sequence with a limited number of inner items and limit at the same time the max occurences of each item type? My actual version does not detect when the arrow definition doesn't contain any elements:
<xs:element name="Arrow">
<xs:annotation>
<xs:documentation>Arrow with fletching, head or both.</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="2">
<xs:element minOccurs="0" name="Fletching" maxOccurs="1">
</xs:element>
<xs:element minOccurs="0" name="Head" maxOccurs="1">
</xs:element>
</xs:sequence>
[...]
With this Schema I expect to validate:
<Arrow>
<Head/>
</Arrow>
<Arrow>
<Fletching/>
</Arrow>
<Arrow>
<Head/>
<Fletching/>
</Arrow>
as OK.
And
<Arrow/>
or
<Arror>
</Arrow>
as not OK.
Upvotes: 0
Views: 81
Reputation: 25054
You can get what you want with
<choice>
<sequence>
<element ref="Fletching"/>
<element ref="Head" minOccurs="0"/>
</sequence>
<sequence>
<element ref="Head"/>
<element ref="Fletching" minOccurs="0"/>
</sequence>
</choice>
In cases where there are more than two elements in the set, it is usually simpler to prescribe an order (unless the order is conveying some information):
<choice>
<sequence>
<element ref="Fletching"/>
<element ref="Head" minOccurs="0"/>
</sequence>
<sequence>
<element ref="Head"/>
</sequence>
</choice>
In XSD 1.1, one could also use xsd:all with an assertion that the parent has at least one child.
Upvotes: 1