xsl-list
[Top] [All Lists]

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

2019-03-12 15:12:46

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
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>