Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The
mode
attribute may also contain a list of modes to which the template is applicable. Each mode is written either as a lexical QName (the actual mode name) or as the token
#default
to indicate that the template is applicable to the default mode.
Mode names are compared using the usual rules for QNames—they match if the local name and namespace URI both match.
The mode specified on the
not
automatically propagated to any
If you have a
mode
attribute on a template and there is no
Evaluating a Template
Once an
When processing of the sequence constructor is complete, the stack frame containing its local variables is discarded, control returns to the calling template, and the context item, position, and size revert to their previous values. The value produced by evaluating the sequence constructor becomes the return value of the calling
The implementation, of course, is free to do things in a different order if it has the same effect. Some products use lazy evaluation, where the parameters are only evaluated when they are first used. Some products also use tail-call optimization, where a recursive template call is deferred until after the stack has been unwound; this reduces the risk of running out of stack space when calls are deeply nested. Such optimizations may show up if you use extension functions that have side effects, or if you use
Usage and Examples
We will look first at using template rules, and then I will give some advice on the use of modes. For examples of the use of named templates, see
Using Template Rules
A
template rule
is an
match
attribute, which can therefore be invoked using the
This rule-based approach to processing is the characteristic way of writing XSLT stylesheets, though it is by no means the only way. Its biggest advantage is that the output for each element type can be defined independently of the context that the element appears in, which makes it very easy to reuse element types in different contexts or to add new element types to an existing document definition without rewriting the stylesheet templates for all the possible containing elements. A classic example of this approach to processing arises when converting semantic markup in a document to rendition markup, as the following example demonstrates.
Example: Template Rules
This example shows a typical use of template rules to handle narrative text with a free-form structure.
Source
The source file is
review.xml
.
This text contains the review of a concert performance, using both structured (data) and narrative (document) markup. Omitting some of the detail, it reads:
“2007-03-27”>27 March another fine group, the
Quartet, was joined by pianist
for a programme of
and
fashion for the chamber versions of
concertos, the players opened with a lively and shapely performance of his
Stylesheet
The stylesheet file is
review.xsl
.
This stylesheet resists the temptation to use varied fonts for the names of composers and artists, but merely uses italics for names of musical works:
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
separator=“ - ”/>
Note the use of CSS to achieve the styling, and the use of a
priority
attribute on the template for
match=“para/work”
to ensure that it is selected in preference to the template for
match=“para/*”
. In fact, the template for
match=“para/*”
(which handles all children of
Output
If the generated HTML is displayed in a browser, it will look like
Figure 6-14
.