xsl-list
[Top] [All Lists]

Re: [xsl] Line break algorithm

2020-05-03 10:24:03
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:

 

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

    xmlns:xs="http://www.w3.org/2001/XMLSchema";

    xmlns:rq="http://www.frameexpert.com";

    exclude-result-prefixes="xs rq"

    version="3.0" expand-text="yes">

    

    <xsl:output indent="yes"/>

    

    <xsl:param name="line" select="'Acceptable HAZMAT Items - Carry-On Or
Checked Bags - Passenger and Cargo Flights'"/>

    

    <xsl:template name="xsl:initial-template">

        <xsl:message select="''"/>

        <line>

            <xsl:call-template name="line">

                <xsl:with-param name="input" select="$line"/>

                <xsl:with-param name="break-count" select="34"
as="xs:integer"/>

            </xsl:call-template>

        </line>

    </xsl:template>    

    

    <xsl:template name="line">

        <xsl:param name="input"/>

        <xsl:param name="break-count" as="xs:integer"/>

        <xsl:iterate select="tokenize($input)">

            <xsl:param name="line-length" select="0"/>

            <xsl:param name="break-count" select="$break-count"/>

            <xsl:message select="position()=last()"></xsl:message>

            <xsl:choose>

                <xsl:when test="$line-length gt $break-count">

                    <break/>

                    <xsl:value-of select="concat(.,if(position()!=last())
then ' ' else '')"/>

                    <xsl:next-iteration>

                        <xsl:with-param name="line-length"
select="string-length(.) + 1"/>

                        <xsl:with-param name="break-count"
select="$break-count - 5"/>

                    </xsl:next-iteration>

                </xsl:when>

                <xsl:otherwise>

                    <xsl:value-of select="concat(.,if(position()!=last())
then ' ' else '')"/>

                    <xsl:next-iteration>

                        <xsl:with-param name="line-length"
select="$line-length + string-length(.) + 1"/>

                        <xsl:with-param name="break-count"
select="$break-count"/>

                    </xsl:next-iteration>

                </xsl:otherwise>

            </xsl:choose>

        </xsl:iterate>

    </xsl:template>

    

</xsl:stylesheet>    

 

which gives me this:

 

<?xml version="1.0" encoding="UTF-8"?>

<line>Acceptable HAZMAT Items - Carry-On <break/>Or Checked Bags - Passenger
and <break/>Cargo Flights</line> 

 

Thanks again!

-Rick
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>