[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Using xsl:key and key() function on xsl:variable looku

Subject: 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
 Using xsl:key and key() function on xsl:variable looku
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.