【问题标题】:XSLT 2.0 Transformation To PDF In Java with Saxon Using Apache FOP and Style Vision使用 Apache FOP 和 Style Vision 使用 Saxon 将 XSLT 2.0 转换为 Java 中的 PDF
【发布时间】:2017-01-27 07:57:26
【问题描述】:

我正在使用 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 模板时。

我读过一篇文章HereHere所以我下载了saxon9.jar [并添加到构建路径],我改变了变压器工厂,如下所示

        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 工厂 = new 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 的完整解决方案吗?

任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: java xml xslt-2.0 saxon apache-fop


    【解决方案1】:

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

    错误信息非常清楚:如果您想使用模式感知 XSLT 2.0,您需要 Saxon-EE。你的问题的答案是:

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

    【讨论】:

    • 嗨,对不起,迟到了,我添加了 saxon9ee.jar 但仍然有同样的错误。
    • 天哪。我不敢相信我得到了 XSLT 2.0 和 XPath 2.0 第 4 版的作者的回复。感谢您帮助我解决问题,尽管 saxon9ee.jar 仍然失败。
    • 除了将 Saxon-EE jar 放在类路径中,您还需要使用 TransformerFactory factory = new com.saxonica.config.EnterpriseTransformerFactory(); 调用它
    • 谢谢。现在我需要许可证。
    猜你喜欢
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2012-03-05
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多