Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
So the reason that a
title
is that it has a parent node, which when used as the context node for the expression
./child::title
returns a sequence that includes that
In an early draft of the XSLT 1.0 specification, the rules allowed almost any path expression to be used as a pattern. For example, you could define a pattern
ancestor::*[3]
, which would match any node that was the great grandparent of some other node in the document. It turned out that this level of generality was neither needed nor possible to implement efficiently, and so a further restriction was imposed, that the only axes you could use in a pattern were the child and attribute axes (the various axes were explained in Chapter 9). A consequence of this is that the only place where the XSLT processor has to look for node
A
(the one to use as a context node for evaluating the expression) is among the ancestors of the node being matched (
N
), including
N
itself.
This brings us to the formal definition of the meaning of a pattern. For the moment let's ignore the complications caused by parentless nodes; I return to these later on page 688.
The node
$N
matches a pattern
PAT
if
$N
is a member of the sequence selected by the expression
root($N)//(PAT)
.
The way this rule is expressed has changed since XSLT 1.0, but the effect is the same. It has become possible to simplify the rule as a result of the generalization of path expressions that has happened in XPath 2.0. In XPath 1.0, XPath expressions such as
//(a|b)
or
//(/a)
were not allowed, so this rule would have made many patterns illegal.
Let's see what this rule means. We start with a node
$N
that we want to test against the pattern. First we find the root node of the tree containing
$N
. Then we look for all the descendant-or-self nodes of this root node, which means all the nodes in the tree except for attributes and namespaces. For each one of these nodes, we evaluate the pattern as if it were an XPath expression, using that node as the context node. If the result includes the original node
$N
, we have a match.
Applying the Definition in Practice