Both are a correct way of expressing the sum.
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
Both foldr() and foldl() are fundamental functions, implemented in FXSL.
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()):
sum( f:map( f:compose(f:product(), f:childElements() ), *) )
--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
On 9/5/06, hanged(_dot_)man(_at_)lycos(_dot_)com
<hanged(_dot_)man(_at_)lycos(_dot_)com> wrote:
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/ .
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--