XML technologies / XML Schema / Defining uniqueness, keys and key references / XPath-based identity checks
(Excerpt from “XML Schema” by Eric van der Vlist)
IDs and IDREFs are stored in the PSVI in a table called the “ID/IDREF table”, which applications can use to find the corresponding nodes. We can expect XPath applications, including XPointer, to provide shortcuts and fast access to nodes identified by a W3C XML Schema, as already happens with DTD IDs.
However simple and convenient within their scope, IDs and IDREFs retain the limitations of their DTD ancestors. W3C XML Schema offers a more flexible way to define identity constraints without lexical-space restrictions, allowing local keys and references as well as multinode keys.
Another important difference is that ID/IDREF checks operate on data types based on the type xs:NMTOKEN, whereas the following checks can operate on other data types. Comparisons use their actual value spaces rather than lexical string representations. These checks rely on XPath expressions and are specified by three different but similar constructions: checking uniqueness, defining a key and defining a key reference.
Uniqueness
The first construction defines a simple uniqueness check. We shall explain it carefully, since the other two follow the same pattern.
These constraints use two consecutive relative XPath expressions evaluated from the position of the element under which they are defined. Specifying them requires a clear view of instance structure. The starting point is the element beneath which the check is defined. This determines the check’s scope and must be chosen carefully: it is the basis for every check of this constraint.
In our library, for example, we can place the book-ISBN uniqueness check under the element library, because it applies across the entire library. Within a book, we can additionally check that a character reference is unique within that book. We can define this second check inside the element book .
Once we have chosen the check’s location, we write it at the end of the element definition:
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
.../...
</xs:complexType>
<xs:unique name="book">
.../...
</xs:unique>
</xs:element>The attribute name used here is useful when referring to this constraint with a keyref.
Having defined the check’s name and root, we define the selector, the relative path to the node to identify. In our example, the relative path from library to a book is book, so we write:
<xs:element name="library">
<xs:complexType>
.../...
</xs:complexType>
<xs:unique name="book">
<xs:selector xpath="book"/>
.../...
</xs:unique>
</xs:element>We have stated that a book must be unique within a library. To complete the check’s description, we must use field elements to specify how a book is identified.
Here, the identifier is the subelement isbn, giving this complete definition:
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
.../...
</xs:complexType>
<xs:unique name="book">
<xs:selector xpath="book"/>
<xs:field xpath="isbn"/>
</xs:unique>
</xs:element>In plain language: “Within each library, a book identified by its ISBN should be unique.”
A uniqueness constraint does not require the identifier node, the field, to be present. Selectors with no available field are simply ignored. For the same check with a required field, use “key” rather than “unique”.
Compound fields
If authors’ names were divided into first, middle and last names, defining a compound field identifying authors would probably be useful. W3C XML Schema allows this by defining several fields in one constraint, for example:
<xs:element name="library">
<xs:complexType>
.../...
</xs:complexType>
<xs:unique name="author">
<xs:selector xpath="author"/>
<xs:field xpath="first-name"/>
<xs:field xpath="middle-name"/>
<xs:field xpath="last-name"/>
</xs:unique>
</xs:element>The check then operates on the triple of field values, first-name, middle-name, last-name, which must be unique in combination.
Keys
A key is a uniqueness constraint additionally requiring every node named as a field to exist.
Key definitions use the same notation as uniqueness constraints, except that the element unique is replaced by the element key:
<xs:element name="library">
<xs:complexType>
.../...
</xs:complexType>
<xs:key name="book">
<xs:selector xpath="book"/>
<xs:field xpath="isbn"/>
</xs:key>
</xs:element>The additional existence check overlaps with other ways of controlling how often an element or attribute occurs. If our author names have a minimum occurrence count of one, using xs:unique or xs:key is equivalent unless the author name can have the value “nil”, discussed in Referencing schemas and schema data types in XML documents.
Key references
Despite its name, xs:keyref can define a reference not only to an xs:key but also to an xs:unique.
Using xs:keyref is straightforward and resembles xs:key and xs:unique, with one important point: the attribute refer of xs:keyref should reference an element xs:key or xs:unique defined under the same element or one of its descendants.
This is because the identity-constraint tables storing keys and references are local to the element and its ancestors.
Matching definitions of xs:unique or xs:key and xs:keyref must be in the same element; otherwise an ancestor influences the choice of location. For example, suppose books and authors occupy different document sections.
<library>
<books>
<book>
.../...
<author-ref ref="Charles M. Schulz"/>
.../...
</book>
.../...
</books>
<authors>
<author>
<name>
Charles M. Schulz
</name>
.../...
</author>
.../...
</authors>
</library>Good modular schema design places constraints as close as possible to the elements concerned. Thus a key in the element authors and a matching keyref in the element books seem appropriate. However, since an xs:keyref must be in the same element as the matching xs:key or in an ancestor, and books is not an ancestor of authors, the definition of xs:keyref can only be inside the element library. The xs:key can be in either the element library or authors.
In the previous example, defining xs:key in library or authors was merely a stylistic choice, since authors were unique both within a library and within the element authors. W3C XML Schema also allows situations where a key is unique within a document region but not throughout the whole document.
Modify the example to include several categories of authors:
<library>
<books>
<book>
.../...
<author-ref ref="Charles M. Schulz"/>
.../...
</book>
.../...
</books>
<authors>
<category id="comics">
<author>
<name>
Charles M. Schulz
</name>
.../...
</author>
.../...
</category>
<category id="romane">
.../...
</category>
.../...
</authors>
</library>Defining an xs:key or an xs:unique within library or authors establishes uniqueness across the whole library. Defining an author list inside category establishes uniqueness only within that category, allowing same-named authors in different categories.
Under W3C XML Schema, it is perfectly valid to define an xs:key under category and a matching xs:keyref under library, because library is an ancestor of category. This adds a new constraint to author names: if an author is referenced within a book, the name must be unique within the scope of xs:keyref. Thus “Charles M. Schulz” can be defined in several categories as long as no book references him. Since a book does reference him, his name may be defined only once.
Although the Recommendation describes this behavior, its results can surprise schema designers. Keeping the definitions of xs:key or xs:unique and the matching xs:keyref within the same element is probably good practice.
Permitted XPath expressions
The Recommendation for W3C XML Schema states for xs:selector and xs:field that only restricted subsets of XPath expressions are allowed to reduce implementation effort, particularly for streaming processors. The result is a limited XPath subset selecting only descendants of the current location or the location itself.
XPath expressions allowed in xs:selector may only descend the XML element-node hierarchy, permit no tests in XPath steps, and must match a set of elements. Expressions in xs:field may additionally select attributes.
The reference section gives the complete BNF for this subset. Rather than a lengthy explanation, let us examine examples of what is and is not possible.
The following expressions are allowed:
- xpath="author"
Selects child elements named
authorbelonging to no namespace.- xpath="author|character"
Selects child elements named
authororcharacterbelonging to no namespace.- xpath="lib:author"
Selects child elements named
authorbelonging to the namespace whose prefix is “lib”.- xpath="*"
Selects all child elements.
- xpath="lib:*"
Selects all child elements in the namespace with prefix “lib”.
- xpath="authors/author"
Selects all child elements
authors/author.- xpath=".//author"
Selects all elements named
authorthat descend from the current node and belong to no namespace.- xpath="author/@id"
Selects attribute
idof child elementauthor, allowed only forxs:field, not forxs:selector.- xpath="@id|@name"
Selects
@idor@name, valid only inxs:field, since attributes are forbidden inxs:selector.- xpath="/library/author"
Absolute paths are not allowed.
- xpath="../author"
The parent axis is not allowed.
- xpath=".//*[@id]"
Tests are not allowed.
- xpath="author[@type='comics']"
Tests are not allowed.
- xpath="substring-after(@xlink:href,'#')"
Function calls are not allowed.
- xpath="//author"
Absolute paths are not allowed.
Default namespaces do not apply inside XPath expressions, so elements and attributes belonging to a namespace must always be qualified with a prefix.
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