This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: HOWTO : Lookup tables


Benoit Aumars wrote:
> 
> I'm looking for a sample code to transform one xml file into another one 
> using a 'lookup tables' xml file.
> Suppose I have this before.xml :
> 
> <?xml version="1.0"?>
> <in>
>     <A code1="01" code2="Hello" />
>     <A code1="02" code2="world" />
> </in>
> 
> and I want to transform it into after.xml :
> <?xml version="1.0"?>
> <in>
>     <Label1>01</Label1>
>     <Label2>Hello</Label2>
>     <Label1>02</Label1>
>     <Label2>world</Label2>
> </in>

> using this 'lookup tables' xml file ( something like this / similar to this
> ) :
> <?xml version="1.0"?>
> <lookup>
>     <abbr>code1</abbr><name>Label1</name>
>     <abbr>code2</abbr><name>Label2</name>
> </lookup>
> 
> Can someone help me out please ?

The following stylesheet produces your desired output when applied on your xml
source:

<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="preceding-sibling::abbr[1]"/>

  <xsl:template match="/">
    <in>
      <xsl:apply-templates select="in/A/@*"/>
    </in>
  </xsl:template>

  <xsl:template match="A/@*">
     <xsl:variable name="input" select="."/>
     <xsl:for-each select="document('lookup.xml')">
        <xsl:for-each select="key('kLookup', name($input))">
          <xsl:element name="{.}">
           <xsl:value-of select="$input"/>
          </xsl:element>
        </xsl:for-each>
     </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]