source: clfs-embedded/BOOK/stylesheets/lfs-xsl/docbook-xsl-snapshot/lib/lib.xsl @ 9882b55

Last change on this file since 9882b55 was 9882b55, checked in by Manuel Canales Esparcia <manuel@…>, 17 years ago

Added the new lfs-xsl stylesheets.

  • Property mode set to 100644
File size: 20.3 KB
Line 
1<?xml version="1.0"?>
2
3<!-- ********************************************************************
4     $Id$
5     ********************************************************************
6
7     This file is part of the XSL DocBook Stylesheet distribution.
8     See ../README or http://docbook.sf.net/release/xsl/current/ for
9     copyright and other information.
10
11     This module implements DTD-independent functions
12
13     ******************************************************************** -->
14
15
16<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://icl.com/saxon" xmlns:dyn="http://exslt.org/dynamic" xmlns:src="http://nwalsh.com/xmlns/litprog/fragment" exclude-result-prefixes="src" version="1.0">
17
18<xsl:template name="dot.count">
19  <!-- Returns the number of "." characters in a string -->
20  <xsl:param name="string"/>
21  <xsl:param name="count" select="0"/>
22  <xsl:choose>
23    <xsl:when test="contains($string, '.')">
24      <xsl:call-template name="dot.count">
25        <xsl:with-param name="string" select="substring-after($string, '.')"/>
26        <xsl:with-param name="count" select="$count+1"/>
27      </xsl:call-template>
28    </xsl:when>
29    <xsl:otherwise>
30      <xsl:value-of select="$count"/>
31    </xsl:otherwise>
32  </xsl:choose>
33</xsl:template>
34<xsl:template name="copy-string">
35  <!-- returns 'count' copies of 'string' -->
36  <xsl:param name="string"/>
37  <xsl:param name="count" select="0"/>
38  <xsl:param name="result"/>
39
40  <xsl:choose>
41    <xsl:when test="$count&gt;0">
42      <xsl:call-template name="copy-string">
43        <xsl:with-param name="string" select="$string"/>
44        <xsl:with-param name="count" select="$count - 1"/>
45        <xsl:with-param name="result">
46          <xsl:value-of select="$result"/>
47          <xsl:value-of select="$string"/>
48        </xsl:with-param>
49      </xsl:call-template>
50    </xsl:when>
51    <xsl:otherwise>
52      <xsl:value-of select="$result"/>
53    </xsl:otherwise>
54  </xsl:choose>
55</xsl:template>
56<xsl:template name="string.subst">
57  <xsl:param name="string"/>
58  <xsl:param name="target"/>
59  <xsl:param name="replacement"/>
60
61  <xsl:choose>
62    <xsl:when test="contains($string, $target)">
63      <xsl:variable name="rest">
64        <xsl:call-template name="string.subst">
65          <xsl:with-param name="string" select="substring-after($string, $target)"/>
66          <xsl:with-param name="target" select="$target"/>
67          <xsl:with-param name="replacement" select="$replacement"/>
68        </xsl:call-template>
69      </xsl:variable>
70      <xsl:value-of select="concat(substring-before($string, $target),                                    $replacement,                                    $rest)"/>
71    </xsl:when>
72    <xsl:otherwise>
73      <xsl:value-of select="$string"/>
74    </xsl:otherwise>
75  </xsl:choose>
76</xsl:template>
77<xsl:template name="xpointer.idref">
78  <xsl:param name="xpointer">http://...</xsl:param>
79  <xsl:choose>
80    <xsl:when test="starts-with($xpointer, '#xpointer(id(')">
81      <xsl:variable name="rest" select="substring-after($xpointer, '#xpointer(id(')"/>
82      <xsl:variable name="quote" select="substring($rest, 1, 1)"/>
83      <xsl:value-of select="substring-before(substring-after($xpointer, $quote), $quote)"/>
84    </xsl:when>
85    <xsl:when test="starts-with($xpointer, '#')">
86      <xsl:value-of select="substring-after($xpointer, '#')"/>
87    </xsl:when>
88    <!-- otherwise it's a pointer to some other document -->
89  </xsl:choose>
90</xsl:template>
91<xsl:template name="length-magnitude">
92  <xsl:param name="length" select="'0pt'"/>
93
94  <xsl:choose>
95    <xsl:when test="string-length($length) = 0"/>
96    <xsl:when test="substring($length,1,1) = '0'                     or substring($length,1,1) = '1'                     or substring($length,1,1) = '2'                     or substring($length,1,1) = '3'                     or substring($length,1,1) = '4'                     or substring($length,1,1) = '5'                     or substring($length,1,1) = '6'                     or substring($length,1,1) = '7'                     or substring($length,1,1) = '8'                     or substring($length,1,1) = '9'                     or substring($length,1,1) = '.'">
97      <xsl:value-of select="substring($length,1,1)"/>
98      <xsl:call-template name="length-magnitude">
99        <xsl:with-param name="length" select="substring($length,2)"/>
100      </xsl:call-template>
101    </xsl:when>
102  </xsl:choose>
103</xsl:template>
104<xsl:template name="length-units">
105  <xsl:param name="length" select="'0pt'"/>
106  <xsl:param name="default.units" select="'px'"/>
107  <xsl:variable name="magnitude">
108    <xsl:call-template name="length-magnitude">
109      <xsl:with-param name="length" select="$length"/>
110    </xsl:call-template>
111  </xsl:variable>
112
113  <xsl:variable name="units">
114    <xsl:value-of select="substring($length, string-length($magnitude)+1)"/>
115  </xsl:variable>
116
117  <xsl:choose>
118    <xsl:when test="$units = ''">
119      <xsl:value-of select="$default.units"/>
120    </xsl:when>
121    <xsl:otherwise>
122      <xsl:value-of select="$units"/>
123    </xsl:otherwise>
124  </xsl:choose>
125</xsl:template>
126<xsl:template name="length-spec">
127  <xsl:param name="length" select="'0pt'"/>
128  <xsl:param name="default.units" select="'px'"/>
129
130  <xsl:variable name="magnitude">
131    <xsl:call-template name="length-magnitude">
132      <xsl:with-param name="length" select="$length"/>
133    </xsl:call-template>
134  </xsl:variable>
135
136  <xsl:variable name="units">
137    <xsl:value-of select="substring($length, string-length($magnitude)+1)"/>
138  </xsl:variable>
139
140  <xsl:value-of select="$magnitude"/>
141  <xsl:choose>
142    <xsl:when test="$units='cm'                     or $units='mm'                     or $units='in'                     or $units='pt'                     or $units='pc'                     or $units='px'                     or $units='em'">
143      <xsl:value-of select="$units"/>
144    </xsl:when>
145    <xsl:when test="$units = ''">
146      <xsl:value-of select="$default.units"/>
147    </xsl:when>
148    <xsl:otherwise>
149      <xsl:message>
150        <xsl:text>Unrecognized unit of measure: </xsl:text>
151        <xsl:value-of select="$units"/>
152        <xsl:text>.</xsl:text>
153      </xsl:message>
154    </xsl:otherwise>
155  </xsl:choose>
156</xsl:template>
157<xsl:template name="length-in-points">
158  <xsl:param name="length" select="'0pt'"/>
159  <xsl:param name="em.size" select="10"/>
160  <xsl:param name="pixels.per.inch" select="90"/>
161
162  <xsl:variable name="magnitude">
163    <xsl:call-template name="length-magnitude">
164      <xsl:with-param name="length" select="$length"/>
165    </xsl:call-template>
166  </xsl:variable>
167
168  <xsl:variable name="units">
169    <xsl:value-of select="substring($length, string-length($magnitude)+1)"/>
170  </xsl:variable>
171
172  <xsl:choose>
173    <xsl:when test="$units = 'pt'">
174      <xsl:value-of select="$magnitude"/>
175    </xsl:when>
176    <xsl:when test="$units = 'cm'">
177      <xsl:value-of select="$magnitude div 2.54 * 72.0"/>
178    </xsl:when>
179    <xsl:when test="$units = 'mm'">
180      <xsl:value-of select="$magnitude div 25.4 * 72.0"/>
181    </xsl:when>
182    <xsl:when test="$units = 'in'">
183      <xsl:value-of select="$magnitude * 72.0"/>
184    </xsl:when>
185    <xsl:when test="$units = 'pc'">
186      <xsl:value-of select="$magnitude * 12.0"/>
187    </xsl:when>
188    <xsl:when test="$units = 'px'">
189      <xsl:value-of select="$magnitude div $pixels.per.inch * 72.0"/>
190    </xsl:when>
191    <xsl:when test="$units = 'em'">
192      <xsl:value-of select="$magnitude * $em.size"/>
193    </xsl:when>
194    <xsl:otherwise>
195      <xsl:message>
196        <xsl:text>Unrecognized unit of measure: </xsl:text>
197        <xsl:value-of select="$units"/>
198        <xsl:text>.</xsl:text>
199      </xsl:message>
200    </xsl:otherwise>
201  </xsl:choose>
202</xsl:template>
203<xsl:template name="pi-attribute">
204  <xsl:param name="pis" select="processing-instruction('BOGUS_PI')"/>
205  <xsl:param name="attribute">filename</xsl:param>
206  <xsl:param name="count">1</xsl:param>
207
208  <xsl:choose>
209    <xsl:when test="$count&gt;count($pis)">
210      <!-- not found -->
211    </xsl:when>
212    <xsl:otherwise>
213      <xsl:variable name="pi">
214        <xsl:value-of select="$pis[$count]"/>
215      </xsl:variable>
216      <xsl:variable name="pivalue">
217        <xsl:value-of select="concat(' ', normalize-space($pi))"/>
218      </xsl:variable>
219      <xsl:choose>
220        <xsl:when test="contains($pivalue,concat(' ', $attribute, '='))">
221          <xsl:variable name="rest" select="substring-after($pivalue,concat(' ', $attribute,'='))"/>
222          <xsl:variable name="quote" select="substring($rest,1,1)"/>
223          <xsl:value-of select="substring-before(substring($rest,2),$quote)"/>
224        </xsl:when>
225        <xsl:otherwise>
226          <xsl:call-template name="pi-attribute">
227            <xsl:with-param name="pis" select="$pis"/>
228            <xsl:with-param name="attribute" select="$attribute"/>
229            <xsl:with-param name="count" select="$count + 1"/>
230          </xsl:call-template>
231        </xsl:otherwise>
232      </xsl:choose>
233    </xsl:otherwise>
234  </xsl:choose>
235</xsl:template>
236<xsl:template name="lookup.key">
237  <xsl:param name="key" select="''"/>
238  <xsl:param name="table" select="''"/>
239
240  <xsl:if test="contains($table, ' ')">
241    <xsl:choose>
242      <xsl:when test="substring-before($table, ' ') = $key">
243        <xsl:variable name="rest" select="substring-after($table, ' ')"/>
244        <xsl:choose>
245          <xsl:when test="contains($rest, ' ')">
246            <xsl:value-of select="substring-before($rest, ' ')"/>
247          </xsl:when>
248          <xsl:otherwise>
249            <xsl:value-of select="$rest"/>
250          </xsl:otherwise>
251        </xsl:choose>
252      </xsl:when>
253      <xsl:otherwise>
254        <xsl:call-template name="lookup.key">
255          <xsl:with-param name="key" select="$key"/>
256          <xsl:with-param name="table" select="substring-after(substring-after($table,' '), ' ')"/>
257        </xsl:call-template>
258      </xsl:otherwise>
259    </xsl:choose>
260  </xsl:if>
261</xsl:template>
262<xsl:template name="xpath.location">
263  <xsl:param name="node" select="."/>
264  <xsl:param name="path" select="''"/>
265
266  <xsl:variable name="next.path">
267    <xsl:value-of select="local-name($node)"/>
268    <xsl:if test="$path != ''">/</xsl:if>
269    <xsl:value-of select="$path"/>
270  </xsl:variable>
271
272  <xsl:choose>
273    <xsl:when test="$node/parent::*">
274      <xsl:call-template name="xpath.location">
275        <xsl:with-param name="node" select="$node/parent::*"/>
276        <xsl:with-param name="path" select="$next.path"/>
277      </xsl:call-template>
278    </xsl:when>
279    <xsl:otherwise>
280      <xsl:text>/</xsl:text>
281      <xsl:value-of select="$next.path"/>
282    </xsl:otherwise>
283  </xsl:choose>
284</xsl:template>
285<xsl:template name="comment-escape-string">
286  <xsl:param name="string" select="''"/>
287
288  <xsl:if test="starts-with($string, '-')">
289    <xsl:text> </xsl:text>
290  </xsl:if>
291
292  <xsl:call-template name="comment-escape-string.recursive">
293    <xsl:with-param name="string" select="$string"/>
294  </xsl:call-template>
295
296  <xsl:if test="substring($string, string-length($string), 1) = '-'">
297    <xsl:text> </xsl:text>
298  </xsl:if>
299</xsl:template>
300<xsl:template name="comment-escape-string.recursive">
301  <xsl:param name="string" select="''"/>
302  <xsl:choose>
303    <xsl:when test="contains($string, '--')">
304      <xsl:value-of select="substring-before($string, '--')"/>
305      <xsl:value-of select="'- -'"/>
306      <xsl:call-template name="comment-escape-string.recursive">
307        <xsl:with-param name="string" select="substring-after($string, '--')"/>
308      </xsl:call-template>
309    </xsl:when>
310    <xsl:otherwise>
311      <xsl:value-of select="$string"/>
312    </xsl:otherwise>
313  </xsl:choose>
314</xsl:template>
315
316  <xsl:template name="str.tokenize.keep.delimiters">
317    <xsl:param name="string" select="''"/>
318    <xsl:param name="delimiters" select="' '"/>
319    <xsl:choose>
320      <xsl:when test="not($string)"/>
321      <xsl:when test="not($delimiters)">
322        <xsl:call-template name="str.tokenize.keep.delimiters-characters">
323          <xsl:with-param name="string" select="$string"/>
324        </xsl:call-template>
325      </xsl:when>
326      <xsl:otherwise>
327        <xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
328          <xsl:with-param name="string" select="$string"/>
329          <xsl:with-param name="delimiters" select="$delimiters"/>
330        </xsl:call-template>
331      </xsl:otherwise>
332    </xsl:choose>
333  </xsl:template>
334 
335  <xsl:template name="str.tokenize.keep.delimiters-characters">
336    <xsl:param name="string"/>
337    <xsl:if test="$string">
338      <token><xsl:value-of select="substring($string, 1, 1)"/></token>
339      <xsl:call-template name="str.tokenize.keep.delimiters-characters">
340        <xsl:with-param name="string" select="substring($string, 2)"/>
341      </xsl:call-template>
342    </xsl:if>
343  </xsl:template>
344 
345  <xsl:template name="str.tokenize.keep.delimiters-delimiters">
346    <xsl:param name="string"/>
347    <xsl:param name="delimiters"/>
348    <xsl:variable name="delimiter" select="substring($delimiters, 1, 1)"/>
349    <xsl:choose>
350      <xsl:when test="not($delimiter)">
351        <token><xsl:value-of select="$string"/></token>
352      </xsl:when>
353      <xsl:when test="contains($string, $delimiter)">
354        <xsl:if test="not(starts-with($string, $delimiter))">
355          <xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
356            <xsl:with-param name="string" select="substring-before($string, $delimiter)"/>
357            <xsl:with-param name="delimiters" select="substring($delimiters, 2)"/>
358          </xsl:call-template>
359        </xsl:if>
360        <!-- output each delimiter -->
361        <xsl:value-of select="$delimiter"/>
362        <xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
363          <xsl:with-param name="string" select="substring-after($string, $delimiter)"/>
364          <xsl:with-param name="delimiters" select="$delimiters"/>
365        </xsl:call-template>
366      </xsl:when>
367      <xsl:otherwise>
368        <xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
369          <xsl:with-param name="string" select="$string"/>
370          <xsl:with-param name="delimiters" select="substring($delimiters, 2)"/>
371        </xsl:call-template>
372      </xsl:otherwise>
373    </xsl:choose>
374  </xsl:template>
375    <xsl:template name="apply-string-subst-map">
376      <xsl:param name="content"/>
377      <xsl:param name="map.contents"/>
378      <xsl:variable name="replaced_text">
379        <xsl:call-template name="string.subst">
380          <xsl:with-param name="string" select="$content"/>
381          <xsl:with-param name="target" select="$map.contents[1]/@oldstring"/>
382          <xsl:with-param name="replacement" select="$map.contents[1]/@newstring"/>
383        </xsl:call-template>
384      </xsl:variable>
385      <xsl:choose>
386        <xsl:when test="$map.contents[2]">
387          <xsl:call-template name="apply-string-subst-map">
388            <xsl:with-param name="content" select="$replaced_text"/>
389            <xsl:with-param name="map.contents" select="$map.contents[position() &gt; 1]"/>
390          </xsl:call-template>
391        </xsl:when>
392        <xsl:otherwise>
393          <xsl:value-of select="$replaced_text"/>
394        </xsl:otherwise>
395      </xsl:choose>
396    </xsl:template>
397
398 
399    <xsl:template name="apply-character-map">
400      <xsl:param name="content"/>
401      <xsl:param name="map.contents"/>
402      <xsl:variable name="replaced_text">
403        <xsl:call-template name="string.subst">
404          <xsl:with-param name="string" select="$content"/>
405          <xsl:with-param name="target" select="$map.contents[1]/@character"/>
406          <xsl:with-param name="replacement" select="$map.contents[1]/@string"/>
407        </xsl:call-template>
408      </xsl:variable>
409      <xsl:choose>
410        <xsl:when test="$map.contents[2]">
411          <xsl:call-template name="apply-character-map">
412            <xsl:with-param name="content" select="$replaced_text"/>
413            <xsl:with-param name="map.contents" select="$map.contents[position() &gt; 1]"/>
414          </xsl:call-template>
415        </xsl:when>
416        <xsl:otherwise>
417          <xsl:value-of select="$replaced_text"/>
418        </xsl:otherwise>
419      </xsl:choose>
420    </xsl:template>
421
422 
423  <xsl:template name="read-character-map">
424    <xsl:param name="use.subset"/>
425    <xsl:param name="subset.profile"/>
426    <xsl:param name="uri"/>
427    <xsl:choose>
428      <xsl:when test="$use.subset != 0">
429        <!-- use a subset of the character map instead of the full map -->
430        <xsl:choose>
431          <!-- xsltproc and Xalan both support dyn:evaluate() -->
432          <xsl:when test="function-available('dyn:evaluate')">
433            <xsl:copy-of select="document($uri)//*[local-name()='output-character']                                  [dyn:evaluate($subset.profile)]"/>
434          </xsl:when>
435          <!-- Saxon has its own evaluate() & doesn't support dyn:evaluate() -->
436          <xsl:when test="function-available('saxon:evaluate')">
437            <xsl:copy-of select="document($uri)//*[local-name()='output-character']                                  [saxon:evaluate($subset.profile)]"/>
438          </xsl:when>
439          <xsl:otherwise>
440            <xsl:message terminate="yes">
441Error: To process character-map subsets, you must use an XSLT engine
442that supports the evaluate() XSLT extension function. Your XSLT engine
443does not support it.
444</xsl:message>
445          </xsl:otherwise>
446        </xsl:choose>
447        </xsl:when>
448        <xsl:otherwise>
449          <!-- value of $use.subset is non-zero, so use the full map -->
450        <xsl:copy-of select="document($uri)//*[local-name()='output-character']"/>
451      </xsl:otherwise>
452    </xsl:choose>
453  </xsl:template>
454<xsl:template name="count.uri.path.depth">
455  <xsl:param name="filename" select="''"/>
456  <xsl:param name="count" select="0"/>
457
458  <xsl:choose>
459    <xsl:when test="contains($filename, '/')">
460      <xsl:call-template name="count.uri.path.depth">
461        <xsl:with-param name="filename" select="substring-after($filename, '/')"/>
462        <xsl:with-param name="count" select="$count + 1"/>
463      </xsl:call-template>
464    </xsl:when>
465    <xsl:otherwise>
466      <xsl:value-of select="$count"/>
467    </xsl:otherwise>
468  </xsl:choose>
469</xsl:template>
470<xsl:template name="trim.common.uri.paths">
471  <xsl:param name="uriA" select="''"/>
472  <xsl:param name="uriB" select="''"/>
473  <xsl:param name="return" select="'A'"/>
474
475  <xsl:choose>
476    <xsl:when test="contains($uriA, '/') and contains($uriB, '/')                     and substring-before($uriA, '/') = substring-before($uriB, '/')">
477      <xsl:call-template name="trim.common.uri.paths">
478        <xsl:with-param name="uriA" select="substring-after($uriA, '/')"/>
479        <xsl:with-param name="uriB" select="substring-after($uriB, '/')"/>
480        <xsl:with-param name="return" select="$return"/>
481      </xsl:call-template>
482    </xsl:when>
483    <xsl:otherwise>
484      <xsl:choose>
485        <xsl:when test="$return = 'A'">
486          <xsl:value-of select="$uriA"/>
487        </xsl:when>
488        <xsl:otherwise>
489          <xsl:value-of select="$uriB"/>
490        </xsl:otherwise>
491      </xsl:choose>
492    </xsl:otherwise>
493  </xsl:choose>
494</xsl:template>
495
496  <xsl:template name="trim.text">
497    <xsl:param name="contents" select="."/>
498    <xsl:variable name="contents-left-trimmed">
499      <xsl:call-template name="trim-left">
500        <xsl:with-param name="contents" select="$contents"/>
501      </xsl:call-template>
502    </xsl:variable>
503    <xsl:variable name="contents-trimmed">
504      <xsl:call-template name="trim-right">
505        <xsl:with-param name="contents" select="$contents-left-trimmed"/>
506      </xsl:call-template>
507    </xsl:variable>
508    <xsl:value-of select="$contents-trimmed"/>
509  </xsl:template>
510
511  <xsl:template name="trim-left">
512    <xsl:param name="contents"/>
513    <xsl:choose>
514      <xsl:when test="starts-with($contents,'&#10;') or                       starts-with($contents,'&#13;') or                       starts-with($contents,' ') or                       starts-with($contents,'&#9;')">
515        <xsl:call-template name="trim-left">
516          <xsl:with-param name="contents" select="substring($contents, 2)"/>
517        </xsl:call-template>
518      </xsl:when>
519      <xsl:otherwise>
520        <xsl:value-of select="$contents"/>
521      </xsl:otherwise>
522    </xsl:choose>
523  </xsl:template>
524
525  <xsl:template name="trim-right">
526    <xsl:param name="contents"/>
527    <xsl:variable name="last-char">
528      <xsl:value-of select="substring($contents, string-length($contents), 1)"/>
529    </xsl:variable>
530    <xsl:choose>
531      <xsl:when test="($last-char = '&#10;') or                       ($last-char = '&#13;') or                       ($last-char = ' ') or                       ($last-char = '&#9;')">
532        <xsl:call-template name="trim-right">
533          <xsl:with-param name="contents" select="substring($contents, 1, string-length($contents) - 1)"/>
534        </xsl:call-template>
535      </xsl:when>
536      <xsl:otherwise>
537        <xsl:value-of select="$contents"/>
538      </xsl:otherwise>
539    </xsl:choose>
540  </xsl:template>
541
542</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.