Re: [xsl] Constructing a sequence: numbers are added up repeatedly

Subject: Re: [xsl] Constructing a sequence: numbers are added up repeatedly
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Thu, 3 Nov 2011 12:35:09 +0000
On 3 November 2011 12:12, Manfred Staudinger
<manfred.staudinger@xxxxxxxxx> wrote:
> Hi,
>
> Given
>   <xsl:variable name="t1" select="'Mlmlmhmhmvmv'"/>
> then
>   <xsl:variable name="t4" select="for  $i in 1 to string-length($t1) return
>      if (substring($t1, $i, 1)=('h', 'v')) then 0 else
>      if (substring($t1, $i, 1)='z') then -1 else 1"/>
>   <xsl:variable name="t5" select="for $i in 1 to string-length($t1)
>      return $i + sum(subsequence($t4, 1, $i - 1))"/>
> gives me what I want in $t5:
>   1 3 5 7 9 11 12 14 15 17 18 20
>
> While $t4 isn't very efficient, the construct in $t5 is simply unacceptable
> for longer strings: the numbers are added up repeatedly instead of adding
> only one number to the previous sum.
>
> Any better solution would be appreciated, thanks!

Instead of substring using
codepoints-to-string(string-to-codepoints($t1)) to get a sequence of
strings (1 char each), then pass that to a recursive function that
sums as it goes, passing the index and subsequence each time.


--
Andrew Welch
http://andrewjwelch.com

Current Thread