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-with-param.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.3 KiB

title, description, date, version, versionLabel, category, syntax, tags
title description date version versionLabel category syntax tags
xsl:with-param Supplies a parameter value to a called or applied template, overriding that template's declared default. 2026-04-18T00:00:00Z 1.0 XSLT 1.0 element <xsl:with-param name="name" select="expression"/>
xslt
reference
xslt1

Description

xsl:with-param is used inside xsl:call-template or xsl:apply-templates to pass a value to a parameter declared with xsl:param in the target template. It is the mechanism that allows XSLT's otherwise static templates to behave like parameterised subroutines.

The name attribute must match the name of an xsl:param in the called or matched template. The value is specified either via the select attribute (an XPath expression evaluated in the current context) or via element content (a result tree fragment). If the target template does not declare a parameter with that name, the value is silently ignored in XSLT 1.0.

xsl:with-param must appear as a child of xsl:call-template or xsl:apply-templates before any other content. When used with xsl:apply-templates, all selected nodes receive the same parameter value.

Attributes

Attribute Type Required Description
name QName Yes Name of the target parameter, matching an xsl:param declaration.
select XPath expression No Expression evaluated in the calling context; its result is the parameter value.
as SequenceType No (2.0+) Expected type of the value being passed.
tunnel yes / no No (2.0+) If yes, the parameter is forwarded through intermediate templates automatically.

Examples

Passing a counter through recursive calls

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<root><count>5</count></root>

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="countdown">
    <xsl:param name="n"/>
    <xsl:if test="$n > 0">
      <xsl:value-of select="$n"/>
      <xsl:text>&#10;</xsl:text>
      <xsl:call-template name="countdown">
        <xsl:with-param name="n" select="$n - 1"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template match="/root">
    <xsl:call-template name="countdown">
      <xsl:with-param name="n" select="count"/>
    </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>

Output:

5
4
3
2
1

Passing a label to applied templates

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<section prefix="SEC">
  <item>Alpha</item>
  <item>Beta</item>
</section>

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 match="/section">
    <list>
      <xsl:apply-templates select="item">
        <xsl:with-param name="label" select="@prefix"/>
      </xsl:apply-templates>
    </list>
  </xsl:template>

  <xsl:template match="item">
    <xsl:param name="label"/>
    <entry id="{$label}-{position()}">
      <xsl:value-of select="."/>
    </entry>
  </xsl:template>
</xsl:stylesheet>

Output:

<list>
  <entry id="SEC-1">Alpha</entry>
  <entry id="SEC-2">Beta</entry>
</list>

Notes

  • In XSLT 1.0, passing a parameter to a template that does not declare it causes no error — the value is silently discarded. This can hide mistakes; double-check parameter names carefully.
  • xsl:with-param is evaluated in the context of the calling template, not the called template. XPath expressions see the calling template's variables and context node.
  • When used inside xsl:apply-templates, all matching templates receive the same parameter values. There is no per-node variation unless you compute different values per node inside the templates themselves.
  • Content-based xsl:with-param (no select, with element children) produces a result tree fragment in XSLT 1.0, subject to the same RTF restrictions as xsl:variable.

See also