This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: xsl numeric calculations-NP
- From: "Andrew Welch" <awelch at piper-group dot com>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Fri, 2 Aug 2002 10:49:21 +0100
- Subject: RE: [xsl] xsl numeric calculations-NP
- Reply-to: xsl-list at lists dot mulberrytech dot com
The easiest solution here I think is to do the first operation (quantity
* unit_price) in a variable and then sum() the values of that using
node-set()
<xsl:variable name="fruits-rtf">
<xsl:for-each select="//fruits/*">
<fruit name="{name()}" totalcost="{@quantity * @unit_price}"/>
</xsl:for-each>
</xsl:variable>
This variable will hold a result-tree-fragment of <fruit> elements with
an attribute @totalcost which holds the result of quantity * unit_price
You can then calculate the sum() of the @totalcost by using the
node-set() extension function of your favourite processor:
<xsl:value-of
select="sum(exsl:node-set($fruits-rtf)/fruit/@totalcost)"/>
cheers
andrew
> -----Original Message-----
> From: NILESH PATEL [mailto:jayganesh786@hotmail.com]
> Sent: 02 August 2002 08:20
> To: XSL-List@lists.mulberrytech.com
> Subject: [xsl] xsl numeric calculations-NP
>
>
>
>
> Dear Friends,
>
> I am trying to achoeve simple calcuateion from xsl. I do not
> know the way
> out of this. Please help. Here is the code.
>
> XML:
> ======
> <Shopping>
> <fruits>
> <apple quantity="10" unit_price="1"></apple>
> <orange quantity="20" unit_price="5"></orange>
> <banana quantity="30" unit_price="5"></banana>
> <grapes quantity="40" unit_price="5"></grapes>
> </fruits>
> <vegs>
> <onion quantity="10" unit_price="5"></onion>
> <carrot quantity="5" unit_price="10"></carrot>
> <chiili quantity="20" unit_price="5"></chilli>
> <tomato quantity="10" unit_price="5"></tomato>
> <potato quantity="20" unit_price="5"></potato>
> </vegs>
> </shopping>
>
> XSL:
> ======
> //suggest xsl template here, because I tried using sum() and
> numbers and so
> on but could not work the way around it. I could do individual
> multiplication using <xsl:for-each select="//fruits/*">. but
> did not achieve
> results.SO I BEG TO THE COMMUNITY, PLEASE HELP ME NOW.
>
>
> Required Result:
> =================
> FRUITS: (10*1) + (20*5) + (30*5) + (40*5) = 460
> VEGS : (10*5) + (5*10) + (20*5) + (10*5) + (20*5) = 350
>
>
>
> Basically, I want to multiply "quantity" and "unit_price"
> first then add the
> multiplication-result.May be using javascript or xsl built in
> functions,
> please suggest.
>
> Thanks very much in advance.
>
> Nilesh Patel
>
> _________________________________________________________________
> MSN Photos is the easiest way to share and print your photos:
> http://photos.msn.com/support/worldwide.aspx
>
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
>
>
>
>
> ---
> Incoming mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.373 / Virus Database: 208 - Release Date: 01/07/2002
>
>
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.373 / Virus Database: 208 - Release Date: 01/07/2002
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list