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
From a section of the the XSLT FAQ:
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 ] )">
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.
When you match against attribute values, use enumerator attributes. Use multiple attribute names as bits, and set their values to true or false.
Keep the source documents small. If necessary split the document first.
Keep the XSLT processor (and Java VM) loaded in memory between runs
If you use the same stylesheet repeatedly, compile it first.
If you use the same source document repeatedly, keep it in memory.
If you perform the same transformation repeatedly, don't. Store the result instead.
Keep the output document small. For example, if you're generating HTML, use CSS.
Never validate the same source document more than once.
Split complex transformations into several stages.
Avoid repeated use of "//item".
Don't evaluate the same node-set more than once; save it in a variable.
Avoid <xsl:number>
if you can. For example, by using position()
.
Use <xsl:key>
, for example to solve grouping problems.
Avoid complex patterns in template rules. Instead, use <xsl:choose>
within the rule.
Be careful when using the preceding[-sibling]
or following[-sibling]
axes. This often indicates an algorithm with n-squared performance.
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.
To output the text value of a simple #PCDATA
element, use <xsl:value-of>
in preference to <xsl:apply-templates>
.
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:
Profile/measure to identify real bottlenecks.
Optimize only the biggest bottlenecks.
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."
A little late to the game, but I thought I'd share this link: Techniques to Improve Performance of XSL Transformations.