使用Apache FOP和Style Vision将XSLT 2.0转换为使用Saxon的Java

时间:2017-01-27 07:57:26

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

我正在使用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

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

我已经阅读了文章HereHere,所以我下载了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 factory = 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将xml + xslt 2.0转换为PDF的完整解决方案。

任何帮助都将受到高度赞赏。

感谢。

1 个答案:

答案 0 :(得分:2)

  

你们有没有人知道改变xml +的完整解决方案   使用saxon HE在java中使用xslt 2.0到PDF。

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

如果您想使用Saxon-HE,那么您的样式表无法使用xsl:import-schema