Keys

The XML linking system, using the ID and IDREF special attribute types, is quite limited in a number of respects. First, without a DTD it is not possible to identify these attributes. Second, there is only one set of identifiers, which means that every identifier in the document must be totally unique. Third, the name of the identifier must conform to the constraints of XML names (for example, spaces are not allowed in these identifiers). Fourth, an element can have only one identifier. Finally, the identifier must take the form of an attribute value.

XSLT 'keys' avoid all these limitations. However, unlike ID/IDREF identifiers, keys are not validated (by the XSLT processor) so are only used to make processing more efficient.

Key element

XSLT allows keys to be defined and associated with particular elements. Every key has a name, a value and a node with which it is associated. A set of keys is defined using the Key element:

<key ... />

The Name attribute provides a name for a set of identifiers in an 'identifiers namespace'. When only one set is needed, a name such as 'global' may be considered.

The Match attribute specifies the elements to be included in this set of identifiers using an XPath pattern. For a global set, this would be '*' (representing all elements).

The Use attribute is an XPath expression that identifies the location of the identifier values. If the identifier is in an attribute called 'id', then the expression would be '@id':

<key name="global" match="*" use="@id" />


   <book id="book">
     <chapter id="chap1">...</chapter>
     <chapter id="chap2">...</chapter>
   </book>

The following example creates a set of keys specifically for Name elements, where the unique key values are taken from the content of the embedded Employee Number elements. Note that the values in this example would not be valid XML ID names, as they begin with digits and have spaces in them:

<key name="nameKeys"
     match="name"
     use="employnum/text()" />


   <name>
     <first>John</first><second>Smith</second>
     <employnum>12345 X12</employnum>
   </name>
   <name>
     <first>Peter</first><second>Jones</second>
     <employnum>18887 X12</employnum>
   </name>

Key function

A function called ' key()' has been added to those provided by XPath. It works in a similar fashion to the 'id()' function, but this function takes two parameters. The first parameter identifies a key set to use, such as 'global' or 'nameKeys', and the second may be the value to locate:

						key("global", "chap2")/para[3]

key("names", "18887 X12")/first

Of course, it is possible to create similar expressions that do not rely upon the use of keys, as the following equivalent examples demonstrate:

/book/chapter[id="chap2"]/para[3]

//name[employnum="18887 X12"]/first

However, it is expected that processing will be much more efficient when keys are used, as the processor can create a list of key values in advance of template processing for quick look-up.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.59.231