0

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:

  1. 'xs:sequence' will always check sequence of elements.
  2. 'xs:all' allows the specified child elements to appear (or not) in any order in the containing element, except they can only appear once.
  3. '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.

vaibhavn
  • 29
  • 7
  • Please [edit] your question and add XML cases which should and should not be valid to clarify your request. Thanks. – kjhughes Jan 06 '16 at 16:44
  • I have added XML cases now. – vaibhavn Jan 07 '16 at 06:24
  • You either need to relax the requirement that order doesn't matter, or you have to use XSD 1.1 assertions. You cannot express your constraints without change in XSD 1.0. – kjhughes Jan 07 '16 at 18:02

0 Answers0