XSLT Variables and Parameters: Definition and Scope

In XSLT stylesheets, <xsl:variable> and <xsl:param> are the primary constructs used to store, evaluate, and pass data during XML transformations. While both hold values that cannot be altered once assigned due to XSLT’s functional design, they serve distinct roles: variables evaluate fixed expressions, whereas parameters accept dynamic input from external processors or calling templates. Mastering the syntax for defining both elements and understanding the structural rules of global versus local scope is essential for creating clean, modular, and maintainable XSLT transformations.

Defining Variables with <xsl:variable>

An XSLT variable stores a value or node-set for reuse. Variables are defined using the <xsl:variable> element and must have a name attribute. A value can be assigned either through a select attribute containing an XPath expression or by placing content inside the opening and closing tags.

<!-- Assigned via XPath select attribute -->
<xsl:variable name="itemCount" select="count(/catalog/item)" />

<!-- Assigned via element content (creates a result tree fragment) -->
<xsl:variable name="formattedHeader">
    <span class="title">Product Catalog</span>
</xsl:variable>

In XSLT, variables are immutable. Once a variable is declared and evaluated within its scope, its value cannot be modified or reassigned like variables in procedural programming languages.

Defining Parameters with <xsl:param>

The <xsl:param> element defines a parameter that can receive an incoming value from an external source or a calling template. Like variables, parameters require a name attribute and can specify a default fallback value via the select attribute or element content. If no value is passed from the caller, the parameter defaults to the specified value.

<xsl:param name="currencyCode" select="'USD'" />

Global Scope vs. Local Scope

The scope of both variables and parameters is determined strictly by their position in the stylesheet hierarchy:

Parameter and Variable Shadowing

If a local variable or parameter shares the same name as a global one, the local declaration shadows (overrides) the global value within that local scope. However, declaring two variables or parameters with the exact same name within the same local block is invalid and results in a transformation error.

Passing Values to Templates

To pass dynamic values into a template parameter, use the <xsl:with-param> instruction inside <xsl:call-template> or <xsl:apply-templates>. The name attribute of <xsl:with-param> must match the corresponding <xsl:param> defined at the very beginning of the target template.

<!-- Calling a template and passing a parameter -->
<xsl:call-template name="renderPrice">
    <xsl:with-param name="discountRate" select="0.15" />
</xsl:call-template>

<!-- Target template receiving the parameter -->
<xsl:template name="renderPrice">
    <xsl:param name="discountRate" select="0.0" />
    <xsl:value-of select="price * (1 - $discountRate)" />
</xsl:template>

Referencing both variables and parameters anywhere in XPath expressions is accomplished by prefixing the identifier name with a dollar sign ($identifierName).