JSTL XML x:transform Tag

The <x:transform> tag is used to transform an xml data based on XSLT script. This tag used to apply an XSL transformation on an XML document.

Syntax of <x:transform> Tag

<x:transform attributes> body content </x:transform>

Attributes of <x:transform> Tag

AttributesDescription
varThis attribute specifies the variable name to which transformed XML document has to be set.
scopeThis attribute specifies the scope of the var attribute.
docThis attribute specifies the source XML document to be transformed.
xsltThis attribute specifies javax.xml transform source transform stylesheet as string, reader or source object.
xmlSystemIdThis attribute is deprecated attribute, used with attribute xml.
docSystemIdThis attribute takes the system identifier(URI) for parsing the XML document.
resultThe javax.xml transform. result object that captures or processes the transformation result.

Example of <x:transform> Tag

Listing 1:transform.xsl file

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="doc"/>

<xsl:template match="/">
<html>
<body>

<h2>Company's Employee detail</h2>

<table border="1">
<tr>
<th align="left">name
</th>
<th align="left">designation
</th>
<th align="left">age
</th>
</tr>
<xsl:for-each select="org/company/emp">
<tr>
<td>
<xsl:value-of select="name"/>
</td>
<td>
<xsl:value-of select="designation"/>
</td>
<td>
<xsl:value-of select="age"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>

</xsl:template>
</xsl:stylesheet>

Details of the Code

  • We have written the above file in xsl sheet and named as transform.xsl
  • <xsl:param name=”doc”/> tag is used to display parameter name.
  • <xsl:template match=”/”> tag is used to match the specified node in the document.
  • <xsl:for-each select=”org/company/emp”> tag is used to loop the nodes in the xml document.
  • <xsl:value-of select=”name”/> tag is used to select the particular value in the document.

Listing 2:Xml File transfor.xml

<?xml version="1.0" encoding="UTF-8"?>
<org>
<company>
<emp>
<name>Mahantesh Nagathan</name>
<designation>Sr. Sofware developer</designation>
<age>25</age>
</emp>

<emp>
<name>Rushali Bhatkande</name>
<designation>Graphics Designer</designation>
<age>23</age>
</emp>
</company>

<company>
<emp>
<name>Ganesh Patil</name>
<designation>Sr. Java Programmer</designation>
<age>24</age>
</emp>

<emp>
<name>Maruti Patil</name>
<designation>Sr. Flash Programmer</designation>
<age>23</age>
</emp></company></org>

Listing 3:JSP file-example.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<c:import var="xml" url="transform.xml" />
<c:import var="xsl" url="transform.xsl" />
<x:transform xml="${xml}" xslt="${xsl}" />

Details of the Code

  • <c:import var=”xml” url=”transform.xml” /> tag is used to include the content of another resource in the current JSP. The var attribute is used to specify the variable name to which transformed XML document has to be set. The url is transform.xml
  • <c:import var=”xsl” url=”transform.xsl” /> in this tag the url is transform.xsl
  • <x:transform xml=”${xml}” xslt=”${xsl}” /> tag is used to transform the xml file into other format.

Steps for Execution

Before executing the xml programs we should add jar files namely:
xalan-2.7.0.jar,serializer-2.7.1.jar,xercesImpl-2.8.1.jar,xml-apis-2.0.2.jar

  • Click on the jsp File
  • Right click on mouse and select run as ->click run on server
  • Then output page will get displayed.

Output

When the execution process is completed successfully we will get the following output :
JSTL xml x transform tag

 

Previous Tutorial :  JSTL XML x:parse Tag :: Next Tutorial : JSTL XML x:choose ,x:when and x:otherwise Tags

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*