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 November 4th, 2009, 06:31 AM
Registered User
 
Join Date: Nov 2009
Posts: 3
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
 
Old November 4th, 2009, 06:47 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
Old November 26th, 2009, 11:34 AM
Registered User
 
Join Date: Nov 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for your advice, I got the solution





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 11:16 AM
Calculate total hours based on Time deepcover1 Access 1 April 5th, 2006 01: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





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