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
13.56Mb 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

Split by Tara Moss
Darkest by Ashe Barker
Night Tide by Mike Sherer
Any Day Now by Denise Roig
Turning Point by Lisanne Norman
Aphrodite by Russell Andrews