Re: attemting to create a mutli-type element

Hi London,

> this works fine, but what i would like to do is have an xsd that
> could be used to validate this:
>
> <data>
>   <value dt="int">1</value>
>   <value dt="string">hello</value>
> </data>
>
> im at a complete loss here, any help would be appreciated.

This is known as a co-occurrence constraint; XML Schema does not have
good support for co-occurrence constraints. If you're willing to
change your vocabulary slightly, however, XML Schema automatically
supports the use of the xsi:type attribute to indicate the type of the
value of an element. Something like:

<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <value xsi:type="xs:int">1</value>
  <value xsi:type="xs:string">hello</value>
</data>

will work with a schema like:

<xs:element name="data">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="value" type="xs:anyType"
                  minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

Otherwise, you need to use Schematron or RELAX NG to enforce the rule
that the value of the <value> element must be of the type specified by
the dt attribute.

Cheers,

Jeni

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

Received on Thursday, 4 December 2003 05:48:32 UTC