Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The next section
The Formal Definition
gives the formal definition of patterns in terms of expressions. In practice, it's easier to think of most patterns as following their own rules—rather like the intuitive examples listed earlier—and referring to the formal definition only to resolve difficult cases. So I'll follow the formal explanation with an informal definition that's not only more intuitive, but also closer to the way most implementations are likely to work.
When patterns are used in template rules, we need to consider what happens if the same node is matched by more than one pattern. This situation is discussed in the section
Conflict Resolution
on page 686 in this chapter.
A new feature of the XPath 2.0 data model is that it is possible to create nodes in a tree whose root node is not a document node. Such trees cause additional complications for the semantics of pattern matching. The section
Matching Parentless Nodes
on page 688 explains how these nodes are handled.
The bulk of the chapter is then devoted to an explanation of the syntax of patterns, and the usage of each syntactic construct that can appear in a pattern.
Changes in XSLT 2.0
The syntax of patterns is probably the area of the specification that has changed least between XSLT 1.0 and XSLT 2.0. However, because many capabilities of patterns are picked up implicitly by virtue of the way they are defined in terms of expressions, they benefit automatically from many of the new features in XPath 2.0.
The most notable facility this introduces is the ability to match nodes according to their schema-defined type. For example, you can match all date-valued attributes with the pattern
match=“attribute(*, xs:date)”
, and you can match all elements in the substitution group of the
event
element with the pattern
match = “schema-element(event)”
.
There are also some changes in the semantics of patterns, designed to cope with the complications introduced by parentless element and attribute nodes.
In XSLT 1.0 there were three restrictions on the content of a predicate used in a pattern. All three restrictions have been removed in XSLT 2.0.
The Formal Definition
The XSLT specification defines the way patterns are evaluated in terms of the XPath expression that is equivalent to the pattern. We've already seen that every pattern is a valid XPath expression. In fact, the rules are written so that the only XPath expressions that can be used as patterns are those that return a sequence of nodes. The idea is that you should be able to decide whether a node matches a pattern by seeing whether the node is in the sequence returned by the corresponding expression.
This then raises the question of context. The result of the XPath expression
title
is all the
title
to match every
N
) matches the pattern
title
if we can find a node (
A
, say) anywhere in the document, which has the property that when we take
A
as the context node and evaluate the expression
title
the node
N
will be selected as part of the result. In this example, we don't have to look very far to find node
A
: in fact, only as far as the parent node of
N
.