sky
sky

Reputation: 447

Referring a parent node in XML while XSLT reduces performance drastically

While doing XSLT optimization, I figured out that by avoiding parent node look-up the XSLT run duration reduces from 1 hour to ~20 seconds.

I just avoided below xsl line by passing relevant params to take the required decision and the performance just boosted.

<xsl:if test="parent::node() = /test">
...
</xsl:if>

I am using saxon8.jar library to perform XSLT that uses SAX-based XML parsing.

I understand that SAX is push-based, so referring back is costly. But, would like to get some more insight on the cost and the algorithm involved here.

Also, in my XSL I am referring same forward nodes many times using Xpath at different steps of execution. Then, why this did not turn into a bottleneck like the way referring to the parent node did?

Upvotes: 0

Views: 220

Answers (2)

Michael Kay
Michael Kay

Reputation: 163587

I suspect that you meant parent::node() is /test. That, is you want to know if the parent node is the same node as the /test node. That would be a very fast test. But using "=" is slow: it forms the string value of parent::node(), and the string-value of /test, and compares them as strings. The string value of /test is the concatenation of all the text nodes in the document, so it involves searching the whole document and building a potentially very large string.

Upvotes: 1

Kevin Brown
Kevin Brown

Reputation: 8877

I would test a count of the ancestors. A nodal comparison like you are doing is comparing everything in the tree. If you are only interested in second level nodes (and not that the whole tree is equal to another tree) then I would expect something like:

<xsl:when test="count(ancestor::node()) = 2"/>

or maybe better performance is

<xsl:when test="count(parent::node() | /test = 1)"/>

Would be better. Just my guess.

Upvotes: 0

Related Questions