XSLT Performance

后端 未结 3 900
一向
一向 2020-12-17 01:04

I am working for a project which has many XSLT transformations. The transformations have to be as fast as possible.

For readability I wrote many of them dividing \"b

相关标签:
3条回答
  • 2020-12-17 01:29

    From a section of the the XSLT FAQ:

    Few Points related to XSLT Performance:

    1. xsl:variables are dynamic values. These variables are not in cache, and run every time that they are referenced in XSL. Explicit type casting of xsl:variable improves the performance. You can do type casting with string() and boolean() functions.

      For example:

      <xsl:variable name="_attr" select="string( /node/child[ @attr ] )">

    2. Instead of using sub-elements, use attributes wherever possible. Using attributes instead of elements improves the performance. When performing XPath matches, attributes are faster because they are loosely typed. This makes validation of the schema easier.

    3. When you match against attribute values, use enumerator attributes. Use multiple attribute names as bits, and set their values to true or false.

    Eight tips for how to use XSLT efficiently:

    1. Keep the source documents small. If necessary split the document first.

    2. Keep the XSLT processor (and Java VM) loaded in memory between runs

    3. If you use the same stylesheet repeatedly, compile it first.

    4. If you use the same source document repeatedly, keep it in memory.

    5. If you perform the same transformation repeatedly, don't. Store the result instead.

    6. Keep the output document small. For example, if you're generating HTML, use CSS.

    7. Never validate the same source document more than once.

    8. Split complex transformations into several stages.

    Eight tips for how to write efficient XSLT:

    1. Avoid repeated use of "//item".

    2. Don't evaluate the same node-set more than once; save it in a variable.

    3. Avoid <xsl:number> if you can. For example, by using position().

    4. Use <xsl:key>, for example to solve grouping problems.

    5. Avoid complex patterns in template rules. Instead, use <xsl:choose> within the rule.

    6. Be careful when using the preceding[-sibling] or following[-sibling] axes. This often indicates an algorithm with n-squared performance.

    7. Don't sort the same node-set more than once. If necessary, save it as a result tree fragment and access it using the node-set() extension function.

    8. To output the text value of a simple #PCDATA element, use <xsl:value-of> in preference to <xsl:apply-templates>.

    0 讨论(0)
  • 2020-12-17 01:34

    Saving the result of function application to a variable isn't going to have any significant impact on performance in the general case (and some XSLT processors such as Saxon use lazy evaluation, so the function will not be evaluated untill the variable is actually needed).

    On the contrary, if the function must be evaluated more than once with the same parameters, saving the result in a variable can result in some cases in significant increase of efficiency.

    The correct way to improve performance is:

    1. Profile/measure to identify real bottlenecks.

    2. Optimize only the biggest bottlenecks.

    3. If there is still need for increased performance, start a new iteration, going to 1. above.

    To quote Donald Knuth: "Premature optimization is the root of all evil" -- which is actually a paraphrase of the wellknown saying: "The road to hell is paved with good intentions."

    0 讨论(0)
  • 2020-12-17 01:41

    A little late to the game, but I thought I'd share this link: Techniques to Improve Performance of XSL Transformations.

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