1<?xml version='1.0'?>
2<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
4                exclude-result-prefixes="doc"
5                version='1.0'>
6
7<!-- ********************************************************************
8     $Id$
9     ********************************************************************
10
11     This file is part of the XSL DocBook Stylesheet distribution.
12     See /README or http://nwalsh.com/docbook/xsl/ for copyright
13     and other information.
14
15     This module implements DTD-independent functions
16
17     ******************************************************************** -->
18
19<doc:reference xmlns="">
20<referenceinfo>
21<releaseinfo role="meta">
22$Id$
23</releaseinfo>
24<author><surname>Walsh</surname>
25<firstname>Norman</firstname></author>
26<copyright><year>1999</year><year>2000</year>
27<holder>Norman Walsh</holder>
28</copyright>
29</referenceinfo>
30<title>Library Template Reference</title>
31
32<partintro>
33<section><title>Introduction</title>
34
35<para>This is technical reference documentation for the DocBook XSL
36Stylesheets; it documents (some of) the parameters, templates, and
37other elements of the stylesheets.</para>
38
39<para>This is not intended to be <quote>user</quote> documentation.
40It is provided for developers writing customization layers for the
41stylesheets, and for anyone who's interested in <quote>how it
42works</quote>.</para>
43
44<para>Although I am trying to be thorough, this documentation is known
45to be incomplete. Don't forget to read the source, too :-)</para>
46</section>
47</partintro>
48</doc:reference>
49
50<xsl:template name="dot.count">
51  <!-- Returns the number of "." characters in a string -->
52  <xsl:param name="string"></xsl:param>
53  <xsl:param name="count" select="0"/>
54  <xsl:choose>
55    <xsl:when test="contains($string, '.')">
56      <xsl:call-template name="dot.count">
57        <xsl:with-param name="string" select="substring-after($string, '.')"/>
58        <xsl:with-param name="count" select="$count+1"/>
59      </xsl:call-template>
60    </xsl:when>
61    <xsl:otherwise>
62      <xsl:value-of select="$count"/>
63    </xsl:otherwise>
64  </xsl:choose>
65</xsl:template>
66
67<!-- ================================================================== -->
68
69<xsl:template name="copy-string">
70  <!-- returns 'count' copies of 'string' -->
71  <xsl:param name="string"></xsl:param>
72  <xsl:param name="count" select="0"/>
73  <xsl:param name="result"></xsl:param>
74
75  <xsl:choose>
76    <xsl:when test="$count>0">
77      <xsl:call-template name="copy-string">
78        <xsl:with-param name="string" select="$string"/>
79        <xsl:with-param name="count" select="$count - 1"/>
80        <xsl:with-param name="result">
81          <xsl:value-of select="$result"/>
82          <xsl:value-of select="$string"/>
83        </xsl:with-param>
84      </xsl:call-template>
85    </xsl:when>
86    <xsl:otherwise>
87      <xsl:value-of select="$result"/>
88    </xsl:otherwise>
89  </xsl:choose>
90</xsl:template>
91
92<!-- ====================================================================== -->
93
94<doc:template name="string.subst" xmlns="">
95<refpurpose>Substitute one text string for another in a string</refpurpose>
96<refdescription>
97<para>The <function>string.subst</function> template replaces all
98occurances of <parameter>target</parameter> in <parameter>string</parameter>
99with <parameter>replacement</parameter> and returns the result.
100</para>
101</refdescription>
102</doc:template>
103
104<xsl:template name="string.subst">
105  <xsl:param name="string"></xsl:param>
106  <xsl:param name="target"></xsl:param>
107  <xsl:param name="replacement"></xsl:param>
108
109  <xsl:choose>
110    <xsl:when test="contains($string, $target)">
111      <xsl:variable name="rest">
112        <xsl:call-template name="string.subst">
113          <xsl:with-param name="string"
114                          select="substring-after($string, $target)"/>
115          <xsl:with-param name="target" select="$target"/>
116          <xsl:with-param name="replacement" select="$replacement"/>
117        </xsl:call-template>
118      </xsl:variable>
119      <xsl:value-of select="concat(substring-before($string, $target),
120                                   $replacement,
121                                   $rest)"/>
122    </xsl:when>
123    <xsl:otherwise>
124      <xsl:value-of select="$string"/>
125    </xsl:otherwise>
126  </xsl:choose>
127</xsl:template>
128
129<!-- ================================================================== -->
130
131<doc:template name="xpointer.idref" xmlns="">
132<refpurpose>Extract IDREF from an XPointer</refpurpose>
133<refdescription>
134<para>The <function>xpointer.idref</function> template returns the
135ID portion of an XPointer which is a pointer to an ID within the current
136document, or the empty string if it is not.</para>
137<para>In other words, <function>xpointer.idref</function> returns
138<quote>foo</quote> when passed either <literal>#foo</literal>
139or <literal>#xpointer(id('foo'))</literal>, otherwise it returns
140the empty string.</para>
141</refdescription>
142</doc:template>
143
144<xsl:template name="xpointer.idref">
145  <xsl:param name="xpointer">http://...</xsl:param>
146  <xsl:choose>
147    <xsl:when test="starts-with($xpointer, '#xpointer(id(')">
148      <xsl:variable name="rest" select="substring-after($xpointer, '#xpointer(id(')"/>
149      <xsl:variable name="quote" select="substring($rest, 1, 1)"/>
150      <xsl:value-of select="substring-before(substring-after($xpointer, $quote), $quote)"/>
151    </xsl:when>
152    <xsl:when test="starts-with($xpointer, '#')">
153      <xsl:value-of select="substring-after($xpointer, '#')"/>
154    </xsl:when>
155    <!-- otherwise it's a pointer to some other document -->
156  </xsl:choose>
157</xsl:template>
158
159<!-- ================================================================== -->
160
161<doc:template name="length-magnitude" xmlns="">
162<refpurpose>Return the unqualified dimension from a length specification</refpurpose>
163<refdescription>
164<para>The <function>length-magnitude</function> template returns the
165unqualified length ("20" for "20pt") from a dimension.
166</para>
167</refdescription>
168</doc:template>
169
170<xsl:template name="length-magnitude">
171  <xsl:param name="length" select="'0pt'"/>
172
173  <xsl:choose>
174    <xsl:when test="string-length($length) = 0"/>
175    <xsl:when test="substring($length,1,1) = '0'
176                    or substring($length,1,1) = '1'
177                    or substring($length,1,1) = '2'
178                    or substring($length,1,1) = '3'
179                    or substring($length,1,1) = '4'
180                    or substring($length,1,1) = '5'
181                    or substring($length,1,1) = '6'
182                    or substring($length,1,1) = '7'
183                    or substring($length,1,1) = '8'
184                    or substring($length,1,1) = '9'
185                    or substring($length,1,1) = '.'">
186      <xsl:value-of select="substring($length,1,1)"/>
187      <xsl:call-template name="length-magnitude">
188        <xsl:with-param name="length" select="substring($length,2)"/>
189      </xsl:call-template>
190    </xsl:when>
191  </xsl:choose>
192</xsl:template>
193
194<!-- ================================================================== -->
195
196<doc:template name="length-spec" xmlns="">
197<refpurpose>Return a fully qualified length specification</refpurpose>
198<refdescription>
199<para>The <function>length-spec</function> template returns the
200qualified length from a dimension. If an unqualified length is given,
201the <parameter>default.units</parameter> will be added to it.
202</para>
203</refdescription>
204</doc:template>
205
206<xsl:template name="length-spec">
207  <xsl:param name="length" select="'0pt'"/>
208  <xsl:param name="default.units" select="'pt'"/>
209  <xsl:variable name="magnitude">
210    <xsl:call-template name="length-magnitude">
211      <xsl:with-param name="length" select="$length"/>
212    </xsl:call-template>
213  </xsl:variable>
214  <xsl:variable name="units">
215    <xsl:value-of select="substring($length, string-length($magnitude)+1)"/>
216  </xsl:variable>
217
218  <xsl:value-of select="$magnitude"/>
219  <xsl:choose>
220    <xsl:when test="$units='cm'
221                    or $units='mm'
222                    or $units='in'
223                    or $units='pt'
224                    or $units='pc'
225                    or $units='px'
226                    or $units='em'">
227      <xsl:value-of select="$units"/>
228    </xsl:when>
229    <xsl:when test="$units = ''">
230      <xsl:value-of select="$default.units"/>
231    </xsl:when>
232    <xsl:otherwise>
233      <xsl:message>
234        <xsl:text>Unrecognized unit of measure: </xsl:text>
235        <xsl:value-of select="$units"/>
236        <xsl:text>.</xsl:text>
237      </xsl:message>
238    </xsl:otherwise>
239  </xsl:choose>
240</xsl:template>
241
242</xsl:stylesheet>
243