XSLT processing in Java - II
17 April 2008This post is in continuation of “XSLT processing in Java”. Do read the first part before this one.
For the XML file given in the first part of this post, we have the following XSLT stylesheet.
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" encoding="UTF-8"/> <xsl:template match="/"> <xsl:value-of select="message"/> </xsl:template> </xsl:stylesheet>
Now we want to transform the XML file using XSLT stylesheet and want to print it on the console. We will use xalan engine for this.
import java.io.*; import java.net.MalformedURLException; import java.net.URL; import org.xml.sax.SAXException; public class SimpleXalan1 { public static void main(String[] args) throws MalformedURLException, SAXException { if (args.length != 2) { System.err.println("Usage:"); System.err.println(" java " + SimpleXalan1.class.getName( ) + " xmlFileName xsltFileName"); System.exit(1); } String xmlFileName = args[0]; String xsltFileName = args[1]; String xmlSystemId = new File(xmlFileName).toURL().toExternalForm( ); String xsltSystemId = new File(xsltFileName).toURL().toExternalForm( ); org.apache.xalan.xslt.XSLTProcessor processor = org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor( ); org.apache.xalan.xslt.XSLTInputSource xmlInputSource = new org.apache.xalan.xslt.XSLTInputSource(xmlSystemId); org.apache.xalan.xslt.XSLTInputSource xsltInputSource = new org.apache.xalan.xslt.XSLTInputSource(xsltSystemId); org.apache.xalan.xslt.XSLTResultTarget resultTree = new org.apache.xalan.xslt.XSLTResultTarget(System.out); processor.process(xmlInputSource, xsltInputSource, resultTree); } }
Happy codding!
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: XSLT processing in Java - II