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
4.44Mb 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

The Burning Point by Mary Jo Putney
My Name Is Leon by Kit de Waal
The News from Spain by Joan Wickersham
Captive Splendors by Fern Michaels
A Love Like Blood by Victor Yates
The Suicide Effect by L. J. Sellers
The Knights of the Black Earth by Margaret Weis, Don Perrin