XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (519 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
12.7Mb size Format: txt, pdf, ePub

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:

  • If for a particular source document you want to use a general-purpose stylesheet, but want to override its behavior for certain selected nodes, you can write a stylesheet that imports the general-purpose one and then write the overriding rules in the form of templates that match specific identified elements in the source document.
  • Sometimes the source document is generated dynamically from a database. Perhaps there is something in the source document you want to highlight, say the search term that was used to locate this record. You could flag this item while generating the source document by giving it a special ID attribute value known to the stylesheet.

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

elements are keyed on their ISBN property, which is implemented as a child element, then the following declarations are equivalent:

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.


Arrive in Cairo

Visit the Pyramids at Gaza

Archaelogical Museum at Cairo

Flight to Luxor; coach to Aswan

Visit Temple at Philae and Aswan High Dam

Cruise to Edfu

Cruise to Luxor; visit Temple at Karnak

Valley of the Kings

Return flight from Luxor


Stylesheet

Let's start with a straightforward stylesheet,
itinerary.xsl
, to display this itinerary.


      xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>


  

    

      Itinerary

    

    


      

    


  



  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:


   

      

      Itinerary

   

   

      


         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.

Other books

Rat Race by Dick Francis
the Big Bounce (1969) by Leonard, Elmore - Jack Ryan 01
What a Hero Dares by Kasey Michaels
Harry Houdini Mysteries by Daniel Stashower
The Guestbook by Hurst, Andrea
The Man in the Monster by Martha Elliott
Casket Case by Rizer, Fran
Breaking the Storm by Sedona Venez
Naked in the Promised Land by Lillian Faderman