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

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

If either operand is an empty sequence, the result is an empty sequence. For example, this would happen if you ran any of the examples above when the context node did not have a
width
attribute. Supplying a sequence that contains more than one item, however, is an error.

If you want a sequence of integers in reverse order, you can use the
reverse()
function described in Chapter 13. For example,
reverse(1 to 5)
gives you the sequence
5,4,3,2,1
. In an earlier draft of the specification you could achieve this by writing
5 to 1
, but the rules were changed because this caused anomalies for the common usage
1 to count($seq)
in the case where
$seq
is empty.

Although the semantics of this operator are expressed in terms of constructing a sequence, a respectable implementation will evaluate the sequence lazily, which means that when you write
1 to 1000000
it won't actually allocate space in memory to hold a million integers. Depending on how you actually use the range expression, in most cases an implementation will be able to iterate over the values without laying them out end-to-end as a list in memory.

Examples

Here are some examples of expressions that make use of the
to
operator to construct sequences.

Expression
Effect
for $n in 1 to 10 return $seq[$n]
Returns the first 10 items of the sequence
$seq
. The
for
expression is described later in this chapter, on page 640.
$seq[position() = 1 to 10]
Returns the first 10 items of the sequence
$seq
. This achieves the same effect as the previous example, but this time using a filter expression alone. It works because the
=
operator compares each item in the first operand (there is only one, the value of
position()
) with each item in the second operand (that is, each of the integers 1 to 10) and returns
true
if any of them matches. It's reasonable to expect that XPath processors will optimize this construct so that this doesn't actually involve 10 separate comparisons for each item in the sequence.
Note that you can't simply write
$seq[1 to 10]
. If the predicate isn't a single number, it is evaluated as a boolean, and attempting to get the effective boolean value of a sequence of numbers is an error.
string-join( for $i in 1 to $N return “ ”,“ ”)
Returns a string containing
$N
space characters.
for $i in 1 to 5 return $i*2
Returns the sequence
2, 4, 6, 8, 10
.
for $i in 1 to count($S) return concat($S[$i]
,
$T[$i])
Returns a sequence that contains pairs of corresponding values from the two input sequences
$S
and
$T
. For example, if
$S
is the sequence
(“a”,“b”,“c”)
and
$T
is the sequence
(“x”,“y”,“z”)
, the result will be the sequence
(“ax”,“by”,“cz”).



This XSLT example shows how to output a sequence of five empty


elements.

Other books

La Ciudad Vampiro by Paul Féval
At Year's End (The 12 Olympians) by Gasq-Dion, Sandrine
El origen perdido by Matilde Asensi
Son of Heaven by David Wingrove
A Little Night Music by Andrea Dale, Sarah Husch