XML technologies / XML Schema / Defining uniqueness, keys and key references / ID/IDREF compared with xs:key/xs:keyref

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

Keys and key references improve on ID and IDREF: unrestricted data types, value-based instead of lexical tests, and independent value sets for each key. For a complete picture, we must see whether keys can simulate ID and IDREF and which capabilities they lack.

First, key and key-reference definitions must be at the document root to simulate document-global ID and IDREF, regardless of their containing element. The best strategy for W3C XML Schema, which does not directly specify the root, is to place declarations in the global element most likely to be used as document element.

To define the xs:selector, list all elements containing ID attributes in one XPath expression.

Finally, ID allows a list of ID references separated by whitespace, using type IDREFS, whereas no equivalent exists for xs:key. There is no xs:keyrefs!

To use xs:key and xs:keyref, change the instance document from our ID and IDREF page so the character-ID list becomes a sequence of references, using the same ID/reference convention in all elements:

<library xsi:noNamespaceSchemaLocation="library-keys-id-key.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <book id="b3810518883">
        <isbn>
            3810518883
        </isbn>
        <title>
            Auf den Hund gekommen
        </title>
        <author-ref ref="au-Charles_M._Schulz"/>
        <character-ref ref="ch-Peppermint_Patty"/>
        <character-ref ref="ch-Snoopy"/>
        <character-ref ref="ch-Schroeder"/>
        <character-ref ref="ch-Lucy"/>
    </book>
    .../...
    <author id="au-Charles-M.-Schulz">
        <name>
            Charles M. Schulz
        </name>
        <nickName>
            SPARKY
        </nickName>
        <born>
            1922-11-26
        </born>
        <dead>
            2000-02-12
        </dead>
    </author>
    <cha
        <name>
            Peppermint Patty
        </name>
        <since>
            1966-08-22
        </since>
        <qualification>
            kühn, dreist und draufgängerisch
        </qualification>
    </character>
    ...
</library>

The definition is:

<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>

This illustrates the main difference: ID/IDREF declarations sit where used and integrate fully with the pseudo-object-oriented features of W3C XML Schema , whereas key/keyref definitions sit at a common ancestor and depend on instance structure rather than the object-oriented schema.

Depending on actual structure, key/keyref ignore features such as substitution groups. Their XPath expressions must explicitly list all possible element names unless “*” denotes any element at a given level.

   

 

 

 

back to top