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

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


searches for a template rule that matches the current node, using the same search rules as

, but considering only those template rules that (a) match the current mode and (b) are defined in a stylesheet module that was imported into the stylesheet module containing the current template rule. For details of import precedence, see

on page 357. If no template rule is found, the built-in template rule is used (see page 243).

Note that the instruction doesn't examine all template rules with lower precedence than the current rule. For an example that illustrates this, see
Figure 6-4
on page 360. If the current template rule is in module C on that diagram, then

will search for rules in module H, but not in modules B, D, F, or G: although those modules have lower import precedence than C, they were not imported into C.

The specification defines what this means in terms of the import tree. If a stylesheet module A includes another module B using

, then A and B are part of the same stylesheet level. If any module in stylesheet level L imports a module in stylesheet level M, then M is a child of L in the import tree. The template rules that are considered are those that are defined in a stylesheet level that is a descendent of the stylesheet level containing the current template rule.

It is possible to specify parameters to be supplied to the called template, using

elements contained within the

element. These work in the same way as parameters for

and

; if the name of the supplied parameter matches the name of an

element within the called template, the parameter will take that value; otherwise, it will take the default value supplied in the

element. It is not an error to supply parameters that don't match any

element in the called template rule, they will simply be ignored. However, if the called template specifies any parameters with
required=“yes”
, then a runtime error occurs if no value is supplied for that parameter.

Usage and Examples

The intended usage pattern behind

is illustrated by the following example.

One stylesheet,
a.xsl
, contains general-purpose rules for rendering elements. For example, it might contain a general-purpose template rule for displaying dates, given as follows:


   

   /

   

   /

   


A second stylesheet,
b.xsl
, contains special-purpose rules for rendering elements. For example, you might want it to display dates that occur in a particular context in the same way, but in bold face. It could be written as:


   

   

   /

   

   /

   

   


However, this involves duplicating most of the original template rule, which is a bad idea from a maintenance point of view. So, in
b.xsl
we could import
a.xsl
, and write instead:



   

   

   


Note that the facility only allows a template rule to invoke one of lower
import precedence
, not one of lower
priority
. The import precedence depends on how the stylesheet module was loaded, as explained under

on page 357. The priority can be specified individually for each template rule, as explained under

on page 483. The code above will work only if the
timeline/date
template rule is in a stylesheet module that directly or indirectly imports the
date
template rule. It will not work, for example, if they are in the same module but defined with different priority. In this respect,

differs from

.

In many situations the same effect can be achieved equally well by giving the general-purpose template rule a name and invoking it from the special-purpose template rule by using

(see page 271). But this approach doesn't work if you want one rule that overrides or supplements many others. One example I encountered was a developer who had a working stylesheet but wanted to add the rule “output an HTML

tag for any source element that has an
anchor
attribute.” Rather than modifying every rule in the existing stylesheet, this can be achieved by defining a new stylesheet module that imports the original one, and contains the single rule:

There is a more complete example of the use of

in the section for

.

See Also


on page 357


on page 399


on page 425


on page 517

xsl:apply-templates

The

instruction defines a set of nodes to be processed and causes the system to process them by selecting an appropriate template rule for each one.

Changes in 2.0

The
mode
attribute may now take the value
#current
to continue processing in the current mode.

Other books

The Comedy of Errors by William Shakespeare
Sydney Bridge Upside Down by David Ballantyne
Runt of the Litter by Sam Crescent
Oasis of Eden by deGrey, Genella
Brief Encounters with the Enemy by Said Sayrafiezadeh
Game for Anything by Summers, Cara
The Broken Window by Christa J. Kinde