|
Subject: Re: [xsl] Mutability of variables From: Meeraj Kunnumpurath <mkunnumpurath@xxxxxxxxxxxxxx> Date: Sat, 25 Sep 2010 15:11:29 +0100 |
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.Ludwig@xxxxxxxxxxxx> 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 Bigui:
>
> [xsl] Center string
> http://markmail.org/thread/ayrb6jyqw2csahfo
>
> --
> Michael Ludwig
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| RE: [xsl] Mutability of variables, Ludwig, Michael | Thread | [xsl] grouping and subgroup problem, ahamed meeran |
| Re: [xsl] Question about grouping, Mukul Gandhi | Date | [xsl] ANN: How to Create XSLT Funct, Costello, Roger L. |
| Month |