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

Re: attemting to create a mutli-type element

From: Jeni Tennison <jeni@jenitennison.com>
Date: Thu, 4 Dec 2003 10:46:03 +0000
Message-ID: <131-835177171.20031204104603@jenitennison.com>
To: "London ." <epsilonknot@hotmail.com>
Cc: xmlschema-dev@w3.org

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 GMT

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