xsl-list
[Top] [All Lists]

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

2011-11-03 07:35:18
On 3 November 2011 12:12, Manfred Staudinger
<manfred(_dot_)staudinger(_at_)gmail(_dot_)com> 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

--~------------------------------------------------------------------
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>
--~--