I have requirement as specific element should be present at least one time & all elements can be in any sequence. For ex:
<xs:element name="ABC">
<xs:complexType>
<xs:choice>
<xs:element ref="SCH" minOccurs="0" maxOccurs="1"/>
<xs:element ref="ELM" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
<xs:attribute type="xs:string" name="id" use="required"/>
.........
</xs:complexType>
</xs:element>
<xs:element name="SCH">
<xs:complexType>
<xs:choice>
<xs:element ref="PTN" minOccurs="1" maxOccurs="unbounded" />
<xs:element ref="ELM" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
<xs:attribute type="String" name="type" use="required"/>
.............
</xs:complexType>
</xs:element>
I would like to test as 'PTN' should always be present in 'SCH' with minOccurs as '1' and 'PTN' & 'ELM' can appear in any sequence. I checked following options:
- 'xs:sequence' will always check sequence of elements.
- 'xs:all' allows the specified child elements to appear (or not) in any order in the containing element, except they can only appear once.
- 'xs:choice' solves my problem partially but it does not check specific element should be present at least one time.
Valid XML Cases:
<ABC id="000021"><SCH id="" type="aaa"><ELM id="dfg" type="fff"/><PTN id="NA" type="gxgfd"><ELM id="dfg2" type="fff"/></SCH></ABC>
<ABC id="000021"><SCH id="" type="aaa"><PTN id="NA" name="Machine" type="dgdh"><ELM id="dfg" type="fff"/><ELM id="dfg2" type="fff"/><PTN id="NA" type="machine"></SCH></ABC>
<ABC id="000021"><PTN id="NA" type="gdfg"></SCH></ABC>
Above XML cases are valid because it contains 'PTN' node at least one time. PTN & ELM can be in any sequence & count.
Invalid cases:
<ABC id="000021"><SCH id="" type="aaa"><ELM id="dfg" type="fff"/><ELM id="dfg2" type="fff"/></SCH></ABC>
<ABC id="000021"><SCH id="" type="aaa"><ELM id="dfg" type="fff"/></SCH></ABC>
Above cases are invalid because it does not have PTN node.