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: xsl:transform


Benoit_Aumars@jltgroup.com 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"?>
><file1>
>    <col1>01</col1>
>    <col2>Hello</col2>
>    <col3>world</col3>
>    <col4>GTP</col4>
>    <col5>02 Jul 1999 09:45:05:706</col5>
></file1>
>
>using this 'lookup' xml file :
><?xml version="1.0"?>
><xref>
>    <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>
></xref>
>
>
>the xml result is :
><?xml version="1.0"?>
><file1>
>    <label1>01</label1>
>    <label2>Hello</label2>
>    <label3>world</label3>
>    <label4>GTP</label4>
>    <label5>02 Jul 1999 09:45:05:706</label5>
></file1>
>
>If the original xml file :
>
><?xml version="1.0"?>
><file2>
>    <col1>12345</col1>
>    <col2>Welcome</col2>
></file2>
>
>using the same 'lookup' xml file, the result is :
><?xml version="1.0"?>
><file2>
>    <header1>12345</header1>
>    <header2>Welcome</header2>
></file2>
>
>Any idea how to ?


Hi Ben,

Use the same solution as I posted yesterday 
(http://sources.redhat.com/ml/xsl-list/2001-06/msg01004.html)

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:element>
  </xsl:template>

  <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"/>
          </xsl:element>
        </xsl:for-each>
     </xsl:for-each>
  </xsl:template>
</xsl:stylesheet> 

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

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]