Help on XSD for a xml

View: New views
3 Messages — Rating Filter:   Alert me  

Help on XSD for a xml

by Vijay Anand-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

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

Thanks and Regards,
Vijay.



RE: Help on XSD for a xml

by Michael Kay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>
> 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 



Re: Help on XSD for a xml

by Vijay Anand-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Michael Kay wrote:

>> 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 
>
>  
The xsd 1.0 works for conditions 2 and 3 but fails for for 1 <type></type>
So I modified the xsd 1.0 as follows
<xs:choice>
  <xs:sequence>
    <xs:element name="foo"/>
    <xs:element name="bar" minOccurs="0"/>
  </xs:sequence>
  <xs:sequence>
    <xs:element name="bar" *minOccurs="1"*/>
  </xs:sequence>
</xs:choice>
and it is working for all the three conditions.
Thanks.

Regards,
Vijay.