Solved

XSL Column Totals

Posted on 2004-10-20
7
471 Views
Last Modified: 2008-02-01
I am writing a reporting procedure for a web based adminstration system database data is returned as xml shown below - i wish to add totals to the bottom of each column. i want this to work for any recordset that i give it so column headings are unknown in advance.

i can use

<xsl:variable name="total" select="concat('/xml/row/',name())" /><xsl:variable name="current" select="concat('/xml/row/',name())" />

to get the full path to each column  as a string

however

<xsl:value-of select="sum($total)" />

gives me an error (expression does not return a dom node)

putting the value in directly as below does work

<xsl:value-of select="sum(/xml/row/MortgageRequired)" )/>


any suggestions?

<xml>
<row>
<submitted sorttype="135">18/08/04 12:22:57</submitted>
<IntroducerEmail sorttype="200">david@emailaddress.co.uk</IntroducerEmail>
<PurchasePrice sorttype="6">100000</PurchasePrice>
<MortgageRequired sorttype="6">90000</MortgageRequired>
<LTV sorttype="131">90</LTV>
</row>
<row>
<submitted sorttype="135">18/08/04 12:22:57</submitted>
<IntroducerEmail sorttype="200">test@emailaddress.co.uk</IntroducerEmail>
<PurchasePrice sorttype="6">175000</PurchasePrice>
<MortgageRequired sorttype="6">100000</MortgageRequired>
<LTV sorttype="131">57</LTV>
</row>
</xml>



0
Comment
Question by:steve_steele
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 12357430
<xsl:for-each select="xml/row">
<TR>
<TD>
   <xsl:value-of select="sum(MortgageRequired)" )/>
</TD>
</TR>
</xsl:for-each>
0
 
LVL 35

Expert Comment

by:YZlat
ID: 12357557
<?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>
<TR>
                  <TD width="35%">Date Submitted</TD>
            <TD width="20%">E-Mail</TD>
            <TD>total1</TD>
            <TD width="20%">total2</TD>
            <TD>LTV</TD>
         </TR>
 <xsl:for-each select="xml/row">
         <TR STYLE="font-family:Verdana; font-size:8pt;color:darkred; padding:0px 2px">
                  <TD>
                        <xsl:value-of select="submitted" />
                  </TD>
                  <TD>
                        <a href="mailto:{link}"><xsl:value-of select="IntroducerEmail" /></a>
                  </TD>
                  <TD>
                        <xsl:value-of select="PurchasePrice" />
                  </TD>
                  <TD>
                        <xsl:value-of select="MortgageRequired" />
                  </TD>  
                  <TD>
                        <xsl:value-of select="LTV" />
                  </TD>      
            </TR>
            </xsl:for-each>
<TR>
<TD></TD>
<TD></TD>
<TD></TD>
<TD><xsl:value-of select="sum(MortgageRequired)"/></TD>
<TD></TD>
</TR>
 </TABLE>
</xsl:template>
</xsl:stylesheet>
0
 

Author Comment

by:steve_steele
ID: 12357626
Cant do either of those as i'm trying to get it it work without knowing the column headings in advance

ie the same xsl would still work for

<xml>
<row>
<somthingelse sorttype="135">18/08/04 12:22:57</somthingelse >
<wovvit sorttype="200">david@emailaddress.co.uk</wovvit >
<gagglepin sorttype="6">100000</gagglepin >
</row>
<row>
<somthingelse sorttype="135">18/08/04 12:22:57</somthingelse >
<wovvit sorttype="200">test@emailaddress.co.uk</wovvit >
<gagglepin sorttype="6">4545454</gagglepin >
</row>
</xml>
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 7

Expert Comment

by:J_Mak
ID: 12357903
Try this variable:

<xsl:variable name="total" select="//row/node()"/>

The problem with yours is that the path /xml/row/name() is that name() is not a node.... it is a string. That's why it complains about no dom node being returned. Try my solution. It hasn't yet been tested but it should give the current node, that is the context node and all others in the following <row> elements. Cheers.

Btw, if that doesn't work, try:

<xsl:variable name="total" select="//row/self::node()"/>
0
 
LVL 7

Expert Comment

by:J_Mak
ID: 12357943
Or even try this:

<xsl:variable name="total" select="//row/."/>

I'm not sure if that'll work. It may need some polishiing up.
0
 
LVL 26

Accepted Solution

by:
rdcpro earned 500 total points
ID: 12362515
Well, this "works" but of course It assumes sorttype=6 means you want the column summed.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:template match="xml">
            <table cellpadding="4px">
                  <tr>
                        <th><xsl:text> </xsl:text></th>
                        <xsl:for-each select="row[1]/*">
                              <th><xsl:value-of select="local-name()"/></th>
                        </xsl:for-each>
                  </tr>
                  <xsl:for-each select="row">
                        <tr>
                              <th><xsl:text> </xsl:text></th>
                              <xsl:for-each select="*">
                                    <td align="center"><xsl:value-of select="."/></td>
                              </xsl:for-each>
                        </tr>
                  </xsl:for-each>
                  <tr>
                        <th>Total:</th>
                        <xsl:for-each select="row[1]/*">
                              <xsl:variable name="vPosition" select="position()"/>
                              <th>
                                    <xsl:if test="@sorttype = 6">
                                          <xsl:value-of select="sum(/xml/row/*[$vPosition])"/>
                                    </xsl:if>
                              </th>
                        </xsl:for-each>
                  </tr>
            
            </table>
      </xsl:template>
</xsl:stylesheet>


Regards,
Mike Sharp
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 12362520
BTW:  the output of the above XSLT is:

<table cellpadding="4px">
      <tr>
            <th> </th>
            <th>submitted</th>
            <th>IntroducerEmail</th>
            <th>PurchasePrice</th>
            <th>MortgageRequired</th>
            <th>LTV</th>
      </tr>
      <tr>
            <th> </th>
            <td align="center">18/08/04 12:22:57</td>
            <td align="center">david@emailaddress.co.uk</td>
            <td align="center">100000</td>
            <td align="center">90000</td>
            <td align="center">90</td>
      </tr>
      <tr>
            <th> </th>
            <td align="center">18/08/04 12:22:57</td>
            <td align="center">test@emailaddress.co.uk</td>
            <td align="center">175000</td>
            <td align="center">100000</td>
            <td align="center">57</td>
      </tr>
      <tr>
            <th>Total:</th>
            <th/>
            <th/>
            <th>275000</th>
            <th>190000</th>
            <th/>
      </tr>
</table>

Regards,
Mike Sharp
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question