Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT: Calculating Across Columns of Table

Reply
Thread Tools

XSLT: Calculating Across Columns of Table

 
 
Newbie
Guest
Posts: n/a
 
      08-20-2006
Suppose that I have an HTML table of numbers, and I need to replace the value of each cell with
itself divided by the total for that column. The best I can think of is as follows:

<xsl:for-each select="tr">
<xsl:copy>
<xsl:for-each select="td">
<xsl:variable name="col" select="position()" />
<xsl:variable name="total" select="sum(../../tr/td[position()=$col])" />
<xsl:copy>
<xsl:value-of select=". div $total" />
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>

This is rather verbose (the temporary col variable), and has to be recalculated for each row. Is
there a more concise/efficient way of doing this?

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Soren Kuula
Guest
Posts: n/a
 
      08-20-2006
Newbie wrote:
> Suppose that I have an HTML table of numbers, and I need to replace the
> value of each cell with itself divided by the total for that column. The
> best I can think of is as follows:


Better make that XHTML

The best I can think of is using keys: All 1'st td's will be mapped
under 1 in the k key, all second under 2, etc.... The key is then looked
up (using position()) where needed. Column node selection is thus done,
but the sum is still recomputed.


<xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform"
version='1.0'>

<xsl:key name="k" match="td" use="1 + count(preceding-sibling::td)"/>

<xsl:template match="tr">
<tr>
<xsl:apply-templates select="td"/>
</tr>
</xsl:template>

<xsl:template match="td">
<td>
<xsl:value-of select=". div sum(key('k', position()))"/>
</td>
</xsl:template>

</xsl:stylesheet>



Sample input:
<html>
<body>
<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
</body>
</html>


Sample output:

<tr><td>0.25</td><td>0.33333333333333</td></tr>
<tr><td>0.75</td><td>0.66666666666667</td></tr>

(oops that's not even XML --- well a / mathcing template could wrap over
it what it is missing )


Søren
 
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
Flowing text across columns James Hutton HTML 13 05-09-2007 04:57 AM
Align table columns with gridview columns Jacksm ASP .Net 2 11-21-2006 09:49 PM
Format datagrid across several columns Andy ASP .Net 0 05-08-2006 02:18 PM
Validation across template columns fdisk ASP .Net Datagrid Control 0 06-14-2004 04:59 PM
calculating in table made by createElement and appendChild kie Javascript 25 10-07-2003 11:38 AM



Advertisments