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 July 22nd, 2003, 02:09 AM
Authorized User
 
Join Date: Jul 2003
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...


 
Old July 22nd, 2003, 10:06 AM
Friend of Wrox
 
Join Date: Jun 2003
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





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





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