XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / call-template

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

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

 

The command xsl:call-template calls a named template rule like a subroutine, optionally passing parameter values.

Classification

Instruction

Function group

template rules and calls

Introduction

XSLT 1.0

Position in the stylesheet and permitted contents:

xsl:call-template is an instruction within a template block. It may be empty but usually contains one or more xsl:with-param instructions to pass values to the called template, provided corresponding xsl:param elements exist, or to add tunnel parameters to the tunnel stream.

Attributes:

Standard attributes apply. The instruction xsl:call-template has a required attribute name whose value must be a QName.

name

Value

QName

Usage

Required

Introduction

XSLT 1.0

The value of the name attribute is a qualified name, so it may have a namespace. The QName must identify a named template in the stylesheet. If none exists, a static error occurs (ERR XTSE0650).

The called template’s name must be explicit, even when different templates are desired on demand. It cannot be replaced with a variable evaluated at runtime.

Purpose:

The instruction xsl:call-template explicitly calls another template by name from a template block, like a subroutine. A named template can perform fixed tasks or output content independently of the caller’s context.

A named-template call differs from implicit rule invocation through sequences constructed by xsl:apply-templates. The current rule, and therefore the calling template’s current node, remains unchanged by xsl:call-template, so the called template can still access the same current node.

Alternatively, pass a sequence as a parameter. This makes xsl:call-template central to recursive processing with self-calling named templates.

A named template can output content into the result sequence, like any template, but xsl:call-template cannot directly return a value to the call site: there is no return facility. Instead, wrap xsl:call-template in a variable definition xsl:variable to capture the output.

No template modes for named templates:

The instruction xsl:call-template, unlike xsl:apply-templates, has no mode attribute. Modes therefore cannot distinguish same-named templates. Names alone control selection, so two same-named templates with equal import precedence are forbidden.

Note – mode requires match
A named template with a match attribute may also carry mode. However, mode matters only when used as a template rule, not when called through xsl:call-template.

Passing parameter values:

The instruction xsl:call-template may contain one or more xsl:with-param elements passing a value to corresponding xsl:param instructions in the named template. No two xsl:with-param elements may use the same name.

XSLT 2.0 has two static errors related to passing values from xsl:call-template, preventing stylesheet processing:

  1. Passing a value to a nonexistent target parameter (ERR XTSE0680).

  2. Omitting a value for a target parameter whose required is set to "yes" (ERR XTDE0700).

Beware of passing values to nonexistent parameters
In XSLT 2.0, it is a static error when the target template has no xsl:with-param instruction with corresponding xsl:param elements (ERR XTSE0680). This excludes backwards-compatible mode and tunnel parameters. XSLT 1.0 simply ignores the attempted value transfer.

Examples:

Example 1 – A named template and matching call:

<!-- ein benanntes Template: -->
<xsl:template name="mein_template" match="egal">
  ...
</xsl:template>
<!-- wird aufgerufen durch: -->
<xsl:template select="/">
  <xsl:call-template name="mein_template"/>
</xsl:template>

Declaration and call order does not matter: names and bodies are stored on reading the stylesheet. Dynamic names are forbidden to ensure unambiguous processing; identifiers are checked in the static context.

Example 2 – Capturing a template result in a variable:

...
<xsl:variable name="rueckgabewert">
  <xsl:call-template name="berechne-was">
    <xsl:with-param name="die-werte" select="$werte"/>
  </xsl:call-template>
</xsl:variable>
<!-- Rückgabewert ausgeben -->
Das Ergebnis ist <xsl:value-of select="$rueckgabewert"/>
...

Note – xsl:variable content is not output directly
Content inside xsl:variable never goes directly to the result, but is stored in the variable as a temporary tree (temporary tree).

Example 3 – Recursion with named templates:

<!-- erster Aufruf des benannten Templates -->
...
<xsl:call-template name="rekursiv">
  <xsl:with-param name="verarbeiten" select="$anfangswert"/>
</xsl:call-template>
...
<!-- das rekursive Template -->
<xsl:template name="rekursiv">
  <xsl:param name="verarbeiten" select="defaultwert"/>
  <!-- irgendeine Verarbeitung speichert in "$restwert" -->
  ...
  <!-- das Template ruft sich selbst auf: -->
  <xsl:call-template name="rekursiv">
    <xsl:with-param name="verarbeiten" select="$restwert"/>
  </xsl:call-template>
</xsl:template>

Warning – Infinite loops in recursive calls
Avoid an infinite loop. A usable version needs a termination condition, for example with xsl:if.

Element definition:

XSLT 1.0:

<!-- Category: instruction -->
<xsl:call-template 
     name = qname>

     <!-- Content: xsl:with-param* -->

</xsl:call-template>

XSLT 2.0:

<!-- Category: instruction -->
<xsl:call-template
     name = qname>

     <!-- Content: xsl:with-param* -->

</xsl:call-template>

   

  

back to top