Solved

XSL Column Totals

Posted on 2004-10-20
7
457 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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. 
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now