如何使用 Java 通过 XSL 转换 XML

发布于 2024-10-01 02:30:21 字数 964 浏览 3 评论 0原文

我目前正在使用标准 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱&纷飞 2024-10-08 02:30:22

尝试使用 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 to net.sf.saxon.TransformerFactoryImpl in the JVM's system properties.

枯寂 2024-10-08 02:30:22

使用撒克逊语。 offtop:如果您使用相同的样式表来转换许多 XML 文件,您可能需要考虑模板(预编译的样式表):

javax.xml.transform.Templates style = tFactory.newTemplates(xslSource);
style.newTransformer().transform(...);

Use saxon. offtop: if you use the same stylesheet to transform many XML files, you might want to consider templates (pre-compiled stylesheets):


javax.xml.transform.Templates style = tFactory.newTemplates(xslSource);
style.newTransformer().transform(...);

软的没边 2024-10-08 02:30:22

我在网上看到一篇文章提到 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.

巡山小妖精 2024-10-08 02:30:22

作为 Saxon 的替代方案,您可以将大模板拆分为更小的模板。

XSLT 样式表中包含的模板定义由 SAP 编译
JVM 的 XSLT 编译器“Xalan”为 Java 方法,以便更快地执行
转变。 Java字节码分支指令包含在这些
Java 方法的偏移量限制为 32K。大型模板定义
现在可以导致非常大的 Java 方法,其中分支偏移量将
需要大于32K。因此这些样式表不能
编译为 Java 方法,因此不能用于
转换。

解决方案

由于 XSLT 样式表的每个模板定义都被编译为
可以使用一个单独的 Java 方法,使用多个较小的模板
作为解决方案。一个非常大的模板可以分成多个较小的模板
使用“call-template”元素来调用模板。

本文深入描述了XSLT 样式表的大小限制< /a>.

旁注:如果 saxon 不可用,我只会建议将此作为最后的手段,因为这需要对您的 xsl 文件进行大量更改。

As an alternative to Saxon, you can split up your large template into smaller templates.

Template definitions contained in XSLT stylesheets are compiled by SAP
JVM's XSLT compiler "Xalan" into Java methods for faster execution of
transformations. Java bytecode branch instructions contained in these
Java methods are limited to 32K offsets. Large template definitions
can now lead to very large Java methods, where the branch offset would
need to be larger than 32K. Therefore these stylesheets cannot be
compiled to Java methods and therefore cannot be used for
transformations.

Solution

Since each template definition of an XSLT stylesheet is compiled into
a separate Java method, using multiple smaller templates can be used
as solution. A very large template can be broken into multiple smaller
templates by using the "call-template" element.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文