473,378 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

XSLT: Calculating Across Columns of Table

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

Aug 20 '06 #1
1 1296
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 xmlns:xsl="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
Aug 20 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Doug Heeren | last post by:
I have the following section of VB.NET code that transforms a simple dataset into an Excel xml workbook. It works fine for < 50 rows or so, but I have about 8,000 rows I need to transform. Is there...
3
by: Andy | last post by:
Hi all, I'm having a problem doing an Xslt transform in code. I've done it before, so I'm not really sure why its not working. The problem is that the result of the transform is an empty...
1
by: Tony Williams | last post by:
I have a table with two fields, txtvalue (a number field) and txtmonth ( a date/time field). I want to create a report that shows the difference in value between the value in txtvalue in one value...
4
by: coleenholley | last post by:
Ok, after all the research on datagrids, I've found that they won't really work for what I'm trying to do...I have a table that I need to have three columns as static data (but each row has a...
4
by: infiniti | last post by:
Hi, I am coming across problems in trying to EFFICIENTLY merge to XML files into one which involves transposing the rows into columns so that I can either generate a single flat xml file or store...
3
by: Ian Roddis | last post by:
Hello, I want to embed SQL type queries within an XML data record. The XML looks something like this: <DISPLAYPAGE> <FIELD NAME="SERVER" TYPE="DROPDOWN"> <OPTION>1<OPTION> <OPTION>2<OPTION>...
3
by: brianc | last post by:
I have a table that holds the employee id, a job name and then 5 columns (Proposal Phase Rating, Preconstruction Phase Rating, etc.) that hold a number rating 1-5. I've read that this design may...
2
by: Scamjunk | last post by:
I have been desperately looking for a treeview-type solution for my problem for the past three weeks and have been greatly unsuccessful. I am totally new to the world of XSLT and I *don't know*...
3
by: realmerl | last post by:
Hi All. I'm trying to transform a html document into plain text via xslt. Simple you say! (i hope) I have got it working, by using the magnificent <xsl:value-of select="."/>. This returns the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.