XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Variables and parameters / xsl:call-template, name

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

Instruction xsl:call-template explicitly calls another template by name from within a template-block. Only a template, not a template rule in this context, defined by an xsl:template element with a name attribute can be called. A corresponding value must appear in the name attribute of xsl:call-template.

This resembles calling a subroutine: code that performs a task relatively independently of the calling context, such as putting parentheses around a string. This instruction is also fundamental to recursive template calls, especially when combined with parameter passing.

This contrasts with implicit invocation (push approach), where nodes in a sequence assembled using xsl:apply-templates find their own matching template rule. Here, a template is brought directly into processing by name (pull approach). The node to process can be passed as a parameter; otherwise processing continues with the calling template rule’s current node.

The calling context is preserved

Calling xsl:call-template preserves the current node and sequence, unlike xsl:apply-templates, which changes both. After the called template finishes, the processor resumes at the same place.

Like any template, a named template can add content to the result sequence. A subroutine, however, often aims to return a value to its caller. Templates have no “return” facility. Instead, surround the call to xsl:call-template with a variable declaration xsl:variable that captures the generated sequence and stores it as a temporary tree.

The instruction is often empty if the called template can use the calling rule’s current node or only accesses tunnel parameters in the current tunnel stream. Usually, however, xsl:call-template contains one or more xsl:with-param instructions, each passing a value determined by an XPath expression to a parameter slot in the called template. Parameters can thus be passed if xsl:param elements with matching names exist there.

The called template’s name must be explicit and static in the call. It cannot be replaced by a runtime-evaluated variable to select different templates as needed. In any case, the required attribute value templates are not evaluated in the name attribute of xsl:call-template.

To obtain that effect, use xsl:choose to choose between several xsl:call-template instructions, each bearing one of the possible names.

A mode attribute is not available on xsl:call-template, unlike xsl:apply-templates. You cannot use it to distinguish two templates with the same name when calling them. Since dispatch is solely by name, duplicate names are forbidden. This matters especially for included stylesheet modules. For imported named templates, import precedence suppresses imported duplicates. Unlike disabled imported template rules, named templates disabled through importing cannot be used in any way.

The call target: the named template

<xsl:template name="mein_template" match="egal">
  ...
</xsl:template>

<!-- wird aufgerufen durch -->
<xsl:call-template name="mein_template"/>

   

back to top