Re: At least 1 element out of 4 is required

Hi Camilla,

Camilla Brenchley wrote:

>I need to write an xml schema that checks that at least one of "Location"'s
>four children (country, area, city, hotel) is present. I don't care which
>one as long as one of them is there. The order of the child elements doesn't
>matter.
>
If you are willing to impose order on the child elements you could do this:
---
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified">
  <xs:element name="Location">
    <xs:complexType>
      <xs:choice>
        <xs:sequence>
          <xs:element ref="country"/>
          <xs:element ref="area" minOccurs="0"/>
          <xs:element ref="city" minOccurs="0"/>
          <xs:element ref="hotel" minOccurs="0"/>
        </xs:sequence>
        <xs:sequence>
          <xs:element ref="area"/>
          <xs:element ref="city" minOccurs="0"/>
          <xs:element ref="hotel" minOccurs="0"/>
        </xs:sequence>
        <xs:sequence>
          <xs:element ref="city"/>
          <xs:element ref="hotel" minOccurs="0"/>
        </xs:sequence>
        <xs:sequence>
          <xs:element ref="hotel"/>
        </xs:sequence>
      </xs:choice>
    </xs:complexType>
  </xs:element>
  <xs:element name="area" type="xs:string"/>
  <xs:element name="city" type="xs:string"/>
  <xs:element name="country" type="xs:string"/>
  <xs:element name="hotel" type="xs:string"/>
</xs:schema>
---

If you really need them to be available in any order you face a bit of a 
complexity explosion. Either that, or use schematron[1], which can be 
embedded in your schema and processed in any environment that supports XSLT.

Francis.

[1] http://www.ascc.net/xml/resource/schematron/schematron.html

-- 
"Never mind manoeuvre, go straight at 'em." - Admiral Horatio Nelson

Received on Thursday, 31 July 2003 11:15:54 UTC