How to transform XML with XSL using Java

后端 未结 4 1175
面向向阳花
面向向阳花 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条回答
  •  不要未来只要你来
    2021-01-21 16:38

    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.

提交回复
热议问题