Saturday, July 3, 2010

XSD, specify sequence of OPTIONAL elements in ANY order

<xs:element name="GEOMQFeed">
 <xs:complexType>
  <xs:all>
   <xs:element ref="HEADER" minOccurs="1" maxOccurs="1"/>
   <xs:element ref="TAGA" minOccurs="0" maxOccurs="1"/>
   <xs:element ref="TAGB" minOccurs="0" maxOccurs="1"/>
   <xs:element ref="TAGC" minOccurs="0" maxOccurs="1"/>
  </xs:all>
 </xs:complexType>
</xs:element>


xs:all allows you to specify TAGA,TAGB,TAGC as well as TAGC,TAGB as well as TAGB,TAGA.
HEADER is the only compulsory element

The only problem, with xs:all, maxOccurs can be either 0 or 1!

If you want maxOccurs to be unbounded, you will have to use some workaround, as explained here:
http://xsd.stylusstudio.com/2003Mar/post09004.htm

The easiest one IMHO is to introduce a new type TAGAList defined as a unbounded sequence of TAGA. GEOMQFeed will be made by a xs:all of TAG?List.
Reality is that expressing a true arbitrary formal grammar in XSD is wishful thinking.


In other words..... with XSD you are only allowed to do what the XSD designers had in mind, not what you actually need :o(

In future I will look into http://www.relaxng.org/  which seems a more flexible language than XSD.

No comments: