Re: elements with descriptors

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 UTC