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

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

Instead, write:


   Book  of

                   

   

   …


An alternative is to assign the sequence of nodes to a variable, so it is only evaluated once.

See Also

sum()
on page 889

last()
on page 820

current

This function is available in XSLT only
.

The
current()
function returns a single item, the item that is the context item at the point in the stylesheet where the XPath expression containing this function call is called.

Changes in 2.0

The function has been generalized so that it may return any item (a node or an atomic value), not only a node. The function may now be used within an XSLT pattern.

Signature

There are no arguments.

Type
Meaning
Result
item()
The item that is the context item at the outermost level of the XPath expression containing the function call

Effect

At any point in the processing of a stylesheet, there is generally a context item. The XPath expression
.
, if it is used as a standalone expression within a stylesheet, will always select the context item and will return the same result as the expression
current()
. Within an XPath expression, the context item may change; for example, within a predicate, the context item is the item being tested using the predicate. The result of the
current()
function, however, is the same wherever it is used within an expression. This makes it useful within predicates, as a way of referring to the item that would have been the context item at the outermost level.

This is best explained by example. The following example processes all

elements that have a
code
attribute whose value is the same as the
code
attribute of the element that is the context item at the point where the instruction is evaluated.


Another way of writing this would be:


and in fact this substitution is completely general; any complete XPath expression that references the
current()
function could be replaced by one in which a variable is bound to the value of
.
at the outermost level of the expression, and the call on
current()
is replaced by a reference to that variable.

The context item in a stylesheet is established as follows:

  • When evaluating a global variable, the context item is the initial context item, supplied by the calling application (it may be undefined if no initial context item has been supplied). Usually, this will be the document node of the main input document.
  • When

    is used to process a selected set of nodes, each selected node in turn becomes the context item. So when a template rule is invoked, the context item is always the node that caused that template rule to be selected. On return from

    , the context item reverts to its previous value.
  • When

    is used to process a selected sequence of items, each selected item in turn becomes the context item. When the

    loop completes, the context item reverts to its previous value.
  • When a stylesheet function is called from within an XPath expression, the context item is undefined. This means that any attempt to reference
    .
    or
    current()
    will raise an error.
  • The

    and

    instructions also change the context item; for details, see the description of these instructions in Chapter 6.
  • All other instructions, including

    and

    , leave the context item unchanged.

When the
current()
function is used in a pattern, it refers to the node that is being matched against the pattern. For example, the pattern
part[ancestor::*/@code !=current()/@code]
matches all
part
elements that have an ancestor with a
code
attribute that differs from the
code
attribute on the element itself.

Usage

The reason the
current()
function is provided is to allow you to determine the XSLT context item when it is different from the XPath context item—specifically, inside a predicate. The XPath context item can always be determined using the expression
.
.

Other books

Isabel's Texas Two-Step by Annie Bryant
Lassoing His Cowgirl by Steele, C.M.
Mistress of the Sun by Sandra Gulland
The Heavenly Man by Brother Yun, Paul Hattaway
Fugue: The Cure by S. D. Stuart
Leaving Jetty Road by Rebecca Burton
The Hunt Club by Bret Lott
Riding In Cars With Boys by Donofrio, Beverly