Merging tables with XSLT

Discussion in 'XML' started by Andrew McFarland, May 29, 2009.

  1. I've got two tables that I want to merge with XSLT:

    <data>

    <table>
    <row>
    <entry>a</entry>
    <entry>b</entry>
    </row>
    <row>
    <entry>c</entry>
    <entry>d</entry>
    </row>
    </table>

    <table>
    <row>
    <entry>c</entry>
    <entry>c content</entry>
    </row>
    <row>
    <entry>a</entry>
    <entry>a content</entry>
    </row>
    </table>

    </data>

    I want to merge on the first entry in each row, so the final data
    looks like this:

    <data>

    <table>
    <row>
    <entry>a</entry>
    <entry>b</entry>
    <entry>a content</entry>
    </row>
    <row>
    <entry>c</entry>
    <entry>d</entry>
    <entry>c content</entry>
    </row>
    </table>

    </data>

    Whitespace is unimportant.

    I can do this with the following XSLT:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
    Transform">

    <xsl:template match="table[position() &gt; 1]"/>

    <xsl:template match="row">
    <xsl:variable name="current_row" select="entry[1]"/>
    <xsl:copy>
    <xsl:apply-templates/>
    <xsl:for-each select="/data/table[2]/row">
    <xsl:if test="./entry[1] = $current_row">
    <xsl:copy-of select="./entry[2]"/>
    </xsl:if>
    </xsl:for-each>
    </xsl:copy>
    </xsl:template>

    <!-- identity transform -->
    <xsl:template match="@*|node()">
    <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>

    Although this works, I'm not convinced that it is the most efficient
    way of doing things. It would be better (well, it would feel better)
    if I looked up the values for the third column of the output using a
    hash of some kind, rather than looping over all the rows. Is this
    possible in XSLT?
     
    Andrew McFarland, May 29, 2009
    #1
    1. Advertising

  2. Its done using keys.

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
    Transform">


    <xsl:key name="rows" match="/data/table[2]/row" use="entry[1]"/>

    <xsl:template match="table[position() &gt; 1]"/>

    <xsl:template match="row">
    <xsl:copy>
    <xsl:apply-templates/>
    <xsl:copy-of select="key('rows',entry[1])/entry[2]"/>
    </xsl:copy>
    </xsl:template>

    <!-- identity transform -->
    <xsl:template match="@*|node()">
    <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>

    Yes, I am replying to my own post. As soon as I'd posted I'd worked
    out how to do it. Usenet is my rubber duck today.
     
    Andrew McFarland, May 29, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9obg==?=

    merging tables

    =?Utf-8?B?Sm9obg==?=, May 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    451
    Mike Bridge
    May 17, 2004
  2. Dylan Parry

    Html-tables-merging rows in a column

    Dylan Parry, Nov 25, 2005, in forum: HTML
    Replies:
    4
    Views:
    600
    Sid Ismail
    Nov 26, 2005
  3. Yannick Patois

    Merging two XML tree: xslt or dom ?

    Yannick Patois, Jul 20, 2004, in forum: XML
    Replies:
    1
    Views:
    922
    Uche Ogbuji
    Jul 23, 2004
  4. Martin Trautmann

    XML tools for merging tables

    Martin Trautmann, May 17, 2005, in forum: XML
    Replies:
    2
    Views:
    441
    Martin Trautmann
    May 17, 2005
  5. Replies:
    4
    Views:
    500
    Martin Honnen
    Nov 1, 2005
Loading...

Share This Page