Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
then concat(., ‘
’)
else string(.))”/>
which outputs the items in
$sequence
with a newline after every 10 items.
Remember that the focus is not changed within a
for
expression. If you need to know within the body of a
for
expression what the position of the item being processed is, you need to rewrite it. Instead of doing:
for $s in $sequence
return EXPR
write:
for $i in 1 to count($sequence),
$s in $sequence[$i]
return EXPR
You can then use
$i
within
EXPR
to refer to the position of
$s
within the sequence.
Usage in XSLT
The
position()
function is often used as a complete XPath expression within an XSLT stylesheet. The function has particular significance in XSLT because it gives the position of the item currently being processed by an
position()
function in XSLT are to
display
the current position, and to
test
the current position.
Displaying the Current Position
In this role the
position()
function can be used for simple numbering of paragraphs, sections, or figures.
In XSLT this provides an alternative to the use of
position()
function has two important advantages:
If you use
position()
, you can still exploit the formatting capabilities of
This determines the position of the node and formats the result according to the given format pattern; the resulting sequence will be
(a)
,
(b)
,
(c)
, and so on.
Testing the Current Position
It is possible to test the position of the current item either in a boolean expression in an
A common requirement is to treat the first or last item in a list differently from the rest. For example, to insert a horizontal rule after every item except the last, the following logic might be used:
Within a predicate in an expression or pattern, a numeric value represents an implicit test against the result of
position()
: for example,
item[1]
is equivalent to
item[position()=1]
, and
item[last()]
is equivalent to
item[position()=last()]
.