JSTL XML Tag transform

JSTL XML Tag transform


Posted in : Java Posted on : April 30, 2012 at 6:56 PM Comments : [ 0 ]

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

JSTL XML Tag transform

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

<x:transform> tag from JSTL is used for transforming an XML document to a specific document types and to format the output using an XSLT stylesheet. In this transformation process xml file is treated as a source document. An XSL document allows to display the browser an XML document according to the information provided by XSL document.

Attributes of <x:transform> tag

  • var : This is an optional attribute associate a name to the variable for the transformed XML document.
  • scope : This is an optional attribute specifies the scope of attribute 'var'.
  • result : This is an optional attribute specified as a result object which captures/processes the transformation result.
  • doc : This is an optional attribute that specifies as a source XML document to be transformed.
  • docSystemId : This is an optional attribute that may be used to identify the XML document for parsing by the system identifier (URI).
  • xslt : This is an optional attribute that specifies as a javax.xml.transform.Source transformation stylesheet.
  • xsltSystemId : This is an optional attribute that may be used to identify the XSLT stylesheet for parsing by the system identifier (URI).

Example :

Here an example is being given below for you is demonstrating about how to transform an XML document into other document types. In this example I have created two files named stuReportCard.xml file and an myStyleSheet.xsl file. Here the xml file is a source file and the xsl file is the file using which 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.

myStyleSheet.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Student Grade 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="students/student">
<tr>
<td>
<xsl:value-of select="name"/>
</td>
<td>
<xsl:value-of select="roll"/>
</td>
<td>
<xsl:value-of select="grade"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

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>

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:transform Example</title>
</head>
<body>
<c:import url="stuReportCard.xml" var="xmlDoc" />
<c:import url="myStyleSheet.xsl" var="xslDoc" />

<x:transform doc="${xmlDoc}" xslt="${xslDoc}" />

</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 JstlXmlTransform.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 JstlXmlTransform.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