Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Olav Tank 6 posts 26 karma points
    Jun 08, 2010 @ 20:38
    Olav Tank
    0

    XSLT Calculation

    Hi

    I try to calculation in XSLT

    I have an XPathNodeIterator with values from a table

    I get this XML in XSLT with

    <xsl:variable name="test" select="GetTest:GetTest()//test" />
    ...
    <xsl:for-each select="$test">
            <xsl:call-template name="prizeTotal">
                    <xsl:with-param name="amount" select="./quantity"/>
                    <xsl:with-param name="prize" select="./price"/>
            </xsl:call-template>
    </xsl:for-each>

    And now i want a total. I tried this with

    <xsl:template name="prizeTotal">
            <xsl:param name="amount"/>
            <xsl:param name="price"/>
            <xsl:variable name="total" select="number($amount) * number($price)"/>
            <xsl:value-of select='format-number($total, "#.00")' />
    </xsl:template>

    but that gives for example that output:

    80.0040.00 (80.00 is from the first node and 40.00 from the second node) how can i calculate 80.00 with 40.00 (total = 120.00)?

  • Bert 128 posts 251 karma points
    Jun 08, 2010 @ 22:46
    Bert
    0
    <xsl:for-each select="$test">
    <xsl:variable name="total" select="quantity * price"/>
    </xsl:for-each>

    should work for xml like:

    ...
    <test>
    <quantity />
    <price />
    </test>
    ...
  • Olav Tank 6 posts 26 karma points
    Jun 08, 2010 @ 23:01
    Olav Tank
    0

    Thanks for your answer, but this not work for me :(

    The output is yet: 80.0080.0040.0040.00

  • Bert 128 posts 251 karma points
    Jun 09, 2010 @ 00:45
    Bert
    0

    do you have and xml -file example?

  • Olav Tank 6 posts 26 karma points
    Jun 09, 2010 @ 08:33
    Olav Tank
    0

    Yes of course

    <tests>
               <test id="2" nodeid="3">
                          <quantity>2</quantity>
                          <price>20.0</price>
                          ...
               </test>
               <test id="3" nodeid="3">
                          <quantity>10</quantity>
                          <price>20.0</price>
                          ...
               </test>
    </tests>
  • Josh Townson 67 posts 162 karma points
    Jun 09, 2010 @ 11:10
    Josh Townson
    0

    Try this:

    <xsl:variable name="test" select="GetTest:GetTest()//test" />
    <xsl:variable name="tmpTotal">
    <total_amount>
    <xsl:for-each select="$test">
    <item>
    <xsl:value-of select="./quantity * ./price"/>
    </item>
    </xsl:for-each>
    </total_amount>
    </xsl:variable>
    <xsl:variable name="myTotal" select="msxml:node-set($tmpTotal)"/>
    <xsl:value-of select="sum($myTotal/total_amount/item)" />
  • Olav Tank 6 posts 26 karma points
    Jun 09, 2010 @ 19:14
    Olav Tank
    0

    Thanks Josh for your reply!

    Your solution give me that output: 120.00120.00, okay it calculate perfect but it give me the value twice, how can i fix that?

  • Olav Tank 6 posts 26 karma points
    Jun 09, 2010 @ 19:16
    Olav Tank
    0

    Okay...  <xsl:if test="position() = 1"> </xsl:if> do that, but I think this is not perfect?

  • Olav Tank 6 posts 26 karma points
    Jun 09, 2010 @ 19:23
    Olav Tank
    0

    DAMN! My failure... I had two <xsl:for-each>... SORRY! Thank Josh!

Please Sign in or register to post replies

Write your reply to:

Draft