xsl-list
[Top] [All Lists]

RE: Conditional splitting of string with XSL

2003-01-26 23:52:26
I once needed that kind of stylesheet myself (not exactly the same of
course, but the same principe).
I can not guarantee it's 100% safe, but it's written in XSLT without any
extension functions (so it
should work with all xslt processors) and it did work for me. Of course it's
possible you need to make
some adaptions in it. For getting only three lines you should make something
up yourself.

<!-- This template returns a string that is splitted according
         to a maximal length of characters.
         Input parameters: 
                original (mandatory): original string that must be splitted
                maxLength (mandatory): maximum length before a split
                separator (mandatory): separator that must be used to split
the string
                wordWrap (optional): set this value to 'true' if a word must
keep together
                                                         instead of
splitting a word in the middle, there will
                                                         be split before the
word. Note that when the word is
                                                         longer than the
maximal length, it's impossible to
                                                         keep the word
together.
-->
<xsl:template name="string_splitter">
        <xsl:param name="original"/>
        <xsl:param name="maxLength"/>
        <xsl:param name="separator"/>
        <xsl:param name="wordWrap"/>
        <xsl:choose>
                <xsl:when test="string-length($original)>$maxLength">
                        <xsl:variable name="length-substring">
                                <xsl:choose>
                                        <xsl:when test="$wordWrap='true'">
                                                <xsl:call-template
name="lastCharPosition">
                                                        <xsl:with-param
name="original" select="substring($original,1,$maxLength)"/>
                                                        <xsl:with-param
name="character" select="' '"/>
                                                </xsl:call-template>
                                        </xsl:when>
                                        <xsl:otherwise>
                                                <xsl:value-of
select="$maxLength"/>
                                        </xsl:otherwise>
                                </xsl:choose>
                        </xsl:variable>
                        <xsl:value-of
select="concat(substring($original,1,$length-substring),$separator)"/>
                        <xsl:call-template name="string_splitter">
                                <xsl:with-param name="original"
select="substring($original,$length-substring+1,string-length($original))"/>
                                <xsl:with-param name="maxLength"
select="$maxLength"/>
                                <xsl:with-param name="separator"
select="$separator"/>
                                <xsl:with-param name="wordWrap"
select="'true'"/>
                        </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                        <xsl:value-of select="$original"/>
                </xsl:otherwise>
        </xsl:choose>
</xsl:template>

<!-- This template returns the position of the last
         given character in the string that can be found. When
         the character is not available in the string, the 
         position of the last character is returned.
         Input parameters:
                original (mandatory): original string in which the character
must be searched
                character(mandatory): character for which the last position
must be returned                 
-->
<xsl:template name="lastCharPosition">
        <xsl:param name="original"/>
        <xsl:param name="character"/>
        <xsl:param name="string_length"/>
        <xsl:variable name="len">
                <xsl:choose>
                        <xsl:when test="$string_length">
                                <xsl:value-of select="$string_length"/>
                        </xsl:when>
                        <xsl:otherwise>
                                <xsl:value-of select="'0'"/>
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:variable>
        <xsl:variable name="char_len">
                <xsl:value-of select="string-length($character)"/>
        </xsl:variable>
        <xsl:choose>
                <xsl:when test="contains($original,$character)">
                        <xsl:choose>
                                <xsl:when
test="contains(substring-after($original,$character),$character)">
                                        <xsl:call-template
name="lastCharPosition">
                                                <xsl:with-param
name="original" select="substring-after($original,$character)"/>
                                                <xsl:with-param
name="character" select="$character"/>
                                                <xsl:with-param
name="string_length"
select="string-length(concat(substring-before($original,$character),'
'))+$len"/>
                                        </xsl:call-template>
                                </xsl:when>
                                <xsl:otherwise>
                                        <xsl:value-of
select="string-length(substring-before($original,$character))+$char_len+$len
"/>
                                </xsl:otherwise>
                        </xsl:choose>
                </xsl:when>
                <xsl:otherwise>
                        <xsl:value-of select="string-length($original)"/>
                </xsl:otherwise>
        </xsl:choose>
</xsl:template>

Hope it helps.

Kind regards,
Ismaël



-----Original Message-----
From: Mukul [mailto:mukulw3(_at_)yahoo(_dot_)com]
Sent: zondag 26 januari 2003 11:36
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Conditional splitting of string with XSL


Hello ,
  I have a simple XML file as below:

<verylongtext>
longtextlongtextlongtext ongtextlongtextlongtextlon
textlongtextlongtextlongtex longtextlongtextlongte
tlongtextlongtextlongtextlongtext ongtextlongtext
ongtextlongte tlongtextlongtextlongtextlongtextlon
textlongtext ongtextlo gtextlongtext..can be very
long..
</verylongtext>

I want to write a XSL stylesheet which can split the
<verylongtext> data into multiple lines of 20
charcaters each. I need only 1st 3 lines after
splitting. 

If there is a space character in data, then the split
needs to be done just before the space, and the space
character should be the <= 20th character..

Can anybody please help me with this problem.. Or is
this at all possible with XSL..? Will appreciate an
early reply..

Regards,
Mukul



__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list