Chapter 12. XSLT Patterns

A pattern is used in XSLT to define a condition that a node must satisfy in order to be selected. The most common use of patterns is in the match attribute of <xsl:template>, where the pattern says which nodes the template rule applies to. For example, <xsl:template match ="abstract" introduces a template rule that matches every <abstract> element. This chapter defines the syntax and meaning of XSLT patterns.

Patterns (sometimes called match patterns) are used in just six places in an XSLT stylesheet:

  • In the match attribute of <xsl:template>, to define the nodes in a source document to which a template applies

  • In the match attribute of <xsl:key>, to define the nodes in a source document to which a key definition applies

  • In the count and from attributes of <xsl:number>, to define which nodes are counted when generating numbers

  • In the group-starting-with and group-ending-with attributes of <xsl:for-each-group>, to identify a node that acts as the initial or final node in a group of related nodes

Patterns and Expressions

Most of the patterns found in stylesheets are simple and intuitive. For example:

Pattern

Meaning

title

Matches any <title> element

chapter/title

Matches any <title> element whose parent is a <chapter> element

speech[speaker = "Hamlet"]

Matches any <speech> element that has a child <speaker> element whose value is «Hamlet»

section/para[1]

Matches any <para> element that is the first <para> child of a <section> element

The rules for the more complex patterns, ...

Get XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.