Defining Match Patterns in <xsl:template>
match attribute on the
<xsl:template> element contains a pattern expression. The syntax is the same as that used to select nodes with
<xsl:apply-templates> elements. However, the pattern is used in quite a different way.
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 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|
For more information, see 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.