2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 
   4  This stylesheet was taken from the XSLT FAQ http://www.dpawson.co.uk/xsl/
 
   6  Comments and adaption to be used without normalize-space()
 
   7   by forrest-dev@xml.apache.org
 
  15  123456 2345 343434 545454 43434 343 
 
  16  12345 343434 545454 43434 343 
 
  17  32345645 343434 545454 43434 343 
 
  18  3422222225 343434 545454 43434 343 
 
  19  llllllllllllllllllllllooooooooooooooonnnnnnnnnnnggggggggg
 
  20  345 343434 545454 43434 343 
 
  29 <PRE>123456 2345 343434 545454 
 
  30 43434 343 12345 343434 545454 
 
  31 43434 343 32345645 343434 
 
  32 545454 43434 343 3422222225 
 
  33 343434 545454 43434 343 
 
  34 lllllllllllllllllllllloooooooo
 
  35 ooooooonnnnnnnnnnnggggggggg 
 
  36 345 343434 545454 43434 
 
  44  <xsl:template match="/doc">
 
  46     <xsl:call-template name="format">
 
  47     <xsl:with-param select="normalize-space(para)" name="txt" /> 
 
  48      <xsl:with-param name="width">30</xsl:with-param> 
 
  55   <xsl:template match="/body">
 
  57       <xsl:call-template name="format">
 
  58         <xsl:with-param select="source" name="txt" />
 
  59         <xsl:with-param name="width">40</xsl:with-param>
 
  63   <xsl:template name="format">
 
  64     <xsl:param name="txt" />
 
  65     <xsl:param name="width" />
 
  66 <!-- if there is still text left -->
 
  68       <xsl:variable name = "pretxt" select = "substring($txt,0, $width)" />
 
  70         <xsl:when test="contains($pretxt, '
')">
 
  71           <xsl:value-of select="substring-before($pretxt, '
')"/>
 
  72 <xsl:text>
</xsl:text>
 
  73           <xsl:call-template name="format">
 
  74             <xsl:with-param name="txt" select="substring-after($txt,'
')"/>
 
  75             <xsl:with-param select="$width" name="width" />
 
  79 <!-- get the width at which to break-->
 
  80           <xsl:variable name="real-width">
 
  81             <xsl:call-template name="tune-width">
 
  82               <xsl:with-param select="$txt" name="txt" />
 
  83               <xsl:with-param select="$width" name="width" />
 
  84               <xsl:with-param select="$width" name="def" />
 
  87 <!-- output the first part of the broken string -->
 
  88           <xsl:value-of select="substring($txt, 1, $real-width)" />
 
  89 <!-- output a newline -->
 
  90 <xsl:text>
</xsl:text>
 
  91 <!-- call itself with the remaining part of the text -->
 
  92           <xsl:call-template name="format">
 
  93             <xsl:with-param select="substring($txt,$real-width + 1)" name="txt" />
 
  94             <xsl:with-param select="$width" name="width" />
 
 100 <!-- used by template "format", it calculates the width at the given def 
 
 102        It starts at def length and comes back till it finds a space -->
 
 103   <xsl:template name="tune-width">
 
 104     <xsl:param name="txt" />
 
 105     <xsl:param name="width" />
 
 106     <xsl:param name="def" />
 
 108       <xsl:when test="$width = 0">
 
 109         <xsl:value-of select="$def" />
 
 111       <xsl:when test="substring($txt, $width, 1 ) = ' '">
 
 112         <xsl:value-of select="$width" />
 
 115 <!-- otherwise need to tune again, trying with $width - 1 -->
 
 116         <xsl:call-template name="tune-width">
 
 117           <xsl:with-param select="$width - 1" name="width" />
 
 118           <xsl:with-param select="$txt" name="txt" />
 
 119           <xsl:with-param select="$def" name="def" />