19.3 Transforming XML with XSLT

We saw the use of the javax.xml.transform package and two of its subpackages in the output( ) method of Example 19-2. There it was used to perform an "identity transform," converting a DOM tree into the corresponding XML file. But transforming the format of an XML document is not the only purpose of these packages. They can also be used to transform XML content according to the rules of an XSL stylesheet. The code required to do this is remarkably simple; it's shown in Example 19-3. This example uses javax.xml.transform.stream to read files containing a source document and a stylesheet, and to write the output document to another file. JAXP can be even more flexible, however: the transform.dom and transform.sax subpackages allow the program to be rewritten to (for example) transform a document represented by a series of SAX parser events into a DOM tree, using a stylesheet read from a file.

Example 19-3. XSLTransform.java
package je3.xml; import java.io.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; /**  * Transforms an input document to an output document using an XSLT stylesheet.  * Usage: java XSLTransform input stylesheet output  **/ public class XSLTransform {     public static void main(String[  ] args) throws TransformerException {         // Set up streams for input, stylesheet, and output.         // These do not have to come from or go to files.  We can also use the         // javax.xml.transform. {dom,sax} packages use DOM trees and streams of         // SAX events as sources and sinks for documents and stylesheets.         StreamSource input = new StreamSource(new File(args[0]));         StreamSource stylesheet = new StreamSource(new File(args[1]));         StreamResult output = new StreamResult(new File(args[2]));                  // Get a factory object, create a Transformer from it, and          // transform the input document to the output document.         TransformerFactory factory = TransformerFactory.newInstance( );         Transformer transformer = factory.newTransformer(stylesheet);         transformer.transform(input, output);     } }

In order to use this example, you'll need an XSL stylesheet. A tutorial on XSL is beyond the scope of this chapter, but Example 19-4 shows one to get you started. This stylesheet is intended for processing the XML log files created by the java.util.logging package. For each <record> tag it encounters in the log file, it extracts the textual contents of the <sequence>, <date>, and <message> subtags, and combines them into a single line of output. This discards some of the log information, but shrinks and simplifies the log file, making it more human-readable.

Example 19-4. simplelog.xsl
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   <xsl:template match="record">     <xsl:value-of select="sequence"/>     <xsl:text>: </xsl:text>     <xsl:value-of select="date"/>     <xsl:text>: </xsl:text>     <xsl:value-of select="message"/>   </xsl:template> </xsl:stylesheet>


Java Examples in a Nutshell
Java Examples in a Nutshell, 3rd Edition
ISBN: 0596006209
EAN: 2147483647
Year: 2003
Pages: 285

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net