Re: [xsl] Re:Re: Joining two XML-files

Subject: Re: [xsl] Re:Re: Joining two XML-files
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 23 Oct 2001 12:13:34 +0100
Hi Sun-fu,

> I try to work out suggestions for joining two XML-files to Jorge,
>> These files are related through the commom value of an attribute.
>> In the previous example file1.att1 is found in file2.fld1 I need to
>> produce a third file where get mixed some attributes of the related
>> "records".For example:
>>
>> file3.xml
>>     <row att1='foo' att2='morefoo" fld2='otherfoo" />
>>     <row att1='foo2' att2='morefoo2" fld2='otherfoo2" />
>>     <row att1='foo2' att2='morefoo2" fld2='anotherfoo2" />
>> ...
>> </data>
>
> However, I got files3 with two rows , do I misinterpret your
> suggestions?

Ah, no, I'd missed the fact that file2.xml had two row elements with
the same value for fld1. Here's a better solution:

  <!-- iterate over all the rows from file1 -->
  <xsl:for-each select="$file1/data/row">
    <xsl:variable name="row-from-file1" select="." />
    <!-- change the context to file2 -->
    <xsl:for-each select="$file2">
      <!-- iterate over all the rows in file2 whose fld1 attribute is
           the same as the current row's att1 attribute -->
      <xsl:for-each select="key('rows-by-fld1,
                                $row-from-file1/@att1)">
        <!-- create a row element -->
        <row>
          <!-- copy the attributes from the row from file1 -->
          <xsl:copy-of select="$row-from-file1/@*" />
          <!-- copy the attributes from this row from file2 -->
          <xsl:copy-of select="@*[name() != 'fld1']" />
        </row>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:for-each>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread