Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

optimization - How do I profile and optimize an XSLT?

I have an XSLT for viewing XML files in the browser. The XSLT is naively written and currently takes a long time to execute (several minutes).

My XML file is of modest size (~1 MiB), and other XSLTs for the same document that do different processing execute much more quickly. So I know it isn't the size of the XML that is the problem, it's my XSLT.

How do I go about profiling and optimizing my XSLT?

(Is it a bad idea to be doing complex XSLTs in the browser? Should I instead apply the XSLT application side?)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

which XSLT engine are you using? If you are using the .NET engine and Visual Studio you could use the XSLT profiler integrated into Visual Studio which is a very useful.

Other excellent profiling tools are Altova's XML Spy and Oxygen.

If you would post your XSLT it would be easier to tell you where possible bottlenecks are. In general be careful with XPath expressions such as '//', preceding::* and following::*. Some more rules and best-practices:

  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)

Following these rules will typically result in very efficient XSLT and you possibly won't need to use a profiler at all.

Concerning your question about XSLT in the browser: I wouldn't recommend it because first you are not platform independent (not every browser might support it or some browsers may only support it with a poorly performing engine) and second you can't control the engine used.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...