Subject: Re: [xsl] split string to whole words based on length From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx> Date: Wed, 26 Apr 2006 10:46:13 -0700 |
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: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.
as="xs:string"/>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'"
<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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] split string to whole wor, David Carlisle | Thread | [xsl] Question about cross referenc, Tech Savvy |
Re: [xsl] split string to whole wor, andrew welch | Date | Re: [xsl] split string to whole wor, Dimitre Novatchev |
Month |