XSLT patterns
This section gives an informal description of the syntax of XSLT patterns. For a formal specification, see the XSLT recommendation.
The documentation here describes patterns in XSLT 3.0. There are extensions in the draft 4.0 specifications, which are mentioned here when appropriate: be aware, however, that these may change.
Patterns define a condition that an item may or may not satisfy: an item either matches the pattern, or it does not. The syntax of patterns in XSLT 3.0 is a subset of that for the XPath syntax. In XSLT 3.0 there are two kinds of pattern:
-
Path patterns. Formally, a node matches a path pattern if it is a member of the node set selected by the corresponding expression, with some ancestor of the node acting as the current node for evaluating the expression. For example a
<TITLE>node matches the patternTITLEbecause it is a member of the node set selected by the expressionTITLEwhen evaluated at the immediate parent node. -
Predicate patterns, for example
.[@x = 2]. A predicate pattern consists of "." followed by zero or more predicates, and it matches an item if all the predicates are true when applied to that item. Predicate patterns can be used to match atomic items and maps as well as nodes.
XSLT 4.0 introduces another kind of pattern, the type pattern. This simply matches
items by their item type, introduced with the ~ (tilde) character. For example ~map(*)
matches any map, and ~xs:string matches a string. The type can be followed by predicates, for
example ~map(*)[?age gt 21] matches any map having a field with key age whose
value exceeds 21.
Patterns are used primarily in the match attribute of the xsl:template element. They are also
used in the count and from attributes of xsl:number, the match
attribute of xsl:key, and the
group-starting-with and group-ending-with attributes of xsl:for-each-group.
The next page gives some examples of match patterns and their meaning. This is followed by a page that gives a summary of the XSLT 3.0 syntax.