Re: [xsl] Find the number of elements that are prior to the seriesof elements that match a string?

Subject: Re: [xsl] Find the number of elements that are prior to the seriesof elements that match a string?
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 12 Mar 2019 20:12:41 -0000
> So what is the easiest/clearest/most-clever way in XPath 3 to break a string
of arbitrary length into a sequence of strings of a fixed size?
>

Yes, I was pondering that too en route to my solution to Roger's problem.

There's a class of solutions involving string-to-codepoints() and
codepoints-to-string(), but that feels inelegant to me. You can use these to
implement a split() function that splits a string into N one-character
strings, and then use grouping to recombine them.

Then there are solutions involving recursion and substring. Something like:

partition($string, $size) ==> if ($string) then (substring($string, 1, $size),
partition(substring($string, $size+1), $size) else ()

And there are solutions that go via a sequence of integer positions:

partition($string, $size) ==> (1 to string-length($string) idiv $size)) !
substring($string, ., $size)

And there are regular expression solutions:

<xsl:analyze-string select="$s" regex="..">
  <xsl:matching-substring>{.}</xsl:matching-substring>
  <xsl:non-matching-substring>{.}</xsl:non-matching-substring>
</xsl:analyze-string>

or

fn:analyze-string($s, '..')/*/string()

All things considered, I think I'll go with the last one. But it's not quite
so attractive if the "fixed size" is dynamically defined rather than being
known at compile time.

Michael Kay
Saxonica

Current Thread