Re: [xsl] split string to whole words based on length

Subject: Re: [xsl] split string to whole words based on length
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Wed, 26 Apr 2006 11:06:02 -0700
On 4/26/06, andrew welch <andrew.j.welch@xxxxxxxxx> wrote:

This fails for the input:


<words>
<word>aaaaaaaaaaaaaaaaaaaa</word>
<word>aaaaaaaaaaaaaaaaaaaaa</word>
<word>aaaaaaaaaaaaaaaaaaaaa</word>
<word>aaaaaaaaaaaaaaaaaaaaaaaaaaaa</word>
<word>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</word>
<word>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</word>
<word>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</word>
<word>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</word>
</words>

It creates too many output elements.

And the so;ution I posted consumes all the stack :o)


Here's one that survives this case:

<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="'aaaaaaaaaaa,aaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaa
aaa,aaaaaaaaaaaaaaaaaaaaaa'"/>
<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:message>
     $pcommaIds: <xsl:value-of select="$pcommaIds"/>
   </xsl:message>

   <xsl:sequence select=
    "if(not($pcommaIds[2]))
       then substring($pStr, $pcommaIds[1]+1)
       else
         for $firstI in $pcommaIds[1],
             $lastI in ($pcommaIds[2],
                         $pcommaIds[position() gt 1]  [.-$firstI le
$pLength+1][last()])

                                [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.

Current Thread