home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / split.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2004-07-12  |  4.4 KB  |  158 lines

  1. <?xml version="1.0"?>
  2. <!--
  3.   Copyright 1999-2004 The Apache Software Foundation
  4.  
  5.   Licensed under the Apache License, Version 2.0 (the "License");
  6.   you may not use this file except in compliance with the License.
  7.   You may obtain a copy of the License at
  8.  
  9.       http://www.apache.org/licenses/LICENSE-2.0
  10.  
  11.   Unless required by applicable law or agreed to in writing, software
  12.   distributed under the License is distributed on an "AS IS" BASIS,
  13.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.   See the License for the specific language governing permissions and
  15.   limitations under the License.
  16. -->
  17. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  18.  
  19. <!--
  20.  This stylesheet was taken from the XSLT FAQ http://www.dpawson.co.uk/xsl/
  21.  
  22.  Comments and adaption to be used without normalize-space()
  23.   by Nicola Ken Barozzi nicolaken@apache.org
  24. --> 
  25.  
  26. <!--
  27.   Input:
  28.  
  29. <doc>
  30.  
  31. <para>
  32.  123456 2345 343434 545454 43434 343 
  33.  12345 343434 545454 43434 343 
  34.  32345645 343434 545454 43434 343 
  35.  3422222225 343434 545454 43434 343 
  36.  llllllllllllllllllllllooooooooooooooonnnnnnnnnnnggggggggg
  37.  345 343434 545454 43434 343 
  38. </para>
  39.  
  40. </doc>
  41.  
  42. Output:
  43.  
  44. <HTML>
  45. <BODY>
  46. <PRE>123456 2345 343434 545454 
  47. 43434 343 12345 343434 545454 
  48. 43434 343 32345645 343434 
  49. 545454 43434 343 3422222225 
  50. 343434 545454 43434 343 
  51. lllllllllllllllllllllloooooooo
  52. ooooooonnnnnnnnnnnggggggggg 
  53. 345 343434 545454 43434 
  54. 343
  55. </PRE>
  56. </BODY>
  57. </HTML>
  58.  
  59. Fragment ised: 
  60.  
  61.  <xsl:template match="/doc">
  62.  <HTML><BODY><PRE>
  63.     <xsl:call-template name="format">
  64.     <xsl:with-param select="normalize-space(para)" name="txt" /> 
  65.      <xsl:with-param name="width">30</xsl:with-param> 
  66.      </xsl:call-template>
  67.   </PRE></BODY></HTML>
  68.   </xsl:template>
  69.  
  70.  
  71. -->
  72.  
  73.  <xsl:template match="/body">
  74.    <body>
  75.     <xsl:call-template name="format">
  76.     <xsl:with-param select="source" name="txt" /> 
  77.      <xsl:with-param name="width">40</xsl:with-param> 
  78.      </xsl:call-template>
  79.   </body>
  80.   </xsl:template>
  81.   
  82.   <xsl:template name="format">
  83.     <xsl:param name="txt" /> 
  84.     <xsl:param name="width" /> 
  85.  
  86.     <!-- if there is still text left -->
  87.     <xsl:if test="$txt">
  88.     
  89.     <xsl:variable name = "pretxt" select = "substring($txt,0, $width)" />
  90.     
  91.     <xsl:choose>
  92.       <xsl:when test="contains($pretxt, ' ')">
  93.         <xsl:value-of select="substring-before($pretxt, ' ')"/>
  94.         <xsl:text> </xsl:text> 
  95.         <xsl:call-template name="format">
  96.           <xsl:with-param name="txt" select="substring-after($txt,' ')"/>
  97.           <xsl:with-param select="$width" name="width" />           
  98.         </xsl:call-template>
  99.       </xsl:when>
  100.       
  101.       <xsl:otherwise>
  102.       <!-- get the width at which to break-->
  103.       <xsl:variable name="real-width">
  104.         <xsl:call-template name="tune-width">
  105.           <xsl:with-param select="$txt" name="txt" /> 
  106.           <xsl:with-param select="$width" name="width" /> 
  107.           <xsl:with-param select="$width" name="def" /> 
  108.         </xsl:call-template>
  109.       </xsl:variable>
  110.  
  111.       <!-- output the first part of the broken string -->
  112.       <xsl:value-of select="substring($txt, 1, $real-width)" /> 
  113.  
  114.       <!-- output a newline -->
  115.       <xsl:text> </xsl:text> 
  116.  
  117.       <!-- call itself with the remaining part of the text -->
  118.       <xsl:call-template name="format">
  119.        <xsl:with-param select="substring($txt,$real-width + 1)" name="txt" /> 
  120.        <xsl:with-param select="$width" name="width" /> 
  121.       </xsl:call-template>
  122.       </xsl:otherwise>
  123.       </xsl:choose>
  124.     </xsl:if>
  125.   </xsl:template>
  126.   
  127.    
  128.   <!-- used by template "format", it calculates the width at the given def 
  129.        
  130.        It starts at def length and comes back till it finds a space -->
  131.   <xsl:template name="tune-width">
  132.     <xsl:param name="txt" /> 
  133.     <xsl:param name="width" /> 
  134.     <xsl:param name="def" /> 
  135.  
  136.     <xsl:choose>
  137.       <xsl:when test="$width = 0">
  138.        <xsl:value-of select="$def" /> 
  139.       </xsl:when>
  140.                  
  141.       <xsl:when test="substring($txt, $width, 1 ) = ' '">
  142.           <xsl:value-of select="$width" /> 
  143.        </xsl:when>
  144.        
  145.        <xsl:otherwise>
  146.          <!-- otherwise need to tune again, trying with $width - 1 -->
  147.          <xsl:call-template name="tune-width">
  148.            <xsl:with-param select="$width - 1" name="width" /> 
  149.            <xsl:with-param select="$txt" name="txt" /> 
  150.            <xsl:with-param select="$def" name="def" /> 
  151.          </xsl:call-template>
  152.        </xsl:otherwise>
  153.      </xsl:choose>
  154.  
  155.   </xsl:template>
  156.  
  157.   </xsl:stylesheet>
  158.