Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Merging tables with XSLT

Reply
Thread Tools

Merging tables with XSLT

 
 
Andrew McFarland
Guest
Posts: n/a
 
      05-29-2009
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" xmlnssl="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?
 
Reply With Quote
 
 
 
 
Andrew McFarland
Guest
Posts: n/a
 
      05-29-2009
Its done using keys.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlnssl="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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Html-tables-merging rows in a column Dylan Parry HTML 4 11-26-2005 02:01 PM
xslt merging question rottyguy70@yahoo.com XML 4 11-01-2005 05:14 PM
XML tools for merging tables Martin Trautmann XML 2 05-17-2005 12:45 PM
Merging two XML tree: xslt or dom ? Yannick Patois XML 1 07-23-2004 07:17 PM
merging tables =?Utf-8?B?Sm9obg==?= ASP .Net 1 05-17-2004 07:37 PM



Advertisments