Xalan is actually one of the easier XSLT processors to interface to Java, despite its size . To show how this works, I create a new Java class, xalanjava , which you can call like this to perform an XSLT transformation:
C:\>java xalanjava planets.xml planets.xsl planets.html
To create an XSLT transformation, I start by creating an object of the TransformerFactory class in the xalanjava class:
import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerConfigurationException; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; public class xalanjava { public static void main(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { TransformerFactory tFactory = TransformerFactory.newInstance(); . . .
The names of the XML document, XSL document, and result document have been passed to us as args[0] , args[1] , and args[2] , respectively. The next step is to load the XSL document into the new TransformerFactory object Ive created. To do that, I create a StreamSource object and pass that object to the TransformerFactory objects newTransformer method to create a new Transformer object:
import javax.xml.transform.Transformer; . . . public class xalanjava { public static void main(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource(args[1])); . . .
This transformer object, transformer , performs the XSLT transformation. To make the transformation happen, you use the transformer objects transform method, passing it a StreamSource object corresponding to the XML document, and a StreamResult object corresponding to the result document:
import javax.xml.transform.Transformer; . . . public class xalanjava { public static void main(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { . . . transformer.transform(new StreamSource(args[0]), new StreamResult(new FileOutputStream(args[2]))); } }
Heres the full Java file, xalanjava.java:
import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerConfigurationException; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; public class xalanjava { public static void main(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource(args[1])); transformer.transform(new StreamSource(args[0]), new StreamResult(new FileOutputStream(args[2]))); } }
To compile xalanjava.java into xalanjava.class and run that class, you set the classpath to include Xalan and the XML parser you usually use with Xalan, Xerces (for more on Xerces, including where to get it, see Chapter 1), in something like this in Windows (as always, update these paths to match your system):
C:\>set classpath=.;c:\xalan\xalan.jar;c:\xalan\xerces.jar
Then you compile xalanjava.java with the Java compiler, javac, as follows :
C:\>javac xalanjava.java
This assumes that javac.exe is in your path so that you can simply invoke it on the command line directly. (The Java compiler, javac.exe, is usually in the Java bin directory, so if its not in your path , you could also call it something like this: C:\>c:\jdk1.3\bin\javac xalanjava.java see Inside XML for more information on Java code.) The Java compiler creates xalanjava.class, and you use that file to perform the transformation:
C:\>java xalanjava planets.xml planets.xsl planets.html
This creates planets.html from planets.xml and planets.xsland this time, I did it with my own Java class.
Using Suns JAXP Package for XSLTSun, the creator of Java, has a Java package for XML processing named JAXP, which you can download at http://java.sun.com/xml. JAXP can also perform XSLT transformations. However, Im not covering JAXP separately in this chapter, because JAXP (currently, at least) uses Xalan for all its transformations, and it comes with xalan.jar. That means that you can use the preceding example, Listing 10.7, unchanged with the Java JAXP package. |