W3C home > Mailing lists > Public > xmlschema-dev@w3.org > August 2009

RE: Help on XSD for a xml

From: Michael Kay <mike@saxonica.com>
Date: Fri, 21 Aug 2009 10:35:47 +0100
To: "'Vijay Anand'" <vijay.gunasekaran@mobax.com>, <xmlschema-dev@w3.org>, <bryce@sysonyx.com>
Message-ID: <86E200D35CAF492BBE6C30D592B4246A@Sealion>
> 
> The following is my xml for which I require to write the xsd schema
> 
> <type>
> <foo/>
> <bar/>
> </type>
> 
> In the above xml for type element the following conditions 
> should be satisfied 1] Atleast one element either foo or bar 
> must be present 2] foo or bar can have a maximum occurance of 
> one 3] Both foo and bar can be present
> 

You don't say whether you want to allow <bar/><foo/>.

In XSD 1.0, you have to spell it out:

<xs:choice>
  <xs:sequence>
    <xs:element name="foo"/>
    <xs:element name="bar" minOccurs="0"/>
  </xs:sequence>
  <xs:sequence>
    <xs:element name="bar" minOccurs="0"/>
  </xs:sequence>
</xs:choice>

In XSD 1.1 it's simpler to write

  <xs:sequence>
    <xs:element name="foo" minOccurs="0"/>
    <xs:element name="bar" minOccurs="0"/>
  </xs:sequence>
  <xs:assert test="exists(*)"/>

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 
Received on Friday, 21 August 2009 09:36:39 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 11 January 2011 00:15:14 GMT