Suppose we manage content using XML which may be data passed by user/application etc .To show it in browser we rely on browser for transformation of XML +XSL , but this is not reliable as different browsers behave in profoundly different ways .Most of time prefer to write servlet to transform it and stream it in ifame tag of xhtml or jsp , jsf page etc.
Take simple example -
XML - catalogue.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
</catalog>
XSL - catalogue.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
SERVLET - servlet to stream the html as we can not depend on browser
XSLTransformerServlet.java
package com.test;
catalogue
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
/**
* @author NEELESH SALPE
*
*/
public class XSLTransformerServlet extends HttpServlet
{
private static final String INIT_PARAM_ERROR_PAGE = "errorPage";
private String errorPage;
private TransformerFactory tf;
private static final String _XSL_FILE_PATH = ".xml";
catalogue
private static final String _XML_FILE_PATH = "/config/.xsl";
/**
* Constructor of the object.
*/
public XSLTransformerServlet()
{
super();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException
{
tf = TransformerFactory.newInstance();
errorPage = getServletConfig().getInitParameter(INIT_PARAM_ERROR_PAGE);
}
/**
* Destruction of the servlet. <br>
*/
public void destroy()
{
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String xslFilePath = (String) request.getAttribute(_XSL_FILE_PATH);
String xmlFilePath = (String) request.getAttribute(_XML_FILE_PATH);
response.setContentType("text/html");
if (null == xslFilePath || null == xmlFilePath)
{
throw new ServletException("XML , XSL not found ");
}
try
{
Source xslSource = new StreamSource(new File(getServletContext()
.getRealPath(xslFilePath)));
Source xmlSource = new StreamSource(new File(getServletContext()
.getRealPath(xmlFilePath)));
try
{
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer(xslSource);
PrintWriter out = response.getWriter();
transformer.transform(xmlSource, new StreamResult(out));
}
catch (TransformerConfigurationException e)
{
e.printStackTrace();
throw new ServletException(e);
}
catch (TransformerFactoryConfigurationError e)
{
e.printStackTrace();
throw new ServletException(e);
}
catch (TransformerException e)
{
e.printStackTrace();
throw new ServletException(e);
}
}
catch (FileNotFoundException flnex)
{
flnex.printStackTrace();
throw new ServletException(flnex);
}
catch (IOException ioex)
{
ioex.printStackTrace();
throw new ServletException(ioex);
}
catch (Exception ex)
{
ex.printStackTrace();
throw new ServletException(ex);
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
In web.xml
<servlet>
<servlet-name>XSLTransformer</servlet-name>
<servlet-class>com.test.XSLTransformerServlet </servlet-class>
<init-param>
<param-name>errorPage</param-name>
<param-value>/errorPage.xhtml</param-value>
</init-param>
</servlet>
<servlet>
<servlet-mapping>
<servlet-name>XSLTransformer</servlet-name>
<url-pattern>/XSLTransform</url-pattern>
</servlet-mapping>
<servlet-mapping>
In actual page
<!--
// Use iframe to show the catalogue on pages in website
// As style depend on XSL we can change it on will or randomize
// the look
// Data depends on XML which is easy to handle and export
-->
<iframe src="/XSLTransform" width="100%" height="300">
<p>Catalogue of latest books</p>
</iframe>
No comments:
Post a Comment