RE: [xsl] Using Saxon to do XSL transforms in Java program

Subject: RE: [xsl] Using Saxon to do XSL transforms in Java program
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Fri, 10 Aug 2007 20:12:44 +0100
> I'm trying to write a Java program to perform an XSL 
> transform using the JAXP API.  The relevent code is as follows:
>  
> // start code snippet
> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
> TransformerFactory tFactory = TransformerFactory.newInstance();
> 
> DocumentBuilder builder = factory.newDocumentBuilder(); 
> StreamSource stylesheet = new StreamSource(xslFile); 
> Transformer transformer = tFactory.newTransformer(stylesheet);
> 
> doc = builder.parse(xmlFile);
> DOMSource source = new DOMSource(doc);
> StreamResult result = new StreamResult(outFile); 
> transformer.transform(source, result); // end code snippet

One point here: for Saxon, and probably for other processors as well, it's
best to supply a StreamSource or SAXSource and let the XSLT processor do the
tree building. It can then choose its own tree model, which will probably be
much more efficient than a general-purpose DOM. Only build a DOM if you need
it in your own application. In the case of Saxon, the native TinyTree can
run at 4 times the speed of a DOM transformation, and it also takes much
less memory.
> 
> Pretty straightforward stuff, I think, and it works fine when 
> using the default XSL processor.  However, I want to make use 
> of the Saxon 6.5.5 processor, as I am using the saxon:output 
> extension in one of my stylesheets (don't want to use XSLT 
> 2.0 - and xsl:result-document - as I've been told some things 
> behave differently).

I think you're better off using a standard XSLT 2.0 construct rather than a
proprietary Saxon extension to XSLT 1.0. They aren't quite the same, and
xsl:result-document is much more carefully specified. It's very unlikely
that you will hit any compatibility issues, especially if you leave the
stylesheet saying version="1.0" which will cause it to run in
backwards-compatibility mode.  
>  
> I add the saxon.jar file to my build path in Eclipse (v3.2.1, using
> v1.4.2 of the JDK), and it appears that it finds Saxon OK 
> without needing to set the 
> javax.xml.parsers.DocumentBuilderFactory or 
> javax.xml.transform.TransformerFactory properties (I get the 
> same behaviour either way).  When the program gets to the line:
> Transformer transformer = tFactory.newTransformer(stylesheet);
>  
> though, an exception occurs 

It looks as if it can't find the xslfile you specified: you haven't shown us
the value of this variable. Perhaps it's a Windows filename rather than a
URI? I think Xalan lets you get away with that although it's incorrect
according to the spec.

Saxon 6.x was a bit particular about filename-to-URI conversion: because it
works with some old Java releases, it can't take advantage of methods such
as File.toURI() (which IIRC was introduced in JDK 1.4).

Michael Kay
http://www.saxonica.com/

Current Thread