Showing posts with label XSD. Show all posts
Showing posts with label XSD. Show all posts

Tuesday, August 21, 2012

Nested Choice and Sequence Groups

The choice group element allows only one of its children to appear in an instance. One child is an inner group element that references the named group shipAndBill consisting of the element sequence shipTo, billTo, and the second child is a singleUSAddress. Hence, in an instance document, the purchaseOrder element must contain EITHER a shipTo element followed by a billTo element OR a singleUSAddress element. The choice group is followed by the comment and items element declarations, and both the choice group and the element declarations are children of a sequence group. The effect of these various groups is that the address element(s) must be followed by comment and items elements in that order.


<xsd:complexType name="PurchaseOrderType">`
 <xsd:sequence>
  <xsd:choice>
   <xsd:group   ref="shipAndBill"/>
   <xsd:element name="singleUSAddress" type="USAddress"/>
  </xsd:choice>
  <xsd:element ref="comment" minOccurs="0"/>
  <xsd:element name="items"  type="Items"/>
 </xsd:sequence>
 <xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>

<xsd:group name="shipAndBill">
  <xsd:sequence>
    <xsd:element name="shipTo" type="USAddress"/>
    <xsd:element name="billTo" type="USAddress"/>
  </xsd:sequence>
</xsd:group>


Reference: http://www.w3.org/TR/2001/REC-xmlschema-0-20010502/

xsd:all

<xsd:all>

  • All the elements in the group may appear once or not at all, and they may appear in any order. 
  • The all group is limited to the top-level of any content model. 
  • The group's children must all be individual elements (no groups - sequence or all)

For example, to allow the child elements of purchaseOrder to appear in any order, we could redefine PurchaseOrderType as:


<xsd:complexType name="PurchaseOrderType">
  <xsd:all>
    <xsd:element name="shipTo" type="USAddress"/>
    <xsd:element name="billTo" type="USAddress"/>
    <xsd:element ref="comment" minOccurs="0"/>
    <xsd:element name="items"  type="Items"/>
  </xsd:all>
  <xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>


Reference: http://www.w3.org/TR/2001/REC-xmlschema-0-20010502/