Re: [xsl] Line break algorithm

Subject: Re: [xsl] Line break algorithm
From: "Terry Badger terry_badger@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 3 May 2020 19:31:03 -0000
Rick,
So I am old fashion with V2 but recursion works for me:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xs="http://www.w3.org/2001/XMLSchema"; exclude-result-prefixes="xs"
B B  B version="3.0">
B B  B <!-- badger 2020-05-03 -->
B B  B <!-- start here -->
B B  B <xsl:template match="/*">
B B  B B B  B <xsl:result-document href="p-normal.xml">
B B  B B B  B B B  B <wrapper>
B B  B B B  B B B  B B B  B <!-- work the string from left to right with after
cleanup and a space at the end -->
B B  B B B  B B B  B B B  B <xsl:call-template name="recurse">
B B  B B B  B B B  B B B  B B B  B <xsl:with-param name="string"
select="normalize-space(.) || ' '"/>
B B  B B B  B B B  B B B  B B B  B <xsl:with-param name="count" select="0"/>
B B  B B B  B B B  B B B  B </xsl:call-template>
B B  B B B  B B B  B </wrapper>
B B  B B B  B </xsl:result-document>
B B  B </xsl:template>
B B  B <!-- takes one word at a time and if less that 35 characters so far
outputs it and adds the space back except at the end -->
B B  B <xsl:template name="recurse">
B B  B B B  B <xsl:param name="string" as="xs:string"/>
B B  B B B  B <xsl:param name="count" as="xs:integer"/>
B B  B B B  B <xsl:variable name="item" select="replace($string,
'(\S*)(\s)(.*)?', '$1')"/>
B B  B B B  B <xsl:choose>
B B  B B B  B B B  B <xsl:when test="$item != '' and $count +
string-length($item) + 1 le 35">
B B  B B B  B B B  B B B  B <xsl:value-of select="$item"/>
B B  B B B  B B B  B B B  B <xsl:if test="replace($string, '(\S*)(\s)(.*)?',
'$3') != ''">
B B  B B B  B B B  B B B  B B B  B <xsl:text> </xsl:text>
B B  B B B  B B B  B B B  B </xsl:if>
B B  B B B  B B B  B B B  B <xsl:call-template name="recurse">
B B  B B B  B B B  B B B  B B B  B <xsl:with-param name="string"
select="replace($string, '(\S*)(\s)(.*)?', '$3')"/>
B B  B B B  B B B  B B B  B B B  B <xsl:with-param name="count" select="$count
+ string-length($item) + 1"/>
B B  B B B  B B B  B B B  B </xsl:call-template>
B B  B B B  B B B  B </xsl:when>
B B  B B B  B B B  B <xsl:when test="$item != '' and $count +
string-length($item) + 1 gt 35">
B B  B B B  B B B  B B B  B <break/>
B B  B B B  B B B  B B B  B <xsl:call-template name="recurse">
B B  B B B  B B B  B B B  B B B  B <xsl:with-param name="string"
select="$string"/>
B B  B B B  B B B  B B B  B B B  B <xsl:with-param name="count" select="0"/>
B B  B B B  B B B  B B B  B </xsl:call-template>
B B  B B B  B B B  B </xsl:when>
B B  B B B  B </xsl:choose>
B B  B </xsl:template>
</xsl:stylesheet>



Terry






On Sunday, May 3, 2020, 11:24:29 AM EDT, Rick Quatro rick@xxxxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:






Thank you Michael. This is my first use of xsl:iterate and very useful in
understanding how it works. Here is my finished test stylesheet:
B 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
B B B  xmlns:xs="http://www.w3.org/2001/XMLSchema";
B B B  xmlns:rq="http://www.frameexpert.com";
B B B  exclude-result-prefixes="xs rq"
B B B  version="3.0" expand-text="yes">
B B B 
B B B B <xsl:output indent="yes"/>
B B B 
B B B B <xsl:param name="line" select="'Acceptable HAZMAT Items - Carry-On Or
Checked Bags - Passenger and Cargo Flights'"/>
B B B 
B B B B <xsl:template name="xsl:initial-template">
B B B B B B B  <xsl:message select="''"/>
B B B B B B B  <line>
B B B B B B B B B B B  <xsl:call-template name="line">
B B B B B B B B B B B B B B B  <xsl:with-param name="input" select="$line"/>
B B B B B B B B B B B B B B B  <xsl:with-param name="break-count" select="34"
as="xs:integer"/>
B B B B B B B B B B B  </xsl:call-template>
B B B B B B B  </line>
B B B  </xsl:template>B B B 
B B B B 
B B B B <xsl:template name="line">
B B B B B B B  <xsl:param name="input"/>
B B B B B B B  <xsl:param name="break-count" as="xs:integer"/>
B B B B B B B  <xsl:iterate select="tokenize($input)">
B B B B B B B B B B B  <xsl:param name="line-length" select="0"/>
B B B B B B B B B B B  <xsl:param name="break-count" select="$break-count"/>
B B B B B B B B B B B  <xsl:message select="position()=last()"></xsl:message>
B B B B B B B B B B B  <xsl:choose>
B B B B B B B B B B B B B B B  <xsl:when test="$line-length gt $break-count">
B B B B B B B B B B B B B B B B B B B  <break/>
B B B B B B B B B B B B B B B B B B B  <xsl:value-of
select="concat(.,if(position()!=last()) then ' ' else '')"/>
B B B B B B B B B B B B B B B B B B B  <xsl:next-iteration>
B B B B B B B B B B B B B B B B B B B B B B B  <xsl:with-param
name="line-length" select="string-length(.) + 1"/>
B B B B B B B B B B B B B B B B B B B B B B B  <xsl:with-param
name="break-count" select="$break-count - 5"/>
B B B B B B B B B B B B B B B B B B B  </xsl:next-iteration>
B B B B B B B B B B B B B B B  </xsl:when>
B B B B B B B  B B B B B B B B <xsl:otherwise>
B B B B B B B B B B B B B B B B B B B  <xsl:value-of
select="concat(.,if(position()!=last()) then ' ' else '')"/>
B B B B B B B B B B B B B B B B B B B  <xsl:next-iteration>
B B B B B B B B B B B B B B B B B B B B B B B  <xsl:with-param
name="line-length" select="$line-length + string-length(.) + 1"/>
B B B B B B B B B B B B B B B B B B B B B B B  <xsl:with-param
name="break-count" select="$break-count"/>
B B B B B B B B B B B B B B B B B B B  </xsl:next-iteration>
B B B B B B B B B B B B B B B  </xsl:otherwise>
B B B B B B B B B B B  </xsl:choose>
B B B B B B B  </xsl:iterate>
B B B  </xsl:template>
B B B 
</xsl:stylesheet>B B B 
B 
which gives me this:
B 
<?xml version="1.0" encoding="UTF-8"?>
<line>Acceptable HAZMAT Items - Carry-On <break/>Or Checked Bags - Passenger
and <break/>Cargo Flights</line>
B 
Thanks again!

-Rick

B B XSL-List info and archive
EasyUnsubscribe (by email)

Current Thread