[xsl] Re: xsl:transform

Subject: [xsl] Re: xsl:transform
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Fri, 22 Jun 2001 22:11:36 -0700 (PDT)
Benoit_Aumars@xxxxxxxxxxxx wrote:
>I would like to transform from one xml into another one
>using a 'lookup'  xml file.
>Here is the original xml file :
><?xml version="1.0"?>
>    <col1>01</col1>
>    <col2>Hello</col2>
>    <col3>world</col3>
>    <col4>GTP</col4>
>    <col5>02 Jul 1999 09:45:05:706</col5>
>using this 'lookup' xml file :
><?xml version="1.0"?>
>    <file1>
>	<abbr>col1</abbr><name>label1</name>
>	<abbr>col2</abbr><name>label2</name>
>	<abbr>col3</abbr><name>label3</name>
>	<abbr>col4</abbr><name>label4</name>
>	<abbr>col5</abbr><name>label5</name>
>    </file1>
>    <file2>
>	<abbr>col1</abbr><name>header1</name>
>	<abbr>col2</abbr><name>header2</name>
>    </file2>
>the xml result is :
><?xml version="1.0"?>
>    <label1>01</label1>
>    <label2>Hello</label2>
>    <label3>world</label3>
>    <label4>GTP</label4>
>    <label5>02 Jul 1999 09:45:05:706</label5>
>If the original xml file :
><?xml version="1.0"?>
>    <col1>12345</col1>
>    <col2>Welcome</col2>
>using the same 'lookup' xml file, the result is :
><?xml version="1.0"?>
>    <header1>12345</header1>
>    <header2>Welcome</header2>
>Any idea how to ?

Hi Ben,

Use the same solution as I posted yesterday 

You've changed a little the problem -- the input arguments are elements -- not
attributes as before. More importantly, your lookup file now has different sections
and you specify (the name of the top element of the input xml file) the name of the
section that must be used in the lookup.

Therefore, the previous solution is changed accordingly:

<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:variable name="lookupSection" select="name(/*)"/>
  <xsl:template match="/">
    <xsl:element name="{name(*)}">
      <xsl:apply-templates select="/*/*"/>

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

This, as the previous solution, has been tested and verified to work as expected.

Dimitre Novatchev.

Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail

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

Current Thread