At 2008-12-10 11:20 +0100, Jos van Roosmalen wrote:
Hello,
I am looking for a very nice mapper "Design Pattern".
I used the XML/XSL snippet below. However the "@key eq ." seems not to work.
If I introduce a variable it works fine:
<xsl:variable name="var" select="."/>
<xsl:value-of select="$map[(_at_)key eq $var]/@val"/>
Questions:
1. How to get this mapper working without variable?
$map[(_at_)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[(_at_)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
--
Upcoming XSLT/XSL-FO, UBL and code list hands-on training classes:
: Sydney, AU 2009-01/02; Brussels, BE 2009-03; Prague, CZ 2009-03
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video sample lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg
Video course overview: http://www.youtube.com/watch?v=VTiodiij6gE
G. Ken Holman mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers: http://www.CraneSoftwrights.com/legal
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--