XSLT 性能
我正在从事一个有许多 XSLT 转换的项目。 转换必须尽可能快。
为了便于阅读,我写了很多内容,将“业务逻辑”和 “输出”。例如
<!-- Business Logic -->
<xsl:variable name="myLocalVar">
<xsl:value-of select="func:whateverComputation(params)" />
</xsl:variable>
<!-- more buss logic here -->
<!-- Output -->
<xsl:element name="mytag">
<xsl:value-of select="$myLocalVar" />
</xsl:element>
,当然这可以写成紧凑的形式,
<xsl:element name="mytag">
<xsl:value-of select="func:whateverComputation(params)" />
</xsl:element>
第一种形式比第二种形式慢吗?
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 "business logic" and
"output". For example
<!-- Business Logic -->
<xsl:variable name="myLocalVar">
<xsl:value-of select="func:whateverComputation(params)" />
</xsl:variable>
<!-- more buss logic here -->
<!-- Output -->
<xsl:element name="mytag">
<xsl:value-of select="$myLocalVar" />
</xsl:element>
Of course this can be written in a compact form
<xsl:element name="mytag">
<xsl:value-of select="func:whateverComputation(params)" />
</xsl:element>
Is the first form slower than the second one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 XSLT 常见问题解答的一部分:
与 XSLT 性能相关的几点:
xsl:variables
是动态值。这些变量不在缓存中,并且每次在 XSL 中引用它们时都会运行。 xsl:variable 的显式类型转换可提高性能。您可以使用string()
和boolean()
函数进行类型转换。例如:
不要使用子元素,而应在任何地方使用属性可能的。使用属性而不是元素可以提高性能。执行 XPath 匹配时,属性速度更快,因为它们是松散类型的。这使得模式验证变得更加容易。
当您匹配属性值时,请使用枚举器属性。使用多个属性名称作为位,并将它们的值设置为 true 或 false。
关于如何有效使用 XSLT 的八个技巧:
保持源文档较小。如果需要,请先拆分文档。
在运行之间保持 XSLT 处理器(和 Java VM)加载在内存中
如果重复使用相同的样式表,请先编译它。
如果您重复使用相同的源文档,请将其保留在内存中。
如果您重复执行相同的转换,请不要这样做。而是存储结果。
保持输出文档较小。例如,如果您要生成 HTML,请使用 CSS。
切勿多次验证同一源文档。
将复杂的转换拆分为多个阶段。
有关如何编写高效 XSLT 的八个技巧:
避免重复使用“//item”。
不要多次评估同一节点集;将其保存在变量中。
如果可以的话,避免使用
。例如,通过使用position()
。使用
,例如解决分组问题。避免模板规则中出现复杂的模式。相反,请在规则中使用
。使用
前面的[-sibling]
或后面的[-sibling]
轴时要小心。这通常表明算法具有 n 平方性能。不要对同一节点集进行多次排序。如有必要,将其保存为结果树片段并使用
node-set()
扩展函数访问它。要输出简单
#PCDATA
元素的文本值,请优先使用
而不是
。From a section of the the XSLT FAQ:
Few Points related to XSLT Performance:
xsl:variables
are dynamic values. These variables are not in cache, and run every time that they are referenced in XSL. Explicit type casting ofxsl:variable
improves the performance. You can do type casting withstring()
andboolean()
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.
Eight tips for how to use XSLT efficiently:
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.
Eight tips for how to write efficient XSLT:
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 usingposition()
.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]
orfollowing[-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>
.将函数应用程序的结果保存到变量不会对性能产生任何重大影响在一般情况下(并且某些 XSLT 处理器(例如 Saxon)使用惰性求值,因此不会对函数求值直到实际需要该变量为止)。
相反,如果必须使用相同参数多次计算函数,则将结果保存在变量中可以在某些情况下显着提高效率。
提高性能的正确方法是:
分析/测量以识别真正的瓶颈。
仅优化最大的瓶颈。
如果仍然需要提高性能,则开始新的迭代,转到上面的 1.。
引用 Donald Knuth 的话:“过早的优化是万恶之源”——这实际上是对一句名言的释义:“通往地狱的道路是都是出于善意。”
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."
有点晚了,但我想我应该分享这个链接:提高性能的技术XSL 转换。
A little late to the game, but I thought I'd share this link: Techniques to Improve Performance of XSL Transformations.