XML technologies / XML Schema / Defining uniqueness, keys and key references / xs:ID and xs:IDREF
(Excerpt from “XML Schema” by Eric van der Vlist)
The first method for describing identifiers and references in W3C XML Schema is inherited from XML DTDs. As discussed in Creating simple data types, the data types xs:ID, xs:IDREF and xs:IDREFS, introduced in W3C XML Schema, emulate XML DTD attribute types ID, IDREF and IDREFS.
Unlike their DTD counterparts, these simple types describe both elements and attributes but inherit other DTD restrictions: their lexical space is that of unqualified XML names, known as data type xs:NCName, and they are document-global. An author and character cannot share an ID in one document.
Lexical restrictions often prevent using an existing node as identifier. An ISBN cannot be an ID in our library, since an xs:NCName cannot start with a digit and whitespace is forbidden. We must create arbitrary IDs from existing node values. ISBN “3810518883” can yield ID isbn-3810518883, and “Charles M. Schulz” can become au-Charles-M.-Schulz. Adding a prefix (isbn, au, etc.) also avoids collisions between IDs for different element types.
These IDs can define attributes or elements. The recommendation advises attributes only for compatibility with XML 1.0 IDs and IDREFs. In either case, their contribution to the PSVI is similarly an ID/IDREF table. Apart from compatibility with this XML 1.0 feature, there is no reason to avoid ID, IDREF and IDREFS for elements.
To combine both styles, a library book element could be written as:
<book identifier="b3810518883">
<isbn>
3810518883
</isbn>
<title>
Auf den Hund gekommen
</title>
<author-ref ref="au-Charles_M._Schulz"/>
<character-refs>
ch-Peppermint_Patty ch-Snoopy ch-Schroeder ch-Lucy
</character-refs>
</book>Element book is identified by attribute identifier , an ID. It references its author through attribute ref, an IDREF, on element author-ref, and a whitespace-separated list of characters through element character-refs, of type IDREFS. Its schema fragment could be:
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="isbn" type="xs:NMTOKEN"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="author-ref">
<xs:complexType>
<xs:attribute name="ref" type="xs:IDREF" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="character-refs" type="xs:IDREFS"/>
</xs:sequence>
<xs:attribute name="identifier" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML Schema” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top