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

Re: Find the number of elements that are prior to the

Subject: Re: 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
Re:  Find the number of elements that are prior to the
> 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

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.