Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
For both the
id()
and
key()
functions, the required value of the ID or key can be specified as either a literal or a variable reference (in the form
$
QName
). For the
id()
function, the only kind of literal that makes sense is a string literal (for example,
“E-102”
). For the key function, numeric literals also make sense if the key values are numeric. It is not possible to supply literals for other data types such as
xs:date
values.
With the
key()
function, the first argument is the name of the key. This must be specified as a string literal, and it must match the name of a key defined in the stylesheet.
Usage
This facility provides an equivalent to the ability in Cascading Style Sheets (CSS) to define a style for a specific node in the source document. Here are some ways it can be used:
In practice, this construct isn't as useful as it might seem. Even though XSLT 2.0 has made it a lot more flexible by allowing the value to be specified as a variable (which in general is likely to be a stylesheet parameter), this form of pattern still achieves nothing that can't be achieved just as easily with a predicate, and it is unlikely to be any more efficient.
For example, if
Using a direct pattern match:
Using a key definition:
Of course, there may be a performance difference between the two, but this depends on how the XSLT processor is implemented. There is certainly no intrinsic reason why the predicate should be less efficient.
Examples
id(‘figure1’) | Matches a node with an ID attribute equal to the string ‘figure1’ . An attribute is an ID attribute if it is defined in the Document Type Definition (DTD) or schema as having type ID (the name of the attribute is irrelevant). |
key(‘empnr’,$ pers) | Matches a node having a value of $pers for the key named empnr , where $pers is typically a stylesheet parameter. |
The following example shows how this feature can be used in a stylesheet.
Example: Using the key() Pattern to Format a Specific Node
This example shows how to use the
key()
pattern to format one selected node differently from the others. The selected node will be specified by a stylesheet parameter.
Source
The source document,
itinerary.xml
, is a tour itinerary.
Stylesheet
Let's start with a straightforward stylesheet,
itinerary.xsl
, to display this itinerary.
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
Day
Now let's specialize this by importing it into another stylesheet,
today.xsl
, which displays the activities for a selected day in red.
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”
xmlns:xs=“http://www.w3.org/2001/XMLSchema”
exclude-result-prefixes=“xs”>
To run this stylesheet using Saxon, enter the command line:
java net.sf.saxon.Transform -t itinerary.xml today.xsl highlight-day=5
Output
The resulting output is as follows, when the
$highlight-day
parameter is set to 5:
Day 1
Arrive in Cairo
Day 2
Visit the Pyramids at Gaza
Day 3
Archaelogical Museum at Cairo
Day 4
Flight to Luxor; coach to Aswan
Day 5
Visit Temple at Philae and Aswan High Dam
Day 6
Cruise to Edfu
Day 7
Cruise to Luxor; visit Temple at Karnak
Day 8
Valley of the Kings
Day 9
Return flight from Luxor
While this example shows one way of using this feature, I have to admit that it's not very convincing. You could achieve the same effect by writing the relevant pattern as
day[@number = 5]
, without the need to introduce a key at algray.