[xsl] Using xsl:key and key() function on xsl:variable lookup data

Subject: [xsl] Using xsl:key and key() function on xsl:variable lookup data
From: Russ Loucks <rjl@xxxxxxxxxxxxxxxx>
Date: Wed, 20 Aug 2008 11:36:03 -0500
I have what I think should be a very easy problem to solve but the solution eludes me.

I have a simple stylesheet that attempts to lookup a value using the xsl:key tag and key() functions.

I can easily get this to work if the lookup table is in a file and read it in using the 'document()' function, as follows:

<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
	xmlns:exsl="http://exslt.org/common";
	extension-element-prefixes="exsl"
	>
	<xsl:variable name="lookupDoc" select="document('lookup.xml')" />
	
	<xsl:key name="lookupKey" match="entry" use="@key"/>
	
	<xsl:template match="dataList/data">
		
		<xsl:variable name="dataKey" select="."/>
		data key: <xsl:value-of select="$dataKey" />

<xsl:for-each select="$lookupDoc" >
lookup entry value: <xsl:value-of select="key('lookupKey', $dataKey)"/>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>


The lookup.xml file:

<entries>
	<entry key="key1">lookup entry 1</entry>
	<entry key="key2">lookup entry 2</entry>
	<entry key="key3">lookup entry 3</entry>
</entries>

The source document to be translated:

<dataList>
	<data>key2</data>
	<data>key1</data>
</dataList>

Output from this follows (correct):

                data key: key2
                        lookup entry value: lookup entry 2

                data key: key1
                        lookup entry value: lookup entry 1

However, I want to my 'lookupDoc' to be derived from a parameter or variable, ideally passed down to the stylesheet as a global parameter. Sort of as follows:

	<xsl:variable name="lookupRTF">
		<entries>
			<entry key="key1">lookup entry 1</entry>
			<entry key="key2">lookup entry 2</entry>
			<entry key="key3">lookup entry 3</entry>
		</entries>
	</xsl:variable>
	
	<xsl:variable name="lookupDoc" select="exsl:node-set($lookupRTF)" />

everything else is the same in the stylesheet. When I run this, the key() function cannot find the key in my lookup doc... Output follows:

                data key: key2
                        lookup entry value:

                data key: key1
                        lookup entry value:

I'm stumped.....

Current Thread