Re: Line Numbering

Subject: Re: Line Numbering
From: Paul Tchistopolskii <paul@xxxxxxx>
Date: Mon, 14 Aug 2000 20:00:56 -0700
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>

> Note that this doesn't take account of the position of spaces in your
> string at all, although it would probably be possible to make it do so in
> some rudimentary way.

To me that  was not rudimentary ( taking into account that there could 
be exotic situations when some long word is really > 60, so 
space-scanning could fail  ). Anyway.

The snippet below takes  into account the whitespace 
not splitting the word in the middle.  It looks back to the first space
to prevent that. if it fails - it just splits.  tune-width does it.

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

 <xsl:template match="/doc">
 <HTML><BODY><PRE>
    <xsl:call-template name="format">
    <xsl:with-param select="normalize-space(para)" name="txt" /> 
     <xsl:with-param name="width">30</xsl:with-param> 
     </xsl:call-template>
  </PRE></BODY></HTML>
  </xsl:template>

  <xsl:template name="format">
   <xsl:param name="txt" /> 
   <xsl:param name="width" /> 

  <xsl:if test="$txt">
   <xsl:variable name="real-width">
    <xsl:call-template name="tune-width">
     <xsl:with-param select="$txt" name="txt" /> 
       <xsl:with-param select="$width" name="width" /> 
       <xsl:with-param select="$width" name="def" /> 
  </xsl:call-template>
   </xsl:variable>

   <xsl:value-of select="substring($txt, 1, $real-width)" /> 

<xsl:text>
</xsl:text> 

   <xsl:call-template name="format">
    <xsl:with-param select="substring($txt,$real-width + 1)" name="txt" /> 
    <xsl:with-param select="$width" name="width" /> 
   </xsl:call-template>

  </xsl:if>
  </xsl:template>


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

  <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 ) = ' '">
  <xsl:value-of select="$width" /> 
  </xsl:when>
  <xsl:otherwise>
  <xsl:call-template name="tune-width">
  <xsl:with-param select="$txt" name="txt" /> 
  <xsl:with-param select="$width - 1" name="width" /> 
  <xsl:with-param select="$def" name="def" /> 
  </xsl:call-template>
  </xsl:otherwise>
  </xsl:choose>
  </xsl:otherwise>
  </xsl:choose>
  </xsl:template>

  </xsl:stylesheet>

Input:

<doc>

<para>
 123456 2345 343434 545454 43434 343 
 12345 343434 545454 43434 343 
 32345645 343434 545454 43434 343 
 3422222225 343434 545454 43434 343 
 llllllllllllllllllllllooooooooooooooonnnnnnnnnnnggggggggg
 345 343434 545454 43434 343 
</para>

</doc>

Output:

<HTML>
<BODY>
<PRE>123456 2345 343434 545454 
43434 343 12345 343434 545454 
43434 343 32345645 343434 
545454 43434 343 3422222225 
343434 545454 43434 343 
lllllllllllllllllllllloooooooo
ooooooonnnnnnnnnnnggggggggg 
345 343434 545454 43434 
343
</PRE>
</BODY>
</HTML>


Rgds.Paul.

PS. I'm also attaching the XSLScript source of this 
snippet, because I doubt the snippet is in any way 
readable ( even the logic is really simple ). I promize 
not to attach the XSLScript notations of the trivial 
code, but I think this is not the case here. Sorry, if 
I'm wrong and people can easily read and write 
netsed when's. I can not.

X:stylesheet {

X:template = "/doc" {
 <HTML><BODY><PRE>
 <% !format( txt="normalize-space(para)", width={30} ) %>
 </PRE></BODY></HTML>  
}

X:template format( txt, width ) {

     <% X:if "$txt" {
          <% X:var real-width = {
               <% !tune-width( txt="$txt", width="$width", def="$width" ) %>
          } %>
 
          <% {substring($txt, 1, $real-width)} %>

<% X:text {
}%>

          <% !format( txt="substring($txt,$real-width + 1)",width="$width" ) %>
     } %>
}

X:template tune-width( txt, width, def ) {
     <% X:if "$width = 0" {
          <% {$def} %>
      } else {
          <% X:if "substring($txt, $width, 1 ) = ' '" {
                <% {$width} %>
          } else {
                <%!tune-width(txt="$txt",width="$width - 1",def="$def")%>
          } %>
     } %>
}

}




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread