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?)
XSLT Processor takes the XSLT stylesheet and applies the transformation rules on the target XML document and then it generates a formatted document in the form of XML, HTML, or text format. This formatted document is then utilized by XSLT formatter to generate the actual output which is to be displayed to the end-user.
XSLT is designed to be used as part of XSL. In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary.
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:
- 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 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>
.(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.
If you provide the XSLT code and the XML document on which you observe the problem, I and other people could try to help.
Here are some XSLT usage and performance tips from Michael Kay:
Eight tips for how to use XSLT efficiently:
Eight tips for how to write efficient XSLT:
//item
".<xsl:number>
if you can. For example, by using position()
.<xsl:key>
, for example to solve grouping problems.<xsl:choose>
within the rule.preceding[-sibling]
or following[-sibling]
axes. This often indicates an algorithm with n
-squared performance.node-set()
extension function.<xsl:value-of>
in preference to <xsl:apply-templates>
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With