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

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

Within the stylesheet as a whole, there are potentially several template rules that match the context node. The rules for the

instruction define an ordering of these rules: They are considered first in decreasing order of import precedence, then within each import precedence in decreasing order of priority, and finally within each priority, by the order of the declarations in the stylesheet (it is actually an error to have two rules with the same priority, but the processor is allowed to ignore this error and select whichever comes last in the stylesheet). At the end of the list is the built-in template rule for the particular kind of node. What

does is choose the template rule that comes next in this pecking order, after 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 this 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 a parameter with
required=“yes”
, then a runtime error occurs if no value is supplied for this parameter.

The effect of the

instruction is very similar to

. The main difference is that with

, the only template rules that can be invoked are those in imported stylesheet modules. By contrast,

can invoke other template rules of lower priority in the same stylesheet module and can also invoke template rules that have lower import precedence because they were imported into the parent stylesheet module earlier than the current template rule. Looking at
Figure 6-4
on page 360, if the current template rule is in module C, then

will only consider template rules in module H, whereas

will also consider lower-priority rules in modules C, E, and J, as well as all rules in modules B, D, F, and G.

There is a special rule for the case where a template uses a union pattern and the two branches have different implicit priority, for example

. This is treated as if it were two separate template rules, which means that when you call

, the same template can be invoked more than once.

Usage and Examples

The intended usage pattern for

is illustrated by the following example.

One template rule might contain a general-purpose rule for formatting headings, as follows:


   

   

      

   


Another set of template rules contains specific rules for particular levels of headings:


   



   



   


These template rules each invoke the first rule using

, which avoids duplicating the common code in the template rule for each level of heading, and makes it easier to define changes later.

In this example I have made the priorities explicit, but in fact the default priorities could have been used. I always prefer to use explicit priorities when several rules match the same nodes, because it makes it clear to someone reading the stylesheet what your intentions were when you wrote it.

On this occasion there are three specialized rules, each invoking one generalized rule. But there are other problems where the structure can be inverted, so that the general rule invokes the special rule. For example, suppose you want to use a special color to render any inline text element that has the attribute
highlight=“yes”
. You might use a set of template rules like this:


   



   



   


and then process the
highlight
attribute in a higher priority template rule:


   


Unlike

, where invoking multiple template rules is possible only by defining multiple stylesheet modules,

allows several rules to be defined within a single module, which is often preferable because it makes the logic more clear.

Note that both

and

impose the constraint that the two (or more) template rules that match a node in the source tree work on the source node completely independently of each other. Neither rule can see or modify the nodes that the other rule has written to the result tree. This also means that (as the examples above show) the second rule can only create nodes that are children or siblings of the nodes created by the first rule; it cannot create parent nodes.

In some situations, therefore, other solutions might work better. In particular, another design pattern (sometimes called a
micropipeline
) is to use a multipass transformation whereby one template rule creates a temporary tree, which it then processes using a second template rule (perhaps in a different mode). The code tends to look like this:


   

     


         <xsl:value-of select=“caption”/>

         

      


   

   


This design pattern does not require use of

or

. One thing to watch out for when using this pattern is that the template rule for processing the


element might be expecting it to be part of a larger document; for example, it might expect its
IDREF
attributes to reference elements in the same tree. This won't be the case when you copy a

element into a temporary tree.

See Also


on page 237


on page 316


on page 425


on page 517

xsl:non-matching-substring

The

element is used within an

instruction to indicate the processing that should be applied to substrings of the input string that appear between the substrings that match the supplied regular expression.

Changes in 2.0

This element is new in XSLT 2.0.

Format


  


Position


can only appear as a child of an

element, and it must not appear more than once.

Attributes

None.

Content

A sequence constructor.

Effect

The sequence constructor contained in the

element is evaluated once for each nonempty substring of the input string that appears between two substrings that match the regular expression. The result of evaluating the sequence constructor is added to the result of the containing

instruction.

If there is no

element, or if its sequence constructor is empty, then non-matching substrings are discarded.

Usage and Examples

See

on page 230.

See Also


on page 230


on page 386

xsl:number

The

element performs two functions. It can be used to allocate a sequential number to the current node, and it can be used to format a number for output. These functions are often performed together, but they can also be done separately.

Note that the facilities for number formatting in the

element are quite separate from those offered by the
format-number()
function and the

element.

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

Other books

Half Magic by Edward Eager
What a Ghoul Wants by Victoria Laurie
Whispers at Willow Lake by Mary Manners
Pretty Girl Thirteen by Coley, Liz
Gods of Green Mountain by V. C. Andrews
Wiser Than Serpents by Susan May Warren
Identity Crisis by Eliza Daly
Ryan Hunter by Shelly, Piper
Arguably: Selected Essays by Christopher Hitchens

© 100 Vampire Novels China Edition 2015 - 2024    Contact for me [email protected]