I'm trying to write an XML schema that allows XML to be expressed in the following ways:
pets can containt both cat and dog elements:
<root>
<pets>
<cat />
<dog />
</pets>
</root>
pets can contain just cat or dog elements
<root>
<pets>
<cat />
</pets>
</root>
-----------
<root>
<pets>
<dog />
</pets>
</root>
if pets has no sub elements, then it should be absent:
<root>
</root>
The best schema that I've come up with to satisfy these conditions is this:
<xs:element name="pets">
<xs:complexType>
<xs:choice>
<xs:sequence>
<xs:element name="cat"/>
<xs:element name="dog" minOccurs="0"/>
</xs:sequence>
<xs:sequence>
<xs:element name="dog"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
</xs:element>
This has always seemed to me like too much schema for such a simple concept. Is there a simpler way to write this schema? Thanks!