XML technologies / XML Schema / XML Schema element reference / xs:key

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

Defining a key.

<xs:key
    id = xs:ID
    name = xs:NCName
    {beliebige Attribute mit einem Nicht-Schema-Namensraum}
    >
    Content: ((xs:annotation?), (xs:selector, xs:field+))
</xs:key>

May occur within: xs:element (within xs:all), xs:element (reference or local definition), xs:element (global definition)

Description

xs:key defines simple or composite keys uniquely identifying each element in a selected list of subelements within a root element’s scope.

xs:key and xs:unique are very similar. Like constraints defined with xs:unique, keys defined with xs:key are unique within their root element’s scope and can be referenced by xs:keyref . The only difference between xs:key and xs:unique is that every selected element must have a key, whereas unique identifiers may be undefined.

The key’s root is the element where it is defined. Choose it carefully: uniqueness is checked only among element nodes below this root. A root occurring repeatedly in a document permits local keys unique only below each occurrence. For a document-wide key, using the document element as root may be less error-prone.

The contribution of xs:key to the PSVI is a node table in each root occurrence listing its key sequences. These tables’ scope includes their containing root and its ancestors. This determines key-reference behavior and affects the choice of root when a key is referenced; see xs:keyref.

Restrictions

These XPath based constructs do not follow the object-oriented, namespace-aware general principles of W3C XML Schema. XPath expressions are specified independently of the element’s content model, and constraints for elements from all possible namespaces must be specified in the schema for the namespace of the root containing the key constraint.

Example

<xs:element name="library">
    <xs:complexType>
        .../...
    </xs:complexType>
    <xs:key name="book">
        <xs:selector xpath="book"/>
        <xs:field xpath="isbn"/>
    </xs:key>
</xs:element>

Attributes

id
Element ID according to W3C XML Schema.

name
Key name.

   

 

 

 

back to top