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

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

Format

  name = qname>

  


Position


is an instruction; it is always used within a sequence constructor.

Attributes

Name
Value
Meaning
name
mandatory
Lexical QName
The name of the template to be called

Content

Zero or more

elements.

Effect

The sections below describe the rules for the template name, the rules for supplying parameters to the called template, and the way the context is affected.

The Template Name

The mandatory
name
attribute must be a lexical QName, and it must match the
name
attribute of an

element in the stylesheet. If the name has a namespace prefix, the names are compared using the corresponding namespace URI in the usual way. It is an error if there is no

element with a matching name.

If there is more than one

in the stylesheet with a matching name, they must have different import precedence, and the one with highest import precedence is used. For information about import precedence, see

on page 357.

The name of the template to be called must be written explicitly in the
name
attribute. There is no way of writing this name as a variable or an expression to be evaluated at runtime. If you want to make a runtime decision on which of several named templates to call, the only way to achieve this is to write an

instruction. Alternatively, there is a technique for using template rules as if they were higher order functions; this is described under
Simulating Higher Order Functions
on page 250.

Parameters

If the name of a child

element matches the name of an

element in the called

, then the

element is evaluated (in the same way as an

element), and the value is assigned to the relevant

variable name within that named template.

If the

element specifies
tunnel=“yes”
, then the parameter is available not only in the immediately called template but to templates at any depth in the call stack, provided they declare the parameter with

and a matching name. Tunnel parameters are described more fully on page 429. In XSLT 2.0 a compile-time error is reported if there is a child

element that isn't a tunnel parameter and that doesn't match the name of any

element in the selected

. However, if the

instruction is in a part of the stylesheet that specifies
[xsl:]version=“1.0”
, the extra parameter is ignored as it was in XSLT 1.0.

If there is an

element in the selected

with no matching

element in the

element, then the

variable is given a default value. But if the

element specifies
required=“yes”
, this is a compile-time error. See

on page 425 for details.

Context

The selected

is evaluated with no change to the context: it uses the same context item, context position, and context size as the calling template. There is also no change to the
current template rule
(a concept that is used only by

, described on page 237, and

, described on page 399). The
current mode
is also unchanged.

Usage and Examples

The

element is similar to a subroutine call in conventional programming languages, and the parameters behave in the same way as conventional call-by-value parameters. It is useful wherever there is common logic to be called from different places in the stylesheet.

Using the Result

The result of an

instruction is the sequence returned by the sequence constructor inside the template that is called. Usually, this consists of nodes that are immediately added to the result tree. However, you can also capture the result by calling

from within an

element, in which case the result of the called template becomes the value of the variable.

For example, the following template outputs the supplied string enclosed in parentheses:


   

   


This may be called as follows:


   

      

   


If the value of the
credit
attribute is
120.00
, the resulting value of the variable
$credit-in- paren
will be the string
(120.00)
.

Other books

Kinked by Thea Harrison
Welcome to Dog Beach by Lisa Greenwald
And the Band Played On by Christopher Ward
Callahan's Crosstime Saloon by Spider Robinson
Shattered by Dick Francis