Search code examples
javaxmlxslt-2.0saxonapache-fop

XSLT 2.0 Transformation To PDF In Java with Saxon Using Apache FOP and Style Vision


I am using Altova StyleVision to generate and XSLT-FO Template, When I generate an XSLT 1.0 FO file, I successfully to PDF with Apache FOP transformation using the code below.

        DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
        Configuration cfg = cfgBuilder
                .buildFromFile(new File("fop.xconf"));
        FopFactoryBuilder fopFactoryBuilder = new FopFactoryBuilder(
                new File("fopbase").toURI()).setConfiguration(cfg);
        FopFactory fopFactory = fopFactoryBuilder.build();

        File xsltFile = new File(
                "xslt1File.xslt");
        StreamSource xmlSource = new StreamSource(
                new File("xmlData.xml"));

        FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
        OutputStream out;
        out = new java.io.FileOutputStream("GeneratedPDF.pdf");
        try {
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(new StreamSource(xsltFile));
            Result res = new SAXResult(fop.getDefaultHandler());
            transformer.transform(xmlSource, res);

        } finally {
            out.close();
        }

This code works great.

XSLT-FO 2.0

The Issue has been trying to use features of XSLT 2.0, When I generate XSLT-FO 2.0 template from StyleVision.

I have read an article Here and Here so I downloaded the saxon9.jar [and added to build path], I changed the Transformer factory just like below

        DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
    Configuration cfg = cfgBuilder
            .buildFromFile(new File("fop.xconf"));
    FopFactoryBuilder fopFactoryBuilder = new FopFactoryBuilder(
            new File("fopbase").toURI()).setConfiguration(cfg);
    FopFactory fopFactory = fopFactoryBuilder.build();

    File xsltFile = new File(
            "xslt1File.xslt");
    StreamSource xmlSource = new StreamSource(
            new File("xmlData.xml"));

    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
    OutputStream out;
    out = new java.io.FileOutputStream("GeneratedPDF.pdf");
    try {
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
        TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl();
        Transformer transformer = factory.newTransformer(new StreamSource(xsltFile));
        Result res = new SAXResult(fop.getDefaultHandler());
        transformer.transform(xmlSource, res);

    } finally {
        out.close();
    }

TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl();

When I run the code, I get an error

    Error at xsl:import-schema on line 12 column 67 of xsltfile.xslt:
  XTSE1650: xsl:import-schema requires Saxon-EE
javax.xml.transform.TransformerConfigurationException: net.sf.saxon.s9api.SaxonApiException: xsl:import-schema requires Saxon-EE
    at net.sf.saxon.jaxp.SaxonTransformerFactory.newTemplates(SaxonTransformerFactory.java:157)
    at net.sf.saxon.jaxp.SaxonTransformerFactory.newTransformer(SaxonTransformerFactory.java:110)
    at main.FopTransformer.convertToPDF(FopTransformer.java:60)
    at main.FopTransformer.main(FopTransformer.java:29)

Does any of you guys know of a complete solution that transforms xml + xslt 2.0 to PDF in java using saxon HE.

Any help would be highly appreciated.

Thanks.


Solution

  • Does any of you guys know of a complete solution that transforms xml + xslt 2.0 to PDF in java using saxon HE.

    The error message is pretty clear: if you want to use schema-aware XSLT 2.0, you need Saxon-EE. The answer to your question is:

    if you want to do it with Saxon-HE, then your stylesheet can't use xsl:import-schema.