XSLT, or Extensible Style 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 DTD into another, as might be needed in a business-to-business (B2B) application where information is passed from one industry-standard DTD to a site that uses another. It can also be used to render XML into another format such as HTML. Think of XSLT as a scripting language for transforming XML.
You need a set of classes called an XSLT
processor
. One
freely available XSLT processor is the
Apache project’s Xalan
(formerly available from Lotus/IBM as the Lotus XSL processor). To
use this, you create an XSL processor by calling the factory method
getProcessor( )
,
then call its parse method passing in two
XSLTInputSources
(one for the XML document and one for the XSL stylesheet) and one
XSLTResultTarget
for the output file.
Assume you have a file of people’s
names,
addresses, and so on, stored in an XML document such as the file
people.xml
, shown in Example 21-1.
Example 21-1. people.xml
<?xml version="1.0"?> <people> <person> <name>Ian Darwin</name> <email>[email protected]</email> <country>Canada</country> </person> <person> <name>Another Darwin</name> <email type="intranet">ad</email> <country>Canada</country> </person> </people>
You can transform the
people.xml
file into HTML by using
the following command:
$ java XSLTransform people.xml people.xsl people.html
Figure 21-2 shows the resulting HTML file opened in a browser.
Let’s look at the file people.xsl
(shown
in Example 21-2). 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-2. 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>
The program XSLTransform
appears in Example 21-3.
Example 21-3. XSLTransform.java
import org.apache.xalan.xslt.*; import java.net.*; import java.io.*; /** * Demonstrate transforming a file using XSLT. */ public class XSLTransform { public static void main(String[] args) { try { // Require three input args if (args.length != 3) { System.out.println("Usage: java XSLTransform " + "<input XML file> <input XSL file> <output file>"); System.exit(1); } XSLTProcessor myProcessor = XSLTProcessorFactory.getProcessor( ); XSLTInputSource xmlSource = new XSLTInputSource(args[0]); XSLTInputSource xslStylesheet = new XSLTInputSource(args[1]); XSLTResultTarget xmlOutput = new XSLTResultTarget(args[2]); myProcessor.process(xmlSource, xslStylesheet, xmlOutput); } catch (org.xml.sax.SAXException exc) { System.err.println("Found invalid XML during processing:"); exc.printStackTrace( ); } } }
A new development in progress is the use of
translets. Sun is developing a program that will
read a stylesheet and generate a
Translet
class,
which is a compiled Java program that transforms XML according to the
stylesheet. This will eliminate the overhead of reading the
stylesheet each time a document is translated. See http://www.sun.com/xml/developers/xsltc/.
13.58.120.57