Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML 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 June 7th, 2005, 12:04 PM
Authorized User
 
Join Date: Dec 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to btado
Default Grouping and totals

Could someone please advise on how to accomplish this task?
XML:
<Promotions>
    <Promotion Code="test" Hits="1">
        <Sale Date="6/5/2005" Amount="114.75" OrderID="31"/>
        <Sale Date="5/5/2005" Amount="60.70" OrderID="29"/>
        <Sale Date="7/5/2004" Amount="500.75" OrderID="28"/>
        <Sale Date="6/1/2005" Amount="110.00" OrderID="30"/>
        <Sale Date="6/1/2004" Amount="40.00" OrderID="32"/>
    </Promotion>
<Promotion Code="test2" Hits="15">
        <Sale Date="4/5/2005" Amount="114.75" OrderID="31"/>
        <Sale Date="1/5/2005" Amount="71.70" OrderID="29"/>
        <Sale Date="7/6/2004" Amount="45.75" OrderID="28"/>
        <Sale Date="3/1/2005" Amount="10.00" OrderID="30"/>
        <Sale Date="10/1/2004" Amount="40.00" OrderID="32"/>
    </Promotion>

</Promotions>

I need to group by month and calculate monthly totals for each code.
Any advice would be greatly appreciated!


 
Old June 8th, 2005, 04:39 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

This site should get you started:
http://www.jenitennison.com/xslt/gro...muenchian.html
You'll have to parse the date string to get the month.


--

Joe (Microsoft MVP - XML)
 
Old June 30th, 2005, 10:29 AM
Authorized User
 
Join Date: May 2005
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, at the moment all I've been able to do is do the grouping bit. Basically, I've taken it as red that the OrderID is incremental which means that the smaller the OrderID the Older the order is and vice versa...I've mentioned that because your last Sale order has the highest number whereas it was done in the year 2004 - I've decided that that was probably a mistake!

So basically I've ordered the list by the OrderID and then grouped them using the year and date!

Unfortunately, as soon as I start trying to accumulate the Amounts I run into Namespace problems! If I alter the NameSpace then the order-by command doesn't work! The NameSpaces are really starting to annoy me!

Anyway, this is how far I've got - You've probably solved it already but that's life - I guess I'm going to start again trying to use the NameSpace I've been using for all my other interesting problems!

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
   <xsl:template match="/">
      <table border="1">
        <xsl:for-each select="/Promotions/Promotion/Sale" order-by="@OrderID">
            <tr>
                <td>
                <xsl:if expr="IsNewGroup(this)">

                     <xsl:eval>
                         SetCurrentGroup(this)
                    </xsl:eval>
                    <xsl:value-of select="@Date"/>

                 </xsl:if>
                 #160;</td>

                <td><xsl:value-of select="@Amount"/>#160;</td>
                 <td><xsl:value-of select="@OrderID"/>#160;</td>
            </tr>
       </xsl:for-each>
        </table>
   </xsl:template>

   <xsl:script language="JavaScript">

        cCurrentGroup = "_empty_" ;

        function SetCurrentGroup(cThis)
        {
            //Get the date value
             cValue = cThis.attributes(0).nodeValue;

            //Initialise the Group with the formatted date
            cCurrentGroup = FormatDate(cValue);
         }

         function FormatDate(cDate)
         {
             //Create an array of the three date values
             aDateTypes = cDate.split("/");

             //Convert the three date values to integers so that we
             //can group by year and month
            cCurrentMonth = parseInt(aDateTypes[1]);
            cCurrentYear = parseInt(aDateTypes[2]);

            //Return the value
            return (cCurrentYear * 100) + cCurrentMonth;
         }

         function IsNewGroup(cThis)
        {
             //Get the date value
             cValue = cThis.attributes(0).nodeValue;

            //Get the Formatted Date
            cTest = FormatDate(cValue);

             //Is the month and year the same as the last test
             if (cTest == cCurrentGroup)
                cReturn = false ;
             else
                 cReturn = true ;

             return cReturn ;
         }

   </xsl:script>
  </xsl:stylesheet>

 
Old June 30th, 2005, 01:15 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's not a question of namespaces. It's a question of which programming language you are using. Your code is written in WD-xsl, a no-longer-supported Microsoft variant of an early working draft of what later became XSLT. There's no earthly reason to write code in that language any more: write in XSLT instead. Apart from anything else, your code is written half in WD-xsl and half in JavaScript, whereas if you used XSLT you could do it all in a single language without any script.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 1st, 2005, 03:08 AM
Authorized User
 
Join Date: May 2005
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

At some point its all going to click together and two and two will make four!
Until then you will probably find that my route to success will be the hardest route going!

Right now, I'm going to go downstairs, get a cup of coffee and then follow my plan as mentioned in my first description)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">









Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with totals rsm42 ASP.NET 1.0 and 1.1 Basics 0 December 15th, 2006 01:18 PM
Protect cells and allow grouping/un-grouping sfreuden Excel VBA 4 December 14th, 2006 08:01 AM
Grouping and totals btado XSLT 3 June 22nd, 2005 01:08 AM
how can i display the totals only squarefish Classic ASP Databases 4 November 9th, 2004 07:23 AM
Totals bjackman Access 1 February 8th, 2004 09:27 PM





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