Copyright by Michael Kay

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required