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

Re: formatting lines to a fixed width.

Subject: Re: formatting lines to a fixed width.
From: Kamal Bhatt <kbhatt@xxxxxxxxx>
Date: Fri, 02 Dec 2005 15:00:37 +1100
select width
For anyone who cares, I found the solution. I have included it.

<xsl:template name="text-print">
   <xsl:param name="txt"/>
   <xsl:param name="width"/>
   <xsl:param name="title"/>
   <xsl:param name="first"/>
   <xsl:param name="text-pos"/>
   <xsl:param name="space-padding"/>

<xsl:param name="space-pad">
<xsl:call-template name="repeat-chars">
<xsl:with-param
name="rep-chars"><xsl:text>&space;</xsl:text></xsl:with-param>
<xsl:with-param name="times" select="$space-padding"/>
<xsl:with-param name="str"></xsl:with-param>
</xsl:call-template>
</xsl:param>
<xsl:if test="$txt">
<xsl:variable name="real-width">
<xsl:choose> <xsl:when test="contains(substring($txt, 1, $width), $newln)">
<xsl:call-template name="find-newline">
<xsl:with-param name="txt" select="$txt"/>
<xsl:with-param name="width">1</xsl:with-param>
<xsl:with-param name="def" select="$width - $space-padding"/>
</xsl:call-template> </xsl:when> <xsl:otherwise>
<xsl:call-template name="tune-width">
<xsl:with-param name="txt" select="$txt"/>
<xsl:with-param name="width" select="$width - $space-padding"/>
<xsl:with-param name="def" select="$width - $space-padding"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose> </xsl:variable>


<xsl:choose>
<xsl:when test="$first = 'true' and $text-pos = 1">
<!-- TEXT OUTPUT BEGINS -->
<xsl:value-of select="$title"/>:<xsl:text>&tab;</xsl:text>
<xsl:value-of select="$space-pad"/>
<xsl:value-of select="substring($txt, 1, $real-width)"/>
<xsl:if test="not(contains(substring($txt, $real-width, 1), $newln))"><xsl:text>&newln;</xsl:text></xsl:if>
<!-- TEXT OUTPUT ENDS -->
</xsl:when>
<xsl:otherwise>
<!-- TEXT OUTPUT BEGINS -->
<xsl:text>&tab;</xsl:text>
<xsl:value-of select="$space-pad"/>
<xsl:value-of select="substring($txt, 1, $real-width)"/>
<xsl:if test="not(contains(substring($txt, $real-width, 1), $newln))"><xsl:text>&newln;</xsl:text></xsl:if>
<!-- TEXT OUTPUT ENDS -->
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="text-print">
<xsl:with-param name="txt" select="substring($txt, $real-width + 1)"/>
<xsl:with-param select="$width" name="width"/>
<xsl:with-param name="first">false</xsl:with-param>
<xsl:with-param name="title" select="$title"/>
<xsl:with-param name="space-padding" select="$space-padding"/>
</xsl:call-template>
</xsl:if>
</xsl:template>


<!-- Finds the point at which a line should be cut. -->

<xsl:template name="tune-width">
   <xsl:param name="txt"/>
   <xsl:param name="width"/>
   <xsl:param name="def"/>

<xsl:variable name="newln"><xsl:text>&newln;</xsl:text></xsl:variable>

<xsl:variable name="space"><xsl:text>&space;</xsl:text></xsl:variable>
<xsl:if test="string-length($txt) &lt; $width">
<xsl:value-of select="string-length($txt)"/>
</xsl:if>


   <xsl:choose>
   <xsl:when test="$width = 0">
        <xsl:value-of select="$def"/>
   </xsl:when>
   <xsl:otherwise>
        <xsl:choose>
        <xsl:when test="substring($txt, $width, 1) = $space">
            <xsl:value-of select="$width"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:call-template name="tune-width">
             <xsl:with-param name="txt" select="$txt"/>
             <xsl:with-param name="width" select="$width - 1"/>
             <xsl:with-param name="def" select="$def"/>
            </xsl:call-template>
        </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="find-newline">
<xsl:param name="txt"/>
<xsl:param name="width"/>
<xsl:param name="def"/>
<xsl:if test="$def=$width">
<xsl:value-of select="$def"/>
</xsl:if> <xsl:choose>
<xsl:when test="string-length(substring($txt, $width, 1)) = 0 or substring($txt, $width, 1) = $newln">
<xsl:value-of select="$width"/>
</xsl:when> <xsl:otherwise>
<xsl:call-template name="find-newline">
<xsl:with-param name="txt" select="$txt"/>
<xsl:with-param name="width" select="$width + 1"/>
<xsl:with-param name="def" select="$def"/>
</xsl:call-template>
</xsl:otherwise> </xsl:choose>
</xsl:template>
Checking if we had reached the end of the string seemed to do the trick.


Cheers.

Kamal.

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.