Copied to clipboard

Flag this post as spam?

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


  • Roger 195 posts 474 karma points
    Apr 25, 2013 @ 13:57
    Roger
    0

    getting total value of item in a loop

    Hi all,

    I have some XSLT that loops and displays sub pages from a parent. I need to calculate the total value of one amountPaid and display it like this:

    <xsl:for-each select="$currentPage/descendant::* [@isDoc]">
        <li>
            <p>Date Paid: <xsl:value-of select="umbraco.library:FormatDateTime(datePaid, 'MMMM d, yyyy')"/></p>
            <p>Amount Paid: £<xsl:value-of select="amountPaid"/></p>
            <p>Notes: <xsl:value-of select="otherInformation"/></p>
           
        </li>
    </xsl:for-each>

        <xsl:variable name="total" select="sum(preceding-sibling::$currentPage/descendant::*) + amountPaid"/>
    <p>Total paid to date: <xsl:value-of select="@total"/></p>

    I know the xslt for to calculate the total is incorrect. Can anyone help in the correct xslt to acheive the total please?

    Thanks!

  • Roger 195 posts 474 karma points
    Apr 25, 2013 @ 14:23
    Roger
    0

    I tried this but it returns '0'

    Total paid to date: £<xsl:value-of select="sum(/*/*/amountPaid)"/>

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Apr 25, 2013 @ 14:34
    Chriztian Steinmeier
    100

    Hi Roger,

    A simple variable should do it:

    <!-- Add up all the amountPaid properties below $currentPage -->
    <xsl:variable name="total" select="sum($currentPage//amountPaid)" />
    
    <p>Total paid to date: <xsl:value-of select="$total"/></p>
    

    Are there likely to be "wrong" data in those fields? E.g., someone entered "$12.80" or "fifty quid" - then you'd probably need to sanitize the data a bit...

    /Chriztian

  • Roger 195 posts 474 karma points
    Apr 25, 2013 @ 14:43
    Roger
    0

    Hi Chriztian, There will only be numeric entries with 2 decimal places.

    Is there a way to add the decimal places to the solution you have provided?

    Thanks for the help

    Roger

  • Roger 195 posts 474 karma points
    Apr 25, 2013 @ 15:39
    Roger
    0

    I had a crack at it the format-number seems to work

    <xsl:value-of select="format-number($total,'0.00')"/>

    Thanks for the help Chriztian

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies