XML technologies / XML Schema / XML Schema element reference / xs:keyref
(Excerpt from “XML Schema” by Eric van der Vlist)
Defining a key reference.
<xs:keyref
id = xs:ID
name = xs:NCName
refer = xs:QName
{beliebige Attribute mit einem Nicht-Schema-Namensraum}
>
Content: ((xs:annotation?), (xs:selector, xs:field+))
</xs:keyref>May occur within: xs:element (within xs:all), xs:element (reference or local definition), xs:element (global definition)
Description
xs:keyref defines a reference to a simple or composite key or uniqueness constraint.
The syntax of xs:keyref resembles that of xs:key or xs:unique. The only difference is attribute refer, which must name the referenced key or uniqueness constraint.
The root element for keyref is the element where the key is defined. A reference often shares its root with the referenced key or constraint, making matching straightforward.
The root of keyref can also be any element within the scope of a matching constraint-node table, meaning any ancestor of the referenced root. An additional check then ensures each referenced key or unique value occurs only once across all matching roots.
Restrictions
References defined in an ancestor impose extra constraints on the referenced key or uniqueness constraint, which may surprise: previously valid identifiers can become invalid when a reference is defined.
For example, local keys identifying characters within each book allow the same character identifiers in different books:
<book>
<isbn>3810518883</isbn>
<title>Auf den Hund gekommen</title>
<character id="PP">
<name>Peppermint Patty</name>
<qualification>kühn, dreist und draufgängerisch</qualification>
</character>
<character id="Snoopy">
<name> Snoopy </name>
<qualification>Held des Buches</qualification>
</character>
<character id="Schroeder">
<name> Schroeder </name>
<qualification>brachte die klassische Musik in die Peanuts-Comics ein</qualification>
</character>
<character id="Lucy">
<name>Lucy</name>
<qualification>herrschsüchtig, kratzbürstig und egoistisch</qualification>
</character>
</book>
<book>
<isbn>3810518719</isbn>
<title>Alles Peanuts</title>
<character id="Sally">
<name>Sally Brown</name>
<qualification>sucht immer nach dem leichten Ausweg</qualification>
</character>
<character id="Snoopy">
<name>Snoopy</name>
<qualification>extrovertierter Beagle</qualification>
</character>
</book>Adding references to these keys from books allows repeated character identifiers across books only when those keys are not referenced.
This example is valid because “Sally” is a unique key:
<library>
<book>
<isbn>3810518883</isbn>
<title>Auf den Hund gekommen</title>
<character id="PP">
<name>Peppermint Patty</name>
<qualification>kühn, dreist und draufgängerisch</qualification>
</character>
<character id="Snoopy">
<name>Snoopy</name>
<qualification>Held des Buches</qualification>
</character>
<character id="Schroeder">
<name>Schroeder</name>
<qualification>brachte die klassische Musik in die Peanuts-Comics ein</qualification>
</character>
<character id="Lucy">
<name>Lucy</name>
<qualification>herrschsüchtig, kratzbürstig und egoistisch</qualification>
</character>
</book>
<book>
<isbn>3810518719</isbn>
<title>Alles Peanuts</title>
<character id="Sally">
<name> Sally Brown </name>
<qualification>sucht immer nach dem leichten Ausweg</qualification>
</character>
<character id="Snoopy">
<name>Snoopy</name>
<qualification>extrovertierter Beagle</qualification>
</character>
</book>
<comment about="Sally">Sally ist süß</comment>
</library>Adding a comment element referring to “Snoopy”, mentioned twice, would make it invalid.
A complete schema could be the following; note that xs:keyref references an xs:unique constraint defined for a child of the current node:
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="isbn" type="xs:NMTOKEN"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="character" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="qualification" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="character">
<xs:selector xpath="character"/>
<xs:field xpath="@id"/>
</xs:key>
</xs:element>
<xs:element name="comment" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="about" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="book">
<xs:selector xpath="book"/>
<xs:field xpath="isbn"/>
</xs:unique>
<xs:keyref name="comment" refer="character">
<xs:selector xpath="comment"/>
<xs:field xpath="@about"/>
</xs:keyref>
</xs:element>
</xs:schema>Example
<xs:element name="library">
<xs:complexType>
.../...
</xs:complexType>
<xs:key name="ID">
<xs:selector xpath="book|author|character"/>
<xs:field xpath="@id"/>
</xs:key>
<xs:keyref name="IDREF" refer="ID">
<xs:selector xpath="book/author-ref|book/character-ref"/>
<xs:field xpath="@ref"/>
</xs:keyref>
</xs:element>Attributes
id
Element ID according to W3C XML Schema.
name
Key-reference name.
refer
Name of the referenced key or uniqueness constraint.
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