Another tricky transformation: Conditional Totaling
This is an open discussion with 4 replies, filed under XSLT.
Search
Ooh, nice one. One way I can think of is the following:
<xsl:template match="/data/entry"> <xsl:param name="total" select="0"/> <xsl:param name="price" select="0"/> <xsl:variable name="new-total"> <xsl:choose> <xsl:when test="outcome = 'Win'"> <xsl:value-of select="$price * odds"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$price * -1"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:choose> <xsl:when test="position() = last()"> <xsl:value-of select="$new-total"/> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="/data/entry[position() = current()/position()+1]"> <xsl:with-param name="total" select="$newtotal"/> <xsl:with-param name="price" select="$price"/> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:template>
which you can then call by using:
<xsl:apply-templates select="/data/entry[position() = 1]"> <xsl:with-param name="price" select="100"/> </xsl:apply-templates>
Where does the price
parameter come from? Is that a hardcoded value, or is it passed in from somewhere?
Correction. The above template will not work, here is a working version:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes" /> <xsl:variable name="count" select="count(/data/entry)"/> <xsl:template match="/data/entry"> <xsl:param name="total" select="0"/> <xsl:param name="price" select="0"/> <xsl:variable name="new-total"> <xsl:choose> <xsl:when test="outcome = 'Win'"> <xsl:value-of select="$total + ($price * odds)"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$total - ($price)"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:choose> <xsl:when test="not(./following-sibling::entry)"> <xsl:value-of select="$new-total"/> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="./following-sibling::entry[1]"> <xsl:with-param name="total" select="$new-total"/> <xsl:with-param name="price" select="$price"/> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="/"> <xsl:apply-templates select="/data/entry[position() = 1]"> <xsl:with-param name="price" select="100"/> </xsl:apply-templates> </xsl:template> </xsl:stylesheet>
Result can be looked at on xpathr
@designermonkey ... see my original post...
creativedutchmen, your example seems to work and so logical. Doh! I will test it in live environment... And I was messing around with XSLT Keys. :D
Create an account or sign in to comment.
Hi guys!
I have another tricky transformation that I have not been able to sort out on my own. I was hoping that maybe somebody could help me out
I have a dataset about sports bets and I need to produce some reports based on it. One of the things I need to do is come up with a "conditional total".
First, here's the sample dataset:
I need to take this data and use it to come up with grand total of these bets. Simple sum won't work here because the actual item to be summed is a result of another equation and the bet amount is param. See, I have to use these two parameters:
The spreadsheet would look like this:
So If it's a winning bet then I need to take the odds and multiply it with "price" and if it's loosing bet then it's just -"price"... As you can see above ("entry-price"), It's very easy to do, but now I need to get the grand total of "entry-price".
So, how can I loop through the entries while calculation the bet value and summon it up. I only need to show the grand total value...