XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / The second-best template – xsl:next-match

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 3.)

If a source element needs special handling under certain circumstances, a corresponding template can be prepared. General processing should also always run, however. The problem is that the special template has higher priority, so the default template would never be activated for those special cases:

<!-- das allgemeine Template: -->
<xsl:template match="beispiel">
  <!-- tu was Allgemeines mit <beispiel> -->
  <!-- viele allgemeine Anweisungen: -->
  ...
</xsl:template>
<!-- das Spezial-Template: -->
<xsl:template match="beispiel[@besonders]">
  <!-- tu was Besonderes mit <beispiel> -->
  ...
  <!-- und jetzt auch das Allgemeine... -->
  <!-- viele allgemeine Anweisungen: -->
  ...
</xsl:template>

The special template therefore needs instructions that also enable normal processing. Repeating all the general template’s instructions, as suggested above, would be unsatisfactory. XSLT 2.0 provides instruction xsl:next-match precisely to avoid that. From the active template rule, it calls the rule that also matches the current node and has the next lower specificity, if such a rule exists.

More simply, xsl:next-match activates a lower-ranking rule matching the current node. Here the special template calls the general template:

<!-- das Spezial-Template: -->
<xsl:template match="beispiel[@besonders]">
  <!-- tu was Besonderes mit <beispiel> -->
  ...
  <!-- und jetzt auch das Allgemeine... -->
  <xsl:next-match />
</xsl:template>

As a clearer example, certain <buch> elements in a book catalogue should be handled specially when their optional attribute status has value bestseller or standardwerk. In that case, a corresponding note is output. All books should also be processed by the ordinary template match="buch".

<buch ersch-jahr="2008">
  <buchtitel>Yet Another Hitchhiker's Guide ...</buchtitel>
  ...
</buch>
<buch status="standardwerk" ersch-jahr="1950">
  <buchtitel>Unter Korallen und Haien</buchtitel>
  ...
</buch>
<buch status="bestseller" ersch-jahr="2008">
  <buchtitel>Wunder der Tiefsee</buchtitel>
  ...
</buch>

Code example: kap03/3.02/buchverkauf_next-match.xml (excerpt).

The basic idea is again simple. Three templates are needed, starting with the general template that processes every book:

<xsl:template match="buch">
  <!-- hier die Ausgabe für normale Bücher -->
  <p><b><xsl:value-of select="buchautor"/>: </b><xsl:text>"</xsl:text><xsl:value-of select="buchtitel"/><xsl:text>"</xsl:text></p>
  ...
</xsl:template>

Code example: kap03/3.02/buchverkauf_next-match.xsl (excerpt).

The other two templates take precedence when <buch> has a status attribute with the tested value. However, they use xsl:next-match to include the general template in processing as well:

<xsl:template match="buch[@status='bestseller']">
  <!-- hier die Ausgabe für Bestseller -->
  <p><b>!!! Bestseller !!!</b></p>
  <xsl:next-match />
</xsl:template>
<xsl:template match="buch[@status='standardwerk']">
  <!-- hier die Ausgabe für Standardwerke -->
  <p><i>Standardwerk</i></p>
  <xsl:next-match />
</xsl:template>

Code example: kap03/3.02/buchverkauf_next-match.xsl (excerpt).

The result might look like the following figure:

Output controlled with xsl:next-match

Figure: output controlled with xsl:next-match.

 

back to top