Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Defining Match Patterns in <xsl:template>

The match attribute on the <xsl:template> element contains a pattern expression. The syntax is the same as that used to select nodes with <xsl:for-each>, <xsl:value-of>, and <xsl:apply-templates> elements. However, the pattern is used in quite a different way.

In a select pattern, the pattern describes a query down from a particular node to locate a new set of nodes. A match pattern is quite different. The match pattern does not locate anything new, but rather compares a specific node against a pattern to see if the node matches that pattern, and thus whether or not to use a particular template.

Here are some of the differences between the use of match patterns and select patterns.

Select pattern Match pattern
Starts with a node (the context), and identifies more nodes. Starts with a node (the target), and yields a true/false (match/didn't match) result.
Starts at the context and drills down into the data. Starts by testing the node against the node target, and then checks the context, including ancestry and descendants.
Represents a query into an XML document. Represents a pattern of contextual information against which a particular node can be tested. Match patterns are quite similar to selectors in cascading style sheets (CSS).

The simplest match pattern is just a single element name, such as "section", or node type, such as "text()". If a text node matches the "text()" pattern, the associated template will be used. If a <section> element matches the "section" pattern, the associated template will be used.

Matches become more complex when they are used to test not only a node, but also the context in which the node appears in the source document. The pattern "section/title" will match <title> elements that are children of <section> elements. Any <title> elements that are not within a <section> (for example, the document title) do not match this pattern, and a separate template must be defined (such as "document/title"). The target of the pattern (the term farthest to the right) represents the node to be tested. This is similar to a select pattern in which the target represents the nodes to select.

The following table lists some match patterns and their meanings.

Pattern Meaning when used as a match pattern
match="title" Matches <title> elements.
match="section/title" Matches <title> elements that are children of <section> elements.
match="section//title" Matches <title> elements contained within <section> elements.
match="section/title[@short-name]" Matches <title> elements that are children of <section> elements, and that have a short-name attribute.
match="appendix//section[@type='reference']/title" Matches <title> elements that are children of <section> elements. The section must also have a type attribute with the value "reference", and have an <appendix> ancestor.
match="appendix[.//section[@type='reference']/title]" Matches <appendix> elements that contain <section> descendants. These <section> elements, in turn, must have both a type attribute with the value "reference" and a <title> child.

For more information, see Introduction to XPath Syntax and XPath Syntax.

You can add another template to the pole.xml sample to provide different formatting (H3 instead of H2) for sections that appear in the context of another section. For example:

<xsl:template match="section">
  <DIV>
    <H2><xsl:value-of select="title"/></H2>
    <xsl:apply-templates />
  </DIV>
</xsl:template>

<xsl:template match="section/section">
  <DIV>
    <H3><xsl:value-of select="title"/></H3>
    <xsl:apply-templates />
  </DIV>
</xsl:template>

Notice that a <section> within a section will match the "section" pattern as well as the "section/section" pattern. Which template will be selected? The rule is as follows: Templates later in the style sheet override earlier templates. To ensure that the XSLT processor checks the "section/section" pattern first, place it farther down in the style sheet. Most of the templates in this sample are mutually exclusive, so their order is unimportant.