Solved

XSL Column Totals

Posted on 2004-10-20
7
469 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XSLT list item selection criteria not working 12 43
C# SQL BULK INSERT CLASS 5 50
.net and XML report from SCCM 5 80
XML extra information 8 47
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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