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.78Mb 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

Wraiths of Time by Andre Norton
Unfinished Business by Karyn Langhorne
Honey and Decadence by Wendi Zwaduk
Wild Open by Bec Linder
Domme By Default by Tymber Dalton
Black Ice by Anne Stuart
One Man's Trash by Yolanda Allen
After the Fall by Meikle, William