[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: sum of price * qty, without node-set()
At 2004-01-28 19:22 -0800, Charles Gebhard wrote:
i'm a newbie and am trying to get a sum of a calculation; i'm hoping to get the value "162.00". Using a recursive-like "walk" of the source node tree, performing calculations at each step of the walk. seems like it would be really simple. If you don't mind recursive-like coding. i've been pounding my head for hours. I hope the working example demonstrated below provides relief. Note how I start at the first RECEIPT element, initializing the result to zero, then at each RECEIPT I perform the local calculation and update a running result. Then, if there is a sibling, pass this intermediate result to the sibling processing of the closest following sibling, using a mode to ensure other templates in my stylesheet don't interfere. If there is no sibling, return the value calculated so far. The template calling returns a string, so the variable is initially a text string in a result tree fragment node, that I then cast to a number for other checking and calculations (this has to be done for checking because all result tree fragments test as true). Note the trick in the variable declaration where I have a leading zero followed by the string calculated by the templates: this way, if there are no RECEIPT elements at all, the string at least contains a zero digit to be converted to a zero result ... otherwise you would get a NaN result. ................. Ken p.s. I note your company is based in San Francisco ... we are scheduled to teach XSL in a publicly subscribed course in Burlingame next month at the SFO Embassy Suites hotel; details linked from our home page.
T:\ftemp>type gebhard.xsl <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:variable name="result-text"> <xsl:text/>0<xsl:apply-templates select="/*/RECEIPT[1]" mode="equation"/> </xsl:variable> <xsl:variable name="result" select="number($result-text)"/> <xsl:value-of select="$result"/> </xsl:template> <xsl:template match="RECEIPT" mode="equation"> <xsl:param name="result" select="0"/> <xsl:variable name="this" select="$result + ( qtyRcpt * line/pr )"/> <xsl:choose> <xsl:when test="following-sibling::RECEIPT"> <xsl:apply-templates select="following-sibling::RECEIPT[1]" mode="equation"> <xsl:with-param name="result" select="$this"/> </xsl:apply-templates> </xsl:when> <xsl:otherwise> <xsl:value-of select="$this"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> T:\ftemp>saxon gebhard.xml gebhard.xsl <?xml version="1.0" encoding="utf-8"?>162 T:\ftemp>
G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|