Solved

XSL Column Totals

Posted on 2004-10-20
7
466 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Access XML API HTTP POST Call Object Send Not Working Correctly 15 78
read an xml file in perl 2 50
Shredding xml into an oracle 11g Database 2 59
tutorial for ebay api 3 56
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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