tags:

views:

34

answers:

2

I am not working with XSLT not too long. I read that variable of XSLT can't be updated on the fly, so how can i do the following task.

I want to sum up Purchase & Sales and put them into a variable, and make some decision according to these values. (For example, if Purchase is greater then sales then do something if not, do something else)

<rows>
    <row>
        <col attr2="Purchase" >100.00</col>
        <col  attr2="Sales" >100.00</col>
    </row>
    <row >
        <col attr2="Purchase" >19.16</col>
        <col  attr2="Sales" >12.94</col>
    </row>
    <row >
        <col attr2="Purchase" >0.67</col>
        <col  attr2="Sales" >2.74</col>
    </row>
    <row >
        <col attr2="Purchase" >71.95</col>
        <col  attr2="Sales" >61.54</col>
    </row>
    <row >
        <col attr2="Purchase" >3.62</col>
        <col  attr2="Sales" >14.72</col>
    </row>
    <row >
        <col attr2="Purchase">8.80</col>
        <col attr2="Sales">1.22</col>
    </row>
    <row >
        <col attr2="Purchase" >-4.28</col>
        <col  attr2="Sales" >6.53</col>
    </row>
</rows>

if anyone knows, please help me.

+1  A: 

XSL variables are more constants: once set, their value cannot be changed. The only way of changing a variable is to use a recursive template, and using a named parameter to hold the current sum.

Or you would, if XSLT didn't have a sum function!

<xsl:variable name="$purchase-total" select="sum(col[@attr2='Purchase'])" />
<xsl:variable name="$sales-total" select="sum(col[@attr2='Sales'])" />
<xsl:choose>
    <xsl:when test="$purchase-total &gt; $sales-total">
        <!-- Do something -->
    </xsl:when>
    <xsl:otherwise>
        <!-- Do something -->
    </xsl:otherwise>
</xsl:choose>
Eric
thanks, for quick apply.and it works. I need the absolute sum of Purchase and Sales. I couldn't use abs function here?sum(arg(number(col[@attr2='Sales']))) is there any other way to use this.
Imrul
`arg()`? Don't you mean `abs()`? And no, I don't think you can: `abs()` doesn't return a node-set. Why would you want the absolute sum?
Eric
sorry! it's abs(). there are '-'ve value in Purchase/Sales. I want only the ABS value of Purchase/Sales. So how can i do it?
Imrul
Not withought a `node-set()` function. Which is an extension for xslt.
Eric
A: 

You can calculate the sums as shown in @Eric 's example.

The question you ask in your comment: To calculate the absolute value of x use the following XPath expression:

(x > 0)*x - not(x > 0)*x

For example:

With the provided XML document,

  <xsl:variable name="x" select="(/*/*/col[@attr2='Purchase'])[position()=last()]"/>

  <xsl:value-of select="($x > 0)*$x - not($x > 0)*$x"/>

produces:

4.28
Dimitre Novatchev
thanks for reply. would you please tell me what will 'x' here? i tried col[@attr2='Sales'] as 'x' but it doesn't work. If you provide little detail then it will be helpful.
Imrul
@Imrul: I have updated my answer to show an example how to use this XPath expression. You probably didn't select as $x the right node -- output this value to be sure what you selected exactly.
Dimitre Novatchev