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
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" 
  <xs:element name="Location">
          <xs:element ref="country"/>
          <xs:element ref="area" minOccurs="0"/>
          <xs:element ref="city" minOccurs="0"/>
          <xs:element ref="hotel" minOccurs="0"/>
          <xs:element ref="area"/>
          <xs:element ref="city" minOccurs="0"/>
          <xs:element ref="hotel" minOccurs="0"/>
          <xs:element ref="city"/>
          <xs:element ref="hotel" minOccurs="0"/>
          <xs:element ref="hotel"/>
  <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"/>

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.


[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

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 14:56:02 UTC