p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > XML > XSLT
I forgot my password Register Now
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 4th, 2009, 06:31 AM
Registered User
Points: 10, Level: 1
Points: 10, Level: 1 Points: 10, Level: 1 Points: 10, Level: 1
Activity: 5%
Activity: 5% Activity: 5% Activity: 5%
 
Join Date: Nov 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default XPath Expression to Calculate total amount for order details

I have a xml file like this

PHP Code:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<orders>
<order orderDate="1/1/2009" orderNo="1">
<customer id="nnghiem" name="nguyen nghiem"/>
<item id="item-1" price="25000" quantity="0"/>
<item id="item-2" price="22000" quantity="3"/>
</order>
<order orderDate="2/2/2009" orderNo="2">
<customer id="lp" name="lampard"/>
<item id="item-1" price="25000" quantity="2"/>
<item id="item-2" price="22000" quantity="8"/>
</order>
<order orderDate="3/3/2007" orderNo="3">
<customer id="nnghiem" name="nguyen nghiem"/>
<item id="item-1" price="25000" quantity="7"/>
<item id="item-2" price="22000" quantity="6"/>
</order>
</orders>
Now i want to calculate total amount for each order

this is my xsl file

PHP Code:
                    <xsl:for-each select="/orders/order">
                        <
tr>
                            <
td> <xsl:value-of select="@orderNo"></xsl:value-of> </td>
                            <
td> <xsl:value-of select="@orderDate"></xsl:value-of> </td>
                            <
td> <xsl:value-of select="customer/@name"></xsl:value-of> </td>
                    <
td> <xsl:value-of select="sum(item/@quantity * item/@price)"></xsl:value-of></td>
                        </
tr>                        
                    </
xsl:for-each
When i transform xml+xsl => html, an error occured at the line
PHP Code:
<xsl:value-of select="sum(item/@quantity * item/@price)"></xsl:value-of
I can only guess that my xpath - expression is not correct, i also tried this:
PHP Code:
<xsl:value-of select="sum(item/@quantity * @price)"></xsl:value-of
But i also got the same problem. How can i fix this?

I need some advices, i've just learnt XSL for 1 week.

Thanks for your attention
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old November 4th, 2009, 06:47 AM
mhkay's Avatar
Wrox Author
Points: 12,735, Level: 48
Points: 12,735, Level: 48 Points: 12,735, Level: 48 Points: 12,735, Level: 48
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
Default

You could get a long way just by googling for "XSLT sum of price times quantity".

In XSLT 2.0 the solution is sum(item/(@price * @quantity))

In XSLT 1.0 it's much harder. The only clean solution is a recursive traversal of the items in which you pass the accumulated sum as a parameter and add in the value of @price * @quantity before making the recursive call to process the next item. You'll often find this in XSLT books as a textbook example of recursive template processing.

Of course, sum(item/@quantity * item/@price) doesn't work. You can't take a set of attribute nodes and multiply it by another set of attribute nodes.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculate running total on form and prevent text Hughesie78 ASP.NET 2.0 Basics 0 November 27th, 2007 10:35 AM
Calculate a running total Neal XSLT 3 March 22nd, 2007 12:16 PM
Calculate total hours based on Time deepcover1 Access 1 April 5th, 2006 02:50 AM
How To Calculate Monthy Total on-the-clock time Michaniker Access 2 December 2nd, 2005 06:06 PM
Total Amount display doosti Classic ASP Databases 2 November 14th, 2004 07:25 AM



All times are GMT -4. The time now is 01:18 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc