Share via


Authoring Match Patterns

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

The match attribute on the <xsl:template> element contains a Patterns 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.

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

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.

For more information, see Introduction to the Syntax of XPath and 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.

<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

Concepts

Advanced XSLT Functionality
Introduction to the Syntax of XPath
XPath Syntax