Keys

Within the XSLT standard, a powerful mechanism is included that overcomes the limitations of the XML ID scheme described above. XSLT can identify elements using a 'key'. A key is a combination of three things. First, it includes a specific target element, and the purpose of the key is to retrieve this element when required. Second, it includes a value by which the element can be identified. Third, it includes a name that creates a distinct namespace for the value. This means that the same value can be used in different contexts, without causing any conflicts. For example, the value 'orange' would have one meaning in the 'colour' space, and a different meaning in the 'fruit' space. The names 'colour' and 'fruit' make this distinction, and the value 'orange' can be used in both without confusion.

While the key as a whole must be unique, any part of it does not have to be so. In the case of the name part, this is obvious. It is clear that the name 'fruit' may be used in many keys, with the value changing to identify elements that describe different kinds of fruit. However, it is also true for the value part of the key as well; more than one element may describe an 'orange' fruit. This flexibility allows a group of related elements to be formed and means that, when a key is used, it may locate more than one element:

Furthermore, even the third part of the key, the element identified, does not have to be unique. This means that a single instance of an element may be involved in more than one key. For example, an element that describes an 'orange' fruit, may also be an element that describes an object of a specific colour (in the XML ID scheme, only one ID attribute can be defined for an element):

Using these keys, it is possible to apply specific formatting to all elements of a particular colour, or to all elements that describe a particular fruit.

Note that the illustration above also includes an example that demonstrates identical values in different namespaces ('orange' as a colour as well as a fruit).

Generating keys

Keys are defined by creating a namespace, then specifying which elements are to participate, and what part of these elements are to be considered an identifier value. The Key element is used to create keys:

<key .../>

The Name attribute specifies the name to give to each key created by this instruction. For example, it may be useful to create identifiers for every term in a glossary, and this list of identifiers could be called 'Glossary':

<key name="Glossary" .../>

It is then necessary to identify the element or elements that are to be included in this set of identifiable objects. This could be just the name of an element, or it could be a more complex XPath pattern. The Match attribute is used to do this, and the choice of name for this attribute is particularly appropriate as it has the same purpose and behaviour as when used in the Template element. For this example, Item elements will be selected, but only when they appear within a Glossary element:

   <glossary>
     <item>...</item>
     <item>...</item>
     <item>...</item>
   </glossary>


<key match="glossary/item" .../>

Finally, it is necessary to identify the text that will provide the unique value within each item. For maximum flexibility, this is done using an XPath expression. The Use attribute contains this expression. It is possible to select an attribute, the text content, or even the content of a sub-element. Assuming each Item element has the following structure, then the Subject child element is a good candidate for the key value:

   <item>
     <subject>XSL</subject>
     <description>XML Stylesheet Language</description>
   </item>
   <item>
     <subject>XSLT</subject>
     <description>XML Stylesheet Language -
     Transformations</description>
   </item>


<key use="subject" .../>

By putting these three attributes together in a single Key element, an instruction can be created to generate keys for all Item elements within Glossary elements, using the content of the embedded Subject elements as the key values:

<key name="Glossary"
     match="glossary/item"
     use="subject" ... />

In this case, there is a single Subject element in each Item element, so that the value is simple to determine. However, it is possible that there may be more than one matching element. When this happens, a key is created for each value. In the following example, two keys would be produced for the Item element, each with the same name but with different values ('XSL/XSLT' and 'XSLT/XSL'):

<item>
  <subject>XSL/XSLT</subject>
  <subject>XSLT/XSL</subject>
  <description>XML Stylesheet Language and
  Transformations</description>
</item>

When keys are present in the main stylesheet, and also in imported stylesheets, the keys defined in imported stylesheets for the same element are not ignored if there are multiple definitions at differing importance levels (according to the rules dictated by the import feature).

Using keys

Once generated, the keys can be used in similar fashion to XML identifiers, except that the function name is 'key()' and takes two parameters instead of one. The first parameter is the key name, specifying the domain to search. The second parameter may be the value to search for, as in the following example. In this example, the Item element for 'XSL/XSLT' is located for formatting:

<template match="key('Glossary', 'XSL/XSLT')">
  ...
</template>

Recall that, because a key with a given name and value is not guaranteed to be unique, it cannot be assumed that such a reference will apply to a single element in the document.

Avoiding keys

A key does not offer anything that cannot be accomplished without this feature. As a key is constructed using expressions, these expressions could just as easily be used directly. The example above could be re-expressed as follows:

<template match="glossary/item[subject='XSL/XSLT']">
  ...
</template>

However, an XSLT processor may include advanced features for indexing elements and may build indexes from the information in the Key element, improving the performance of later searches. This feature also simplifies the expressions in the templates, making the meanings more clear.

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

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