How to transform XML with XSL using Java

后端 未结 4 1147
面向向阳花
面向向阳花 2021-01-21 15:54

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

相关标签:
4条回答
  • 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.

    0 讨论(0)
  • 2021-01-21 16:49

    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.

    0 讨论(0)
  • 2021-01-21 16:52

    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(...);
    

    0 讨论(0)
  • 2021-01-21 16:59

    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.

    0 讨论(0)
提交回复
热议问题