[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Two versions of sum over node list by recursion--why a
Here are two examples of summing a node list by recursion: the first sends the result of the addition back to itself by a parameter: the second does not. The first was posted by G. Ken Holman--the second is from Michael Kay s _XSLT Programmer's Reference_. I know that Mr. Kay's code works...I just can't figure out _why_. FIRST EXAMPLE: <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> SECOND EXAMPLE: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template name="total-sales-value"> <xsl:param name="list"/> <xsl:choose> <xsl:when test="$list"> <xsl:variable name="first" select="$list[1]"/> <xsl:variable name="total-of-rest"> <xsl:call-template name="total-sales-value"> <xsl:with-param name="list" select="$list[position()!=1]"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="$first/sales * $first/price + $total-of-rest"/> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="/"> <xsl:variable name="total"> <xsl:call-template name="total-sales-value"> <xsl:with-param name="list" select="//book"/> </xsl:call-template> </xsl:variable> Total sales value is: <xsl:value-of select="format-number($total, '$#.00')"/> </xsl:template> </xsl:stylesheet> QUESTION: In Mr. Kay's example, why does the variable $total-of-rest indeed contain the total and not just the result of the addition in the last recursion? -------------------------------------------------------------------- mail2web - Check your email from the web at http://mail2web.com/ .
|
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
|