XSLT - Passing Parameters with <xsl:with-param> in XSLT
In XSLT, <xsl:with-param> is used to pass a value from one template to another template. It is especially useful when a template needs some information from the calling template to perform a particular task.
Parameters make XSLT stylesheets more flexible and reusable. Instead of creating separate templates for every possible value, we can create one template and pass different values to it whenever required.
1. What is a Parameter in XSLT?
A parameter is a value that can be supplied to a template when the template is called.
For example, suppose we have a template that displays a student's name and we want to pass the student's marks to that template. Instead of hardcoding the marks inside the template, we can pass them as a parameter.
A parameter is generally declared using:
<xsl:param name="parameterName"/>
The value can then be supplied using:
<xsl:with-param name="parameterName" select="value"/>
The basic relationship is:
Calling Template
|
| passes a value
v
<xsl:with-param>
|
v
Called Template
|
| receives the value
v
<xsl:param>
2. <xsl:param> and <xsl:with-param>
These two elements normally work together.
<xsl:param> declares the parameter in the template that receives the value.
<xsl:with-param> supplies the value when the template is called.
For example:
<xsl:template name="displayStudent">
<xsl:param name="name"/>
<p>
Student Name:
<xsl:value-of select="$name"/>
</p>
</xsl:template>
Here, name is a parameter of the displayStudent template.
We can call this template as follows:
<xsl:call-template name="displayStudent">
<xsl:with-param name="name" select="'Rahul'"/>
</xsl:call-template>
The value Rahul is passed to the name parameter.
The output will be:
Student Name: Rahul
3. Why Use <xsl:with-param>?
Without parameters, a template may depend heavily on fixed values.
For example:
<xsl:template name="displayStudent">
<p>Student Name: Rahul</p>
</xsl:template>
This template can only display Rahul unless its code is changed.
Using a parameter makes the template reusable:
<xsl:template name="displayStudent">
<xsl:param name="name"/>
<p>
Student Name:
<xsl:value-of select="$name"/>
</p>
</xsl:template>
Now different names can be passed:
<xsl:call-template name="displayStudent">
<xsl:with-param name="name" select="'Rahul'"/>
</xsl:call-template>
<xsl:call-template name="displayStudent">
<xsl:with-param name="name" select="'Priya'"/>
</xsl:call-template>
This allows the same template to produce different results.
4. Using select with <xsl:with-param>
The select attribute is commonly used to specify the value being passed.
Example:
<xsl:with-param name="age" select="25"/>
Here, the value 25 is passed to the parameter named age.
For a string value, quotation marks are normally used:
<xsl:with-param name="city" select="'Bengaluru'"/>
For a value from the source XML document, an XPath expression can be used:
<xsl:with-param name="studentName" select="student/name"/>
This means that the value of student/name from the current XML context is passed to the parameter.
5. Example with XML Data
Consider the following XML document:
<students>
<student>
<name>Rahul</name>
<marks>85</marks>
</student>
<student>
<name>Priya</name>
<marks>92</marks>
</student>
</students>
An XSLT stylesheet can pass the student information to a named template.
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="students/student">
<xsl:call-template name="displayStudent">
<xsl:with-param name="studentName" select="name"/>
<xsl:with-param name="studentMarks" select="marks"/>
</xsl:call-template>
</xsl:for-each>
</body>
</html>
</xsl:template>
The receiving template can be written as:
<xsl:template name="displayStudent">
<xsl:param name="studentName"/>
<xsl:param name="studentMarks"/>
<p>
Name:
<xsl:value-of select="$studentName"/>
<br/>
Marks:
<xsl:value-of select="$studentMarks"/>
</p>
</xsl:template>
The parameters receive values from the calling template.
For Rahul:
studentName = Rahul
studentMarks = 85
For Priya:
studentName = Priya
studentMarks = 92
6. Passing Multiple Parameters
A template can receive multiple parameters.
For example:
<xsl:template name="studentDetails">
<xsl:param name="name"/>
<xsl:param name="age"/>
<xsl:param name="course"/>
<p>
Name: <xsl:value-of select="$name"/>
<br/>
Age: <xsl:value-of select="$age"/>
<br/>
Course: <xsl:value-of select="$course"/>
</p>
</xsl:template>
The template can be called like this:
<xsl:call-template name="studentDetails">
<xsl:with-param name="name" select="'Anita'"/>
<xsl:with-param name="age" select="21"/>
<xsl:with-param name="course" select="'Computer Science'"/>
</xsl:call-template>
Here, three different values are passed to the same template.
7. Passing XML Nodes as Parameters
Parameters do not have to contain only simple values such as strings and numbers. They can also represent nodes or sequences of nodes.
For example:
<xsl:with-param name="student" select="student"/>
The receiving template can declare:
<xsl:param name="student"/>
The parameter can then be used:
<xsl:value-of select="$student/name"/>
This is useful when an entire XML element needs to be processed by another template.
8. Using a Parameter to Control Output
Parameters can also determine how a template behaves.
For example:
<xsl:template name="displayMessage">
<xsl:param name="showDetails"/>
<h2>Student Information</h2>
<xsl:if test="$showDetails = true()">
<p>Additional student details are displayed here.</p>
</xsl:if>
</xsl:template>
The template can be called with:
<xsl:call-template name="displayMessage">
<xsl:with-param name="showDetails" select="true()"/>
</xsl:call-template>
If the parameter is true, the additional information is displayed.
This makes the template configurable.
9. Default Values for Parameters
A parameter can have a default value.
For example:
<xsl:param name="country" select="'India'"/>
If the calling template does not provide a value, the default value is used.
For example:
<xsl:template name="displayCountry">
<xsl:param name="country" select="'India'"/>
<p>
Country:
<xsl:value-of select="$country"/>
</p>
</xsl:template>
If the template is called without supplying country, the output will be:
Country: India
However, if a value is supplied:
<xsl:call-template name="displayCountry">
<xsl:with-param name="country" select="'Canada'"/>
</xsl:call-template>
the output becomes:
Country: Canada
10. Passing Parameters Using Content
Instead of using select, <xsl:with-param> can also contain content.
For example:
<xsl:with-param name="message">
Welcome to the student portal.
</xsl:with-param>
The receiving template can declare:
<xsl:param name="message"/>
and use:
<xsl:value-of select="$message"/>
This approach is useful when the parameter value needs to be constructed using XSLT instructions rather than being a simple XPath expression.
For example:
<xsl:with-param name="message">
Welcome,
<xsl:value-of select="name"/>
</xsl:with-param>
Here, the parameter value is constructed dynamically.
11. Difference Between select and Content
There are two common ways to supply a parameter.
Using select:
<xsl:with-param name="name" select="name"/>
Using content:
<xsl:with-param name="name">
<xsl:value-of select="name"/>
</xsl:with-param>
The first approach directly evaluates an XPath expression.
The second approach constructs the parameter value using the contents of <xsl:with-param>.
For simple values, select is usually clearer and more concise.
12. Important Point About Parameter Names
The parameter name used in <xsl:with-param> must correspond to the parameter declared using <xsl:param>.
For example:
<xsl:param name="studentName"/>
should be supplied using:
<xsl:with-param name="studentName" select="name"/>
If you use a different name:
<xsl:with-param name="student" select="name"/>
it will not supply the value to studentName.
Therefore, parameter names should be matched carefully.
13. Difference Between <xsl:with-param> and <xsl:param>
| Element | Purpose |
|---|---|
<xsl:param> |
Declares a parameter |
<xsl:with-param> |
Supplies a value to a parameter |
name |
Identifies the parameter |
select |
Specifies the value using XPath |
| Content inside the element | Can be used to construct the parameter value |
In simple terms:
<xsl:param>
|
| receives
|
<xsl:with-param>
|
| gets value from caller
More accurately, the caller uses <xsl:with-param> and the called template declares the corresponding <xsl:param>.
14. Complete Example
XML:
<products>
<product>
<name>Laptop</name>
<price>50000</price>
</product>
<product>
<name>Tablet</name>
<price>25000</price>
</product>
</products>
XSLT:
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="products/product">
<xsl:call-template name="displayProduct">
<xsl:with-param
name="productName"
select="name"/>
<xsl:with-param
name="productPrice"
select="price"/>
</xsl:call-template>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template name="displayProduct">
<xsl:param name="productName"/>
<xsl:param name="productPrice"/>
<p>
Product:
<xsl:value-of select="$productName"/>
<br/>
Price:
<xsl:value-of select="$productPrice"/>
</p>
</xsl:template>
The output will contain:
Product: Laptop
Price: 50000
Product: Tablet
Price: 25000
The important part is that the displayProduct template does not directly access the product's name and price. Instead, the calling template explicitly passes those values using <xsl:with-param>.
15. Advantages of <xsl:with-param>
Using <xsl:with-param> provides several benefits:
-
Improves reusability
One template can process different values. -
Reduces duplication
The same processing logic does not need to be written multiple times. -
Makes templates configurable
Different parameters can change the behavior or output of a template. -
Separates data from processing logic
The calling template decides what data should be supplied, while the called template decides how to process it. -
Supports dynamic transformations
Values can be calculated using XPath expressions before they are passed to another template. -
Makes complex stylesheets easier to maintain
Large transformations can be divided into smaller reusable templates.
Conclusion
<xsl:with-param> is an important XSLT instruction for passing values to templates. It normally works together with <xsl:param>. The receiving template declares the parameter using <xsl:param>, while the calling template supplies the value using <xsl:with-param>.
The basic pattern is:
<xsl:call-template name="templateName">
<xsl:with-param name="parameterName" select="value"/>
</xsl:call-template>
and the receiving template contains:
<xsl:template name="templateName">
<xsl:param name="parameterName"/>
...
</xsl:template>
Understanding <xsl:with-param> is particularly useful for building reusable, flexible, and maintainable XSLT stylesheets, especially when the same template needs to process different XML data or behave differently depending on the values supplied to it.