pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / site / src / documentation / skins / common / xslt / html / split.xsl
1 <?xml version="1.0"?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
3 <!--
4  This stylesheet was taken from the XSLT FAQ http://www.dpawson.co.uk/xsl/
5  
6  Comments and adaption to be used without normalize-space()
7   by forrest-dev@xml.apache.org
8 -->
9 <!--
10   Input:
11
12 <doc>
13
14 <para>
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 
21 </para>
22
23 </doc>
24
25 Output:
26
27 <HTML>
28 <BODY>
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 
37 343
38 </PRE>
39 </BODY>
40 </HTML>
41
42 Fragment ised: 
43
44  <xsl:template match="/doc">
45  <HTML><BODY><PRE>
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> 
49      </xsl:call-template>
50   </PRE></BODY></HTML>
51   </xsl:template>
52
53
54 -->
55   <xsl:template match="/body">
56     <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>
60       </xsl:call-template>
61     </body>
62   </xsl:template>
63   <xsl:template name="format">
64     <xsl:param name="txt" />
65     <xsl:param name="width" />
66 <!-- if there is still text left -->
67     <xsl:if test="$txt">
68       <xsl:variable name = "pretxt" select = "substring($txt,0, $width)" />
69       <xsl:choose>
70         <xsl:when test="contains($pretxt, '&#xA;')">
71           <xsl:value-of select="substring-before($pretxt, '&#xA;')"/>
72 <xsl:text>&#xA;</xsl:text>
73           <xsl:call-template name="format">
74             <xsl:with-param name="txt" select="substring-after($txt,'&#xA;')"/>
75             <xsl:with-param select="$width" name="width" />
76           </xsl:call-template>
77         </xsl:when>
78         <xsl:otherwise>
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" />
85             </xsl:call-template>
86           </xsl:variable>
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>&#xA;</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" />
95           </xsl:call-template>
96         </xsl:otherwise>
97       </xsl:choose>
98     </xsl:if>
99   </xsl:template>
100 <!-- used by template "format", it calculates the width at the given def 
101        
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" />
107     <xsl:choose>
108       <xsl:when test="$width = 0">
109         <xsl:value-of select="$def" />
110       </xsl:when>
111       <xsl:when test="substring($txt, $width, 1 ) = ' '">
112         <xsl:value-of select="$width" />
113       </xsl:when>
114       <xsl:otherwise>
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" />
120         </xsl:call-template>
121       </xsl:otherwise>
122     </xsl:choose>
123   </xsl:template>
124 </xsl:stylesheet>