Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 22nd, 2003, 02:09 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need Help with the Sum Function

Hello i am really new with XLST i was just wondering on how do you go about adding values from the following.

<Rank>
    <Position>
        <UserID>16</UserID>
                <WeekID>1</WeekID>
        <NickName>Eel</NickName>
        <Points>3</Points>
    </Position>
</Rank>
<Rank>
    <Position>
        <UserID>16</UserID>
                <WeekID>2</WeekID>
        <NickName>Eel</NickName>
        <Points>2</Points>
    </Position>
</Rank>
<Rank>
    <Position>
        <UserID>17</UserID>
                <WeekID>1</WeekID>
        <NickName>Pepper</NickName>
        <Points>2</Points>
    </Position>
</Rank>
<Rank>
    <Position>
        <UserID>17</UserID>
                <WeekID>2</WeekID>
        <NickName>Pepper</NickName>
        <Points>0</Points>
    </Position>
</Rank>

What i want to do is to add the points for each seperate user week by week and show them as a total.

This is week 2 and Pepper has a total of X points ..
This is week 2 and Eel has a total of Y Points ..

Hope this makes sence..

Thanks to anyone that can help me...


Reply With Quote
  #2 (permalink)  
Old July 22nd, 2003, 10:06 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Hi,

It's a grouping problem. Probably the code below needs some optimization and
simplification... However, I tried to simplify the code as much as possible.
This approach with keys will be better(efficient) if the source XML will be bigger.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="weeks" match="Rank" use="Position/WeekID"/>
    <xsl:key name="users" match="Rank" use="Position/UserID"/>

    <xsl:template match="/">
        <html>
            <body>
                <table border="1">
                    <xsl:for-each select="/Ranks/Rank/Position/WeekID[generate-id(ancestor::Rank) = generate-id(key('weeks', .)[1])]">
                        <xsl:sort select="." data-type="number"/>
                        <xsl:variable name="current-week-id" select="."/>
                        <tr>
                            <td>Week <xsl:value-of select="."/>:</td>
                            <td>
                                <xsl:for-each select="/Ranks/Rank/Position/UserID[generate-id(ancestor::Rank) = generate-id(key('users', .)[1])]">
                                    <xsl:sort select="following-sibling::NickName" data-type="text"/>
                                    <xsl:sort select="." data-type="number"/>
                                    <!-- the following "xsl:if" element can be stripped out(keeping its content of course), 
                                    but in that case persons which are only, say, in weeks 1 through 2, 
                                    will be also listed for weeks greater than 2 with points 0 (or generally, for weeks in which they have no entry) -->   
                                    <xsl:if test="/Ranks/Rank/Position[WeekID = $current-week-id and UserID = current()]">
                                        <xsl:value-of select="concat(following-sibling::NickName, '[', current(), ']')"/> has a total of <xsl:value-of select="sum(/Ranks/Rank/Position/Points[preceding-sibling::WeekID = $current-week-id and preceding-sibling::UserID = current()])"/> points
                                        <br/>
                                    </xsl:if>    
                                </xsl:for-each>
                            </td>
                        </tr> 
                    </xsl:for-each>    
                </table> 
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
Regards,
Armen
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sum function pcase XSLT 2 January 2nd, 2008 05:51 PM
sum() function felixm_jr Reporting Services 1 April 22nd, 2007 01:59 AM
Xpath: sum function gracehanh XSLT 13 September 27th, 2005 09:30 AM
sum function problem sherr8 Access 1 February 13th, 2004 03:06 PM
SUM Function jmss66 Classic ASP Basics 17 July 29th, 2003 08:00 AM



All times are GMT -4. The time now is 06:05 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.