Range expressions
The expression E1 to E2
returns a sequence of integers. For example, 1
to 5
returns the sequence 1, 2, 3, 4, 5
. This is useful in
for
expressions, for example the first five nodes of a node sequence can be
processed by writing for $i in 1 to 5 return (//x)[$i]
.
If you prefer, you can write this as (//x)[position() = 1 to 5]
. This works
because comparison of a single integer (position()
) to a sequence of integers
(1 to 5
) is true if the integer on the left is equal to any integer in the
sequence.
Saxon does a good job of optimizing range expressions. For example given the expression
$x = (1 to 1000000)
, it won't actually compare $x
with every
integer in this range; rather, it tests that $x
is a whole number and that it
is greater than 0 and less than 1000001.