2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3 xmlns:d="http://docbook.org/ns/docbook"
4 xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
5 xmlns:dyn="http://exslt.org/dynamic"
6 xmlns:saxon="http://icl.com/saxon"
7 xmlns:xlink="http://www.w3.org/1999/xlink"
8 exclude-result-prefixes="doc dyn saxon d"
11 <!-- ********************************************************************
13 This file is part of the XSL DocBook Stylesheet distribution.
14 See ../README or http://cdn.docbook.org/release/xsl/current/ for
15 copyright and other information.
17 ******************************************************************** -->
18 <doc:reference xmlns="" xml:id="utility">
20 <title>Common » Utility Template Reference</title>
21 <releaseinfo role="meta">
24 <!-- * yes, partintro is a valid child of a reference... -->
25 <partintro xml:id="partintro">
26 <title>Introduction</title>
27 <para>This is technical reference documentation for the
28 miscellaneous utility templates in the DocBook XSL
31 <para>These templates are defined in a separate file from the set
32 of “common” templates because some of the common templates
33 reference DocBook XSL stylesheet parameters, requiring the
34 entire set of parameters to be imported/included in any
35 stylesheet that imports/includes the common templates.</para>
36 <para>The utility templates don’t import or include any DocBook
37 XSL stylesheet parameters, so the utility templates can be used
38 without importing the whole set of parameters.</para>
40 <para>This is not intended to be user documentation. It is
41 provided for developers writing customization layers for the
46 <!-- ====================================================================== -->
48 <doc:template name="log.message" xmlns="">
49 <refpurpose>Logs/emits formatted notes and warnings</refpurpose>
51 <refdescription id="log.message-desc">
52 <para>The <function>log.message</function> template is a utility
53 template for logging/emitting formatted messages – that is,
54 notes and warnings, along with a given log “level” and an
55 identifier for the “source” that the message relates to.</para>
58 <refparameter id="log.message-params">
60 <varlistentry><term>level</term>
62 <para>Text to log/emit in the message-level field to
63 indicate the message level
64 (<literal>Note</literal> or
65 <literal>Warning</literal>)</para>
68 <varlistentry><term>source</term>
70 <para>Text to log/emit in the source field to identify the
71 “source” to which the notification/warning relates.
72 This can be any arbitrary string, but because the
73 message lacks line and column numbers to identify the
74 exact part of the source document to which it
75 relates, the intention is that the value you pass
76 into the <literal>source</literal> parameter should
77 give the user some way to identify the portion of
78 their source document on which to take potentially
79 take action in response to the log message (for
80 example, to edit, change, or add content).</para>
81 <para>So the <literal>source</literal> value should be,
82 for example, an ID, book/chapter/article title, title
83 of some formal object, or even a string giving an
84 XPath expression.</para>
87 <varlistentry><term>context-desc</term>
89 <para>Text to log/emit in the context-description field to
90 describe the context for the message.</para>
93 <varlistentry><term>context-desc-field-length</term>
95 <para>Specifies length of the context-description field
96 (in characters); default is 12</para>
97 <para>If the text specified by the
98 <literal>context-desc</literal> parameter is longer
99 than the number of characters specified in
100 <literal>context-desc-field-length</literal>, it is
101 truncated to <literal>context-desc-field-length</literal>
102 (12 characters by default).</para>
103 <para>If the specified text is shorter than
104 <literal>context-desc-field-length</literal>,
105 it is right-padded out to
106 <literal>context-desc-field-length</literal> (12 by
108 <para>If no value has been specified for the
109 <literal>context-desc</literal> parameter, the field is
110 left empty and the text of the log message begins with
111 the value of the <literal>message</literal>
115 <varlistentry><term>message</term>
117 <para>Text to log/emit in the actual message field</para>
120 <varlistentry><term>message-field-length</term>
122 <para>Specifies length of the message
123 field (in characters); default is 45</para>
128 <refreturn id="log.message-returns">
129 <para>Outputs a message (generally, to standard error).</para></refreturn>
131 <xsl:template name="log.message">
132 <xsl:param name="level"/>
133 <xsl:param name="source"/>
134 <xsl:param name="context-desc"/>
135 <xsl:param name="context-desc-field-length">12</xsl:param>
136 <xsl:param name="context-desc-padded">
137 <xsl:if test="not($context-desc = '')">
138 <xsl:call-template name="pad-string">
139 <xsl:with-param name="leftRight">right</xsl:with-param>
140 <xsl:with-param name="padVar"
141 select="substring($context-desc, 1, $context-desc-field-length)"/>
142 <xsl:with-param name="length" select="$context-desc-field-length"/>
146 <xsl:param name="message"/>
147 <xsl:param name="message-field-length" select="45"/>
148 <xsl:param name="message-padded">
149 <xsl:variable name="spaces-for-blank-level">
150 <!-- * if the level field is blank, we'll need to pad out -->
151 <!-- * the message field with spaces to compensate -->
153 <xsl:when test="$level = ''">
154 <xsl:value-of select="4 + 2"/>
155 <!-- * 4 = hard-coded length of comment text ("Note" or "Warn") -->
156 <!-- * + 2 = length of colon-plus-space separator ": " -->
159 <xsl:value-of select="0"/>
163 <xsl:variable name="spaces-for-blank-context-desc">
164 <!-- * if the context-description field is blank, we'll need -->
165 <!-- * to pad out the message field with spaces to compensate -->
167 <xsl:when test="$context-desc = ''">
168 <xsl:value-of select="$context-desc-field-length + 2"/>
169 <!-- * + 2 = length of colon-plus-space separator ": " -->
172 <xsl:value-of select="0"/>
176 <xsl:variable name="extra-spaces"
177 select="$spaces-for-blank-level + $spaces-for-blank-context-desc"/>
178 <xsl:call-template name="pad-string">
179 <xsl:with-param name="leftRight">right</xsl:with-param>
180 <xsl:with-param name="padVar"
181 select="substring($message, 1, ($message-field-length + $extra-spaces))"/>
182 <xsl:with-param name="length"
183 select="$message-field-length + $extra-spaces"/>
186 <!-- * emit the actual log message -->
188 <xsl:if test="not($level = '')">
189 <xsl:value-of select="$level"/>
190 <xsl:text>: </xsl:text>
192 <xsl:if test="not($context-desc = '')">
193 <xsl:value-of select="$context-desc-padded"/>
194 <xsl:text>: </xsl:text>
196 <xsl:value-of select="$message-padded"/>
197 <xsl:text> </xsl:text>
198 <xsl:value-of select="$source"/>
202 <!-- ===================================== -->
203 <doc:template name="get.doc.title" xmlns="">
204 <refpurpose>Gets a title from the current document</refpurpose>
205 <refdescription id="get.doc.title-desc">
206 <para>The <function>get.doc.title</function> template is a
207 utility template for returning the first title found in the
208 current document.</para>
210 <refreturn id="get.doc.title-returns">
211 <para>Returns a string containing some identifying title for the
212 current document .</para></refreturn>
214 <xsl:template name="get.doc.title">
216 <xsl:when test="//*[local-name() = 'title'
217 or local-name() = 'refname']">
218 <xsl:value-of select="//*[local-name() = 'title'
219 or local-name() = 'refname'][1]"/>
221 <xsl:when test="substring(local-name(*[1]),
222 string-length(local-name(*[1])-3) = 'info')
223 and *[1]/*[local-name() = 'title']">
224 <xsl:value-of select="*[1]/*[local-name() = 'title'][1]"/>
229 <!-- ===================================== -->
230 <doc:template name="pad-string" xmlns="">
231 <refpurpose>Right-pads or left-pads a string out to a certain length</refpurpose>
232 <refdescription id="pad-string-desc">
233 <para>This function takes string <parameter>padVar</parameter> and
234 pads it out in the direction <parameter>rightLeft</parameter> to
235 the string-length <parameter>length</parameter>, using string
236 <parameter>padChar</parameter> (a space character by default) as
237 the padding string (note that <parameter>padChar</parameter> can
238 be a string; it is not limited to just being a single
241 <para>This function began as a copy of Nate Austin's
242 <function>prepend-pad</function> function in the <link
243 xlink:href="http://www.dpawson.co.uk/xsl/sect2/padding.html" >Padding
244 Content</link> section of Dave Pawson's <link
245 xlink:href="http://www.dpawson.co.uk/xsl/index.html" >XSLT
249 <refreturn id="pad-string-returns">
250 <para>Returns a (padded) string.</para></refreturn>
252 <xsl:template name="pad-string">
253 <!-- * recursive template to right/left pad the value with -->
254 <!-- * whatever padChar is passed in -->
255 <xsl:param name="padChar" select="' '"/>
256 <xsl:param name="leftRight">left</xsl:param>
257 <xsl:param name="padVar"/>
258 <xsl:param name="length"/>
260 <xsl:when test="string-length($padVar) < $length">
261 <xsl:call-template name="pad-string">
262 <xsl:with-param name="padChar" select="$padChar"/>
263 <xsl:with-param name="leftRight" select="$leftRight"/>
264 <xsl:with-param name="padVar">
266 <!-- * determine whether string should be -->
267 <!-- * right- or left-padded -->
268 <xsl:when test="$leftRight = 'left'">
269 <!-- * pad it to left -->
270 <xsl:value-of select="concat($padChar,$padVar)"/>
273 <!-- * otherwise, right-pad the string -->
274 <xsl:value-of select="concat($padVar,$padChar)"/>
278 <xsl:with-param name="length" select="$length"/>
283 select="substring($padVar,string-length($padVar) - $length + 1)"/>