XSLT processing in Java - II

17 April 2008

This 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!

del.icio.us:XSLT processing in Java - II  digg:XSLT processing in Java - II  spurl:XSLT processing in Java - II  wists:XSLT processing in Java - II  simpy:XSLT processing in Java - II  newsvine:XSLT processing in Java - II  blinklist:XSLT processing in Java - II  furl:XSLT processing in Java - II  reddit:XSLT processing in Java - II  fark:XSLT processing in Java - II  blogmarks:XSLT processing in Java - II  Y!:XSLT processing in Java - II  smarking:XSLT processing in Java - II  magnolia:XSLT processing in Java - II  segnalo:XSLT processing in Java - II  gifttagging:XSLT processing in Java - II

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

Leave a Reply