In this post, you will learn how to use the <x:transform> tag in the JSTL XML tags library with code example.

The <x:transform> tag does transformation of an XML document using an XSLT template.

The XSLT (Extensible Stylesheet Language Transformation) is a language used for transforming an XML document into any other format. Using XSLT, we can transform XML document to other format like HTML, Text etc.

 

JSTL <x:transform> Syntax:

<x:transform

   var="<string>"

   scope="<string>"

   result="<string>"

   doc="<string>"

   docSystemId="<string>"

   xslt="<string>"

   xsltSystemId="<string>"/>

 

Attributes:

Name

Description

var

Name of the variable to store the transformed XML document. This variable is of type org.w3c.dom.Document.

scope

Scope to store the var.

result

Result object which contains transformation result.

doc

XML document to be transformed. This should be a well formed XML document.

docSystemId

The URI of the XML document being parsed.

xslt

XSLT Template to be used to transform the XML document.

xsltSystemId

The URI of the XSLT document.

 

JSTL <x:transform> Example:

The following JSP code transforms an XML document which contains citizens information and prints the data in a tabular format. We’re using a simple XSLT template to transform the XML document.

<%@ 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>&lt;x:transform&gt; Demo</title>
  </head>
  <body>
    <h1>&lt;x:transform&gt; Demo</h1>
    <c:import 
      url="http://localhost:8080/JSTL-Tag-Reference/tag-types/xml/citizen-template.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}"/>
  </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:template match="/">
        <html>
            <body>
                <xsl:apply-templates />
            </body>
        </html>
    </xsl:template>
    <xsl:template match="citizens">
        <table border="1" width="200">
            <xsl:for-each select="citizen">
                <tr>
                    <td>
                        <xsl:value-of select="ssn" />
                    </td>
                    <td>                        
                        <xsl:value-of select="firstname" />                        
                    </td>
                    <td>
                        <xsl:value-of select="lastname" />
                    </td>
                    <td>
                        <xsl:value-of select="role" />
                    </td>
                    <td>
                        <xsl:value-of select="salary" />
                    </td>
                    
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>
 

XML Document (given for reference):

<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<citizens>
    <citizen>
        <ssn>Z345T</ssn>
        <firstname>Cheryl</firstname>
        <lastname>Johnson</lastname>
        <role>Manager</role>
        <salary>12000</salary>
    </citizen>
    <citizen>
        <ssn>Z446T</ssn>
        <firstname>John</firstname>
        <lastname>Smith</lastname>
        <role>Employee</role>
        <salary>1000</salary>
    </citizen>
    <citizen>
        <ssn>Z335T</ssn>
        <firstname>Justin</firstname>
        <lastname>Claire</lastname>
        <role>Senior Manager</role>
        <salary>14000</salary>
    </citizen>
    <citizen>
        <ssn>Z389T</ssn>
        <firstname>Clark</firstname>
        <lastname>Rick</lastname>
        <role>Employee</role>
        <salary>2000</salary>
    </citizen>
</citizens>
 

Output:

x-transform

Recommended Usage of <x:transform> tag:

The <x:transform> tag is used to transform an XML document into another format using defined XSLT template. XSLT is a flexible way of transforming any XML document to other formats like HTML, Text to name a few. The XSLT is really powerful and flexible language transformation language.

 



XPath JAR download

We may have to download the following JAR files and put them under WEB-INF/lib folder. These JAR files are necessary especially when we have XPath expressions.

 

Other JSTL XML Tags:

forEach  |  if  |  out  |  param  |  parse  |  set  |  choose, when, otherwise


About the Author:

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.



Add comment