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

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

Usage

When
last()
is used within a predicate in a filter expression, the focus refers to the sequence of items being filtered. If the filter is used within a step of a path expression, then the context size is the number of nodes selected by the current step of the expression, after applying any previous filters. For example, suppose the source document is as follows:


  

  

  

  

  

  

  

  


Then:

  • The expression
    countries/country[last()]
    returns the

    element for Nigeria.
  • The expression
    countries/country[@continent=‘Europe’][last()]
    returns the

    element for Poland.
  • The expression
    countries/country[@continent=‘Europe’][last()-1]
    returns the

    element for Italy.
  • The expression
    countries/country[@continent=‘Africa’]
    [position() !=last()]
    returns the

    elements for Egypt and Libya.

An easy mistake is to think that
last()
returns a boolean value. You can use
last()
in a predicate to match the last node, for example
para[last()]
. This is a shorthand for the predicate
[position()=last()]
, because in a predicate, a numeric value X is equivalent to a test for the condition
position()=X
. However, this doesn't extend to other contexts, for example if you write:

if (last()) then ...

then the numeric value of the
last()
function is simply converted to a boolean as if the
boolean()
function were used. The result will always be true, because
last()
can never be zero.

Other books

The Big Finish by James W. Hall
Tears of the Broken by A.M Hudson
Urden, God of Desire by Anastasia Rabiyah
Perfect Ruin by Lauren DeStefano
The Crow by Alison Croggon
The End of Summer by Rosamunde Pilcher
A Small Death in lisbon by Robert Wilson
Katy's Men by Carr, Irene
The Heaven Trilogy by Ted Dekker