optimization - 如何分析和优化XSLT?

标签 optimization xslt profiling

我有一个XSLT,用于在浏览器中查看XML文件。 XSLT是天真的编写的,并且当前需要很长时间才能执行(几分钟)。

我的XML文件大小适中(〜1 MiB),并且同一文档的其他XSLT执行不同的处理的速度要快得多。所以我知道问题不是XML的大小,而是我的XSLT。

如何进行性能分析和优化XSLT?

(在浏览器中执行复杂的XSLT是一个坏主意吗?我应该改用XSLT应用程序端吗?)

最佳答案

您正在使用哪个XSLT引擎?如果您使用的是.NET引擎和Visual Studio,则可以使用集成到Visual Studio中的XSLT profiler,这非常有用。

其他出色的性能分析工具是Altova的XML SpyOxygen

如果您要发布XSLT,则更容易告诉您可能出现的瓶颈。通常,请谨慎使用XPath表达式,例如'//',previous::*和following::*。其他一些规则和最佳做法:

  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 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>.

(from http://www.dpawson.co.uk/xsl/sect4/N9883.html#d15756e150)



遵循这些规则通常会产生非常高效的XSLT,您可能根本不需要使用探查器。

关于您在浏览器中有关XSLT的问题:我不建议您这样做,因为首先您不是平台独立的(并非每个浏览器都可能支持它,或者某些浏览器可能只通过性能不佳的引擎支持它),其次您无法控制使用的引擎。

关于optimization - 如何分析和优化XSLT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/434976/

相关文章:

mysql - 在多列上使用单个值的 WHERE 优化

javascript - 延迟加载 addthis 脚本? (或延迟加载依赖于已触发事件的外部 js 内容)

xslt - 使用 xslt 查找唯一节点

c - 了解使用GCC编译器在ARM平台上优化C代码

ios - 优化多个单元格的 CellForRow 方法

xml - XPath contains() 条件含义?

xml - XSLT:将同级文本节点移动到选定节点中以进行 XLIFF 修复

sql-server - 此后,在SQL Server分析 session 中,将已编译的准备好的语句与exec进行协调

performance - 分析 Haskell 程序的缓慢性能

linux - 为什么perf显示sleep占用所有内核?