[xsl] Functional Programming: How do I convert an xsl:for-each loop into a functional style?

Subject: [xsl] Functional Programming: How do I convert an xsl:for-each loop into a functional style?
From: "Costello, Roger L." <costello@xxxxxxxxx>
Date: Thu, 21 Jan 2010 07:38:13 -0500
Hi Folks,

My question is about writing XSLT in a functional programming style.

My understanding is that a litmus test for whether a program is written in a
functional style is whether it can be executed in any order.

Consider this problem: convert a sequence of N numbers into a cumulative
sequence, in which each number is the sum of the previous numbers.

Here is a loop that solves the problem:

<xsl:for-each select="Number">
      <xsl:value-of select="sum((., preceding-sibling::Number))" />
</xsl:for-each>

Can this code be executed in any order?

Suppose the loop is unraveled into a series of <xsl:value-of> statements. If
those statements are evaluated in arbitrary order then the output would not be
in the desired sequence.  Thus, I conclude, this loop is not written in a
functional style. Do you agree?

How would the problem be solved in a functional style?

/Roger

Current Thread