Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 3rd, 2006, 04:19 PM
Registered User
 
Join Date: Mar 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Summing values

I'm still learning. How can I sum a set of numbers? Part of my xsl follows (I am building a table of items and dollar amounts and then want a total at the bottom:
<xsl:template match="File/Detail">

<tr>
<td align="center">
Billing<br/>Provider
</td>
<td align="center">
Total Claim<br/>Count
</td>
<td align="center">
Total Billed<br/>Amount
</td>
<td align="center">
Total Allowed<br/>Amount
</td>
<td align="center">
Total Denied<br/>Amount
</td>
<td align="center">
Total Paid<br/>Amount
</td>
</tr>



<xsl:for-each select="BillingProvider">
<tr>
<td>
  <xsl:value-of select="TS3/TS301"></xsl:value-of>
</td>
<td align="right">
  <xsl:value-of select="TS3/TS304"></xsl:value-of>
</td>
<td align="right">
  <xsl:text>$</xsl:text>
  <xsl:value-of select="format-number(TS3/TS305,'#,###.00')"></xsl:value-of>
</td>
<td align="right">
  <xsl:text>$</xsl:text>
  <xsl:value-of select="format-number(TS3/TS306,'#,###.00')"></xsl:value-of>
</td>
<td align="right">
  <xsl:text>$</xsl:text>
  <xsl:value-of select="format-number(TS3/TS308,'#,###.00')"></xsl:value-of>
</td>
<td align="right">
  <xsl:text>$</xsl:text>
  <xsl:value-of select="format-number(TS3/TS309,'#,###.00')"></xsl:value-of>
</td>
</tr>
</xsl:for-each>





<tr>
<td colspan="6">
  <xsl:for-each select="BillingProvider">
    <xsl:value-of select="sum(TS3/TS309)"/>
  </xsl:for-each>
</td>
</tr>

</xsl:template>

I have searched this forum and so far cannot find an example for this. Other sum examples I found elsewhere were overly complicated and I failed to grasp what was going on. What is the best way to sum up a list on numbers found in specific nodes? Note that "BillingProvider" is a child element found in several places in the overall xml file. And as I said, I can pull each of those values in fine.

P.S. Thanks Michael for your prior help and I have now your book on order!
 
Old March 3rd, 2006, 05:08 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You haven't shown your source data, which make it difficult to help.

If the numbers are contained as element or attribute values, you can simply do sum(NODESET) where NODESET is an expression that selects the relevant nodes.

If you need to compute the numbers (which includes something as simple as stripping a "$" sign off), it's a bit more complicated. Let's say that EXP($n) is the expression that computes the number for a given node $n.

In XPath 2.0 you can simply do

sum(for $n in NODESET return EXP($n))

In 1.0 you have a choice:

(a) a two pass solution where the first pass creates a tree in which the numbers appear as element or attribute values

(b) a recursive named template

(c) a library routine from EXSLT or FXSL

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 3rd, 2006, 05:54 PM
Registered User
 
Join Date: Mar 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry for not including the source. Here is part of it, reduced (big file). I am working with xpath 1.0.

<Detail>
<BillingProvider>
    <LX>
        <LX01>1</LX01>
    </LX>
    <TS3>
        <TS301>105590000</TS301>
        <TS302>11</TS302>
        <TS303>20041231</TS303>
        <TS304>933</TS304>
        <TS305>162166.96</TS305>
        <TS306>146255.44</TS306>
        <TS307>-82178.49</TS307>
        <TS308>15911.52</TS308>
        <TS309>146255.44</TS309>
    </TS3>
    <ClaimLineItem>
        <CLP>
            <CLP01>21830/TEN002</CLP01>
            <CLP02>1</CLP02>
            <CLP03>119.84</CLP03>
            <CLP04>119.84</CLP04>
            <CLP05/>
            <CLP06>MC</CLP06>
            <CLP07>992006039047695001</CLP07>
            <CLP08>12</CLP08>
            <CLP09>3</CLP09>
        </CLP>
        <DTM>
            <DTM01>232</DTM01>
            <DTM02>20060116</DTM02>
        </DTM>
        <DTM>
            <DTM01>233</DTM01>
            <DTM02>20060119</DTM02>
        </DTM>
        <SVC>
            <SVC01>HC:XX</SVC01>
            <SVC02>119.84</SVC02>
            <SVC03>119.84</SVC03>
            <SVC04/>
            <SVC05>16</SVC05>
        </SVC>
        <DTM>
            <DTM01>150</DTM01>
            <DTM02>20060116</DTM02>
        </DTM>
        <DTM>
            <DTM01>151</DTM01>
            <DTM02>20060119</DTM02>
        </DTM>
    </ClaimLineItem>
    <ClaimLineItem>
        <CLP>
    ...etc many ClaimLineItems

    then....

    </ClaimLineItem>
</BillingProvider>
<BillingProvider>
    <LX>
        <LX01>2</LX01>
    </LX>
    <TS3>
        <TS301>105590200</TS301>
        <TS302>11</TS302>
        <TS303>20041231</TS303>
        <TS304>361</TS304>
        <TS305>87696.11</TS305>
        <TS306>78049.33</TS306>
        <TS307>-81341.63</TS307>
        <TS308>8809.92</TS308>
        <TS309>78049.33</TS309>
    </TS3>
    <ClaimLineItem>
            <CLP>
            <CLP01>XXX</CLP01>
            <CLP02>1</CLP02>
            <CLP03>39</CLP03>
            <CLP04>39</CLP04>
            <CLP05/>
            <CLP06>MC</CLP06>
            <CLP07>992006039038540001</CLP07>
            <CLP08>12</CLP08>
            <CLP09>1</CLP09>
        </CLP>
            ...etc

Hope this part of the source helps.
 
Old March 3rd, 2006, 06:47 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This code:

 <xsl:for-each select="BillingProvider">
    <xsl:value-of select="sum(TS3/TS309)"/>
  </xsl:for-each>

looks fine if you want one total for each BillingProvider (though the totals will all be squidged up against each other).

If you want a total across all the BillingProviders, just do

<xsl:value-of select="sum(BillingProvider/TS3/TS309)"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 4th, 2006, 12:13 PM
Registered User
 
Join Date: Mar 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wow. Too simple. Works perfectly! Thanks, Michael!





Similar Threads
Thread Thread Starter Forum Replies Last Post
summing column values hydriswall PHP Databases 1 December 8th, 2006 04:54 AM
Help with summing calculated fields on a report dbartelt Access 6 January 4th, 2006 03:33 PM
SQL vs CR summing daniel SQL Server 2000 5 October 3rd, 2005 04:59 PM
Summing up in a loop Tschuri XSLT 0 May 3rd, 2005 07:16 AM
Summing and Exporting imhotep666 Excel VBA 3 August 18th, 2004 11:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.