XSLT - Working with Parameters and Variables Across Templates in XSLT

Parameters and variables are essential components of XSLT that help developers create flexible, reusable, and maintainable transformations. They allow data to be stored temporarily, passed between templates, and reused throughout the transformation process. Understanding how parameters and variables work is crucial for building efficient XSLT stylesheets.

Understanding Variables in XSLT

A variable in XSLT is used to store a value that can be referenced later in the stylesheet. Variables are declared using the <xsl:variable> element. Once a variable is assigned a value, that value cannot be changed. This means XSLT variables are immutable, unlike variables in many programming languages where values can be modified during execution.

Syntax of a Variable

<xsl:variable name="variableName" select="expression"/>

Example:

<xsl:variable name="companyName" select="'ABC Technologies'"/>

In this example, the variable companyName stores a text value that can be used throughout the stylesheet.

Variables can also store node sets, numbers, strings, booleans, or the results of XPath expressions.

Example:

<xsl:variable name="employeeCount" select="count(employee)"/>

This variable stores the total number of employee elements.

Scope of Variables

The scope of a variable determines where it can be accessed.

Global Variables

Global variables are declared directly under the <xsl:stylesheet> element and are accessible from all templates.

Example:

<xsl:variable name="currency" select="'USD'"/>

Any template in the stylesheet can use this variable.

Local Variables

Local variables are declared inside a template and are only accessible within that template.

Example:

<xsl:template match="employee">
    <xsl:variable name="salary" select="salary"/>
    <xsl:value-of select="$salary"/>
</xsl:template>

The variable salary can only be used within this template.

Understanding Parameters in XSLT

Parameters are similar to variables but are designed to receive values from outside a template or stylesheet. Parameters make templates reusable because different values can be supplied each time the template is called.

Parameters are declared using the <xsl:param> element.

Syntax of a Parameter

<xsl:param name="parameterName"/>

Example:

<xsl:param name="department"/>

A parameter may also have a default value.

<xsl:param name="department" select="'HR'"/>

If no value is supplied, the parameter will use the default value.

Global Parameters

Global parameters are declared at the stylesheet level and can receive values from the transformation processor.

Example:

<xsl:param name="reportYear" select="'2025'"/>

This allows external applications to pass different years when running the transformation.

Template Parameters

Templates can accept parameters when they are called using <xsl:call-template>.

Defining a Template Parameter

<xsl:template name="displayEmployee">
    <xsl:param name="empName"/>
    <p>
        <xsl:value-of select="$empName"/>
    </p>
</xsl:template>

Passing a Parameter

<xsl:call-template name="displayEmployee">
    <xsl:with-param name="empName" select="'John Smith'"/>
</xsl:call-template>

The value "John Smith" is passed to the template parameter.

Passing Multiple Parameters

Templates can receive multiple parameters simultaneously.

Example:

<xsl:template name="employeeInfo">
    <xsl:param name="name"/>
    <xsl:param name="designation"/>

    <p>
        Name: <xsl:value-of select="$name"/>
        Designation: <xsl:value-of select="$designation"/>
    </p>
</xsl:template>

Calling the template:

<xsl:call-template name="employeeInfo">
    <xsl:with-param name="name" select="'Alice'"/>
    <xsl:with-param name="designation" select="'Manager'"/>
</xsl:call-template>

This approach makes templates highly reusable.

Using Parameters with Apply-Templates

Parameters can also be passed when using <xsl:apply-templates>.

Example:

<xsl:apply-templates select="employee">
    <xsl:with-param name="showSalary" select="true()"/>
</xsl:apply-templates>

Receiving template:

<xsl:template match="employee">
    <xsl:param name="showSalary"/>

    <xsl:value-of select="name"/>

    <xsl:if test="$showSalary">
        <xsl:value-of select="salary"/>
    </xsl:if>
</xsl:template>

This allows templates to behave differently based on supplied values.

Difference Between Variables and Parameters

Feature Variable Parameter
Declaration <xsl:variable> <xsl:param>
Purpose Store data Receive data
Default Value Assigned during declaration Optional default value
Value Modification Not allowed Value can be supplied externally
Template Communication Limited Designed for passing values

Variables are mainly used for storing information, while parameters are intended for data exchange and customization.

Practical Example

Consider an XML file containing product information:

<products>
    <product>
        <name>Laptop</name>
        <price>800</price>
    </product>
</products>

XSLT stylesheet:

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

<xsl:template match="product">
    <p>
        Product:
        <xsl:value-of select="name"/>
        -
        <xsl:value-of select="price"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="$currency"/>
    </p>
</xsl:template>

If the parameter value changes from USD to EUR, the output automatically adjusts without modifying the template logic.

Benefits of Using Variables and Parameters

  1. Improves code readability.

  2. Reduces duplication of expressions.

  3. Enables reusable templates.

  4. Simplifies maintenance and updates.

  5. Supports dynamic transformations.

  6. Enhances flexibility by allowing external values.

  7. Improves performance by avoiding repeated calculations.

Best Practices

  • Use variables for storing intermediate calculations and frequently used values.

  • Use parameters when data needs to be supplied from outside a template.

  • Give meaningful names to variables and parameters.

  • Avoid creating unnecessary variables for simple one-time expressions.

  • Use global parameters for configuration settings.

  • Keep variable scope as limited as possible to improve maintainability.

  • Document important parameters so other developers understand their purpose.

Conclusion

Parameters and variables play a vital role in XSLT development. Variables help store and reuse values within a stylesheet, while parameters enable dynamic communication between templates and external applications. By properly using global variables, local variables, template parameters, and parameter passing mechanisms, developers can create scalable, reusable, and efficient XSLT transformations that are easier to maintain and extend.