|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
anyone know??? I'm still struggling with this....
|
|
#3
|
|||
|
|||
|
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"? |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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"> <xsl utput 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"> <xsl aram name="currSum"/><xsl aram 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> <xsl therwise><xsl:value-of select="$CycleSum"/></xsl therwise></xsl:choose> </xsl:template> </xsl:stylesheet> |
|
#6
|
|||
|
|||
|
If I want to calculate the sum of the qty of all nodes how should I write ?
Thanks in advance. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > XSL - sum of products of two values |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|