W3C home > Mailing lists > Public > xmlschema-dev@w3.org > July 2003

Re: At least 1 element out of 4 is required

From: Francis Norton <francis@redrice.com>
Date: Thu, 31 Jul 2003 16:13:44 +0100
Message-ID: <3F293228.7010604@redrice.com>
To: Camilla Brenchley <cbrenchley@transhotel.com>
CC: xmlschema-dev@w3.org

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 GMT

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