使用 Apache FOP 和 Style Vision 在 Java 中使用 Saxon 将 XSLT 2.0 转换为 PDF

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

我正在使用 Altova StyleVision 生成 XSLT-FO 模板,当我生成 XSLT 1.0 FO 文件时,我使用下面的代码成功地将 Apache FOP 转换为 PDF。

        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();
        }

这段代码效果很好。

XSLT-FO 2.0

问题 一直在尝试使用 XSLT 2.0 的功能,当我从 StyleVision 生成 XSLT-FO 2.0 模板时。

我读了一篇文章 Here and Here 所以我下载了 saxon9.jar [并添加到构建路径],我像下面那样更改了 Transformer 工厂

        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 工厂=新net.sf.saxon.TransformerFactoryImpl();

当我 运行 代码时,我得到一个错误

    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)

你们中有人知道使用 saxon HE 在 java 中将 xml + xslt 2.0 转换为 PDF 的完整解决方案吗?

非常感谢任何帮助。

谢谢。

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

错误信息很清楚:如果你想使用 schema-aware XSLT 2.0,你需要 Saxon-EE。您问题的答案是:

如果你想使用 Saxon-HE,那么你的样式表不能使用 xsl:import-schema