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

Subject: RE: [xsl] key() and document() - scoping problem?
From: "Bryn Lewis" <blewis@xxxxxxxxxx>
Date: Wed, 20 Dec 2006 09:56:43 +1100
ok, I now understand the problem, but not the solution. What I actually want
is this:

<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', .))" />
        </span>
</xsl:for-each>

to produce this:
<span class="x">4</span>
<span class="y">4</span>

and not:
<span class="x">4</span>
<span class="y">0</span>

That is, each of the options/option values is an index into the key. I don't
want to have to specify what each of them is.

If I change the context node I don't have the value to look up.  ?

Bryn

-----Original Message-----
From: Andrew Welch [mailto:andrew.j.welch@xxxxxxxxx]
Sent: Tuesday, 19 December 2006 8:56 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] key() and document() - scoping problem?

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


--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.432 / Virus Database: 268.15.24/592 - Release Date: 18/12/2006
1:45 PM


--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.432 / Virus Database: 268.15.25/593 - Release Date: 19/12/2006
1:17 PM

The contents of this email and any attachments are confidential and may be
privileged
or otherwise protected from disclosure.

If you are not the intended recipient of this email
please notify the sender immediately, delete the email and
any attachments from your system and do not print, distribute,
store, commercialise or act on any information it contains.

Current Thread