1 <?xml version="1.0" encoding="UTF-8"?><!-- ********************************************************************
3 This file is part of the XSL DocBook Stylesheet distribution.
4 See ../README or http://cdn.docbook.org/release/xsl/current/ for
5 copyright and other information.
7 This module implements DTD-independent functions
9 ******************************************************************** --><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
11 <xsl:template name="dot.count">
12 <!-- Returns the number of "." characters in a string -->
13 <xsl:param name="string"/>
14 <xsl:param name="count" select="0"/>
16 <xsl:when test="contains($string, '.')">
17 <xsl:call-template name="dot.count">
18 <xsl:with-param name="string" select="substring-after($string, '.')"/>
19 <xsl:with-param name="count" select="$count+1"/>
23 <xsl:value-of select="$count"/>
27 <xsl:template name="copy-string">
28 <!-- returns 'count' copies of 'string' -->
29 <xsl:param name="string"/>
30 <xsl:param name="count" select="0"/>
31 <xsl:param name="result"/>
34 <xsl:when test="$count>0">
35 <xsl:call-template name="copy-string">
36 <xsl:with-param name="string" select="$string"/>
37 <xsl:with-param name="count" select="$count - 1"/>
38 <xsl:with-param name="result">
39 <xsl:value-of select="$result"/>
40 <xsl:value-of select="$string"/>
45 <xsl:value-of select="$result"/>
49 <xsl:template name="string.subst">
50 <xsl:param name="string"/>
51 <xsl:param name="target"/>
52 <xsl:param name="replacement"/>
55 <xsl:when test="contains($string, $target)">
56 <xsl:variable name="rest">
57 <xsl:call-template name="string.subst">
58 <xsl:with-param name="string" select="substring-after($string, $target)"/>
59 <xsl:with-param name="target" select="$target"/>
60 <xsl:with-param name="replacement" select="$replacement"/>
63 <xsl:value-of select="concat(substring-before($string, $target), $replacement, $rest)"/>
66 <xsl:value-of select="$string"/>
70 <xsl:template name="xpointer.idref">
71 <xsl:param name="xpointer">http://...</xsl:param>
73 <xsl:when test="starts-with($xpointer, '#xpointer(id(')">
74 <xsl:variable name="rest" select="substring-after($xpointer, '#xpointer(id(')"/>
75 <xsl:variable name="quote" select="substring($rest, 1, 1)"/>
76 <xsl:value-of select="substring-before(substring-after($xpointer, $quote), $quote)"/>
78 <xsl:when test="starts-with($xpointer, '#')">
79 <xsl:value-of select="substring-after($xpointer, '#')"/>
81 <!-- otherwise it's a pointer to some other document -->
84 <xsl:template name="length-magnitude">
85 <xsl:param name="length" select="'0pt'"/>
88 <xsl:when test="string-length($length) = 0"/>
89 <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) = '.'">
90 <xsl:value-of select="substring($length,1,1)"/>
91 <xsl:call-template name="length-magnitude">
92 <xsl:with-param name="length" select="substring($length,2)"/>
97 <xsl:template name="length-units">
98 <xsl:param name="length" select="'0pt'"/>
99 <xsl:param name="default.units" select="'px'"/>
100 <xsl:variable name="magnitude">
101 <xsl:call-template name="length-magnitude">
102 <xsl:with-param name="length" select="$length"/>
106 <xsl:variable name="units">
107 <xsl:value-of select="substring($length, string-length($magnitude)+1)"/>
111 <xsl:when test="$units = ''">
112 <xsl:value-of select="$default.units"/>
115 <xsl:value-of select="$units"/>
119 <xsl:template name="length-spec">
120 <xsl:param name="length" select="'0pt'"/>
121 <xsl:param name="default.units" select="'px'"/>
123 <xsl:variable name="magnitude">
124 <xsl:call-template name="length-magnitude">
125 <xsl:with-param name="length" select="$length"/>
129 <xsl:variable name="units">
130 <xsl:value-of select="substring($length, string-length($magnitude)+1)"/>
133 <xsl:value-of select="$magnitude"/>
135 <xsl:when test="$units='cm' or $units='mm' or $units='in' or $units='pt' or $units='pc' or $units='px' or $units='em'">
136 <xsl:value-of select="$units"/>
138 <xsl:when test="$units = ''">
139 <xsl:value-of select="$default.units"/>
143 <xsl:text>Unrecognized unit of measure: </xsl:text>
144 <xsl:value-of select="$units"/>
145 <xsl:text>.</xsl:text>
150 <xsl:template name="length-in-points">
151 <xsl:param name="length" select="'0pt'"/>
152 <xsl:param name="em.size" select="10"/>
153 <xsl:param name="pixels.per.inch" select="90"/>
155 <xsl:variable name="magnitude">
156 <xsl:call-template name="length-magnitude">
157 <xsl:with-param name="length" select="$length"/>
161 <xsl:variable name="units">
162 <xsl:value-of select="substring($length, string-length($magnitude)+1)"/>
166 <xsl:when test="$units = 'pt'">
167 <xsl:value-of select="$magnitude"/>
169 <xsl:when test="$units = 'cm'">
170 <xsl:value-of select="$magnitude div 2.54 * 72.0"/>
172 <xsl:when test="$units = 'mm'">
173 <xsl:value-of select="$magnitude div 25.4 * 72.0"/>
175 <xsl:when test="$units = 'in'">
176 <xsl:value-of select="$magnitude * 72.0"/>
178 <xsl:when test="$units = 'pc'">
179 <xsl:value-of select="$magnitude * 12.0"/>
181 <xsl:when test="$units = 'px'">
182 <xsl:value-of select="$magnitude div $pixels.per.inch * 72.0"/>
184 <xsl:when test="$units = 'em'">
185 <xsl:value-of select="$magnitude * $em.size"/>
189 <xsl:text>Unrecognized unit of measure: </xsl:text>
190 <xsl:value-of select="$units"/>
191 <xsl:text>.</xsl:text>
196 <xsl:template name="pi-attribute">
197 <xsl:param name="pis" select="processing-instruction('BOGUS_PI')"/>
198 <xsl:param name="attribute">filename</xsl:param>
199 <xsl:param name="count">1</xsl:param>
202 <xsl:when test="$count>count($pis)">
206 <xsl:variable name="pi">
207 <xsl:value-of select="$pis[$count]"/>
209 <xsl:variable name="pivalue">
210 <xsl:value-of select="concat(' ', normalize-space($pi))"/>
213 <xsl:when test="contains($pivalue,concat(' ', $attribute, '='))">
214 <xsl:variable name="rest" select="substring-after($pivalue,concat(' ', $attribute,'='))"/>
215 <xsl:variable name="quote" select="substring($rest,1,1)"/>
216 <xsl:value-of select="substring-before(substring($rest,2),$quote)"/>
219 <xsl:call-template name="pi-attribute">
220 <xsl:with-param name="pis" select="$pis"/>
221 <xsl:with-param name="attribute" select="$attribute"/>
222 <xsl:with-param name="count" select="$count + 1"/>
229 <xsl:template name="lookup.key">
230 <xsl:param name="key" select="''"/>
231 <xsl:param name="table" select="''"/>
233 <xsl:if test="contains($table, ' ')">
235 <xsl:when test="substring-before($table, ' ') = $key">
236 <xsl:variable name="rest" select="substring-after($table, ' ')"/>
238 <xsl:when test="contains($rest, ' ')">
239 <xsl:value-of select="substring-before($rest, ' ')"/>
242 <xsl:value-of select="$rest"/>
247 <xsl:call-template name="lookup.key">
248 <xsl:with-param name="key" select="$key"/>
249 <xsl:with-param name="table" select="substring-after(substring-after($table,' '), ' ')"/>
255 <xsl:template name="xpath.location">
256 <xsl:param name="node" select="."/>
257 <xsl:param name="path" select="''"/>
259 <xsl:variable name="next.path">
260 <xsl:value-of select="local-name($node)"/>
261 <xsl:if test="$path != ''">/</xsl:if>
262 <xsl:value-of select="$path"/>
266 <xsl:when test="$node/parent::*">
267 <xsl:call-template name="xpath.location">
268 <xsl:with-param name="node" select="$node/parent::*"/>
269 <xsl:with-param name="path" select="$next.path"/>
273 <xsl:text>/</xsl:text>
274 <xsl:value-of select="$next.path"/>
278 <xsl:template name="comment-escape-string">
279 <xsl:param name="string" select="''"/>
281 <xsl:if test="starts-with($string, '-')">
282 <xsl:text> </xsl:text>
285 <xsl:call-template name="comment-escape-string.recursive">
286 <xsl:with-param name="string" select="$string"/>
289 <xsl:if test="substring($string, string-length($string), 1) = '-'">
290 <xsl:text> </xsl:text>
293 <xsl:template name="comment-escape-string.recursive">
294 <xsl:param name="string" select="''"/>
296 <xsl:when test="contains($string, '--')">
297 <xsl:value-of select="substring-before($string, '--')"/>
298 <xsl:value-of select="'- -'"/>
299 <xsl:call-template name="comment-escape-string.recursive">
300 <xsl:with-param name="string" select="substring-after($string, '--')"/>
304 <xsl:value-of select="$string"/>
308 <xsl:template name="str.tokenize.keep.delimiters">
309 <xsl:param name="string" select="''"/>
310 <xsl:param name="delimiters" select="' '"/>
312 <xsl:when test="not($string)"/>
313 <xsl:when test="not($delimiters)">
314 <xsl:call-template name="str.tokenize.keep.delimiters-characters">
315 <xsl:with-param name="string" select="$string"/>
319 <xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
320 <xsl:with-param name="string" select="$string"/>
321 <xsl:with-param name="delimiters" select="$delimiters"/>
326 <xsl:template name="str.tokenize.keep.delimiters-characters">
327 <xsl:param name="string"/>
328 <xsl:if test="$string">
329 <ssb:token xmlns:ssb="http://sideshowbarker.net/ns" xmlns="http://docbook.org/ns/docbook" xmlns:src="http://nwalsh.com/xmlns/litprog/fragment" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dyn="http://exslt.org/dynamic" xmlns:saxon="http://icl.com/saxon"><xsl:value-of select="substring($string, 1, 1)"/></ssb:token>
330 <xsl:call-template name="str.tokenize.keep.delimiters-characters">
331 <xsl:with-param name="string" select="substring($string, 2)"/>
335 <xsl:template name="str.tokenize.keep.delimiters-delimiters">
336 <xsl:param name="string"/>
337 <xsl:param name="delimiters"/>
338 <xsl:variable name="delimiter" select="substring($delimiters, 1, 1)"/>
340 <xsl:when test="not($delimiter)">
341 <ssb:token xmlns:ssb="http://sideshowbarker.net/ns" xmlns="http://docbook.org/ns/docbook" xmlns:src="http://nwalsh.com/xmlns/litprog/fragment" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dyn="http://exslt.org/dynamic" xmlns:saxon="http://icl.com/saxon"><xsl:value-of select="$string"/></ssb:token>
343 <xsl:when test="contains($string, $delimiter)">
344 <xsl:if test="not(starts-with($string, $delimiter))">
345 <xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
346 <xsl:with-param name="string" select="substring-before($string, $delimiter)"/>
347 <xsl:with-param name="delimiters" select="substring($delimiters, 2)"/>
350 <!-- output each delimiter -->
351 <xsl:value-of select="$delimiter"/>
352 <xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
353 <xsl:with-param name="string" select="substring-after($string, $delimiter)"/>
354 <xsl:with-param name="delimiters" select="$delimiters"/>
358 <xsl:call-template name="str.tokenize.keep.delimiters-delimiters">
359 <xsl:with-param name="string" select="$string"/>
360 <xsl:with-param name="delimiters" select="substring($delimiters, 2)"/>
365 <xsl:template name="apply-string-subst-map">
366 <xsl:param name="content"/>
367 <xsl:param name="map.contents"/>
368 <xsl:variable name="replaced_text">
369 <xsl:call-template name="string.subst">
370 <xsl:with-param name="string" select="$content"/>
371 <xsl:with-param name="target" select="$map.contents[1]/@oldstring"/>
372 <xsl:with-param name="replacement" select="$map.contents[1]/@newstring"/>
376 <xsl:when test="$map.contents[2]">
377 <xsl:call-template name="apply-string-subst-map">
378 <xsl:with-param name="content" select="$replaced_text"/>
379 <xsl:with-param name="map.contents" select="$map.contents[position() > 1]"/>
383 <xsl:value-of select="$replaced_text"/>
388 <xsl:template name="count.uri.path.depth">
389 <xsl:param name="filename" select="''"/>
390 <xsl:param name="count" select="0"/>
393 <xsl:when test="contains($filename, '/')">
394 <xsl:call-template name="count.uri.path.depth">
395 <xsl:with-param name="filename" select="substring-after($filename, '/')"/>
396 <xsl:with-param name="count" select="$count + 1"/>
400 <xsl:value-of select="$count"/>
404 <xsl:template name="trim.common.uri.paths">
405 <xsl:param name="uriA" select="''"/>
406 <xsl:param name="uriB" select="''"/>
407 <xsl:param name="return" select="'A'"/>
409 <!-- Resolve any ../ in the path -->
410 <xsl:variable name="trimmed.uriA">
411 <xsl:call-template name="resolve.path">
412 <xsl:with-param name="filename" select="$uriA"/>
416 <xsl:variable name="trimmed.uriB">
417 <xsl:call-template name="resolve.path">
418 <xsl:with-param name="filename" select="$uriB"/>
423 <xsl:when test="contains($trimmed.uriA, '/') and contains($trimmed.uriB, '/') and substring-before($trimmed.uriA, '/') = substring-before($trimmed.uriB, '/')">
424 <xsl:call-template name="trim.common.uri.paths">
425 <xsl:with-param name="uriA" select="substring-after($trimmed.uriA, '/')"/>
426 <xsl:with-param name="uriB" select="substring-after($trimmed.uriB, '/')"/>
427 <xsl:with-param name="return" select="$return"/>
432 <xsl:when test="$return = 'A'">
433 <xsl:value-of select="$trimmed.uriA"/>
436 <xsl:value-of select="$trimmed.uriB"/>
442 <xsl:template name="resolve.path">
443 <xsl:param name="filename" select="''"/>
445 <!-- Leading .. are not eliminated -->
446 <xsl:when test="starts-with($filename, '../')">
447 <xsl:value-of select="'../'"/>
448 <xsl:call-template name="resolve.path">
449 <xsl:with-param name="filename" select="substring-after($filename, '../')"/>
452 <xsl:when test="contains($filename, '/../')">
453 <xsl:call-template name="resolve.path">
454 <xsl:with-param name="filename">
455 <xsl:call-template name="dirname">
456 <xsl:with-param name="filename" select="substring-before($filename, '/../')"/>
458 <xsl:value-of select="substring-after($filename, '/../')"/>
463 <xsl:value-of select="$filename"/>
468 <xsl:template name="dirname">
469 <xsl:param name="filename" select="''"/>
470 <xsl:if test="contains($filename, '/')">
471 <xsl:value-of select="substring-before($filename, '/')"/>
472 <xsl:text>/</xsl:text>
473 <xsl:call-template name="dirname">
474 <xsl:with-param name="filename" select="substring-after($filename, '/')"/>
480 <xsl:template name="trim.text">
481 <xsl:param name="contents" select="."/>
482 <xsl:variable name="contents-left-trimmed">
483 <xsl:call-template name="trim-left">
484 <xsl:with-param name="contents" select="$contents"/>
487 <xsl:variable name="contents-trimmed">
488 <xsl:call-template name="trim-right">
489 <xsl:with-param name="contents" select="$contents-left-trimmed"/>
492 <xsl:value-of select="$contents-trimmed"/>
495 <xsl:template name="trim-left">
496 <xsl:param name="contents"/>
498 <xsl:when test="starts-with($contents,'
') or starts-with($contents,'
') or starts-with($contents,' ') or starts-with($contents,'	')">
499 <xsl:call-template name="trim-left">
500 <xsl:with-param name="contents" select="substring($contents, 2)"/>
504 <xsl:value-of select="$contents"/>
509 <xsl:template name="trim-right">
510 <xsl:param name="contents"/>
511 <xsl:variable name="last-char">
512 <xsl:value-of select="substring($contents, string-length($contents), 1)"/>
515 <xsl:when test="($last-char = '
') or ($last-char = '
') or ($last-char = ' ') or ($last-char = '	')">
516 <xsl:call-template name="trim-right">
517 <xsl:with-param name="contents" select="substring($contents, 1, string-length($contents) - 1)"/>
521 <xsl:value-of select="$contents"/>