XSLT - Working with <xsl:variable> and <xsl:param> in XSLT

In XSLT, <xsl:variable> and <xsl:param> are used to store values that can be reused during the transformation process. They help make stylesheets more organized, readable, and flexible by allowing developers to store data temporarily and pass values between templates. Although both elements hold values, they are used in slightly different ways. Variables store fixed values during transformation, while parameters allow values to be passed from outside the stylesheet or between templates.

An <xsl:variable> is used to define a value that remains constant once it is assigned. In XSLT, variables are immutable, meaning their values cannot be changed after they are created. Variables can store numbers, strings, XML node sets, or the result of XPath expressions. They are often used to simplify complex XPath expressions or to reuse calculated results multiple times within the stylesheet.

Example of a variable:

<xsl:variable name="siteName" select="'Evidhya Learning Portal'"/>

In this example, the variable named siteName stores a string value. Once defined, it can be used anywhere within its scope by referencing it with the dollar sign, such as $siteName.

Variables can also be defined using content instead of the select attribute.

<xsl:variable name="message">
   Welcome to the XML Transformation Tutorial
</xsl:variable>

In this case, the text inside the element becomes the variable's value.

While variables store fixed values, <xsl:param> is used to define parameters that allow external input or values passed between templates. Parameters make XSLT stylesheets more dynamic because their values can change depending on the context in which the transformation is executed.

Example of a parameter:

<xsl:param name="authorName" select="'Guest Author'"/>

Here, the parameter authorName has a default value. However, this value can be overridden if a different value is passed when the XSLT transformation is executed.

Parameters are also commonly used inside templates to pass values from one template to another.

Example:

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

Another template can call this template and pass a value using <xsl:with-param>.

<xsl:call-template name="displayMessage">
   <xsl:with-param name="text" select="'Hello from XSLT'"/>
</xsl:call-template>

In this example, the value "Hello from XSLT" is passed to the parameter text, and the template prints it inside a paragraph element.

Both <xsl:variable> and <xsl:param> follow scope rules. If they are declared at the top level of the stylesheet, they are global and can be accessed anywhere in the document. If they are declared inside a template, they are local and can only be used within that template.

Using variables and parameters correctly improves the structure and flexibility of XSLT stylesheets. Variables help store intermediate results or frequently used values, while parameters enable customization and communication between templates, making complex XML transformations easier to manage and maintain.