First, Saxon always builds indexes at the level of a document node. If the third param of key() is an element node, it still builds an index for the whole document.

Secondly, for a given xsl:key definition and a given document node, it only builds the index once. This is based on run-time node identity, not on any static analysis of what variables are used to refer to the node.

Finally, Saxon uses weak references to ensure that the index is made eligible for garbage collection if either the compiled stylesheet or the document node is garbage collected. For temporary documents of course, the document node will always be the first to go.

Having written this explanation, it occurs to me that there is a potential problem if the match attribute of xsl:key references global variables and the global variable depends on the context item. In this case it would be wrong for two invocations of the same stylesheet, with different initial context items, to share an index. I will log a bug on this.

Michael Kay

On 30 Dec 2013, at 08:47, David Rudel <> wrote:

I use keys a lot and I would like to know how reliably they transfer when the same document is is referenced by different variables.

For example, assume $temp is a temporary document and key() is called with $temp as its third argument.

If a new variable is declared with <xsl:variable name="doc" select="($temp,$fallback)[1]"/>, will the hash created for $temp be applied if key() is called with $doc as the third argument?

What about if a template or function is called with $temp as a parameter? Will the hash be used inside the called template if key() is called with the appropriate argument, or will it be recreated?

I'm assuming in both cases that the hash is built only once and passed along until $temp goes out of scope. At that point I would assume that the memory used for the hash is released. Is this accurate?

David Rudel
Senior Data Analyst
Rapidly troubleshoot problems before they affect your business. Most IT 
organizations don't have a clear picture of how application performance 
affects their revenue. With AppDynamics, you get 100% visibility into your 
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
saxon-help mailing list archived at