JSTL XML Tag x:param Example
- Details
- Written by Nam Ha Minh
- Last Updated on 01 September 2019   |   Print Email
In this post, I will help you understand how to use the <x:param> tag in the JSTL XML tags library with code example.
The <x:param> tag is used to send parameter(s) to the <x:transform> tag. This is can be nested in the <x:transform> tag in order to send parameters along with values. Subsequently, the parameters need to be defined in the XSLT template as placeholders. The actual values for the parameters will be supplied using <x:param> tags.
JSTL <x:param> Syntax:
<x:param name="<string>" value="<string>"/>
Attributes:
Name | Required | Description |
name | True | Name of the XSLT parameter. |
value | False | Value of the XSLT parameter. |
JSTL <x:param> Example:
The following JSP example supplies two parameters using <x:param> tag to the XSLT template. The first parameter is HTML table background color and second parameter is the HTML font color.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><x:param> Demo</title>
</head>
<body>
<h1><x:param> Demo</h1>
<c:import
url="http://localhost:8080/JSTL-Tag-Reference/tag-types/xml/citizen-template-param.xsl"
var="xsltTemplate"/>
<c:import url="http://localhost:8080/JSTL-Tag-Reference/tag-types/xml/citizens.xml"
var="citizenXML"/>
<x:transform doc="${citizenXML}" xslt="${xsltTemplate}">
<x:param name="tableBGColor" value="#cccccc"/>
<x:param name="fontColor" value="#009900"/>
</x:transform>
</body>
</html>
XSLT Template (given for reference):
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:param name="tableBGColor"/>
<xsl:param name="fontColor"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="citizens">
<table border="1" width="200" BGCOLOR="{$tableBGColor}">
<xsl:for-each select="citizen">
<tr>
<td>
<font color="{$fontColor}">
<xsl:value-of select="ssn" />
</font>
</td>
<td>
<font color="{$fontColor}">
<xsl:value-of select="firstname" />
</font>
</td>
<td>
<font color="{$fontColor}">
<xsl:value-of select="lastname" />
</font>
</td>
<td>
<font color="{$fontColor}">
<xsl:value-of select="role" />
</font>
</td>
<td>
<font color="{$fontColor}">
<xsl:value-of select="salary" />
</font>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Output:

Recommended Usage of <x:param> tag:
The <x:param> is useful to send parameters along with associated values to the XSLT template. These parameter values will be used during transformation process.
Other JSTL XML Tags:
forEach | if | out | parse | set | transform | choose, when, otherwise
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments