如何使用 Java 通过 XSL 转换 XML
我目前正在使用标准 javax.xml.transform 库通过 XSL 将 XML 转换为 CSV。我的 XSL 文件很大 - 大约 950 行。我的 XML 文件也可能很大。
它在原型阶段运行良好,只有大约 50 行左右的一小部分 XSL。现在,在“最终系统”中,当执行转换时,它会出现错误分支目标偏移量过大。
private String transformXML() {
String formattedOutput = "";
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer =
tFactory.newTransformer( new StreamSource( xslFilename ) );
StreamSource xmlSource = new StreamSource(new ByteArrayInputStream( xmlString.getBytes() ) );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform( xmlSource, new StreamResult( baos ) );
formattedOutput = baos.toString();
} catch( Exception e ) {
e.printStackTrace();
}
return formattedOutput;
}
我看到了一些关于此错误的帖子,但不太确定该怎么做。
我在代码方面做错了什么吗? 是否有任何替代的第三方变压器可以做到这一点?
谢谢,
安德斯
I am currently using the standard javax.xml.transform library to transform my XML to CSV using XSL. My XSL file is large - at around 950 lines. My XML files can be quite large also.
It was working fine in the prototype stage with a fraction of the XSL in place at around 50 lines or so. Now in the 'final system' when it performs the transform it comes up with the error Branch target offset too large for short.
private String transformXML() {
String formattedOutput = "";
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer =
tFactory.newTransformer( new StreamSource( xslFilename ) );
StreamSource xmlSource = new StreamSource(new ByteArrayInputStream( xmlString.getBytes() ) );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform( xmlSource, new StreamResult( baos ) );
formattedOutput = baos.toString();
} catch( Exception e ) {
e.printStackTrace();
}
return formattedOutput;
}
I came across a few postings on this error but not too sure what to do.
Am I doing anything wrong code wise?
Are there any alternative 3rd Party transformers available that could do this?
Thanks,
Andez
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用 Saxon 代替。
您的代码将保持不变。您需要做的就是在 JVM 的系统属性中将 javax.xml.transform.TransformerFactory 设置为 net.sf.saxon.TransformerFactoryImpl 。
Try Saxon instead.
Your code would stay the same. All you would need to do is set
javax.xml.transform.TransformerFactory
tonet.sf.saxon.TransformerFactoryImpl
in the JVM's system properties.使用撒克逊语。 offtop:如果您使用相同的样式表来转换许多 XML 文件,您可能需要考虑模板(预编译的样式表):
Use saxon. offtop: if you use the same stylesheet to transform many XML files, you might want to consider templates (pre-compiled stylesheets):
我在网上看到一篇文章提到 apache XALAN。所以我将罐子添加到我的项目中。尽管我没有在代码中直接引用任何 XALAN 类,但一切都已开始工作。据我所知,它仍然应该使用 jaxax.xml 类。
不太确定那里发生了什么。但它正在发挥作用。
I came across a post on the net that mentioned apache XALAN. So I added the jars to my project. Everything has started working since even though I do not directly reference any XALAN classes in my code. As far as I can tell it still should use the jaxax.xml classes.
Not too sure what is happening there. But it is working.
作为 Saxon 的替代方案,您可以将大模板拆分为更小的模板。
解决方案
本文深入描述了XSLT 样式表的大小限制< /a>.
旁注:如果 saxon 不可用,我只会建议将此作为最后的手段,因为这需要对您的 xsl 文件进行大量更改。
As an alternative to Saxon, you can split up your large template into smaller templates.
Solution
It is described in-depth in this article Size limitation for XSLT stylesheets.
Sidenote: I would only recommend this as a last resort if saxon is not available, as this requires quite a few changes to your xsl file.