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

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

Changes in 2.0

None.

Signature

This function takes no arguments.

Type
Meaning
Result
xs:integer
A number, the value of the context size. As the name implies, this is context dependent
.

Effect

The XPath specification defines the value of the
last()
function in terms of the
context size
.

The context size is part of the
focus
, which is described in the spec as having three components: the context item, the context position, and the context size. However, it may be easier to think of the focus as being a bit like an
Iterator
object in a language such as Java. Behind the iterator is a list of items that are processed individually (though not necessarily in any particular order). The context item, position, and size can be thought of as three methods provided by this iterator object: the context position is a number that ranges from 1 to the size of the list, the context item is the item found at the context position, and the context size is the number of items in the list.

When a top-level XPath expression is evaluated (that is, an XPath expression that is not part of another expression), the context size is set by the host language. In XSLT, it is set from the XSLT context. For example:

  • When a global

    declaration is being evaluated, or in certain other contexts such as evaluating the
    use
    expression in

    , or evaluating the initial template that matches the root node, it is normally set to 1 (one).
  • When

    is called to process a sequence of nodes, the context size is the number of nodes selected in the call of

    .
  • When

    is called to process a sequence of items, the context size is the number of items selected in the call of

    .

This means that within an

iteration, the test

succeeds when the last item in the sequence is being processed.

Many APIs that enable XPath expressions to be executed from languages like Java or JavaScript allow the caller to set the context item, but not the context position or size. In such cases, the context position and size on entry to the XPath expression will normally both be one.

Within an XPath expression, the context size changes within a predicate and on the right-hand side of the
/
operator.

  • In a predicate,
    last()
    refers to the number of items in the sequence that is being filtered using the predicate. For example,
    $seq[last()]
    selects the last item in a sequence (this is short for
    $seq[position()=last()]
    ), while
    $seq[ceiling(last() div 2)]
    selects the item at the midway position of the list (the fourth item in a list of eight, the fifth item in a list of nine).
  • It's less common to find
    last()
    being used on the right-hand side of a
    /
    operator. It refers to the number of items in the sequence selected by the left-hand operand of the
    /
    . I can't find a very plausible way of using this, but it can be done. For example,
    $a/(if (last()=2) then . else ())
    returns all items from
    $a
    if there are exactly two items, and in all other cases returns nothing. But there are simpler ways of writing this!

Other books

Escapology by Ren Warom
Duncan's Bride by Linda Howard
Censoring an Iranian Love Story by Shahriar Mandanipour
And De Fun Don't Done by Robert G. Barrett
Kaitlyn O'Connor by Enslaved III: The Gladiators
Crotch Rocket: A Bad Boy Motorcycle Club Romance by Natasha Tanner, Amelia Clarke