Inside Xslt

Interfacing Saxon to Java

The Saxon processor also defines an API for use with Java, but, of course, the details differ from Xalan. Here, I create a new Java class, saxonjava, to show how to create transformations using the Saxon API, version 6.0.2. You start by creating a new XSLT Processor object by calling the Processor classs newInstance method in the file saxonjava.java:

import java.io.*; import org.xml.sax.*; import org.w3c.dom.*; import com.icl.saxon.trax.*; public class saxonjava { public static void main(String args[]) throws ProcessorException, ProcessorFactoryException, TransformException, SAXException, IOException { Processor processor = Processor.newInstance("xslt"); . . .

Next, you must create a Templates object based on the XSL stylesheet you want to use, whose name is stored in args[1] . You can use the InputSource class as follows :

import java.io.*; . . . public class saxonjava { public static void main(String args[]) throws ProcessorException, ProcessorFactoryException, TransformException, SAXException, IOException { Processor processor = Processor.newInstance("xslt"); Templates templates = processor.process(new InputSource(args[1])); . . . } }

Using the new Templates object, you can create a Transformer object, which does the actual work:

import java.io.*; . . . public class saxonjava { public static void main(String args[]) throws ProcessorException, ProcessorFactoryException, TransformException, SAXException, IOException { Processor processor = Processor.newInstance("xslt"); Templates templates = processor.process(new InputSource(args[1])); Transformer transformer = templates.newTransformer(); . . . } }

Finally, you can perform the XSLT transformation using the transformer objects transform method this way, writing the result out to the result document using a FileWriter object:

Listing 10.8 saxonjava.java, Interfacing Saxon to Java

import java.io.*; import org.xml.sax.*; import org.w3c.dom.*; import com.icl.saxon.trax.*; public class saxonjava { public static void main(String args[]) throws ProcessorException, ProcessorFactoryException, TransformException, SAXException, IOException { Processor processor = Processor.newInstance("xslt"); Templates templates = processor.process(new InputSource(args[1])); Transformer transformer = templates.newTransformer(); transformer.transform(new InputSource(args[0]), new Result(new FileWriter(args[2]))); } }

To compile and use this new class, saxonjava, you set the classpath to include saxon.jar, something like this:

C:\>set classpath=.;c:\saxon\saxon.jar

Then you use the Java compiler, javac, to create saxonjava.class.

Like many API-based XSLT processors, Saxon expects to be passed URLs of the documents you want to work with, so Ill do that here:

C:\>java saxonjava http://www.starpowder.com/planets.xml http://www.starpowder.com/ planets.xsl planets.html

This creates planets.html as before. Note that if your documents are local, you can use a file URL instead. For example, in Windows, if the XML document is at c:\XSL\saxonjava\planets.xml and the XSL document is at c:\XSL\saxonjava\planets.xsl , you might use this command line:

C:\>java saxonjava file:///XSL/saxonjava/planets.xml file:///XSL/saxonjava/planets.xsl planets.html

Converting from filenames to URLs

If you prefer to use local filenames on the command line rather than URLs, you can convert those filenames to URLs in your code. To do that, you need the filenames full path to pass to the Java URL class, and you can get that path with the File classs getAbsolutePath method like this: File file = new File(filename) ; String fullpath = file.getAbsolutePath() ;.

This example used Saxon 6.0.2, which is still listed as the stable version of Saxon on the Saxon site, but as I write this, the book is in tech review and theres a new, not fully tested , version available, Saxon 6.2.2. (There was no Saxon version 6.1.x.) In that newer version, it looks as if Saxon is going back to the same API model that Xalan uses, and code targeted at version 6.0.2 wont work with version 6.2.2 (surprise!). Heres what saxonjava.java would look like for version 6.2.2make sure you include the new saxon.jar in your classpath when using this code (which is identical to xalanjava.java, shown earlier, except for the class name, saxonjava ), and note that you dont have to supply file URLs, just filenames, when running it, as with 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 saxonjava { 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]))); } }

Категории