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

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

In this case, the number of items in the result will be the same as the number of items in the input sequence.

However, the return expression isn't constrained to return a single item; it can return any sequence of zero or more items. For example, you could write:

for $s in (“red”, “blue”, “green”) return string-to-codepoints($s)

The function
string-to-codepoints()
, which is part of the standard library defined in Chapter 13, returns for a given string, the Unicode code values of the characters that make up the string. For example,
string-to-codepoints(“red”)
returns the sequence
114, 101, 100
. The result of the above expression is a sequence of 12 integers, as illustrated in
Figure 10-2
.

The integers are returned in the order shown, because unlike a path expression, there is nothing in the rules for a
for
expression that causes the result sequence to be sorted into document order. Indeed, document order is not a meaningful concept when we are dealing with atomic values rather than nodes.

Examples

Expression
Description
for $i in 1 to 5 return $i*$i
Returns the sequence
1,4,9,16,25
. This example is a one-to-one mapping.
for $i in 0 to 4 return 1 to $i
Returns the sequence
1,1,2,1,2,3,1,2,3,4
. This example is a one-to-many mapping. Note that for the first item in the input sequence (0), the mapping function returns an empty sequence, so this item contributes nothing to the result.

The Context Item in a
for
Expression

Other books

Jo Beverley - [Malloren] by Secrets of the Night
Memoirs of a Porcupine by Alain Mabanckou
Unto All Men by Caldwell, Taylor
All That Follows by Jim Crace
Broken World by Mary, Kate L.
Jason Frost - Warlord 05 - Terminal Island by Jason Frost - Warlord 05
hidden by Tomas Mournian
Skeleton Man by Joseph Bruchac