UPDATE 17.Jul.2013:
XALAN 2.7
does not cache document()
calls within a request. So it is crucial to store each needed document in a variable in the XSL.
I have searched for quite a while and didn't find concrete answers to my simple question:
Which approach is faster or is the compiler "smart" enough so that both variants are the same?
Note: I am using Xalan 2.7 (default implementation in JDK 1.6):
1) I have to read a property in an external XML:
<xsl:value-of select="document($path)/person/address/city"/>
Whenever I need the city, I use the expression above (let's say 100 times)
2) Instead of calling the document() 100 times, I store the XML node in a variable:
<xsl:variable name="node" select="document($path)"/>
And then I use 100 times
<xsl:value-of select="$node/person/address/city"/>
Which one is faster, better, for which reasons? Thank you!
document(path_to_doc)
are dependent on the xslt processor caching realization, in the case, when document node stored in the variable it must be loaded once in any case. – Tarpdocument()
results, but xsltproc does. However thedocument()
argument is interpreted as an URI (see spec), so an aggressive caching would make perfect sense. – Trierarchdocument()
call will be executed and includes physical file access. So at least for XALAN 2.7 it makes a lot of sense to store the document in a variable. I updated my question with the test results. – Matilda