gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:04:41 25 4
gpt4 key购买 nike

我正在使用 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 工厂 = 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 在 java 中转换为 PDF 的完整解决方案吗?

任何帮助将不胜感激。

谢谢。

最佳答案

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

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

如果您想使用 Saxon-HE 执行此操作,则您的样式表不能使用 xsl:import-schema

关于java - 使用 Apache FOP 和 Style Vision 通过 Saxon 将 XSLT 2.0 在 Java 中转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41889215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com