XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Variables and parameters / Tunnel parameters

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

Handling a parameter passed in a call is fairly simple as long as it is used directly in the called template. Things become more complex, and impossible in XSLT 1.0, when a template rule or named template must be skipped because the parameter is read only by a later template, while explicit forwarding is impossible or undesirable.

XSLT 2.0 addresses this problem with tunnel parameters. Such a parameter is effectively thrown into the processing stream and can be retrieved by a later template. The medium through which it travels is aptly called the tunnel stream.

Like any other parameter, a tunnel parameter is created with xsl:with-param. The only addition to the usual syntax is the tunnel attribute:

<xsl:with-param name="p1" tunnel="yes">
  Nachricht von Template 1
</xsl:with-param>

The mysterious tunnel parameter

A small, somewhat playful example illustrates the principle. It works with any source document because it does not access its information.

A template rule invoked for the document node calls a named template while releasing parameter $param_1 with tunnel="yes" into the tunnel stream. Strange things then happen to it: its output value suddenly changes.

Figure: The mysterious tunnel parameter (tunnel.html).

Here things become interesting. Surely parameter values cannot be overwritten? Or can they?

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xhtml">
  <xsl:output method="xhtml" encoding="ISO-8859-1" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
  <xsl:template match="/">
    <html>
      <head><title>Tunnelbotschaften</title></head>
      <body>
        <h1>Ein Template schickt eine Nachricht:</h1>
        <p>Die Nachricht lautet: Nachricht vom Haupttemplate.</p>
        <xsl:call-template name="template1">
          <xsl:with-param name="param_1" tunnel="yes">Nachricht vom Haupttemplate</xsl:with-param>
        </xsl:call-template>
      </body>
    </html>
  </xsl:template>
  <xsl:template name="template1">
    <xsl:param name="param_1">Da gehe ich nicht darauf ein!</xsl:param>
    <p><b>Wert von $param_1 in Template 1: </b><xsl:value-of select="$param_1"/></p>
    <xsl:call-template name="template2"/>
  </xsl:template>
  <xsl:template name="template2">
    <p>Hier Template 2: Hier ist weiter nicht los. Übergebe an Template 3!</p>
    <xsl:call-template name="template3"/>
  </xsl:template>
  <xsl:template name="template3">
    <xsl:param name=" param_1" tunnel="yes">Hier steht ein Defaultwert</xsl:param>
    <p><b>Wert von $param_1 in Template3: </b><xsl:value-of select="$param_1"/></p>
    <xsl:if test="not(contains($param_1, 'Stop'))">
      <xsl:call-template name="template4"/>
    </xsl:if>
  </xsl:template>
  <xsl:template name="template4">
    <p>Hier Template 4: Hier ist auch nichts los. Übergebe an Template 5! Anbei eine neue Nachricht an alle, die es angeht.</p>
    <xsl:call-template name="template5">
      <xsl:with-param name="param_1" tunnel="yes">Neue Nachricht von Template 4.</xsl:with-param>
    </xsl:call-template>
  </xsl:template>
  <xsl:template name="template5">
    <xsl:param name=" param_1" tunnel="yes">Hier steht ein Defaultwert</xsl:param>
    <p><b>Wert von $param_1 in Template 5: </b><xsl:value-of select="$param_1"/></p>
    <p>Gebe zurück an Template 3! Und Stop.</p>
    <xsl:call-template name="template3"/>
  </xsl:template>
</xsl:stylesheet>

Code example: kap03/3.01.6/tunnel.xsl.

The stylesheet contains a chain of named templates calling one another. The first, Template 1, has a parameter with the required name. However, without the tunnel attribute set to "yes", it has no “fishing licence” to retrieve the parameter value from the tunnel stream. It uses its default value and calls Template 2. That template has no parameter at all: it merely outputs a message and calls Template 3.

Template 3 also has parameter param_1, this time with attribute tunnel="yes". Since a corresponding parameter is in the tunnel stream, it is read and displayed. Template 3 calls Template 4, which reports its presence and calls Template 5, passing another parameter param_1 with the tunnel attribute, but a new value.

Template 5 also has the required parameter. It reads the new value and calls Template 3 again. Template 3 rereads the parameter and now sees the new value. Has an assignment actually occurred? It appears so, but it has not: that would violate a fundamental XSLT principle.

What actually happens is not reassignment, but an unusual form of binding shadowing. Several parameters with the same name and identifier can coexist in the tunnel stream. A new parameter introduced in a processing branch hides its namesake predecessor.

The earlier parameter is neither deleted nor overwritten; it simply cannot be read. As soon as the new parameter leaves scope, the earlier one becomes visible again with its original value.

The following tunnel parameter properties apply here:

  • A tunnel parameter is visible in all template rules subordinate to the one from which it was passed.

  • A tunnel parameter can be hidden by another tunnel parameter of the same name.

  • A tunnel parameter can be read only by xsl:param with tunnel="yes", even when the identifier matches.

  • Reading a tunnel parameter does not delete it; it remains available to subordinate templates.

Note:
For demonstration, this example directs the processing branch back into itself. This is risky because such use of named templates causes an infinite loop. A code word in the newly passed value prevents this, somewhat inelegantly, by stopping another call to the next template.

How useful this apparent reassignment of parameter values will be in practice remains to be seen. Alongside this rather nebulous form of tunnelling through templates, however, there is another, less spectacular and less hidden, but all the more practical form.

   

back to top