XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Variables and parameters / xsl:with-param

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

The instruction xsl:with-param creates a parameter value and passes it, when calling subordinate templates or template rules, either directly to their parameters or, with tunnel="yes", to the tunnel stream available to the called templates and their descendants.

<xsl:with-param name="qname" select="XPath-Ausdruck">
  <!-- hier kann ein Template-Block stehen -->
</xsl:with-param>

The instruction xsl:with-param is mainly used when calling named templates inside xsl:call-template. It sets the value of a parameter with the same name, xsl:param, in the called template. That parameter must always be its first child element, preceding any literal result elements or other XSLT instructions.

The instruction may also occur inside xsl:apply-templates and inside xsl:apply-imports. Several xsl:with-param may appear consecutively; their order does not matter. However, each parameter may be passed only once in the same context: all xsl:with-param occurring together must target different parameters. Passing values under the same name is prohibited even if one goes to the tunnel stream, with tunnel="yes", and the other directly.

Determining the value as with xsl:variable

Inside xsl:with-param, a literal expression or template block can be evaluated and passed to the target parameter. When evaluating the template block, instruction xsl:with-param starts from the same current node as the enclosing element (xsl:call-template, xsl:apply-templates or xsl:apply-imports).

If the instruction is empty, containing no template block, the passed value comes from attribute select. If that too is absent, an empty string is passed as the parameter value.

Thus, the passed value is determined in the same way as a variable value with xsl:variable.

Example: A counter using recursive template calls

To count a template’s iterations, you cannot simply set a counter variable:

for(i=0; i<10; i++){ ... tu etwas zehnmal ... }

This cannot be expressed so simply in XSLT (Note: Using xsl:for-each, XPath 2.0 offers a simple trick, shown in connection with stylesheet functions ), because of freedom from side effects. However, you can simulate a counter’s iteration using recursion. Simply make a named template call itself, passing the latest counter value as an input parameter:

<xsl:template name="zaehler">
  <xsl:param name="der_zaehler" select="0"/>
  <xsl:variable name="neuer_zaeh¬ler">
    <xsl:value-of select="$der_zaehler + 1"/>
  </xsl:variable>
  <xsl:if test="$neuer_zaehler &lt; 10">
    <xsl:call-template name="zaehler">
      <xsl:with-param name="der_zaehler" select="$neuer_zaehler"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

The condition xsl:if or a similar construct at the end is essential. This is the only way to prevent an “infinite loop” in which the template continually calls itself. As with any conventional loop, a termination condition is required. The same precaution is necessary for ordinary WHILE or DO WHILE loops elsewhere.

Here is the counter template with a few comments added:

<xsl:template name="zaehler">
  <!-- beim Start ist der Zähler 0: -->
  <xsl:param name="der_zaehler" select="0"/>
  <!-- neue Variable – hier wird gezählt: -->
  <xsl:variable name="neuer_zaehler">
    <xsl:value-of select="$zaehler + 1"/>
  </xsl:variable>
  <!-- Abbruchbedingung prüfen: -->
  <xsl:if test="$neuer_zaehler &lt; 10">
    <!-- Template wieder aufrufen -->
    <xsl:call-template name="zaehler">
      <!-- den Zählerstand übergeben: -->
      <xsl:with-param name="der_zaehler" select="$neuer_zaeh¬ler"/>
      <!-- überschreibt den Defaultwert -->
    </xsl:call-template>
  </xsl:if>
</xsl:template>

Code example: kap03/3.01.5/zaehler.xsl.

   

back to top