[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] key() and document() - scoping problem?


Subject: Re: [xsl] key() and document() - scoping problem?
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Tue, 19 Dec 2006 09:55:41 +0000

On 12/19/06, Bryn Lewis <blewis@xxxxxxxxxx> wrote:
I have defined a key, then use its value in the following xsl (xslt v.1):

<span class="x">
        <xsl:value-of select="count(key('key2_1', 'general'))" />
</span>
<xsl:for-each select="document(string($options))/options/option">
        <span class="y">
                <xsl:value-of select="count(key('key2_1', 'general'))" />
        </span>
</xsl:for-each>

Results in this:

<span class="x">4</span>
<span class="y">0</span>

I would expect to get a '4' in both cases.

Why? The key is used on the document that the context node belongs to, so you are seeing the results from two different documents.

In this case to get the same result inside that for-each as outside it
you can either:

- store the result of the call to key() in a variable and use that
- store a pointer to the original source document and switch back to
it before calling key(), either by using another for-each in XSLT 1.0
or the third argument to key() in XSLT 2.0

cheers
andrew


Current Thread
Keywords