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

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

                                 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

instruction (as well as other instructions such as

and

). The two main uses of the
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

. There is much less flexibility to control how the numbering is done than when using

, but the
position()
function has two important advantages:

  • It is generally faster.
  • It numbers items in the order they are output, whereas

    can only allocate a number based on the position of a node in the source document. This means

    is of little use when a list has been sorted using

    .

If you use
position()
, you can still exploit the formatting capabilities of

by writing, for example:


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

or

element, or in a predicate within a filter expression or pattern.

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()]
.

Other books

Death in Paradise by Kate Flora
Bad Intentions by Karin Fossum
Kushiel's Dart by Jacqueline Carey
The Enemy At Home by Dinesh D'Souza
Criminal That I Am by Jennifer Ridha
Fixing Freddie by Mona Ingram
Breeding Mom and Daughter by Natalia Darque
Nailed by Desiree Holt
The Bug - Episode 1 by Barry J. Hutchison
Teaching Roman by Gennifer Albin