JSTL XML Tag param

JSTL XML Tag param


Posted in : Java Posted on : April 30, 2012 at 7:02 PM Comments : [ 0 ]

In this tutorial you will learn about the JSTL xml tag param.

JSTL XML Tag param

In this tutorial you will learn about the JSTL xml tag param.

<x:param> tag of from JSTL xml library is used as a subtag of transform to add a parameter in the XSLT stylesheet.

Attributes of <x:param> tag

  • name : This is a required attribute that defines the name of the transformation parameter.
  • value : This is an optional attribute that specifies the parameter value.

Example :

Here I am giving an example for you which will demonstrate you about how to use JSTL xml <x:param> tag in JSP. In this example I have created an xsl file named myStyleSheet.xsl and stuReportCard.xml where the xml file is a source file to be transformed and the xsl file is a file using which the the xml document will be transformed and an output will be formatted according to the format used in the xsl file. Then I have created a JSP page where I have imported both of the above files using <c:import> tag from JSTL core then used the tag <x:transform> to transform the xml file to the other format output file and the subtag <x:param> to add the parameter.

stuReportCard.xml

<?xml version="1.0" ?>
<students> 
<student>
<name>Amit</name>
<roll>33</roll>
<grade>A</grade>
</student>
<student>
<name>Manoj</name>
<roll>24</roll>
<grade>B</grade>
</student>
<student>
<name>Rajnish</name>
<roll>84</roll>
<grade>C</grade>
</student>
<student>
<name>Ayush</name>
<roll>35</roll>
<grade>D</grade>
</student>
<student>
<name>Arpit</name>
<roll>26</roll>
<grade>E</grade>
</student>
</students>

myStyleSheet1.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="color"/>
<xsl:param name="color1"/>
<xsl:param name="color2"/>

<xsl:template match="students">
<html>
<body>
<h2>Student Report Card</h2>
<table border="1">
<tr>
<td align="left"><b>Name</b></td>
<td align="left"><b>Roll</b></td>
<td align="left"><b>Age</b></td>
</tr>
<xsl:for-each select="student">
<tr>
<td bgColor="{$color}">
<i><xsl:value-of select="name"/></i>
</td>
<td bgColor="{$color1}">
<xsl:value-of select="roll"/>
</td>
<td bgColor="{$color2}">
<xsl:value-of select="grade"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

JstlXmlTransform.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>JSTL x:param Example</title>
</head>
<body>
<c:import url="stuReportCard.xml" var="xmlDoc" />
<c:import url="myStyleSheet1.xsl" var="xslDoc" />

<x:transform doc="${xmlDoc}" xslt="${xslDoc}">
<x:param name="color" value="grey"/>
<x:param name="color1" value="red"/>
<x:param name="color2" value="yellow"/>
</x:transform>
</body>
</html>

How to run this example

Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :

  • jstl.jar
  • standard.jar
  • xalan-2.3.1.jar
  • xercesImpl-2.7.1.jar

After adding of these jar files you may execute your program in the following ways :

  • Select JstlXmlParam.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlXmlParam.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
  • A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish

NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.

Output :

When you will execute the above JSP page you will get the output on your eclipse browser as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics