RE: [xsl] Can you index document() nodeset with <xsl:key?

Subject: RE: [xsl] Can you index document() nodeset with <xsl:key?
From: "Hondros, Constantine" <Constantine.Hondros@xxxxxxxxxxxxxxxx>
Date: Mon, 4 Jul 2005 16:01:52 +0200
Thanks David,
Yes, I should have been switching context with <xsl:for-each. It turns out
that there was also some unexpected behaviour with the key matching.

Xalan bundled with JDK1.5 only retrieves my keyed nodes if the lookup value
is first wrapped in a variable, then has string() applied to it. Saxon just
needs it wrapped in a variable.

Below, you can see how I set up the key and read in my look-up table :

<xsl:key name="topicid" match="topic" use="@id" />
<xsl:variable name="catalog" select="document('../build/catalog.xml')" />

Next, in the document being transformed, I need to refer to keyed values
using the attribute @href as a key:

<xsl:variable name="hrefvalue" select="@href" />
<xsl:for-each select="$catalog">
	<xsl:value-of select="key('topicid', $hrefvalue)" />
</xsl:for-each>


The above doesn't work as I expect it to with Xalan: the <xsl:value-of>
statement returns the string "http://www.w3.org/XML/1998/namespace"; instead
of the node I want. Saxon behaves as expected. When I instead do the
following :

<xsl:variable name="hrefvalue" select="@href" />
<xsl:for-each select="$catalog">
	<xsl:value-of select="key('topicid', string($hrefvalue))" />
</xsl:for-each>

... Xalan is happy.

Odd, because I thought that the key() function itself would take care of the
required string conversion stuff.

Cheers.


-----Original Message-----
From: David Carlisle [mailto:davidc@xxxxxxxxx]
Sent: 01 July 2005 17:40
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Can you index document() nodeset with <xsl:key?



keys automatically work (separately) on any loaded document.
so you just want
<xsl:key name="x" match="..." use="..."/>
at the top level

and make sure that you are in teh correct document when you call key()
as key (in xslt1) only ever returns nodes in the current document.


<xsl:for-each select="$mynodeset">
  <xsl:apply-templates select="key('x','something')"/>
</xsl:for-each>

> Is the approach at least valid? 
you didn't show  any of your usage of keys so hard to say.

David



________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________


-- 
The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it. 

Current Thread