Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

The matchattribute on the <xsl:template> element contains a Patternsexpression. 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.

The following table shows some of the differences between the use of match patterns and 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/did not 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 not only to test a node, but also to test 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". The target of the pattern, or the rightmost term, represents the node to be tested, in a way similar to a selectpattern 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

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

appendix//section[@type='reference']/title

Matches title elements that are children of section elements. The section must also have a typeattribute 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 typeattribute with the value "reference" and a title child.

For more information, see Introduction to the Syntax of XPathand XPath Syntax.

You can add another template to the Pole sample to provide different formatting, such as H3 instead of H2, for sections when they appear in the context of another section, as in the following code example.

Copy Code
<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. To determine which template will be selected, the rule is that 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.

See Also