W3C home > Mailing lists > Public > xmlschema-dev@w3.org > August 2004

RE: XML Schema Question

From: Michael Kay <mhk@mhk.me.uk>
Date: Mon, 23 Aug 2004 14:40:50 +0100
To: "'Jeni Tennison'" <jeni@jenitennison.com>, "'Asleson, Ryan'" <asleson@BIWORLDWIDE.com>
Cc: <xmlschema-dev@w3.org>
Message-ID: <E1BzF4e-0006di-3T@frink.w3.org>

> 
> This is the most frequent of FAQs. It can't be done in XML Schema.

Actually, I think someone did discover a way of doing it. You can use a
choice as in (2) below, and then use a uniqueness constraint to ensure there
is only one father and only one mother. Under the element declaration for
family, you add:

<xs:unique name="one-father">
  <xs:selector xpath="."/>
  <xs:field xpath="father"/>
</xs:unique>

This will cause a validation error if a the xs:field expression selects more
than one node, that is, if a family has more than one father.

Michael Kay
Saxonica Limited
www.saxonica.com

> Your options are:
> 
> 1. Constrain the order as in your current schema, and use a simple
> transformation to transform the any-order documents into the
> constrained-order documents, which you can then validate.
> 
> 2. Use a content model with no constraints on occurrence, as in:
> 
>   <xs:choice minOccurs="0" maxOccurs="unbounded">
>     <xs:element ref="father" />
>     <xs:element ref="mother" />
>     <xs:element ref="son" />
>     <xs:element ref="daughter" />
>     <xs:element ref="pet" />
>   </xs:choice>
> 
> and add supplementary Schematron rules to constrain the occurrence of
> each of the elements. The Schematron rules can be processed in a
> separate step.
> 
> 3. Switch to using RELAX NG, where you can do:
> 
>   <interleave>
>     <optional><ref name="father" /></optional>
>     <optional><ref name="mother" /></optional>
>     <zeroOrMore><ref name="son" /></zeroOrMore>
>     <zeroOrMore><ref name="daughter" /></zeroOrMore>
>     <zeroOrMore><ref name="pet" /></zeroOrMore>
>   </interleave>
> 
> or, in compact syntax:
> 
>   father? & mother? & son* & daughter* & pet*
> 
> Cheers,
> 
> Jeni
> 
> ---
> Jeni Tennison
> http://www.jenitennison.com/
> 
> 
> 
Received on Monday, 23 August 2004 13:41:24 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 5 February 2014 07:15:10 UTC