Recipe 21.2 Transforming XML with XSLT


Problem

You need to make significant changes to the output format.

Solution

Use XSLT; it is fairly easy to use and does not require writing much Java.

Discussion

XSLT, the Extensible Stylesheet Language for Transformations, allows you a great deal of control over the output format. It can be used to change an XML file from one vocabulary into another, as might be needed in a business-to-business (B2B) application where information is passed from one industry-standard vocabulary to a site that uses another. It can also be used to render XML into another format such as HTML. Some open source projects even use XSLT as a tool to generate Java source files from an XML description of the required methods and fields. Think of XSLT as a scripting language for transforming XML.

This example uses XSLT to transform a document containing people's names, addresses, and so on such as the file people.xml, shown in Example 21-2 into printable HTML.

Example 21-2. people.xml
<?xml version="1.0"?> <people> <person>     <name>Ian Darwin</name>     <email>http://www.darwinsys.com/contact.html</email>     <country>Canada</country> </person> <person>     <name>Another Darwin</name>     <email type="intranet">afd@node1</email>     <country>Canada</country> </person> </people>

You can transform the people.xml file into HTML by using the following command:

$  java  JAXPTransform people.xml people.xsl  people.html

The output is something like the following:

<html> <head> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Our People</title> </head> <body> <table border="1"> <tr> <th>Name</th><th>EMail</th> </tr> <tr> <td>Ian Darwin</td><td>http://www.darwinsys.com/</td> </tr> <tr> <td>Another Darwin</td><td>afd@node1</td> </tr> </table> </body> </html>

Figure 21-2 shows the resulting HTML file opened in a browser.

Figure 21-2. XML to HTML final result
figs/jcb2_2102.gif


Let's look at the file people.xsl (shown in Example 21-3). Since an XSL file is an XML file, it must be well-formed according to the syntax of XML. As you can see, it contains some XML elements but is mostly (well-formed) HTML.

Example 21-3. people.xsl
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <head><title>Our People</title></head> <body>     <table border="1">     <tr>         <th>Name</th>         <th>EMail</th>     </tr>     <xsl:for-each select="people/person">         <tr>             <td><xsl:value-of select="name"/></td>             <td><xsl:value-of select="email"/></td>         </tr>     </xsl:for-each>     </table> </body></html> </xsl:template> </xsl:stylesheet>

I haven't shown the JAXPTransform program yet. To transform XML using XSL, you use a set of classes called an XSLT processor . Java has included this since JDK 1.4, as part of JAXP. Another freely available XSLT processor is the Apache XML Project's Xalan (formerly available from Lotus/IBM as the Lotus XSL processor). To use JAXP's XSL transformation, you create an XSL processor by calling the factory method TransformerFactory.newInstance( ).newTransformer( ), passing in a Streamsource for the stylesheet. You then call its transform( ) method, passing in a StreamSource for the XML document and a StreamResult for the output file. The code for JAXPTransform appears in Example 21-4.

Example 21-4. JAXPTransform.java
import java.io.File; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; /** Illustrate simple use of JAXP to transform using XSL */ public class JAXPTransform {     public static void main(String[] args) throws Exception {         if (args.length != 3) {         // Create a transformer object         Transformer tx = TransformerFactory.newInstance( ).newTransformer(             new StreamSource(new File(args[1]))); // not 0         // Use its transform( ) method to perform the transformation         tx.transform(             new StreamSource(new File(args[0])),             new StreamResult(new File(args[2])));     } }

See Also

A recent development is the use of translets. Sun has developed a program that reads a stylesheet and generates a Translet class, which is a compiled Java program that transforms XML according to the stylesheet. This eliminates the overhead of reading the stylesheet each time a document is translated. Translets have been incorporated under the name XSLTC into the Apache XML Xerces-Java project; see http://xml.apache.org/xerces-j/.



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

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