Subject: Re: [xsl] Two versions of sum over node list by recursion--why and how does second one work? From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx> Date: Tue, 5 Sep 2006 10:42:01 -0700 |
Dr. Kay's solution is a special case of using the foldr() function, the Haskell definition of which is below:
foldr f z [ ] = z foldr f z (x:xs) = f x (foldr f z xs)
G.Ken Hollman's solution is a special case of using the foldl() function, the Haskell definition of which is below:
foldl f z [ ] = z foldl f z (x:xs) = foldl f (f z x) xs
To conclude this, one can also express the solution to this problem as a special case of using the FXSL f:product() function (which is itself trivially defined using f:foldl() or f:foldr()):
-- Cheers, Dimitre Novatchev --------------------------------------- Truly great madness cannot be achieved without significant intelligence. --------------------------------------- To invent, you need a good imagination and a pile of junk
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/ .
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Two versions of sum over , Michael Kay | Thread | [xsl] Sorting in order to count uni, Steve |
Re: [xsl] XSLT2.0 compatibility, Mukul Gandhi | Date | Re: [xsl] XSLT2.0 compatibility, David Carlisle |
Month |