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

Re: elements with descriptors

From: Jeni Tennison <jeni@jenitennison.com>
Date: Fri, 7 Dec 2001 10:16:15 +0000
Message-ID: <5244809247.20011207101615@jenitennison.com>
To: Saul Farber <saul@syncline.com>
CC: xmlschema-dev@w3.org
Hi Saul,

> How would one go about creating schema for an element like the
> following:
>
> <me:mydata type="integer">123456</me:mydata>
>
> where the value of the "type" attribute is a restriction?

It depends what you mean. If you mean where the type attribute
limits the type of the value of the me:mydata element, then you can't
in XML Schema (you have to use xsi:type instead, as the
type-controlling attribute - let us know if you need more details
about that).

If you just mean where the type attribute is limited to particular
values, then what you have will work, but it's usually better to
derive from xs:token rather than xs:string because it means that
whitespace isn't so much of an issue. If you use:

  <xs:attribute name="type">
    <xs:simpleType>
      <xs:restriction base="xs:token">
        <xs:enumeration value="integer" />
        <xs:enumeration value="float" />
        <xs:enumeration value="other" />
      </xs:restriction>
    </xs:simpleType>
  </xs:attribute>

then it will also allow things like:

  <me:mydata type="   integer   ">123456</me:mydata>

If you don't want it to allow that whitespace, then deriving from
xs:string is fine.

Creating a mixed complex type to deal with this content, as you have
done, is arguably a good move because it means that other complex
types could be derived from mydataType, either with simple content or
with complex content - it's very flexible.

The other option would have been to create a complex type that's
derived by extension from xs:anySimpleType, as follows:

<xs:complexType name="mydataType">
  <xs:extension base="xs:anySimpleType">
    <xs:attribute name="type">
      ...
    </xs:attribute>
  </xs:extension>
</xs:complexType>

However, you are then more restricted about what you can derive from
this complex type definition - you can't add elements to it later on,
for example.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/
Received on Friday, 7 December 2001 05:16:21 GMT

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