Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
XSLT also initializes the context position and size when a sequence of nodes is processed using
In other host languages, for example, in APIs for invoking XPath, it's quite common that there is no provision for setting the context position and size, only the context item. There is no obligation on the host language to provide this capability. It can choose always to set the context position and size to one, or to leave them undefined (in which case it's an error to use the functions
position()
or
last()
).
The context item will very often be a node, but in principle it can be any kind of item, that is, a node or an atomic value. In XSLT 2.0, for example, you can use the
@code
), it will fail with an error.
The focus is initialized by the host language on entry to an XPath expression, but the focus can change when evaluating a subexpression. There are two constructs in XPath that change the focus: the path expression
A/B
, and the filter expression
S[P]
. Path expressions are described in full detail in Chapter 9 of this book, and filter expressions in Chapter 10. Let's take the filter expressions first.
In fact there are two very similar constructs of the form S[P] that use predicates in square brackets, and we'll explain the difference between them in Chapters 9 and 10. For the purpose of this discussion, there is no distinction—they both handle the focus in the same way.
In this construct,
S
is a sequence (that is, it's some expression whose value is a sequence—and as every expression evaluates to a sequence, this actually means it can be any expression whatsoever).
P
is a predicate, which filters the sequence by selecting only those items that match a given condition. So if we write
author[@surname=“Smith”]
we are selecting those
surname
attribute whose value is
Smith
.