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

Re: Lookup in a xml file with a key

Subject: Re: Lookup in a xml file with a key
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Tue, 10 Jul 2001 03:48:59 -0700 (PDT)
xsl key lookup
Gaston Annebicque wrote:

> Hi,

> Here is my problem :
> I have this xml file :
> <in>
> 	<country>010</country>
> 	<country>020</country>
> </in>
> 
> and with this other xml file that describe a table, I named the file
> "lookup.xml" :
> <?xml version="1.0"?>
> <lookup>
> 	<countrydef>
> 		<abbr>010</abbr>
> 		<name>Germany</name>
> 	</countrydef>
> 	<countrydef>
> 		<abbr>020</abbr>
> 		<name>France</name>
> 	</countrydef>
> </lookup>
> 
> I would like to output the following :
> <out>
> Germany
> France
> </out>
> 
> I can't manage to get my xslt to work, here is the current version of it :
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>   <xsl:output indent="yes" omit-xml-declaration="yes"/>
>   <xsl:key name="kLookup" match="abbr" use="name"/>
> 
>   <xsl:template match="/">
>     <in>
>       <xsl:apply-templates select="in/country"/>
>     </in>
>   </xsl:template>
> 
> <xsl:template match="in/country">
>     <xsl:variable name="input" select="."/>
>      <xsl:for-each select="document('c:/gaston/lookup.xml')">
>         <xsl:for-each select="key('kLookup',name($input))">
>             <xsl:value-of select="."/>
>         </xsl:for-each>
>      </xsl:for-each>
> </xsl:template>
> </xsl:stylesheet>
> 
> 
> If someone could help me with that, I'm totaly lost !!
> 
> Thanks, Gaston.

Hi Gaston,

It is not enough to have working code -- one needs to understand it.

Why isn't your stylesheet producing the desired results?

1. You must understand how xsl:key and the key() function work. 
   in case you want to transform an abbreviation to "name" (which is exactly your 
   case), then the way you define the key:

>   <xsl:key name="kLookup" match="abbr" use="name"/>

   this performs exactly the opposite transformation. Also, you failed to notice 
   that in your lookup file "abbr" and "name" are not parent/child but just
siblings.

   The correct definition of the key will be:

  <xsl:key name="kLookup" match="name" use="../abbr"/>

2. Using name($input) in
>         <xsl:for-each select="key('kLookup',name($input))">
   in a template matching "country" will always return "country" -- and of course
   this is not what you need. 

   The correct code will read:

   <xsl:for-each select="key('kLookup',$input)">


Putting all together, here's the stylesheet that will produced the required output
when applied on your xml source file:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" omit-xml-declaration="yes"/>
  <xsl:key name="kLookup" match="name" use="../abbr"/>

  <xsl:template match="/">
    <out>
      <xsl:apply-templates select="in/country"/>
      <xsl:text>&#xA;</xsl:text>
    </out>
  </xsl:template>

<xsl:template match="country">
    <xsl:variable name="input" select="."/>
     <xsl:for-each select="document('lookup2.xml')">
        <xsl:for-each select="key('kLookup',$input)">
            <xsl:text>&#xA;</xsl:text>
            <xsl:value-of select="."/>
        </xsl:for-each>
     </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Hope this helped.

Cheers,
Dimitre Novatchev.








__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.