[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Line break algorithm

Subject: Re: Line break algorithm
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 3 May 2020 09:26:44 -0000
Re:  Line break algorithm
If (like me) you find xsl:iterate easier to get right than recursive
functions, then you could do something like

<xsl:iterate select="tokenize($input)">
  <xsl:param name="line-length" select="0"/>
  <xsl:choose>
     <xsl:when test="$line-length gt 35">
        <break/>
        <xsl:value-of select=". || ' '"/>
        <xsl:next-iteration>
           <xsl:with-param name="line-length" select="string-length(.) + 1"/>
        </xsl:next-iteration>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select=". || ' '"/>
        <xsl:next-iteration>
           <xsl:with-param name="line-length" select="$line-length +
string-length(.) + 1"/>
        </xsl:next-iteration>
    </xsl:otherwise>
  </xsl:choose>
</xsl:iterate>

Note that a solution using tokenize loses the original separators; if you want
to retain separators such as multiple spaces or tabs then you'll need to use
analyse-string instead.

There are probably more concise/elegant solutions using higher-order functions
and folding, but this one seems easy to read and understand.

Michael Kay
Saxonica


> On 3 May 2020, at 01:45, Rick Quatro rick@xxxxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Hi All,
>
> I have lines in my input that I want to add <break> elements to in my
output. For example, this might be the input:
>
> <xsl:param name="line" select="'Takeoff from Unlisted and Alternate
Airports'"/>
>
> and I want to replace a space with a <break> so that each line doesn't
exceed, for example, 35 characters
>
> <line>Takeoff from Unlisted and Alternate<break/>Airports</line>
>
> I am thinking the I can tokenize the line and then recursively build the
string back up from the beginning, checking its length. Any other suggestions
on a general-purpose algorithm that I can use in XSLT 3 would be appreciated.
Thanks in advance.
>
> Rick
>
> Rick Quatro
> Carmen Publishing Inc.
> rick@xxxxxxxxxxxxxxx <mailto:rick@xxxxxxxxxxxxxxx>
> 585-729-6746
> www.frameexpert.com/store/ <http://www.frameexpert.com/store/>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
email <>)

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.