Magazine
 
Introduction to XSL
<xsl:template match=”/”>
<html>
<title>XSLT Style Sheet</title>
<body>
<h1><p align=”center”>Employee Details</
p></h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match=”Employee-Detail”>
<table border=”2" width=”50%”
align=”center”>
<tr bgcolor=”LIGHTBLUE”>
<td><b>Emp_Id</b></td>
<td><b>Emp_Name</b></td>
<td><b>Emp_E-mail</b></td>
</tr>
<xsl:for-each select=”Employee”>
<tr>
<td><i><xsl:value-of select=”Emp_Id”/
></i></td>
<td><xsl:value-of select=”Emp_Name”/
></td>
<td><xsl:value-of select=”Emp_E-mail”/
></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

3. Create a Java program using XSLT APIs

Now we will develop a class in Java that takes both XML and XSL file as an input and transforms them to generate a formatted HTML file.

Here is the source code of the SimpleXMLTransform.java:

import javax.xml.transform.ErrorListener;
import javax.xml.transform.Transformer;
import javax.xml.transform.Transformer
ConfigurationException;
import

 

javax.xml.transform.TransformerException;
import
javax.xml.transform.TransformerFactory;
import
javax.xml.transform.stream.StreamResult;
import
javax.xml.transform.stream.StreamSource;
public class SimpleXMLTransform {
static public void main(String[] arg) {
if(arg.length != 3) {
System.err.println(“Usage:
SimpleXMLTransform “ +
“<input.xml> <input.xsl>
<output>”);
System.exit(1);
}
String inXML = arg[0];
String inXSL = arg[1];
String outTXT = arg[2];
SimpleXMLTransform st = new
SimpleXMLTransform();
try {
st.transform(inXML,inXSL,outTXT);
}
catch(TransformerConfigurationException e) {
System.err.println(“Invalid factory
configuration”);
System.err.println(e);
} catch(TransformerException e) {
System.err.println(“Error during
transformation”);
System.err.println(e);
}
}
public void transform(String inXML,String
inXSL,String outTXT)
throws
TransformerConfigurationException,
TransformerException {
TransformerFactory factory =
TransformerFactory.newInstance();
StreamSource xslStream = new
StreamSource(inXSL);
Transformer transformer =
factory.newTransformer(xslStream);
transformer.setErrorListener(new
MyErrorListener());

     
Mar 2008 | Java Jazz Up | 16
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59,

Download PDF