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

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

If you omitted the
as=“xs:string”
from the

element, the result would not be a string, but a temporary tree consisting of a document node that owns a single text node, and the contents of that text node would be
(120.00)
; but for all practical purposes the value could still be used as if it were a string. One difference is that you won't get such good type checking: For example, if you try to use the variable
$credit-in-paren
as defined above in a context where a number is required, this will be reported as an error, quite possibly at compile time. But if you leave off the
as
attribute, you will probably not get an error at all, just a wrong answer: The system will treat the value of the variable as
NaN
(not a number).

Changing the Context Item

If you want to use

to process an item that is not the context item, the easiest way to achieve this is to nest the

inside an

instruction. An alternative, however, is to give the target template a distinctive mode name and call it using

with the specified mode.

For example, suppose you have written a template that returns the depth of the current node (the number of ancestors it has). The template has been given a unique name and an identical mode name:


   


Now, suppose you want to obtain the depth of a node other than the context node—let's say the depth of the next node in document order, which need not be on the same level as the context node. You can call this template in either of two ways.

Using

:


   

      

   


or using

with a special mode:


   


In both cases the variable
$next-depth
will, on return, hold a value, which is the depth in the tree of the node following the context node. If the context item is not a node, a runtime error will occur. Because the

element has an
as
attribute, the result is of type
xs:integer
. Without the
as
attribute, the result would be a temporary document containing a single text node, whose value is the string representation of this integer. For details, see

on page 500.

Recursion: Processing a List of Values

Named templates are sometimes used to process a list of values. As XSLT has no updateable variables like a conventional programming language, it also has no conventional
for
or
while
loop, because these constructs can only terminate if there is a control variable whose value is changing.

In XSLT 2.0 most processing of sequences can be done iteratively, using the XSLT

instruction or the XPath 2.0
for
expression; for example:

Other books

The Painted Bridge by Wendy Wallace
Fields of Blue Flax by Sue Lawrence
In the Company of Ghosts by Stephen A Hunt
Hush Little Baby by James Carol
Torn Apart by Peter Corris
There You'll Find Me by Jenny B. Jones
Space Chronicles: Facing the Ultimate Frontier by Tyson, Neil deGrasse, Avis Lang
The Wildings by Nilanjana Roy
Blind Trust by Terri Blackstock
Fireworks: Riley by Liliana Hart