The match attribute on the <xsl:template> element contains an XSL Pattern expression. The syntax is the same as that used to select nodes with <xsl:for-each>, <xsl:value-of>, and <xsl:apply-templates>. 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 doesn't locate anything new, but rather compares a specific node against an XSL 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 XSL Patterns as match patterns or select patterns.
Select patterns | Match patterns |
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, 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 CSS. |
The simplest match pattern is just a single element name (such as "section") or node type (such as "text()"). It is pretty simple to see whether a node matches or not. A text node will match the "text()" pattern and the associated template will be used; a "section" element will match the "section" pattern and that template will be used.
Matches become a bit more complex when they are used to not only test 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. "title" elements 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"). Note that the target of the pattern (the rightmost term) represents the node to be tested, similar to in a "select" pattern where the target represents the nodes to select.
Here are some match patterns and their meanings:
XSL Pattern | Meaning when used as a match pattern |
title | Matches "title" elements. |
section/title | Matches "title" elements that are children of "section" elements. |
section//title | Matches "title" elements contained within "section" elements. |
section/title[@short-name] | Matches "title" elements that are children of "section" elements, and that have a "short-name" attribute. |
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. |
appendix[.//section[@type='reference']/title] | Matches "appendix" elements that contain "section" descendants, which in turn have both a "type" attribute with the value "reference" and a "title" child. |
See also Introduction to the Syntax of XSL Patterns and the XSL Patterns Reference.
You can add another template to the Pole sample, to provide different formatting (H3 instead of H2) for sections when they 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 pattern "section" as well as the "section/section" pattern. Which template will be selected? The rule is: templates later in the style sheet override earlier templates. To ensure that the XSL 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.