【发布时间】: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 模板时。
我读过一篇文章Here和Here所以我下载了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