I would like my XSD to be able to enforce that at least one of a set of elements must be present. Some of those elements can appear unlimited times, some are limited.
None of the elements are compulsory by themselves but at least one must be present.
Finally the elements can appear in any order. i.e. the order should neither be enforced nor have any impact on the processing of the document
My attempt at this:
<xs:element name="Root">
<xs:complexType>
<xs:choice>
<xs:element minOccurs="0" maxOccurs="unbounded" name="A" type="a:a"/>
<xs:element minOccurs="0" maxOccurs="unbounded" name="B" type="a:a"/>
<xs:element minOccurs="0" maxOccurs="1" name="C" type="a:a"/>
<xs:element minOccurs="0" maxOccurs="1" name="D" type="a:a"/>
</xs:choice>
</xs:complexType>
</xs:element>
This doesn't quite work as it doesn't enforce there being at least one of these elements.
So in the example above there must be at least one of A, B, C or D. There can be multiple A or B elements but only 1 each of C or D
So the following should all be valid:
<root><A/></root>
<root><A/><A/></root>
<root><A/><B/><A/></root>
<root><C/></root>
<root><D/><C/></root>
But the following should not be valid:
<root/>
- at least one element must be present<root></root>
- at least one element must be present<root><C/><C/></root>
- duplicate<C/>
elements
There is the added complication that this is an extension on an existing complexType but I don't think that should affect anything?
Is this possible?
I tried adding minOccurs="1" to the xs:choice element. This had no effect, presumably because each instance of the choice can be empty!
I also tried having both an xs:choice (with no minOccurs in the children) and an xs:sequence (without C & D nodes) But I couldn't find any parent element which would allow both to co-exist.