[Home] [By Thread] [By Date] [Recent Entries]
At 2008-12-10 11:20 +0100, Jos van Roosmalen wrote:
Hello, $map[@key eq current()]/@val The current() function returns the current node at the beginning of the XPath expression: T:\ftemp>type jos1.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:variable name="map" as="element()+">
<elem key="key1" val="value1"/>
<elem key="key2" val="value2"/>
<elem key="key3" val="value3"/>
</xsl:variable>
<xsl:template match="demos/demo">
Found key: <xsl:value-of select="."/>
Corresponding value: <xsl:value-of select="$map[@key eq current()]/@val"/>
<br/>
</xsl:template>
</xsl:stylesheet>T:\ftemp>xslt2 jos.xml jos1.xsl <?xml version="1.0" encoding="UTF-8"?> Found key: key1
Corresponding value: value1<br/> Found key: key2
Corresponding value: value2<br/>T:\ftemp> 2. What is the the 'defacto XPath standard' for mapping a list hardcoded keys to hardcoded values? By using keys on trees (rather than on flat sets of elements). In the code below, note that I changed your $map to be a tree instead of a flat set of elements, and then I used a key to access the information in that tree. The third argument of key() is the apex of a subtree to use for keyed access, thus preventing its use when $map is simply a set of elements. T:\ftemp>type jos2.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:variable name="map">
<elem key="key1" val="value1"/>
<elem key="key2" val="value2"/>
<elem key="key3" val="value3"/>
</xsl:variable>
<xsl:key name="mapkey" match="elem" use="@key"/>
<xsl:template match="demos/demo">
Found key: <xsl:value-of select="."/>
Corresponding value: <xsl:value-of select="key('mapkey',.,$map)/@val"/>
<br/>
</xsl:template>
</xsl:stylesheet>T:\ftemp>xslt2 jos.xml jos2.xsl <?xml version="1.0" encoding="UTF-8"?> Found key: key1
Corresponding value: value1<br/> Found key: key2
Corresponding value: value2<br/>T:\ftemp> I hope this helps. . . . . . . . . . . Ken
|

Cart



