XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / next-match
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
The instruction xsl:next-match, like xsl:apply-imports, is used within a template rule to activate the template rule whose attribute match defines the pattern that most closely resembles the current rule’s pattern, adding that rule to processing.
Classification | Instruction |
Function group | template rules and invocation |
Introduction | XSLT 2.0 |
Position in the stylesheet and permitted content:
The instruction xsl:next-match occurs only within template rules, but never inside xsl:for-each or xsl:for-each-group instructions (this causes a runtime error, ERR XTDE0560).
The instruction may contain any sequence of xsl:with-param or xsl:fallback elements. No other content is permitted. Alternatively, the element may be empty.
Attributes:
Standard attributes apply. Otherwise, the instruction xsl:next-match has no element-specific attributes.
Usage:
Like xsl:apply-imports, instruction xsl:next-match activates another template rule applicable to the processed node, ranked below the current rule but with the same mode, or likewise no explicit mode.
Whereas xsl:apply-imports considers only rules from imported modules with lower import precedence than the rule containing the invocation, xsl:next-match can additionally activate rules with lower priority, possibly at the same import precedence.
Choosing the template rule to activate:
When xsl:next-match is used inside a template rule, rules capable of processing the current node but with lower priority are examined first. If any are found, the highest-ranked one is chosen: the next-best match relative to the current rule. (Note: Only rules with a match attribute can be activated. A named template of lower import precedence cannot be activated this way, even from a higher-precedence template with the same name, for example through xsl:apply-imports.)
If no matching lower-priority rule exists, rules with lower import precedence are examined next. Built-in default templates terminate the chain, ensuring that processing never stops for lack of a rule.
Determining a template rule’s priority:
A template rule has lower priority than another in the following cases:
Its
matchpattern matches the current node less explicitly (lower default priority; see the table).A
priorityattribute has a lower value than thepriorityattribute on the compared rule.The explicit
priorityvalue is lower than the other rule’s default priority, derived from itsmatchpattern.
A rule’s default priority (default priority) is determined by the structure of its match pattern and ranges from +0.5 to -0.5. Patterns matching the node more precisely generally rank higher.
Pattern form | Matches | Priority |
|---|---|---|
Pattern naming ancestors | Any node | +0.5 |
| element node | +0.25 |
| Attribute node | +0.25 |
| element node | 0 |
| Attribute node | 0 |
| element node | 0 |
| Attribute node | 0 |
| PI node | 0 |
| element node | -0.25 |
| element node | -0.25 |
| Attribute node | -0.25 |
| Attribute node | -0.25 |
| Element node, | -0.5 |
| Attribute node, | -0.5 |
| Document node | -0.5 |
| Comment | -0.5 |
| Text node | -0.5 |
| Any node | -0.5 |
Table: Default priorities of template rules.
If a priority attribute is set on a rule, its value takes precedence, regardless of a potentially higher default priority.
Automatic priority assignment does not always give the desired result, so consider using the priority attribute. Under the rules above, a template with match="node()[self::*]" receives priority +0.5 because of its predicate, although it matches an element <beispiel> less explicitly than a template with pattern match="beispiel", which receives only priority 0. Downgrading the first rule would make sense here.
Current template rule is preserved:
Activating an imported template does not change the current template rule (current template rule): processing uses the same current node that applied when the activating rule was called. The tunnel-parameter stream is also passed on.
Invocation from a named template:
If an invocation xsl:next-match occurs in a named template, the current template rule is the rule from which that named template was invoked through xsl:call-template, since xsl:call-template also leaves the current template rule unchanged.
No invocation from xsl:for-each or xsl:for-each-group:
Activating an imported or lower-priority rule requires a non-null current template rule at the moment of invocation.
This is always true inside a template rule except within xsl:for-each or xsl:for-each-group instructions. There, the current node changes and the current template rule is temporarily null until the block finishes. Within either instruction, xsl:next-match, like xsl:apply-imports, must therefore not be used. Doing so causes a runtime error that terminates processing (ERR XTDE0560).
No applicable template rule found:
It is not an error if no imported or lower-priority rule can be activated. The instruction then behaves essentially like xsl:apply-templates: it activates the built-in default template rule for the current node’s type.
Any xsl:with-param elements in the invocation are ignored, but the default rule transparently passes on the supplied parameter values. (This mirrors the behavior of xsl:apply-imports and xsl:apply-templates.)
Fallback for XSLT 1.0 processors:
Inside xsl:next-match, alongside xsl:with-param elements, xsl:fallback instructions are allowed. An XSLT 2.0 processor executing xsl:next-match ignores these fallback instructions.
Fallback is useful when an XSLT 1.0 processor processes the stylesheet in forwards-compatible mode (FC Mode). It ignores the unknown enclosing xsl:next-match element but executes its fallback instructions.
Example – a specific template rule invokes a general rule:
A stylesheet has two template rules matching an element <beispiel>:
<!-- Template für wichtige Inhalte -->
<xsl:template match="beispiel[@rang='wichtig']">
<span class="wichtig"><xsl:next-match/></span>
</xsl:template>
<!-- 'normales' Template -->
<xsl:template match="beispiel">
<b>Beispiel: </b><br/><xsl:apply-templates/>
</xsl:template>
The first template, with pattern beispiel[@rang='wichtig'], has higher default priority (+0.5) and processes all relevant <beispiel> elements with the specified attribute value. The lower-priority second rule would normally be ignored.
The xsl:next-match instruction in the first rule also activates the regular template for ordinary <beispiel> elements, with priority 0, and executes its content too.
If an <beispiel> element lacks the required attribute value, the second template is activated directly.
Alternatives in XSLT 1.0:
XSLT 1.0 has no equally simple way to invoke the second rule from the first. One option is to give the second rule an additional name attribute and invoke it from the first using xsl:call-template.
Element definition:
XSLT 1.0:
Element unavailable in XSLT 1.0.
XSLT 2.0:
<!-- Category: instruction -->
<xsl:next-match>
<!-- Content: (xsl:with-param | xsl:fallback)* -->
</xsl:next-match>
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn