1 <?xml version=
"1.0" encoding=
"ISO-8859-1"?> <!-- -*- sgml -*- -->
2 <xsl:stylesheet version=
"1.0"
3 xmlns:
xsl=
"http://www.w3.org/1999/XSL/Transform"
4 xmlns:
ext=
"http://exslt.org/common"
5 xmlns:
str-split2lines-func=
"f:str-split2lines-func"
6 exclude-result-prefixes=
"xsl ext str-split2lines-func">
9 <!-- how much do I love haskell ... -->
10 <xsl:template name=
"str-foldl">
11 <xsl:param name=
"pFunc" select=
"/.."/>
12 <xsl:param name=
"pA0"/>
13 <xsl:param name=
"pStr"/>
16 <xsl:when test=
"not(string($pStr))">
17 <xsl:copy-of select=
"$pA0"/>
20 <xsl:variable name=
"vFunResult">
21 <xsl:apply-templates select=
"$pFunc[1]">
22 <xsl:with-param name=
"arg0" select=
"$pFunc[position() > 1]"/>
23 <xsl:with-param name=
"arg1" select=
"$pA0"/>
24 <xsl:with-param name=
"arg2" select=
"substring($pStr,1,1)"/>
25 </xsl:apply-templates>
28 <xsl:call-template name=
"str-foldl">
29 <xsl:with-param name=
"pFunc" select=
"$pFunc"/>
30 <xsl:with-param name=
"pStr" select=
"substring($pStr,2)"/>
31 <xsl:with-param name=
"pA0" select=
"ext:node-set($vFunResult)"/>
39 <str-split2lines-func:str-split2lines-func/>
41 <xsl:template name=
"str-split-to-lines">
42 <xsl:param name=
"pStr"/>
43 <xsl:param name=
"pLineLength" select=
"60"/>
44 <xsl:param name=
"pDelimiters" select=
"' 
'"/>
45 <xsl:variable name=
"vsplit2linesFun"
46 select=
"document('')/*/str-split2lines-func:*[1]"/>
48 <xsl:variable name=
"vrtfParams">
49 <delimiters><xsl:value-of select=
"$pDelimiters"/></delimiters>
50 <lineLength><xsl:copy-of select=
"$pLineLength"/></lineLength>
53 <xsl:variable name=
"vResult">
54 <xsl:call-template name=
"str-foldl">
55 <xsl:with-param name=
"pFunc" select=
"$vsplit2linesFun"/>
56 <xsl:with-param name=
"pStr" select=
"$pStr"/>
58 <xsl:with-param name=
"pA0" select=
"ext:node-set($vrtfParams)"/>
62 <xsl:for-each select=
"ext:node-set($vResult)/line">
63 <xsl:for-each select=
"word">
64 <xsl:value-of select=
"concat(., ' ')"/>
66 <xsl:value-of select=
"'
'"/>
71 <xsl:template match=
"str-split2lines-func:*">
72 <xsl:param name=
"arg1" select=
"/.."/>
73 <xsl:param name=
"arg2"/>
75 <xsl:copy-of select=
"$arg1/*[position() < 3]"/>
76 <xsl:copy-of select=
"$arg1/line[position() != last()]"/>
79 <xsl:when test=
"contains($arg1/*[1], $arg2)">
80 <xsl:if test=
"string($arg1/word)">
81 <xsl:call-template name=
"fillLine">
82 <xsl:with-param name=
"pLine" select=
"$arg1/line[last()]"/>
83 <xsl:with-param name=
"pWord" select=
"$arg1/word"/>
84 <xsl:with-param name=
"pLineLength" select=
"$arg1/*[2]"/>
89 <xsl:copy-of select=
"$arg1/line[last()]"/>
90 <word><xsl:value-of select=
"concat($arg1/word, $arg2)"/></word>
96 <!-- This template recognises every new word and accumulates the result, -->
97 <!-- which is a list of 'line' elements, each having a list of 'word' -->
98 <!-- children. After the last 'line' element there's a single 'word', in -->
99 <!-- which the 'current word; is being accumulated. Whenever the current -->
100 <!-- character is one of the specified delimiters, this signals the -->
101 <!-- formation of a new word. This word is either added to the last line -->
102 <!-- (if the total line length will not exceed the specified line-length), -->
103 <!-- or a new line is started and this word becomes the 1st in the new line -->
104 <xsl:template name=
"fillLine">
105 <xsl:param name=
"pLine" select=
"/.."/>
106 <xsl:param name=
"pWord" select=
"/.."/>
107 <xsl:param name=
"pLineLength" />
109 <xsl:variable name=
"vnWordsInLine" select=
"count($pLine/word)"/>
110 <xsl:variable name=
"vLineLength"
111 select=
"string-length($pLine) + $vnWordsInLine"/>
114 <xsl:when test=
"not($vLineLength + string-length($pWord) > $pLineLength)">
116 <xsl:copy-of select=
"$pLine/*"/>
117 <xsl:copy-of select=
"$pWord"/>
121 <xsl:copy-of select=
"$pLine"/>
123 <xsl:copy-of select=
"$pWord"/>