1
0
mirror of https://github.com/alexandrev/xslt-lab.git synced 2026-09-18 19:43:16 +00:00
Files
xslt-lab/site/content/xslt/functions/xsl-call-template.md
T
alexandrev-tibco 53e90ef86e feat(blog): complete XSLT/XPath reference — 229 function pages
Full coverage of XSLT 1.0, 2.0 and 3.0 elements and XPath functions:
- 59 XSLT elements (xsl:stylesheet → xsl:use-accumulators)
- 170 XPath functions (1.0 node/string/numeric/boolean, 2.0 sequence/
  date/QName/string, 3.0 HOF/map/array/JSON/streaming)
Each page: description, parameters table, return value, 2 runnable
Saxon examples with input XML + stylesheet + output, notes, cross-links.
xsltCompletions.js: all 229 entries now have blogSlug for hover links.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 09:28:05 +02:00

4.4 KiB

title, description, date, version, versionLabel, category, syntax, tags
title description date version versionLabel category syntax tags
xsl:call-template Invokes a named template by name, optionally passing parameters, without changing the current context node. 2026-04-18T00:00:00Z 1.0 XSLT 1.0 element <xsl:call-template name="name"/>
xslt
reference
xslt1

Description

xsl:call-template invokes a template that was declared with the name attribute on xsl:template. Unlike xsl:apply-templates, which selects nodes and fires matching rules, xsl:call-template is a direct, unconditional call — similar to a function call in a procedural language. The context node, position, and size remain exactly what they were in the calling template; the named template operates in that same context.

Named templates serve as reusable subroutines in XSLT. Because XSLT variables are immutable, the primary technique for producing loops or accumulations is recursive named-template calls, where each invocation passes updated values via xsl:with-param.

Parameters are passed via child xsl:with-param elements. Inside the named template, xsl:param elements declare the expected parameters and provide default values. If a with-param element supplies a value, it overrides the default.

Attributes

Attribute Type Required Description
name QName Yes The name of the template to call. Must match the name attribute of an xsl:template in scope.

Examples

Reusable formatting template

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<report>
  <product name="Alpha" price="1200"/>
  <product name="Beta" price="450"/>
  <product name="Gamma" price="89"/>
</report>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template name="format-currency">
    <xsl:param name="amount"/>
    <xsl:text>$</xsl:text>
    <xsl:value-of select="format-number($amount, '#,##0.00')"/>
  </xsl:template>

  <xsl:template match="/report">
    <prices>
      <xsl:for-each select="product">
        <row>
          <name><xsl:value-of select="@name"/></name>
          <price>
            <xsl:call-template name="format-currency">
              <xsl:with-param name="amount" select="@price"/>
            </xsl:call-template>
          </price>
        </row>
      </xsl:for-each>
    </prices>
  </xsl:template>
</xsl:stylesheet>

Output:

<prices>
  <row><name>Alpha</name><price>$1,200.00</price></row>
  <row><name>Beta</name><price>$450.00</price></row>
  <row><name>Gamma</name><price>$89.00</price></row>
</prices>

Recursive call for string padding

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<label>Warning</label>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template name="pad-right">
    <xsl:param name="str"/>
    <xsl:param name="width" select="20"/>
    <xsl:param name="char" select="'.'"/>
    <xsl:value-of select="$str"/>
    <xsl:if test="string-length($str) &lt; $width">
      <xsl:call-template name="pad-right">
        <xsl:with-param name="str" select="concat($str, $char)"/>
        <xsl:with-param name="width" select="$width"/>
        <xsl:with-param name="char" select="$char"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template match="/label">
    <xsl:call-template name="pad-right">
      <xsl:with-param name="str" select="."/>
    </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>

Output:

Warning.............

Notes

  • The named template must be resolvable at compile time; forward references are allowed — template order in the file does not matter.
  • The context node inside the called template is the same as in the caller. Use xsl:with-param to pass node-set context explicitly if needed.
  • A named template can also have a match attribute, making it both callable by name and matchable by pattern.
  • Recursive calls are the standard XSLT 1.0 technique for iteration, but deep recursion can overflow the processor's stack. For large counts, consider chunk-based recursion strategies.

See also