Thanks for the Michael. In the meanwhile I wrote another
implementation shown below. I am quite new to using XSLT, so I would
appreciate of someone could please comment on anything obviously poor
in below implementation.
<xsl:function name="gpg:fold" as="xsd:string">
<xsl:param name="line" />
<xsl:param name="result" />
<xsl:param name="current-line-length" />
<xsl:param name="line-length" />
<!-- The next token is the either the next word or the next
space, whichever is first -->
<xsl:variable name="current-token" select="if
(substring($line, 1, 1) = ' ') then ' ' else tokenize($line, ' ')[1]"
/>
<xsl:choose>
<!-- We haven't reached the end of the line -->
<xsl:when test="$current-token">
<xsl:variable name="current-token-length"
select="string-length($current-token)" />
<xsl:variable name="new-line-length"
select="$current-token-length + $current-line-length" />
<xsl:choose>
<!-- Next word or space won't fit into the current line. -->
<xsl:when test="$new-line-length > $line-length">
<xsl:choose>
<!-- Next word is longer than the line length. -->
<xsl:when test="$current-token-length >
$line-length">
<xsl:variable
name="remaining-line-length" select="$line-length -
$current-line-length" />
<xsl:variable name="first-half"
select="substring($current-token, 1, $remaining-line-length)" />
<xsl:variable name="new-line"
select="substring($line, $remaining-line-length + 1)" />
<xsl:variable name="new-result"
select="concat($result, $first-half, ' ')" />
<xsl:value-of
select="gpg:fold($new-line, $new-result, 0, $line-length)" />
</xsl:when>
<!-- Next word or space can go onto the
next line. -->
<xsl:otherwise>
<xsl:variable name="new-result"
select="concat($result, ' ')" />
<xsl:value-of select="gpg:fold($line,
$new-result, 0, $line-length)" />
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<!-- Next word or space will fit into the current line -->
<xsl:otherwise>
<xsl:variable name="new-line"
select="substring($line, $current-token-length + 1)" />
<xsl:variable name="new-result"
select="concat($result, $current-token)" />
<xsl:value-of select="gpg:fold($new-line,
$new-result, $new-line-length, $line-length)" />
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<!-- Exit recursion when we are at the end of the line -->
<xsl:otherwise>
<xsl:value-of select="$result" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
Kind regards
Meeraj
On Wed, Sep 22, 2010 at 1:57 PM, Ludwig, Michael
<Michael(_dot_)Ludwig(_at_)delphi-mb(_dot_)de> wrote:
I realize XSLT variables are immutable. However, I have a requirement
to split a piece of text into multiple lines based on a pre-defined
length respecting word boundaries. This means if the last word on a
line doesn't fit into the current line, the whole word needs to be
moved into a new line. I am using XSLT 2.0 with Saxon 9.* HE.
See this thread and the solution by Emmanuel Bégué:
[xsl] Center string
http://markmail.org/thread/ayrb6jyqw2csahfo
--
Michael Ludwig
--~------------------------------------------------------------------
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>
--~--
--~------------------------------------------------------------------
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>
--~--