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

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

If the selected template rule is a built-in rule, then any parameters that are supplied are passed on to any template rules called by the built-in rule, in the same way that the mode is passed on. This is a change from XSLT 1.0. For example, consider:



      

        

      




  

  …


When a

is contained directly within a


, the parameter
$in-section
will of course take the value
true
as expected. If there is an intervening element, with no explicit template rule, so that the

is the grandchild of the

element, the

template rule will still be invoked, and the parameter
$in-section
will still have the value
true
. The reason is that the built-in template rule for the intermediate element calls

to process its

children, supplying all the parameters that were supplied when it itself was invoked.

Result

The result of evaluating the

instruction is an arbitrary sequence, which may include both nodes and atomic values. This sequence is formed by concatenating the sequences produced by each of the template rules that is invoked. These sequences are concatenated in the order of the selected nodes (after any sorting), so that if the selected nodes after sorting were (A, B, C), and the template rule for node A generates the sequence T(A), then the final result sequence is (T(A), T(B), T(C)).

This doesn't mean that the XSLT processor has to process the nodes sequentially. It can process them in any order it likes, or in parallel, as long as it assembles the results in the right order at the end.

Usually,

is used to produce a sequence of nodes that become siblings in the result tree. This is what happens when

, or any other instruction, is used in the sequence constructor within an element such as an

instruction or a literal result element. But

is not confined to such uses. For example, the following code (which is written to take an XSLT stylesheet as input) uses

to decide in which version of XSLT the current element was first introduced:


  

    

  

  

    …

  


       match=“xsl:analyze-string | xsl:for-each-group |

        xsl:character-map | xsl:next-match | …”>

   



   


In this example, the result returned by

is a single decimal number.

Note that it's quite legitimate to use

within the body of a global variable definition, for example:


   


In this situation the context node is taken as the root of the principal source document, so the

processes the children of the root node. If the transformation is invoked without supplying a principal source document, this causes a runtime error. There will also be an error reported if a template rule that is invoked attempts to access the value of the global variable currently being defined: This kind of error is referred to in the specification as a
circularity
.

Usage and Examples

First, some simple examples are given below.

Construct
Effect

Processes all the children of the context node
select=“para”/>
Processes all the

elements that are children of the context node
select=“//*” mode=“toc”/>
Processes every element in the document in mode
toc
select=“para”>
Process all the

elements that are children of the context node, setting the value of the
indent
parameter in each called template to the value of the variable
$n
plus 4
select=“//book”>
Process all the

elements in the document, sorting them in ascending order of their
isbn
attribute

The following sections give some hints and tips about using

. First, I'll discuss when to use

and when to use

. Then I'll explain how to use modes.

versus


is most useful when processing an element that may contain children of a variety of different types in an unpredictable sequence. This is a
rule-based
design pattern: the definition of each individual template rule declares which nodes it is interested in, rather than the template rule for the parent node defining in detail how each of its children should be processed. The rule-based approach works particularly well when the document design is likely to evolve over time. As new child elements are added, template rules to process them can also be added, without changing the logic for the parent elements in which they might appear.

This style of processing is sometimes called
push
processing. It will be familiar if you have used text-processing languages such as awk or Perl, but it may be unfamiliar if you are more used to procedural programming in C++ or Visual Basic.

Where the structure is more regular and predictable, it may be simpler to navigate around the document using

, or by accessing the required data directly using

. This is sometimes called
pull
processing. The

instruction allows you to fetch data from the XML document using an arbitrarily complex XPath expression. In this sense it is similar to a
SELECT
statement in SQL.

A unique strength of XSLT is the ability to mix these two styles of programming. I'll discuss both approaches, and their relative merits, in more detail in Chapter 17.

Modes

Modes are useful where the same data is to be processed more than once.

A classic example is when building a table of contents. The main body of the output can be produced by processing the nodes in default mode, while the table of contents is produced by processing the same nodes with
mode=“TOC”
.

Other books

Savior by Jessica Gadziala
The Journey Back by Priscilla Cummings
The Music Lesson by Katharine Weber
Murder on the Lusitania by Conrad Allen
LeOmi's Solitude by Curtis, Gene
Legio XVII: Battle of Zama by Thomas A. Timmes
The Perfect Bride by Putman, Eileen