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

Subject: [xsl] Using Saxon to do XSL transforms in Java program
From: "Adam Schweitzer" <adam.schweitzer@xxxxxxxxxxxxx>
Date: Fri, 10 Aug 2007 14:14:49 -0400
Hi all,
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

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 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 (note that nothing has been changed except
adding the saxon.jar to my build path - I remove it, and the program
executes successfully, with the desired results):

javax.xml.transform.TransformerConfigurationException:
java.io.EOFException: no more input
 at
com.icl.saxon.PreparedStyleSheet.prepare(PreparedStyleSheet.java:121)
 at
com.icl.saxon.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl
.java:120)
 at
com.icl.saxon.TransformerFactoryImpl.newTransformer(TransformerFactoryIm
pl.java:72)
 at DoTranforms.main(DoTranforms.java:28)
Caused by: java.io.EOFException: no more input
 at com.icl.saxon.aelfred.XmlParser.popInput(XmlParser.java:4083)
 at com.icl.saxon.aelfred.XmlParser.pushURL(XmlParser.java:3620)
 at com.icl.saxon.aelfred.XmlParser.doParse(XmlParser.java:159)
 at com.icl.saxon.aelfred.SAXDriver.parse(SAXDriver.java:320)
 at com.icl.saxon.om.Builder.build(Builder.java:265)
 at
com.icl.saxon.PreparedStyleSheet.prepare(PreparedStyleSheet.java:111)
 ... 3 more
---------
java.io.EOFException: no more input
 at com.icl.saxon.aelfred.XmlParser.popInput(XmlParser.java:4083)
 at com.icl.saxon.aelfred.XmlParser.pushURL(XmlParser.java:3620)
 at com.icl.saxon.aelfred.XmlParser.doParse(XmlParser.java:159)
 at com.icl.saxon.aelfred.SAXDriver.parse(SAXDriver.java:320)
 at com.icl.saxon.om.Builder.build(Builder.java:265)
 at
com.icl.saxon.PreparedStyleSheet.prepare(PreparedStyleSheet.java:111)
 at
com.icl.saxon.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl
.java:120)
 at
com.icl.saxon.TransformerFactoryImpl.newTransformer(TransformerFactoryIm
pl.java:72)
 at DoTranforms.main(DoTranforms.java:28)
Exception in thread "main"

Am I missing something here?  I can't figure out why this doesn't work -
when I apply the same stylesheet to the same xml file in Oxygen - using
the same XSL processor - it works fine.. so I'm scratching my head here
a bit.

Cheers,
Adam Schweitzer
Orenda Aerospace Corporation

Current Thread