Search code examples
xsltapache-fop

Empty xsl result in eclipse using Apache FOP 2.3


I have XSLstyle sheets and some XML example files to change into PDFs. I've verified that the XSL sheets are correct (the PDF generated using the command line version of Apache-FOP 2.3 are consistent and no errors are thrown). When I use Eclipse > Run > Transform with Xalan 2.7.1 the result is the same as the FO output of the command line version.

However I've since then tried to implement the embedded version in java and it hasn't been working. I've checked the FO result and it appears to be void of any FO markers, the result file only containing the text value of the XML. This results in a fop.fo.ValidationException.

I suspect it has something to do with conflicting versions of some library or maybe a missing parameter in a property file, but so far I haven't been able find the error.

EDIT I can't share the XSL and XML files, but here is the my java snippet:

  private void transformToPdf(Document docJDOM, OutputStream destination, File xslFile)
throws SAXException, IOException, JDOMException, TransformerException {
    FopFactory fopFactory = FopFactory.newInstance(FOPCONFIGFILE);
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, destination);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new 
    StreamSource(xslFile));
    Source source = new DOMSource(transformJdomToDom(docJDOM));        
    Result result = new SAXResult(fop.getDefaultHandler());
    transformer.transform(source, result);
}

Where FOPCONFIGFILE is my fop.xconf file (I'm using the default file provided in the Apache-FOP website), docJDom is a JDomDocument of the xml file I wish to transform and xslFile is the xslStylesheet I wish to apply. I've checked the values of docJDom, xslFile and they are correct.

EDIT 2 I've just tried outputting the fo result file using the embedded code by changing this

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new 
    StreamSource(xslFile));
    Source source = new DOMSource(transformJdomToDom(docJDOM));        
    Result result = new SAXResult(fop.getDefaultHandler());
    transformer.transform(source, result);

to this

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xslFile));
Source source = new DOMSource(transformJdomToDom(docJDOM));
Result result = new StreamResult(destination);
transformer.transform(source, result);

And checked the resulting fo for errors. I was able to find multiple lines such as <fo:table-row height="0.5cm"/> which are the source of the error. However I then transformed the same XML file using the same XSL stylesheet but with the Apache-FOP 2.3 binary distribution, the resulting fo was correct and did not contain those errors.


Solution

  • I have located the error: the XSL stylesheets are correct, the problem is that the module that is using these files copies them to a different folder and uses those copies to do the pdf transformation. By running the unit tests individually, my corrected XSL sheets weren't copied, and thus I was using the old incorrect versions.