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 August 4th, 2003, 11:28 PM
Authorized User
 
Join Date: Jul 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help Sorting

hi i have a problem where i want to sort the total of the sum as in the points allocated to each user from highest to lowest and then once i have done that is it possible sorting them by alphabetical order ...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href="e2template.xsl" />

<xsl:key name="nickname" match="Rank" use="Position/NickName"/>
<xsl:key name="weeks" match="Rank" use="Position/WeekID"/>
<xsl:key name="users" match="Rank" use="Position/UserID"/>

<xsl:template match="Page">

    <table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr><td height="53"></td></tr>
    <xsl:if test="//Page/SectionName = 'Members'">
    <tr><td background="/images/Header_stretch.gif"><img alt="" width="378" height="17"><xsl:attribute name="src">images/<xsl:value-of select="//Page/SectionName" />_Header.gif</xsl:attribute></img></td></tr>
    </xsl:if>
    <tr><td height="20"></td></tr>
    </table>

    <table border="1" cellpadding="2" cellspacing="0" width="250">
    <tr>
        <td>Username</td>
        <td>Points</td>

    </tr>
    <xsl:for-each select="//Common/Ranks/Rank/Position/NickName[generate-id(ancestor::Rank) = generate-id(key('nickname', .)[1])]">
    <xsl:variable name="current-week-id" select="."/>
    <tr>
        <td><xsl:value-of select="."/></td>
        <td>
        <xsl:for-each select="//Common/Ranks/Rank/Position/UserID[generate-id(ancestor::Rank) = generate-id(key('users', .)[1])]">

            <xsl:if test="//Common/Ranks/Rank/Position[NickName = $current-week-id and UserID = current()]">
                <xsl:value-of select="sum(//Common/Ranks/Rank/Position/Points[preceding-sibling::NickName = $current-week-id and preceding-sibling::UserID = current()])"/> points
            </xsl:if>

        </xsl:for-each>
        </td>
    </tr>
    </xsl:for-each>
    </table>

</xsl:template>

</xsl:stylesheet>

So i want my end result to be

user 1 5 points
user 2 3 points
user 3 3 points

etc ...

If anyone can help me out it would be much appreciated ..


 
Old August 7th, 2003, 01:35 PM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default

Maybe you should post an example of your XML document... It'll be easier to understand.

Help yourself so we can help you ;o)

Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.
 
Old August 8th, 2003, 03:13 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Without seeing source document it's difficult but it looks like you need "Muenchian Grouping", serach for this on Google, there are examples at:
http://www.jenitennison.com/


--

Joe
 
Old August 10th, 2003, 07:50 PM
Authorized User
 
Join Date: Jul 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default


This is the XML

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

all i want to do is for each new week
show who is in the lead by adding up the points for each user

user 1 has 5 points
user 3 has 3 points
user 2 has 1 point

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href="e2template.xsl" />

<xsl:key name="nickname" match="Rank" use="Position/NickName"/>
<xsl:key name="weeks" match="Rank" use="Position/WeekID"/>
<xsl:key name="users" match="Rank" use="Position/UserID"/>

<xsl:template match="Page">

    <table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr><td height="53"></td></tr>
    <xsl:if test="//Page/SectionName = 'Members'">
    <tr><td background="/images/Header_stretch.gif"><img alt="" width="378" height="17"><xsl:attribute name="src">images/<xsl:value-of select="//Page/SectionName" />_Header.gif</xsl:attribute></img></td></tr>
    </xsl:if>
    <tr><td height="20"></td></tr>
    </table>

    <table border="1" cellpadding="2" cellspacing="0" width="250">
    <tr>
        <td>Username</td>
        <td>Points</td>

    </tr>
    <xsl:for-each select="//Common/Ranks/Rank/Position/NickName[generate-id(ancestor::Rank) = generate-id(key('nickname', .)[1])]">
    <xsl:variable name="current-week-id" select="."/>
    <tr>
        <td><xsl:value-of select="."/></td>
        <td>
        <xsl:for-each select="//Common/Ranks/Rank/Position/UserID[generate-id(ancestor::Rank) = generate-id(key('users', .)[1])]">

            <xsl:if test="//Common/Ranks/Rank/Position[NickName = $current-week-id and UserID = current()]">
                <xsl:value-of select="sum(//Common/Ranks/Rank/Position/Points[preceding-sibling::NickName = $current-week-id and preceding-sibling::UserID = current()])"/> points
            </xsl:if>

        </xsl:for-each>
        </td>
    </tr>
    </xsl:for-each>
    </table>

</xsl:template>

</xsl:stylesheet>

thanks for you help.


 
Old August 11th, 2003, 06:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

this stylesheet will give you the output you specified. You can add back in all the html to make your page pretty...
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:key name="users" match="Position" use="UserID"/>
  <xsl:template match="/">
    <html>
      <body>

        <xsl:apply-templates select="Common/Rank/Position[generate-id()=generate-id(key('users', UserID)[1])]">
          <xsl:sort select="sum(key('users', UserID)/Points)" data-type="number" order="descending"/>
          <xsl:sort select="key('users', UserID)" data-type="text" order="ascending"/>
        </xsl:apply-templates>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="Position">

    User <xsl:value-of select="UserID"/> has <xsl:value-of select="sum(key('users', UserID)/Points)"/> points<br/>
  </xsl:template>
</xsl:stylesheet>
hth
Phil
 
Old August 11th, 2003, 07:51 PM
Authorized User
 
Join Date: Jul 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help

:D






Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting member XSLT 25 July 13th, 2007 08:44 AM
SORTING pallone XSLT 3 October 29th, 2006 08:45 AM
Sorting sunny76 Excel VBA 2 September 19th, 2005 09:31 PM
Datagrid sorting by non alphabetical sorting? LLAndy VS.NET 2002/2003 1 July 15th, 2004 01:20 AM
Sorting? pbernardo XSLT 2 October 27th, 2003 11:34 AM





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