Re: choice for attributes?

Hi Urs,

> How do I validate an element {If} which must have exactly ONE
> attribute out of a set of two attributes {defined,notDefined}:
>
> <If defined="...">      OR <If notDefined=".."> (both valid)
> BUT NOT <If defined="..." notDefined="..."> AND NOT <If> (both not valid)
>
> I would need anything like <xs:choice>, but this does only exist for
> elements, not for attributes.

You can't do this in XML Schema without changing the instance. To do
it, you have to use an xsi:type attribute on the element in the
instance to indicate which of the two attributes the element should
have:

  <If xsi:type="defined" defined="...">...</If>
  <If xsi:type="notDefined" notDefined="...">...</If>

To do this, define an abstract complex type for the element:

<xs:complexType name="IfType" abstract="true">
  ...
</xs:complexType>

And declare that the If element takes this type:

<xs:element name="If" type="IfType" />

Then declare two types that are derived from the IfType type - one
called defined that adds the 'defined' attribute and one called
notDefined that adds the 'notDefined' attribute:

<xs:complexType name="defined">
  <xs:complexContent>
    <xs:extension base="IfType">
      <xs:attribute name="defined" type="..." />
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="notDefined">
  <xs:complexContent>
    <xs:extension base="IfType">
      <xs:attribute name="notDefined" type="..." />
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

If one or other of these cases is more common than the other, another
alternative is to derive the uncommon one from the common one and make
If take the common one as its type. You could make it allow, for
example, either of the following:

  <If defined="...">...</If>
  <If xsi:type="notDefined" notDefined="...">...</If>

The other alternative is to use a different schema language for this
kind of constraint. RELAX NG is particularly good at it, and
Schematron could also articulate the constraint very easily.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Friday, 9 November 2001 11:39:30 UTC