[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XSLT repetition constructs

Subject: Re: XSLT repetition constructs
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 7 Mar 2019 15:19:04 -0000
Re:  XSLT repetition constructs
The best I could come up with was

tail(fold-left($x, 0, function($a, $b) {$a, $a[last()] + $b}))

Your solution could be rewritten

fold-left($x, (), function($a, $b) {$a, ($a[last()], 0)[1] + $b})

Another option is

fold-left(tail($x), $x[1], function($a, $b) {$a, $a[last()] + $b})

xsl:iterate is more intuitive though of course more verbose:

<xsl:iterate select="1 to 4">
  <xsl:param name="total" select="0"/>
  <xsl:variable name="new-total" select="$total + ."/>
  <xsl:sequence select="$new-total"/>
  <xsl:next-iteration>
    <xsl:with-param name="total" select="$new-total"/>
  </xsl:next-iteration>
</xsl:iterate>

Michael Kay
Saxonica

> On 7 Mar 2019, at 13:28, Martin Honnen martin.honnen@xxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Am 07.03.2019 um 12:55 schrieb Michael Kay mike@xxxxxxxxxxxx:
>> A good simple use case for fold-left() is to accumulate a running total,
i.e. turn (1,2,3,4) into (1,3,6,10).
>>
>
> The example to simply compute the running total (e.g. map (1,2,3,4) to 10)
is in the spec with
>
>    fold-left((1 to 4), 0, function($a, $b) { $a + $b})
>
>
> But to map the whole sequence (1,2,3,4) with fold-left to a new sequence of
(1,3,6,10) I am already struggling to express that in a compact way, is
>
> fold-left(
>   (1 to 4),
>   (),
>   function ($a, $b) {  $a, if (empty($a)) then $b else $b + $a[last()] }
> )
>
> a good way? Or can the third argument, the function be expressed in a more
compact way?

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.