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

Re: split string to whole words based on length

Subject: Re: split string to whole words based on length
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Wed, 26 Apr 2006 10:46:13 -0700
split string variable
In case someone has difficulties with the regexp solution, here is
another one -- a little bit unusual.

As for the "quality" of a recursive vs non-recursive solution, there
are many cases like this one, in which it is very diffivult to come up
with a non-recursive solution. To put it in other words, for this
particular problem a recursive solution is much more natural than a
non-recursive. I believe that a quality solution must be a natural
one.

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:f="http://fxsl.sf.net/"
exclude-result-prefixes="f xs">

<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:variable name="vStr"  as="xs:string"
     select="'one,two,three,four,five'"/>
<xsl:variable name="vLength" as="xs:integer"
     select="10" />

 <xsl:template match="/" name="initial">
   <xsl:variable name="vcommaInds" as="xs:integer*"
     select="0, index-of(for $i in 1 to string-length($vStr)
                          return substring($vStr,$i,1),
                         ','
                         )"/>
<!--    <xsl:value-of select="$vcommaInds"/> -->

   <xsl:value-of select="f:strSplit($vStr, $vcommaInds, 10)" separator="|"/>
 </xsl:template>

 <xsl:function name="f:strSplit" as="xs:string*">
   <xsl:param name="pStr" as="xs:string"/>
   <xsl:param name="pcommaIds" as="xs:integer*"/>
   <xsl:param name="pLength" as="xs:integer"/>

   <xsl:sequence select=
    "if(not($pcommaIds[2]))
       then substring($pStr, $pcommaIds[1]+1)
       else
         for $firstI in $pcommaIds[1],
             $lastI in ($pcommaIds[.-$firstI le $pLength+1]
                                                       [last()]),
             $lastIPos in index-of($pcommaIds, $lastI)
           return
              (substring($pStr, $firstI+1, $lastI - $firstI - 1),
               f:strSplit($pStr,
                         subsequence($pcommaIds, $lastIPos),
                         $pLength
                         )
               )
    "
    />

 </xsl:function>
</xsl:stylesheet>


-- Cheers, Dimitre Novatchev --------------------------------------- Truly great madness cannot be achieved without significant intelligence.


On 4/26/06, andrew welch <andrew.j.welch@xxxxxxxxx> wrote:
I'm sure there's a good xslt 2.0 way of doing this that doesn't
involve a recursive named template or function.  In fact I'm sure it's
come up a few times but I can't find it in the archives.

Given a string of comma delimeted words and a length, split the string
into substrings so that whole words are kept intact with proper use of
the delimiter, where no string exceeds the length.

So, given the following:

<xsl:variable name="str" select="'one,two,three,four,five'"
as="xs:string"/>
<xsl:variable name="length" select="10" as="xs:integer"/>

Should be output as:

<words>one,two</words>
<words>three,four</words>
<words>five</words>

As I say, I'm sure there's a quality solution to this, rather than the
long winded recursive approach.

Any takers?

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-2007 All Rights Reserved.