XML technologies / XML Schema / Creating simple data types / Derivation by list

(Excerpt from “XML Schema” by Eric van der Vlist)

List derivation derives a list type from an atomic type. Every item must have the same type.

List data types

List data types are special cases defining structure inside one attribute or element. This practice is normally discouraged because current XML APIs, XPath expressions and the infoset cannot access atomic values. This may change as these types should be adopted by XPath 2.0, likely providing some way to access list items.

The feature seems introduced chiefly for compatibility with DTD IDREFs in SGML and XML. W3C XML Schema is cautious: list separators cannot be chosen, nor complex lists built from complex base types or differently structured items. Some vocabularies use comma-separated values or heterogeneous lists, such as values with units, which XML Schema cannot describe except partially through regular expressions:

<commaSeparated> 1, 2, 25 </commaSeparated>

<valueWithUnit> 10 em </valueWithUnit>

whitespace separated lists and separate XML elements or attributes are preferred:

<blankSeparated> 1 2 25 <blankSeparated>

<valueWithUnit unit="em"> 10 </valueWithUnit>

<valueWithUnit> 10em </valueWithUnit>

IDREFS, ENTITIES and NMTOKENS are predefined list types derived this way from atomic types.

As these three show, every definable list type must be whitespace-separated. No other separator is accepted.

Accepting these limits, defining a list is easy. W3C XML Schema defines two notations using element xs:list, allowing references to existing types or embedded definitions. They cannot be mixed.

A list referencing an existing type uses attribute itemType:

<xs:simpleType name="integerList">
   <xs:list itemType="xs:integer"/>
</xs:simpleType>

This type defines elements or attributes accepting whitespace-separated integers such as “1 –25000 1000”.

A list type may also embed element xs:simpleType :

<xs:simpleType name="myIntegerList">
   <xs:list>
      <xs:simpleType>
          <xs:restriction base="xs:integer">
             <xs:maxInclusive value="100"/>
          </xs:restriction>
      </xs:simpleType>
   </xs:list>
</xs:simpleType>

This defines elements or attributes accepting whitespace-separated integers at most 100, such as “1 –25000 100”.

List types have their own value space, constrained by facets shared by all list types.

These facets are xs:length, xs:maxLength, xs:minLength, xs:enumeration and xs:whitespace. Length always counts list items.

Applying these facets to a user-defined list takes two steps: define the list, then a type restricting it. Each xs:simpleType, global definition, accepts only one derivation method.

Restriction must come first in this process, since a list loses its atomic type’s facets and has only the five list-specific facets above.

Atomic types permitting whitespace

Lists of atomic types allowing whitespace, such as xs:string, may be defined; whitespace always acts as separator.

The effect becomes clear when applying a length facet:

<xs:simpleType name="myStringList">
  <xs:list itemType="xs:string"/>
</xs:simpleType>
<xs:simpleType name="myRestrictedStringList">
  <xs:restriction base="myStringList">
    <xs:maxLength value="10"/>
  </xs:restriction>
</xs:simpleType>

Type myRestrictedStringList is a list of at most ten items. Since whitespace separates them, myRestrictedStringList is at most ten string portions containing no whitespace, ten words.

It validates this value:

<myRestrictedStringList>
  Dieser Wert hat weniger als zehn Wörter.
</myRestrictedStringList>

It does not validate this one:

<myRestrictedStringList>
  Dieser Wert hat mehr als zehn Wörter … auch wenn sie sich vielleicht über weniger als zehn "Strings" erstrecken.
</myRestrictedStringList>

Nested lists of lists are forbidden by the W3C XML Schema recommendation.

   

 

 

 

back to top