Altova Mailing List Archives


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

From: "Andrew Welch" <andrew.j.welch@--------->
To:
Date: 12/19/2006 9:56:00 AM
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

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.