XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – getting started / Structure controls processing / One template rule per element – almost…

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

Here we reduce complexity and add a number of simple template rules instead.

Starting with stylesheet adressen.xsl from A comparison: expression versus instruction, make a few changes. In the template rule for <adresse>, omit attribute select from xsl:apply-templates:

<!-- Template-Regel für <adresse>: -->
<xsl:template select="adresse">
  <!-- Sequenz der Kindelemente von Adresse: -->
  <p><xsl:apply-templates/></p>
  <!-- Trennlinie: --><hr/>
</xsl:template>

A horizontal separator should follow each address, using HTML element <hr> in XML notation in the stylesheet. The rule for <name> is also adjusted slightly:

<!-- Template-Regel für <name>: -->
<xsl:template match="name">
  <xsl:text>Name: </xsl:text>
  <b><xsl:apply-templates/></b><br/>
</xsl:template>

Note:
Instruction xsl:text prevents unnecessary copying of the line break after the template rule’s start tag.

The template rule for <anschrift> is radically simplified:

<!-- Template-Regel für <anschrift>: -->
<xsl:template match="anschrift">
  <xsl:text>Anschrift: </xsl:text>
  <xsl:apply-templates/>
</xsl:template>

These rules are added “in compensation”:

<!-- Template-Regel für <strasse>: -->
<xsl:template match="strasse">
  <xsl:apply-templates/>
</xsl:template>

<!-- Template-Regel für <nr>: -->
<xsl:template match="nr">
  <xsl:apply-templates/>
</xsl:template>

<!-- Template-Regel für <plz>: -->
<xsl:template match="plz">
  <xsl:apply-templates/>
</xsl:template>

<!-- Template-Regel für <ort>: -->
<xsl:template match="ort">
  <xsl:apply-templates/>
</xsl:template>

Code example: kap01/1.17.2/addressen.xsl (excerpt).

“They do nothing,” you might object. And why use xsl:apply-templates rather than xsl:value-of? Indeed, activating these rules does nothing else. For now, these elements only need their text nodes transferred to the result. Once the rules exist, however, extending them is easy.

Example extensions

To apply CSS formatting to the HTML result, you want HTML element <span> to include suitable CSS classes? No problem:

<!-- alternative Template-Regel für <ort> mit CSS-Klasse: -->
<xsl:template match="ort">
  <span class="ort"><xsl:apply-templates/></span>
</xsl:template>

Why use xsl:apply-templates as the inner instruction? This is more a matter of principle; for this source document, xsl:value-of would work equally well.

But if an element contains further elements as well as text, these would not be processed separately; only their string contents would be extracted.

Using xsl:apply-templates creates an exit in a template rule through which the processor can activate rules for child elements, if present. Using xsl:apply-templates everywhere means you need not worry about that.

Consistent use of xsl:apply-templates lets the source document’s content order, its structure, control processing.

Without attribute select, xsl:apply-templates constructs a sequence of all children of the context node. Its default behavior is equivalent to select="node()". All node kinds are selected, including text, so those nodes are certainly processed, which is all we intend. Also, <xsl:apply-templates/> is shorter (Note: Under certain circumstances, xsl:value-of may also omit attribute select, but not here.) than <xsl:value-of select="."/>.

One template rule for two elements

If two elements should always be processed identically, use a shared template rule. Pass two matching patterns to the rule’s attribute match, separating them with the OR operator “|”:

<!-- Template-Regel für <plz> und <nr>: -->
<xsl:template match="plz | nr">
  <xsl:apply-templates/>
</xsl:template>

Three or more alternative patterns are also possible. The order of patterns in the expression does not matter because each is tested independently.

   

back to top