xsl-list
[Top] [All Lists]

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

2006-04-26 11:06:22
On 4/26/06, andrew welch <andrew(_dot_)j(_dot_)welch(_at_)gmail(_dot_)com> 
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,aaaaaaaaaaaaaaaaaaaa,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.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--