[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 11:06:02 -0700
xsl word length
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

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.