Reputation: 1023
Are there some general tips or best practices on how to create more efficient xpath selectors in xslt?
For example, say you have a DocBook XML document that only contains first-level sections, like this:
<chapter>
<section><info><title>my first section</title></info> ... </section>
<section userlevel="expert"><info><title>my second section</title></info> ... </section>
<section><info><title>my third section</title></info> ... </section>
</chapter>
I think that in nearly all cases a selector like //d:section
(1) is worse that one like /d:chapter/d:section
(2). Can I generalize that and say that the more selective the selector the faster it will be?
For another example: If you want the second section element, you could pull it from a sub-tree of all sections or select it directly with //d:section[@userlevel]
. This selector uses the attribute which is more selective but would it be faster?
Rules of thumb of what to do (or to avoid) when using XPath?
Upvotes: 2
Views: 1299
Reputation: 167716
I think you need to look at a particular XSLT or XPath or XQuery processor or even at a particular processor and a particular tree model and then run tests. For instance //foo
so so common that it might be optimized in an implementation.
When I run Saxon 9.5 Java HE with the XQuery //section
ten times on your input sample I get "Average execution time: 1.007739ms" while with /chapter/section
I get "Average execution time: 1.443799ms".
Upvotes: 2