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

RE: Keeping a map in my XSL

Subject: RE: Keeping a map in my XSL
From: "Nathan Young \(natyoung\)" <natyoung@xxxxxxxxx>
Date: Tue, 13 Sep 2005 10:43:21 -0700
nathan keeping
Hi.

Kevin, I've done something like what you want to do using the document
function and xpaths.  I agree with Andrew's suggestion about not relying
on document order to associate key/value pairs, but we have values that
can contain elements so we use something more like:

<map>
   <key name="1">value of name 1</key>
   <key name="7">value of name 7</key>
</map>

That's in a separate file from the xsl (say map.xml).

Then in the xsl you can use:

<xsl:variable name="map" select="document('map.xml')"/>

To get then value for 1 you can use:

<xsl:value-of select="$map//key[@name='1']/>

Andrew would your xsl:key solution apply to this?  If so how?

---->Nathan

> -----Original Message-----
> From: andrew welch [mailto:andrew.j.welch@xxxxxxxxx]
> Sent: Tuesday, September 13, 2005 9:02 AM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re:  Keeping a map in my XSL
>
> On 9/13/05, Kevin L. Cobb <kevin.cobb@xxxxxxxxxxxx> wrote:
> > I want to be able to set a variable that is retreived from
> my XML I am
> > parsing to a mapped values. For example, lets say I have an
> XML map that
> > looks like this:
> >
> > <map>
> >         <key>12345</key><value>abcde</key>
> >         <key>78910</key><value>lmnop</value>
> > </map>
> >
> > My source XML has the value '12345' in a certain field. I
> retreive the
> > field as usual in XSL and then I want to get the mapped
> value of this
> > field. The map above is "included" in my XSL. Is there a
> best way to do
> > this in XSL?
>
> yes, first restructure your map as associating a key and value by
> position is a bad idea, much better to use attributes:
>
> <map>
>   <key name="12345" value="abcde"/>
>   <key name="78910" value="lmnop"/>
> </map>
>
> then define a key to return a value for a given name:
>
> <xsl:key name="mappings" match="key" use="@name"/>
>
> then in xslt 1.0 use:
>
> <xsl:variable name="lookup" select="the_name_you_want_lookup"/>
> <xsl:for-each select="$map">
>   <xsl:value-of select="key('mappings', $lookup)/@value"/>
> </xsl:for-each>
>
> in 2.0 you don't have the hassle of switching the context node because
> key() can take it as the third argument:
>
>   <xsl:value-of select="key('mappings', the_name_you_want_to_lookup,
> $mappings)/@value"/>
>
> cheers
> andrew

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.