views:

53

answers:

1

If an address can be composed of child elements: Street, City, State, PostalCode...how do you allow this XML:

 <Address>
         <Street>Somestreet</Street> 
         <PostalCode>zip</PostalCode>                     
 </Address>

and allow this:

<Address>
     <City>San Jose</City>
     <Street>Somestreet</Street> 
     <State>CA</State>
</Address>

but not this:

<Address>
    <Street>Somestreet</Street> 
    <City>San Jose</City>
</Address>

What schema will do such things!?

+2  A: 

There is a convoluted way using choice to create choices where only valid combinations are allowed...

In you example this should have the desired result:

 <xs:complexType name="Address">
  <xs:choice>
   <xs:sequence>
    <xs:element name="city"/>
    <xs:element name="street"/>
    <xs:element name="state"/>
   </xs:sequence>
   <xs:sequence>
    <xs:element name="street"/>
    <xs:element name="postcode"/>
   </xs:sequence>
  </xs:choice>
 </xs:complexType>

Another simple example if you want to allow any two from three.. you could do this, say you have elements A B C and you want to allow any two from three you could use the following xsd:

<xs:complexType name="anyTwo">
  <xs:choice>
   <xs:sequence>
    <xs:element name="A"/>
    <xs:element name="B"/>
   </xs:sequence>
   <xs:sequence>
    <xs:element name="A"/>
    <xs:element name="C"/>
   </xs:sequence>
   <xs:sequence>
    <xs:element name="B"/>
    <xs:element name="C"/>
   </xs:sequence>
  </xs:choice>
 </xs:complexType>

You can see that this would soon become unwieldy for large sets but the principal does work!

Edit: see also this answer

Dog Ears