XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Templates and template modes

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

If the same elements are output repeatedly in different contexts, the same template rule generally cannot be used. For example, a book chapter’s headings <titel> may be addressed at two points in a stylesheet: when generating a table of contents and when outputting text as chapter headings.

Template rules in different modes: the mode attribute

The same formatting cannot be used, because the heading text is used once in an HTML <li> list item and elsewhere in an HTML <h1> container:

<!-- für das Inhaltsverzeichnis -->
<xsl:template match="titel">
  <li><xsl:value-of select="."/></li>
</xsl:template> 
<!-- für die Textausgabe im Buch -->
<xsl:template match="titel">
  <h1><xsl:value-of select="."/>
</xsl:template>

This clearly creates a template conflict, since both template rules match exactly the same node. In practice, the processor will probably resolve this by considering only the last rule of equal precedence. That would be highly undesirable here.

The solution: the mode attribute

To solve this problem, xsl:template has another attribute not mentioned so far: mode. This creates an artificial distinction between otherwise identical templates. Extend the two templates above accordingly to make them distinguishable to the processor:

<!-- für das Inhaltsverzeichnis -->
<xsl:template match="titel" mode="inhalt">
  <li><xsl:value-of select="."/></li>
</xsl:template> 
<!-- für die Textausgabe im Buch -->
<xsl:template match="titel" mode="text">
  <h1><xsl:value-of select="."/>
</xsl:template>

A template is essentially called indirectly. For a constructed node sequence, the processor finds the template with the best-matching attribute match. The dilemma was that two such templates existed. At least one step toward a solution has now been taken.

Which mode should be used?

With attribute mode now set, the processor still needs to know which template mode to use. Therefore, add the mode attribute to the call xsl:apply-templates when constructing the sequence:

<xsl:template match="inhalt">
  <div>Inhaltsverzeichnis:
    <ul>
      <xsl:apply-templates select="//titel" mode="inhalt"/>
      ...
    </ul>
  </div>
</xsl:template> 
<xsl:template match="kapitel">
  <xsl:apply-templates select="titel" mode="text"/>
  <!-- und den Kapiteltext verarbeiten -->
</xsl:template>

A template with attribute mode is invoked only if the call also sets a corresponding attribute mode. If no corresponding attribute mode is found, a modeless template is not used; no processing occurs! Templates with and without modes can therefore coexist.

Named templates: mode not needed

A named xsl:template element has no template mode. This would make little sense, since it is called through xsl:call-template, which does not support passing a mode.

However, a named template may also have attribute match and thus also serve as a template rule. It can then be invoked through xsl:apply-templates, including with a mode. Therefore, it can also have attribute mode. When called by template name through xsl:call-template, neither attribute match nor mode is considered.

   

back to top