XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 6th, 2003, 04:00 AM
cmicalle cmicalle is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: melbourne
Posts: 17 cmicalle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
XSL - sum of products of two values

Hi people,

I am new to the XML/XSLT world, so excuse me if I'm asking a silly question. My question is solely about some simple arithmetic in XSL.

Below is the XML data I have
<JOB>
<PROCESS>
<RESOURCE>
<rQty>10</rQty>
<rCost>10</rCost>
</RESOURCE>
<RESOURCE>
<rQty>20</rQty>
<rCost>20</rCost>
</RESOURCE>
</PROCESS>
</JOB>

All I have been able to do is get the sum of all the rQty or rCost with an expression similar to this one:-
<xsl:value-of select="sum(/JOB/PROCESS/RESOURCE/rQty)" />

What I really am trying to do is get the sum of (rQty * rCost) for each JOB, which will be used for the purpose of yielding a grand total.

Is this possible?

Many thanks in advance

Reply With Quote
  #2  
Old September 7th, 2003, 09:59 AM
cmicalle cmicalle is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: melbourne
Posts: 17 cmicalle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
anyone know??? I'm still struggling with this....

Reply With Quote
  #3  
Old September 8th, 2003, 01:12 PM
D-flyer D-flyer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 261 D-flyer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 18 m
Reputation Power: 8
Don't jump arouond i can' help, i am a newbie on the xsl subject.

But about <xsl:value-of select="sum(/JOB/PROCESS/RESOURCE/rQty)" /> is this valid procedure. And if so, where can i find an article on this.
Furhtermore i was thinking if you can use "sum" is it not possible to use "multiple"?

Reply With Quote
  #4  
Old September 8th, 2003, 02:29 PM
mono mono is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Bulgaria
Posts: 30 mono User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to mono
Hola

I'll give you the idea how to do this. If you can't then I'll give you the solution, but you have to practise ;-)

Hint 1: Use <xsl:variable>, <xsl:template>, <xsl:call-template>, <xsl:with-param>, maybe others

Hint 2: You have to make a recursion (call template in his body with parameters: current rQty & rCost and most inportant CurrentSum who you'll add to new multiply and return for new iteration while there are more RESOURCE'S).

Wish you luck
__________________
Regards
mono

Reply With Quote
  #5  
Old September 9th, 2003, 01:53 PM
mono mono is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Bulgaria
Posts: 30 mono User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to mono
Hi again

Well I will not harass you anymore

This is my little code for you:


<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0">
<xslutput method = "xml" indent = "no" omit-xml-declaration="yes"/>

<xsl:template match = "/jobs/process" >

<xsl:variable name="TotalSum">

<xsl:call-template name="calculator">
<xsl:with-param name="currSum">0</xsl:with-param>
<xsl:with-param name="count"><xsl:value-of select="count(resource)"/></xsl:with-param>
</xsl:call-template>

</xsl:variable>

<xsl:value-of select="$TotalSum"/>

</xsl:template>

<xsl:template name="calculator">
<xslaram name="currSum"/>
<xslaram name="count"/>

<xsl:variable name="qty" select="number(resource[number($count)]/qty)"/>
<xsl:variable name="cost" select="number(resource[number($count)]/cost)"/>

<xsl:variable name="sum" select="number($qty * $cost)"/>

<xsl:variable name="CycleSum" select="number($currSum + $sum)"/>

<xsl:choose>
<xsl:when test="number($count - 1) > 0 ">
<xsl:call-template name="calculator">
<xsl:with-param name="currSum"><xsl:value-of select="$CycleSum"/></xsl:with-param>
<xsl:with-param name="count"><xsl:value-of select="number($count - 1)"/></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsltherwise><xsl:value-of select="$CycleSum"/></xsltherwise>
</xsl:choose>

</xsl:template>

</xsl:stylesheet>

Reply With Quote
  #6  
Old October 10th, 2003, 04:11 AM
sr_paduri sr_paduri is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Tokyo
Posts: 1 sr_paduri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
If I want to calculate the sum of the qty of all nodes how should I write ?

Thanks in advance.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XSL - sum of products of two values


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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 On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT