Debian Codegraph @

en/ch04.xml

raw symbols SCIP
1 <chapter id="ch-publish">
2 <?dbhtml filename="ch04.html"?>
3 <chapterinfo>
4 <pubdate>$Date$</pubdate>
5 <releaseinfo>$Revision$</releaseinfo>
6 </chapterinfo>
7 <title>Publishing DocBook Documents</title>
8 <para>
9 <indexterm id="publishDocBookch04" class="startofrange"><primary>DocBook DTD</primary>
10 <secondary>publishing documents</secondary></indexterm>
11 <indexterm id="DocBookpublishch04" class="startofrange"><primary>publishing DocBook documents</primary></indexterm>
12 <indexterm><primary>stylesheets</primary>
13 <secondary>publishing SGML/XML documents</secondary></indexterm>
14 <indexterm><primary>HTML</primary>
15 <secondary>translating SGML/XML documents</secondary></indexterm>
16
17 Creating and editing &SGML;/&XML; documents is usually only half the
18 battle. After you've composed your document, you'll want to publish
19 it. Publishing, for our purposes, means either print or web
20 publishing. For &SGML; and &XML; documents, this is usually
21 accomplished with some kind of <glossterm>stylesheet</glossterm>. In
22 some environments, it is now possible to publish an &XML;
23 document on the Web simply by putting it online with a stylesheet.
24 </para>
25 <para>
26 <indexterm><primary>Jade</primary>
27 <secondary>Modular DocBook Stylesheets</secondary></indexterm>
28 <indexterm><primary>Modular DocBook Stylesheets</primary></indexterm>
29 <indexterm><primary>Linux Documentation Project</primary></indexterm>
30
31 There are many ways, using both free and commercial tools, to publish
32 &SGML; documents. In this chapter, we're going to survey a number of
33 possibilities, and then look at just one solution in detail: <ulink url="http://www.jclark.com/jade/">Jade</ulink> and the <ulink url="http://nwalsh.com/docbook/dsssl/">Modular DocBook
34 Stylesheets.</ulink> We used jade to produce this
35 book and to produce the online versions on the
36 <acronym>CD-ROM</acronym>; it is also being deployed in other projects
37 such as <ulink url="http://www.sgmltools.org/"><literal>&lt;SGML&gt;&amp;tools;</literal>,</ulink>
38 which originated with the Linux Documentation Project.
39 </para>
40 <para>
41 For a brief survey of other tools, see <xref linkend="app-resources"/>.
42 </para>
43
44 <section><title>The MIME Type for DocBook</title>
45 <para>When published on the web, documents must be identified with a MIME type.
46 The MIME type for SGML DocBook documents published directly on the web
47 is “<literal>application/sgml</literal>”.
48 The most appropriate MIME type for XML DocBook documents published directly on
49 the web is currently
50 “<literal>application/xml</literal>”, however the DocBook Technical Committee
51 may choose to register a more specific MIME type in the future.</para>
52 </section>
53
54 <section><title>A Survey of Stylesheet Languages</title>
55 <para>
56 <indexterm><primary>stylesheets</primary>
57 <secondary>languages</secondary></indexterm>
58 <indexterm><primary>languages</primary>
59 <secondary>stylesheets</secondary></indexterm>
60
61 Over the years, a number of attempts have been made to produce a
62 standard stylesheet language and, failing that, a large number of
63 proprietary languages have been developed.
64 </para>
65 <variablelist>
66 <varlistentry><term>FOSIs</term>
67 <listitem>
68 <para>
69 <indexterm><primary>FOSI stylesheets</primary></indexterm>
70 <indexterm><primary>Formatting Output Specification Instance</primary><see>FOSI stylesheets</see></indexterm>
71 <indexterm><primary>U.S. Department of Defense stylesheet standard</primary></indexterm>
72
73 First, the U.S. Department of Defense, in an attempt to
74 standardize stylesheets across military branches, created the
75 <citetitle>Output Specification</citetitle>, which is defined in
76 <acronym>MIL-PRF-28001C</acronym>, <citetitle>Markup Requirements and
77 Generic Style Specification for Electronic Printed Output and Exchange
78 of Text</citetitle>.<footnote>
79 <para>
80 See <citetitle><ulink url="http://www-cals.itsi.disa.mil/core/formal/fps.htm">Formally
81 Published CALS Standards</ulink></citetitle> for more information.
82 </para>
83 </footnote>
84 </para>
85 <para>
86 <indexterm><primary>ADEPT Publisher (Arbortext), FOSIs support</primary></indexterm>
87 <indexterm><primary>Arbortext ADEPT Publisher, FOSIs support</primary></indexterm>
88
89 Commonly called FOSIs (for Formatting Output Specification Instances),
90 they are supported by a few products including ADEPT Publisher by
91 <ulink url="http://www.arbortext.com/">Arbortext</ulink> and DL
92 Composer by <ulink url="http://www.datalogics.com/">Datalogics</ulink>.
93 </para>
94 </listitem>
95 </varlistentry>
96 <varlistentry><term>DSSSL</term>
97 <listitem>
98 <para>
99 <indexterm><primary>DSSSL</primary></indexterm>
100 <indexterm><primary>Document Style Semantics and Specification Language</primary><see>DSSSL</see></indexterm>
101 <indexterm><primary>ISO standards</primary>
102 <secondary>DSSSL</secondary></indexterm>
103 <indexterm><primary>International Organization for Standardization</primary><see>ISO Entity Sets; ISO standards</see></indexterm>
104
105 Next, the International Organization for Standardization
106 (<acronym>ISO</acronym>) created <acronym>DSSSL</acronym>, the
107 Document Style Semantics and Specification Language. Subsets of
108 <acronym>DSSSL</acronym> are supported by Jade and a few other tools,
109 but it never achieved widespread support.
110 </para>
111 </listitem>
112 </varlistentry>
113 <varlistentry><term>CSS</term>
114 <listitem>
115 <para>
116 <indexterm><primary>CSS</primary></indexterm>
117 <indexterm><primary>HTML</primary>
118 <secondary>CSS</secondary></indexterm>
119 <indexterm><primary>XML</primary>
120 <secondary>CSS</secondary></indexterm>
121
122 The <acronym>W3C</acronym> CSS Working Group created
123 <acronym>CSS</acronym> as a style attachment language for &HTML;, and,
124 more recently, &XML;.
125 </para>
126 </listitem>
127 </varlistentry>
128 <varlistentry><term>XSL</term>
129 <listitem>
130 <para>
131 <indexterm><primary>XSL</primary></indexterm>
132 <indexterm><primary>XML</primary>
133 <secondary>XSL</secondary></indexterm>
134 <indexterm><primary>Extensible Style Language</primary><see>XSL</see></indexterm>
135 Most recently, the &XML; effort has identified a standard Extensible Style
136 Language (<acronym>XSL</acronym>) as a requirement. The
137 <acronym>W3C</acronym> <acronym>XSL</acronym> Working Group is
138 currently pursuing that effort.
139 </para>
140 </listitem>
141 </varlistentry>
142 </variablelist>
143 <section><title>Stylesheet Examples</title>
144 <para>
145 <indexterm id="stylsheetsch04" class="startofrange"><primary>stylesheets</primary>
146 <secondary>examples</secondary></indexterm>
147
148
149
150 By way of comparison, here's an example of each of the standard style
151 languages. In each case, the stylesheet fragment shown contains the
152 rules that reasonably formatted the following paragraph:
153 </para>
154 <screen>
155 <![CDATA[<para>
156 This is an example paragraph. It should be presented in a
157 reasonable body font. <emphasis>Emphasized</emphasis> words
158 should be printed in italics. A single level of
159 <emphasis>Nested <emphasis>emphasis</emphasis> should also
160 be supported.</emphasis>
161 </para>
162 ]]></screen>
163 <section>
164 <title><acronym>FOSI</acronym> stylesheet</title>
165 <para>
166 <indexterm><primary>FOSI stylesheets</primary>
167 <secondary>example</secondary></indexterm>
168 <indexterm><primary>SGML</primary>
169 <secondary>FOSIs</secondary></indexterm>
170 <indexterm><primary>e-i-c element</primary></indexterm>
171 <indexterm><primary>element in context (e-i-c)</primary></indexterm>
172
173 <acronym>FOSI</acronym>s are &SGML; documents. The element in the
174 <acronym>FOSI</acronym> that controls the presentation of specific
175 elements is the <sgmltag>e-i-c</sgmltag> (element in context) element.
176 A sample <acronym>FOSI</acronym> fragment is shown in <xref linkend="ex-fosi"/>.
177 </para>
178 <example id="ex-fosi">
179 <title>A Fragment of a <acronym>FOSI</acronym> Stylesheet</title>
180 <programlisting><inlinegraphic format='linespecific' fileref="examples/style-fosi.txt"/></programlisting>
181 </example>
182 </section>
183 <section><title><acronym>DSSSL</acronym> stylesheet</title>
184 <para>
185 <indexterm><primary>DSSSL</primary>
186 <secondary>stylesheets</secondary>
187 <tertiary>example</tertiary></indexterm>
188 <indexterm><primary>Scheme language</primary>
189 <secondary>DSSSL stylesheets</secondary></indexterm>
190
191 <acronym>DSSSL</acronym> stylesheets are written in a Scheme-like
192 language (see <xref linkend="scheme"/> later in this chapter). It is the
193 <function>element</function> function that controls the presentation of
194 individual elements. See the example in <xref linkend="ex-dsssl"/>.
195 </para>
196 <example id="ex-dsssl">
197 <title>A Fragment of a <acronym>DSSSL</acronym> Stylesheet</title>
198 <programlisting><inlinegraphic format='linespecific' fileref="examples/style-dsssl.txt"/></programlisting>
199 </example>
200 </section>
201 <section><title><acronym>CSS</acronym> stylesheet</title>
202 <para>
203 <indexterm><primary>CSS stylesheets</primary>
204 <secondary>example</secondary></indexterm>
205
206 <acronym>CSS</acronym> stylesheets consist of selectors and formatting
207 properties, as shown in <xref linkend="ex-css"/>.
208 </para>
209 <example id="ex-css">
210 <title>A Fragment of a <acronym>CSS</acronym> Stylesheet</title>
211 <programlisting><inlinegraphic format='linespecific' fileref="examples/style-css.txt"/></programlisting>
212 </example>
213 </section>
214 <section><title><acronym>XSL</acronym> stylesheet</title>
215 <para>
216 <indexterm><primary>XSL stylesheets</primary>
217 <secondary>example</secondary></indexterm>
218
219 <acronym>XSL</acronym> stylesheets are &XML; documents, as shown in
220 <xref linkend="ex-xsl"/>. The element in the <acronym>XSL</acronym>
221 stylesheet that controls the presentation of specific elements is the
222 <sgmltag>xsl:template</sgmltag> element.
223 </para>
224 <example id="ex-xsl">
225 <title>A Fragment of an <acronym>XSL</acronym> Stylesheet</title>
226 <programlisting><inlinegraphic format='linespecific' fileref="examples/style-xsl.txt"/></programlisting>
227 </example>
228 </section>
229 </section>
230 </section>
231 <section id="jade"><title>Using Jade and <acronym>DSSSL</acronym> to Publish DocBook Documents</title>
232 <para>
233 <indexterm><primary>Jade</primary>
234 <secondary>DSSSL, using (DocBook document)</secondary></indexterm>
235 <indexterm><primary>SGML</primary>
236 <secondary>DSSSL stylesheets</secondary></indexterm>
237 <indexterm><primary>XML</primary>
238 <secondary>DSSSL stylesheets</secondary></indexterm>
239 <indexterm><primary>DSSSL</primary>
240 <secondary>stylesheets</secondary>
241 <tertiary>print and HTML output (DocBook)</tertiary></indexterm>
242 <indexterm><primary>HTML</primary>
243 <secondary>DSSSL stylesheets</secondary></indexterm>
244
245 Jade is a free tool that applies <ulink url="http://www.jclark.com/dsssl/"> <acronym>DSSSL</acronym></ulink>
246 stylesheets to &SGML; and &XML; documents. As distributed, Jade can
247 output <acronym>RTF</acronym>, &TeX;, <acronym>MIF</acronym>, and
248 &SGML;. The &SGML; backend can be used for &SGML; to &SGML;
249 transformations (for example, DocBook to &HTML;).
250 </para>
251 <para>
252 A complete set of <acronym>DSSSL</acronym> stylesheets for creating
253 print and &HTML; output from DocBook is included on the
254 <acronym>CD-ROM</acronym>. More information about obtaining and
255 installing Jade appears in <xref linkend="app-install"/>.
256 <indexterm startref="stylsheetsch04" class="endofrange"/>
257 </para>
258 </section>
259 <section id="dsssl"><title>A Brief Introduction to <acronym>DSSSL</acronym></title>
260 <para>
261 <acronym>DSSSL</acronym> is a stylesheet language for both print and
262 online rendering. The acronym stands for <citetitle>Document Style
263 Semantics and Specification Language</citetitle>. It is defined by
264 <acronym>ISO</acronym>/<acronym>IEC</acronym> 10179:1996. For more
265 general information about <acronym>DSSSL</acronym>, see <ulink url="http://www.jclark.com/dsssl/">the <acronym>DSSSL</acronym>
266 Page</ulink>.
267 </para>
268 <section id="scheme"><title>Scheme</title>
269 <para>
270 <indexterm><primary>Scheme language</primary>
271 <secondary>syntax</secondary></indexterm>
272 <indexterm><primary>LISP language, syntax</primary></indexterm>
273 <indexterm><primary>syntax</primary>
274 <secondary>LISP language</secondary></indexterm>
275
276 The <acronym>DSSSL</acronym> expression language is Scheme, a variant
277 of Lisp. Lisp is a functional programming language with a remarkably
278 regular syntax. Every expression looks like this:
279 <screen>
280 (<replaceable>operator</replaceable> <optional>arg1</optional> <optional>arg2</optional> ... <optional>arg<replaceable>n</replaceable></optional> )
281 </screen>
282 <indexterm><primary>syntax</primary>
283 <secondary>Scheme language</secondary></indexterm>
284 This is called <quote>prefix</quote> syntax because the operator comes
285 before its arguments.</para> <para>In Scheme, the expression that
286 subtracts 2 from 3, is <literal>(- 3 2)</literal>. And <literal>(+ (-
287 3 2) (* 2 4))</literal> is 9. While the prefix syntax and the
288 parentheses may take a bit of getting used to, Scheme is not hard to
289 learn, in part because there are no exceptions to the syntax.
290 </para>
291 </section>
292 <section><title><acronym>DSSSL</acronym> Stylesheets</title>
293 <para>
294 <indexterm><primary>DSSSL</primary>
295 <secondary>stylesheets</secondary></indexterm>
296 <indexterm><primary>formatting</primary>
297 <secondary>DSSSL stylesheets, elements</secondary></indexterm>
298
299 A complete <acronym>DSSSL</acronym> stylesheet is shown in <xref linkend="dsssl.stylesheet"/>. After only a brief examination of the
300 stylesheet, you'll probably begin to have a feel for how it works. For
301 each element in the document, there is an element rule that describes
302 how you should format that element. The goal of the rest of this
303 chapter is to make it possible for you to read, understand, and even
304 write stylesheets at this level of complexity.
305 </para>
306 <example id="dsssl.stylesheet">
307 <title>A Complete <acronym>DSSSL</acronym> Stylesheet</title>
308 <programlisting><inlinegraphic format='linespecific' fileref="examples/simple.dsl"/></programlisting>
309 </example>
310 <para>
311 <indexterm><primary>DocBook DTD</primary>
312 <secondary>documents</secondary>
313 <tertiary>DSSSL stylesheets</tertiary></indexterm>
314
315 This stylesheet is capable of formatting simple DocBook documents like
316 the one shown in <xref linkend="simple.document"/>.
317 </para>
318 <example id="simple.document">
319 <title>A Simple DocBook Document</title>
320 <programlisting><inlinegraphic format='linespecific' fileref="examples/simple.sgm"/></programlisting>
321 </example>
322 <para>
323 The result of formatting a simple document with this stylesheet can be
324 seen in <xref linkend="simple.document.formatted"/>.
325 </para>
326 <figure id="simple.document.formatted">
327 <title>The formatted simple document</title>
328 <graphic fileref="figures/simple-page.png"></graphic>
329 </figure>
330 <para>
331 We'll take a closer look at this stylesheet after you've learned a
332 little more <acronym>DSSSL</acronym>.
333 </para>
334 </section>
335 <section><title><acronym>DSSSL</acronym> Stylesheets Are &SGML; Documents</title>
336 <para>
337 <indexterm><primary>SGML</primary>
338 <secondary>DSSSL stylesheets</secondary></indexterm>
339 <indexterm><primary>entities</primary>
340 <secondary>DSSSL stylesheets</secondary></indexterm>
341 <indexterm><primary>marked sections</primary>
342 <secondary>DSSSL stylesheets</secondary></indexterm>
343
344 One of the first things that may strike you about
345 <acronym>DSSSL</acronym> stylesheets (aside from all the parentheses),
346 is the fact that the stylesheet itself is an &SGML; document! This
347 means that you have all the power of &SGML; documents at your disposal
348 in <acronym>DSSSL</acronym> stylesheets. In particular, you can use
349 entities and marked sections to build a modular stylesheet.
350 </para>
351 <para>
352 <indexterm><primary>architecture</primary>
353 <secondary>DSSSL</secondary></indexterm>
354 <indexterm><primary>DTDs</primary>
355 <secondary>DSSSL stylesheets, changing</secondary></indexterm>
356
357 In fact, <acronym>DSSSL</acronym> stylesheets are defined so that they
358 correspond to a particular <glossterm>architecture</glossterm>. This
359 means that you can change the &DTD; used by stylesheets within the
360 bounds of the architecture. A complete discussion of document
361 architectures is beyond the scope of this book, but we'll show you one
362 way to take advantage of them in your <acronym>DSSSL</acronym>
363 stylesheets in <xref linkend="dsssl.arch"/> later in the
364 chapter.
365 </para>
366 </section>
367 <section><title><acronym>DSSSL</acronym> Processing Model</title>
368 <para>
369 <indexterm><primary>processing model (DSSSL)</primary></indexterm>
370 <indexterm><primary>trees (DSSSL)</primary></indexterm>
371 <indexterm><primary>nodes (DSSSL)</primary></indexterm>
372
373 A <acronym>DSSSL</acronym> processor builds a tree out of the source
374 document. Each element in the source document becomes a node in the
375 tree (processing instructions and other constructs become nodes as
376 well). Processing the source tree begins with the root rule and
377 continues until there are no more nodes to process.
378 </para>
379 </section>
380 <section><title>Global Variables and Side Effects</title>
381 <para>
382 <indexterm><primary>variables (DSSSL)</primary>
383 <secondary>global</secondary></indexterm>
384 <indexterm><primary>side effects (DSSSL)</primary></indexterm>
385
386 There aren't any global variables or side effects. It can be difficult
387 to come to grips with this, especially if you're just starting out.
388 </para>
389 <para>
390 <indexterm><primary>functions</primary>
391 <secondary>DSSSL</secondary></indexterm>
392 <indexterm><primary>constants (DSSSL)</primary></indexterm>
393
394 It is possible to <link linkend="dsssl.expr.define">define constants
395 and functions</link> and to create local variables with <link linkend="dsssl.expr.let"> <literal>let</literal> expressions</link>,
396 but you can't create any global variables or change anything after
397 you've defined it.
398 </para>
399 </section>
400 <section><title><acronym>DSSSL</acronym> Expressions</title>
401 <para>
402 <indexterm><primary>expressions (DSSSL)</primary></indexterm>
403
404 <acronym>DSSSL</acronym> has a rich vocabulary of expressions for
405 dealing with all of the intricacies of formatting. Many, but by no
406 means all of them, are supported by Jade. In this introduction, we'll
407 cover only a few of the most common.
408 </para>
409 <section id="dsssl.expr.element"><title>Element expressions</title>
410 <para>
411 <indexterm><primary>DSSSL</primary>
412 <secondary>stylesheets</secondary>
413 <tertiary>element expressions</tertiary></indexterm>
414 <indexterm><primary>elements</primary>
415 <secondary>element expressions (DSSSL)</secondary></indexterm>
416 <indexterm><primary>paragraphs</primary>
417 <secondary>DSSSL stylesheets, formattting</secondary></indexterm>
418
419 Element expressions, which define the rules for formatting particular
420 elements, make up the bulk of most <acronym>DSSSL</acronym>
421 stylesheets. A simple element rule can be seen in <xref linkend="ex.d.simple"/>. This rule says that a <sgmltag> para</sgmltag>
422 element should be formatted by making a paragraph (see <xref linkend="dsssl.expr.make"/>).
423 </para>
424 <example id="ex.d.simple">
425 <title>A Simple <acronym>DSSSL</acronym> Rule</title>
426 <screen>
427 (element para
428 (make paragraph
429 space-before: 8pt
430 (process-children)))</screen>
431 </example>
432 <para>
433 <indexterm><primary>ancestors (elements), DSSSL</primary></indexterm>
434
435 An element expression can be made more specific by specifying an
436 element and its ancestors instead of just specifying an element. The
437 rule <literal>(element title ...)</literal> applies to all
438 <sgmltag>Title</sgmltag> elements, but a rule that begins
439 <literal>(element (figure title) ...)</literal> applies only to
440 <sgmltag>Title</sgmltag> elements that are immediate children of
441 <sgmltag>Figure</sgmltag> elements.
442 </para>
443 <para>
444 If several rules apply, the most specific rule is used.
445 </para>
446 <para>
447 When a rule is used, the node in the source tree that was matched
448 becomes the <quote>current node</quote> while that element expression
449 is being processed.
450 </para>
451 </section>
452 <section id="dsssl.expr.make"><title>Make expressions</title>
453 <para>
454 <indexterm><primary>flow objects (DSSSL)</primary></indexterm>
455 <indexterm><primary>content</primary>
456 <secondary>flow objects (DSSSL)</secondary></indexterm>
457
458 A make expression specifies the characteristics of a <quote>flow
459 object.</quote> Flow objects are abstract representations of content
460 (paragraphs, rules, tables, and so on). The expression:
461 <screen>
462 (make paragraph
463 font-size: 12pt
464 line-spacing: 14pt ...)
465 </screen>
466 specifies that the content that goes <quote>here</quote> is to be
467 placed into a paragraph flow object with a font-size of 12pt and a
468 line-spacing of 14pt (all of the unspecified characteristics of the
469 flow object are defaulted in the appropriate way).
470 </para>
471 <para>
472 They're called flow objects because <acronym>DSSSL</acronym>, in its
473 full generality, allows you to specify the characteristics of a
474 sequence of flow objects and a set of areas on the physical page where
475 you can place content. The content of the flow objects is then
476 <quote>poured on to</quote> (or flows in to) the areas on the page(s).
477 </para>
478 <para>
479 <indexterm><primary>sosofo (DSSSL)</primary></indexterm>
480 <indexterm><primary>make expressions (DSSSL)</primary></indexterm>
481
482 In most cases, it's sufficient to think of the make expressions as
483 constructing the flow objects, but they really only specify the
484 <emphasis>characteristics</emphasis> of the flow objects. This detail
485 is apparent in one of the most common and initially confusing pieces
486 of <acronym>DSSSL</acronym> jargon: the
487 <firstterm>sosofo</firstterm>. Sosofo stands for a
488 <quote>specification of a sequence of flow objects.</quote> All this
489 means is that processing a document may result in a nested set of
490 <literal>make</literal> expressions (in other words, the paragraph may contain a table that
491 contains rows that contain cells that contain paragraphs, and so on).
492 </para>
493 <para>
494 The general form of a <literal>make</literal> expression is:
495 <screen>
496 (make <replaceable>flow-object-name</replaceable>
497 <replaceable>keyword1:</replaceable> <replaceable>value1</replaceable>
498 <replaceable>keyword2:</replaceable> <replaceable>value2</replaceable>
499 ...
500 <replaceable>keywordn:</replaceable> <replaceable>valuen</replaceable>
501 (<replaceable>content-expression</replaceable>))
502 </screen>
503 </para>
504 <para>
505 <indexterm><primary>keywords</primary>
506 <secondary>flow objects (DSSSL), defining</secondary></indexterm>
507
508 Keyword arguments specify the characteristics of the flow object. The
509 specific characteristics you use depends on the flow object. The
510 <replaceable>content-expression</replaceable> can vary; it is usually
511 another make expression or one of the <link linkend="dsssl.expr.proc">processing expressions</link>.
512 </para>
513 <para>
514 <indexterm><primary>flow objects (DSSSL)</primary></indexterm>
515
516 Some common flow objects in the print stylesheet are:
517 </para>
518 <variablelist>
519 <varlistentry><term><literal>simple-page-sequence</literal></term>
520 <listitem>
521 <para>
522 <indexterm><primary>simple-page-sequence (DSSSL)</primary>
523 <secondary>flow object</secondary></indexterm>
524 Contains a sequence of pages. The keyword arguments of this flow
525 object let you specify margins, headers and footers, and other
526 page-related characteristics. Print stylesheets should always produce
527 one or more <literal>simple-page-sequence </literal> flow objects.
528 </para>
529 <para>
530 Nesting <literal>simple-page-sequence</literal> does not
531 work. Characteristics on the inner sequences are ignored.
532 </para>
533 </listitem>
534 </varlistentry>
535 <varlistentry><term><literal>paragraph</literal></term>
536 <listitem>
537 <para>
538 <indexterm><primary>paragraphs</primary>
539 <secondary>DSSSL flow objects</secondary></indexterm>
540 <indexterm><primary>nesting</primary>
541 <secondary>paragraphs (DSSSL)</secondary></indexterm>
542
543 A paragraph is used for any block of text. This may include not only
544 paragraphs in the source document, but also titles, the terms in a
545 definition list, glossary entries, and so on. Paragraphs in
546 <acronym>DSSSL</acronym> can be nested.
547 </para>
548 </listitem>
549 </varlistentry>
550 <varlistentry><term><literal>sequence</literal></term>
551 <listitem>
552 <para>
553 <indexterm><primary>sequences</primary>
554 <secondary>DSSSL flow objects</secondary></indexterm>
555
556 A sequence is a wrapper. It is most frequently used to change
557 inherited characteristics (like font style) of a set of flow objects
558 without introducing other semantics (such as line breaks).
559 </para>
560 </listitem>
561 </varlistentry>
562 <varlistentry><term><literal>score</literal></term>
563 <listitem>
564 <para>
565 <indexterm><primary>score (DSSSL flow object)</primary></indexterm>
566
567 A score flow object creates underlining, strike-throughs, or
568 overlining.
569 </para>
570 </listitem>
571 </varlistentry>
572 <varlistentry><term><literal>table</literal></term>
573 <listitem>
574 <para>
575 <indexterm><primary>tables</primary>
576 <secondary>DSSSL flow objects</secondary></indexterm>
577
578 A table flow object creates a table of rows and cells.</para>
579 </listitem>
580 </varlistentry>
581 </variablelist>
582 <para>
583 <indexterm><primary>HTML</primary>
584 <secondary>DSSSL flow objects</secondary></indexterm>
585
586 The &HTML; stylesheet uses the &SGML; backend, which has a different
587 selection of flow objects.
588 </para>
589 <variablelist>
590 <varlistentry><term><literal>element</literal></term>
591 <listitem>
592 <para>
593 <indexterm><primary>make expressions (DSSSL)</primary></indexterm>
594 <indexterm><primary>elements</primary>
595 <secondary>creating</secondary></indexterm>
596
597 Creates an element. The content of this <literal>make</literal>
598 expression will appear between the start and end tags. The expression:
599 </para>
600 <screen>
601 (make element gi: "H1"
602 (literal "Title"))
603 </screen>
604 <para>produces <literal>&lt;H1>Title&lt;/H1></literal>.</para>
605 </listitem>
606 </varlistentry>
607 <varlistentry><term><literal>empty-element</literal></term>
608 <listitem>
609 <para>
610 <indexterm><primary>empty elements</primary>
611 <secondary>creating</secondary></indexterm>
612 Creates an empty element that may not have content. The expression:</para>
613 <screen>
614 (make empty-element gi: "BR"
615 attributes: '(("CLEAR" "ALL")))
616 </screen>
617 <para>produces <literal>&lt;BR CLEAR="ALL"></literal>.</para>
618 </listitem>
619 </varlistentry>
620 <varlistentry><term><literal>sequence</literal></term>
621 <listitem>
622 <para>
623 <indexterm><primary>wrappers</primary>
624 <secondary>sequences (DSSSL)</secondary></indexterm>
625
626 Produces no output in of itself as a wrapper, but is still
627 required in <acronym>DSSSL</acronym> contexts in which you want to
628 output several flow objects but only one object top-level object may
629 be returned.
630 </para>
631 </listitem>
632 </varlistentry>
633 <varlistentry><term><literal>entity-ref</literal></term>
634 <listitem>
635 <para>
636 <indexterm><primary>entities</primary>
637 <secondary>entity-ref (DSSSL)</secondary></indexterm>
638 <indexterm><primary>references</primary>
639 <secondary>entities (DSSSL)</secondary></indexterm>
640 <indexterm><primary>entity-ref (DSSSL flow objects)</primary></indexterm>
641
642 Inserts an entity reference. The expression:</para>
643 <screen>(make entity-ref name: "nbsp")</screen>
644 <para>produces <literal>&amp;nbsp;</literal>.</para>
645 </listitem>
646 </varlistentry>
647 </variablelist>
648 <para>
649 <indexterm><primary>DSSSL</primary>
650 <secondary>stylesheets</secondary>
651 <tertiary>flow objects, empty</tertiary></indexterm>
652 <indexterm><primary>empty flow objects (DSSSL)</primary></indexterm>
653 <indexterm><primary>flow objects (DSSSL)</primary>
654 <secondary>empty</secondary></indexterm>
655
656 In both stylesheets, a completely empty flow object is constructed
657 with <literal>(empty-sosofo)</literal>.
658 </para>
659 </section>
660 <section id="dsssl.expr.data"><title>Selecting data</title>
661 <para>
662 <indexterm><primary>data, selecting (DSSSL)</primary></indexterm>
663 <indexterm><primary>selecting data (DSSSL)</primary></indexterm>
664
665 Extracting parts of the source document can be accomplished with these
666 functions:
667 </para>
668 <variablelist>
669 <varlistentry><term><literal>(data <replaceable>nd</replaceable>)</literal></term>
670 <listitem>
671 <para>
672 Returns all of the character data from <replaceable>nd</replaceable> as a
673 string.
674 </para>
675 </listitem>
676 </varlistentry>
677 <varlistentry><term><literal>(attribute-string "<replaceable>attr</replaceable>" <replaceable>nd</replaceable>)</literal></term>
678 <listitem>
679 <para>
680 Returns the value of the <replaceable>attr</replaceable> attribute of <replaceable>nd</replaceable>.
681 </para>
682 </listitem>
683 </varlistentry>
684 <varlistentry><term><literal>(inherited-attribute-string "<replaceable>attr</replaceable>" <replaceable>nd</replaceable>)</literal></term>
685 <listitem>
686 <para>
687 Returns the value of the <replaceable>attr</replaceable> attribute of
688 <replaceable>nd</replaceable>. If that attribute is not specified on
689 <replaceable>nd</replaceable>, it searches up the hierarchy for the first
690 ancestor element that does set the attribute, and returns its value.
691 </para>
692 </listitem>
693 </varlistentry>
694 </variablelist>
695 </section>
696 <section id="dsssl.expr.nodes"><title>Selecting elements</title>
697 <para>
698 <indexterm><primary>elements</primary>
699 <secondary>selecting</secondary></indexterm>
700 <indexterm><primary>selecting elements (DSSSL)</primary></indexterm>
701 <indexterm><primary>content</primary>
702 <secondary>reordering, DSSSL</secondary></indexterm>
703 <indexterm><primary>functions</primary>
704 <secondary>DSSSL</secondary>
705 <tertiary>elements, selecting</tertiary></indexterm>
706 <indexterm><primary>nodes (DSSSL)</primary></indexterm>
707
708 A common requirement of formatting is the ability to reorder content.
709 In order to do this, you must be able to select other elements in the
710 tree for processing. <acronym>DSSSL</acronym> provides a number of
711 functions that select other elements. These functions all return a
712 list of nodes.
713 </para>
714 <variablelist>
715 <varlistentry><term><literal>(current-node)</literal></term>
716 <listitem>
717 <para>Returns the current node.</para>
718 </listitem>
719 </varlistentry>
720 <varlistentry><term><literal>(children <replaceable>nd</replaceable>)</literal></term>
721 <listitem>
722 <para>Returns the children of <replaceable>nd</replaceable>.</para>
723 </listitem>
724 </varlistentry>
725 <varlistentry><term><literal>(descendants <replaceable>nd</replaceable>)</literal></term>
726 <listitem>
727 <para>
728 Returns the descendants of <replaceable>nd</replaceable> (the children of
729 <replaceable>nd</replaceable> and all their children's children, and so on).
730 </para>
731 </listitem>
732 </varlistentry>
733 <varlistentry><term><literal>(parent <replaceable>nd</replaceable>)</literal></term>
734 <listitem>
735 <para>Returns the parent of <replaceable>nd</replaceable>.</para>
736 </listitem>
737 </varlistentry>
738 <varlistentry><term><literal>(ancestor "<replaceable>name</replaceable>" <replaceable>nd</replaceable>)</literal></term>
739 <listitem>
740 <para>
741 Returns the first ancestor of <replaceable>nd</replaceable> named
742 <replaceable>name</replaceable>.
743 </para>
744 </listitem>
745 </varlistentry>
746 <varlistentry><term><literal>(element-with-id "<replaceable>id</replaceable>")</literal></term>
747 <listitem>
748 <para>
749 Returns the element in the document with the <acronym>ID</acronym>
750 <replaceable>id</replaceable>, if such an element exists.
751 </para>
752 </listitem>
753 </varlistentry>
754 <varlistentry><term><literal>(select-elements <replaceable>node-list</replaceable> "<replaceable>name</replaceable>")</literal></term>
755 <listitem>
756 <para>
757 Returns all of the elements of the <replaceable>node-list</replaceable> that have
758 the name <replaceable>name</replaceable>. For example, <literal>(select-elements
759 (descendants (current-node)) "para")</literal> returns a list of all
760 the paragraphs that are descendants of the current node.
761 </para>
762 </listitem>
763 </varlistentry>
764 <varlistentry><term><literal>(empty-node-list)</literal></term>
765 <listitem>
766 <para>
767 Returns a node list that contains no nodes.</para>
768 </listitem>
769 </varlistentry>
770 </variablelist>
771 <para>
772 Other functions allow you to manipulate node lists.
773 </para>
774 <variablelist>
775 <varlistentry><term><literal>(node-list-empty? <replaceable>nl</replaceable>)</literal></term>
776 <listitem>
777 <para>
778 Returns true if (and only if) <replaceable>nl</replaceable> is an empty node
779 list.
780 </para>
781 </listitem>
782 </varlistentry>
783 <varlistentry><term><literal>(node-list-length <replaceable>nl</replaceable>)</literal></term>
784 <listitem>
785 <para>Returns the number of nodes in <replaceable>nl</replaceable>.</para>
786 </listitem>
787 </varlistentry>
788 <varlistentry><term><literal>(node-list-first <replaceable>nl</replaceable>)</literal></term>
789 <listitem>
790 <para>
791 Returns a node list that consists of the single node that is the first
792 node in <replaceable>nl</replaceable>.
793 </para>
794 </listitem>
795 </varlistentry>
796 <varlistentry><term><literal>(node-list-rest <replaceable>nl</replaceable>)</literal></term>
797 <listitem>
798 <para>
799 Returns a node list that contains all of the nodes in
800 <replaceable>nl</replaceable> except the first node.
801 </para>
802 </listitem>
803 </varlistentry>
804 </variablelist>
805 <para>
806 <indexterm><primary>expressions (DSSSL)</primary>
807 <secondary>nodes and node lists, manipulating</secondary></indexterm>
808 <indexterm><primary>elements</primary>
809 <secondary>element expressions (DSSSL)</secondary></indexterm>
810 <indexterm><primary>lists</primary>
811 <secondary>nodes, manipulating</secondary></indexterm>
812
813 There are many other expressions for manipulating nodes and node
814 lists.
815 </para>
816 </section>
817 <section id="dsssl.expr.proc"><title>Processing expressions</title>
818 <para>
819 <indexterm><primary>processing expressions (DSSSL)</primary></indexterm>
820
821 Processing expressions control which elements in the document will be
822 processed and in what order. Processing an element is performed by
823 finding a matching element rule and using that rule.
824 </para>
825 <variablelist>
826 <varlistentry><term><literal>(process-children)</literal></term>
827 <listitem>
828 <para>
829 Processes all of the children of the current node. In most cases, if
830 no process expression is given, processing the children is the default
831 behavior.
832 </para>
833 </listitem>
834 </varlistentry>
835 <varlistentry><term><literal>(process-node-list <replaceable>nl</replaceable>)</literal></term>
836 <listitem>
837 <para>Processes each of the elements in <replaceable>nl</replaceable>.</para>
838 </listitem>
839 </varlistentry>
840 </variablelist>
841 </section>
842 <section id="dsssl.expr.define">
843 <title>Define expressions</title>
844 <para>
845 <indexterm><primary>functions</primary>
846 <secondary>declaring your own (DSSSL)</secondary></indexterm>
847 <indexterm><primary>constants (DSSSL)</primary></indexterm>
848 <indexterm><primary>declarations</primary>
849 <secondary>functions and constants (DSSSL)</secondary></indexterm>
850
851 You can declare your own functions and constants in
852 <acronym>DSSSL</acronym>. The general form of a function declaration
853 is:
854 <screen>
855 (define (<replaceable>function</replaceable> <replaceable>args</replaceable>)
856 <replaceable>function-body</replaceable>)
857 </screen>
858 A constant declaration is:
859 <screen>(define <replaceable>constant</replaceable>
860 <replaceable>constant-function-body</replaceable>)
861 </screen>
862 </para>
863 <para>
864 The distinction between constants and functions is that the body of a
865 constant is evaluated when the definition occurs, while functions are
866 evaluated when they are used.
867 </para>
868 </section>
869 <section id="dsssl.expr.conditional"><title>Conditionals</title>
870 <para>
871 <indexterm><primary>conditionals (DSSSL)</primary></indexterm>
872 <indexterm><primary>false, testing for (DSSSL)</primary></indexterm>
873 <indexterm><primary>true, testing for (DSSSL)</primary></indexterm>
874 <indexterm><primary>expressions (DSSSL)</primary>
875 <secondary>conditionals</secondary></indexterm>
876
877 In <acronym>DSSSL</acronym>, the constant <literal>#t</literal>
878 represents true and <literal>#f</literal> false. There are several
879 ways to test conditions and take action in <acronym>DSSSL</acronym>.
880 </para>
881 <variablelist>
882 <varlistentry><term><literal>if</literal></term>
883 <listitem>
884 <para>
885 <indexterm><primary>if expressions (DSSSL)</primary></indexterm>
886
887 The form of an <literal>if</literal> expression is:
888 <screen>
889 (if <replaceable>condition</replaceable>
890 <replaceable>true-expression</replaceable>
891 <replaceable>false-expression</replaceable>)
892 </screen>
893 </para>
894 <para>
895 If the condition is true, the
896 <replaceable>true-expression</replaceable> is evaluated, otherwise the
897 <replaceable>false-expression</replaceable> is evaluated. You must
898 always provide an expression to be evaluated when the condition is not
899 met. If you want to produce nothing, use
900 <literal>(empty-sosofo)</literal>.
901 </para>
902 </listitem>
903 </varlistentry>
904 <varlistentry><term><literal>case</literal></term>
905 <listitem>
906 <para>
907 <indexterm><primary>case expression (DSSSL)</primary></indexterm>
908
909 <literal>case</literal> selects from among several alternatives:
910 <screen>
911 (case <replaceable>expression</replaceable>
912 ((<replaceable>constant1</replaceable>) (<replaceable>expression1</replaceable>)
913 ((<replaceable>constant2</replaceable>) (<replaceable>expression2</replaceable>)
914 ((<replaceable>constant3</replaceable>) (<replaceable>expression3</replaceable>)
915 (else <replaceable>else-expression</replaceable>))
916 </screen>
917 </para>
918 <para>
919 The value of the expression is compared against each of the constants
920 in turn and the expression associated with the first matching constant
921 is evaulated.
922 </para>
923 </listitem>
924 </varlistentry>
925 <varlistentry><term><literal>cond</literal></term>
926 <listitem>
927 <para>
928 <indexterm><primary>conditionals (DSSSL)</primary></indexterm>
929
930 <literal>cond</literal> also selects from among several alternatives,
931 but the selection is performed by evaluating each expression:
932 <screen>
933 (cond
934 ((<replaceable>condition1</replaceable>) (<replaceable>expression1</replaceable>)
935 ((<replaceable>condition2</replaceable>) (<replaceable>expression2</replaceable>)
936 ((<replaceable>condition3</replaceable>) (<replaceable>expression3</replaceable>)
937 (else <replaceable>else-expression</replaceable>))
938 </screen>
939 </para>
940 <para>
941 The value of each conditional is calculated in turn. The expression
942 associated with the first condition that is true is evaluated.
943 </para>
944 </listitem>
945 </varlistentry>
946 </variablelist>
947 <para>
948 <indexterm><primary>empty sets (DSSSL)</primary></indexterm>
949
950 Any expression that returns <literal>#f</literal> is false; all other
951 expressions are true. This can be somewhat counterintuitive. In many
952 programming languages, it's common to assume that <quote>empty</quote>
953 things are false (0 is false, a null pointer is false, an empty set is
954 false, for example.) In <acronym>DSSSL</acronym>, this isn't the case;
955 note, for example, that an empty node list is not
956 <literal>#f</literal> and is therefore true. To avoid these
957 difficulties, always use functions that return true or false in
958 conditionals. To test for an empty node list, use
959 <literal>(node-list-empty?)</literal>.
960 </para>
961 </section>
962 <section id="dsssl.expr.let"><title>Let expressions</title>
963 <para>
964 <indexterm><primary>let expressions (DSSSL)</primary></indexterm>
965
966 The way to create local variables in <acronym>DSSSL</acronym> is with
967 <literal>(let)</literal>. The general form of a <literal>let</literal>
968 expression is:
969 <screen>
970 (let ((<replaceable>var1</replaceable> <abbrev>expression1</abbrev>)
971 (<replaceable>var2</replaceable> <abbrev>expression2</abbrev>)
972 ...
973 (<replaceable>varn</replaceable> <abbrev>expressionn</abbrev>))
974 <replaceable>let-body</replaceable>)
975 </screen>
976 </para>
977 <para>
978 <indexterm><primary>variables (DSSSL)</primary>
979 <secondary>let expressions</secondary></indexterm>
980 In a <literal>let</literal>; expression, all of the variables are
981 defined <quote>simultaneously.</quote> The expression that defines
982 <replaceable> var2</replaceable> cannot contain any references to any
983 other variables defined in the same <literal>let</literal>
984 expression. A <literal>let*</literal> expression allows variables to
985 refer to each other, but runs slightly slower.
986 </para>
987 <para>
988 <indexterm><primary>define expresssions (DSSSL)</primary></indexterm>
989 Variables are available only within the
990 <replaceable>let-body</replaceable>. A common use of
991 <literal>let</literal> is within a <literal>define</literal>
992 expression:
993 <screen>
994 (define (cals-rule-default nd)
995 (let* ((table (ancestor "table" nd))
996 (frame (if (attribute-string "frame" table)
997 (attribute-string "frame" table)
998 "all")))
999 (equal? frame "all")))
1000 </screen>
1001 </para>
1002 <para>
1003 This function creates two local variables <literal>table</literal> and
1004 <literal>frame</literal>. <literal>let</literal> returns the value of
1005 the last expression in the body, so this function returns true if the
1006 <sgmltag class="attribute">frame</sgmltag> attribute on the table is
1007 <literal> all</literal> or if no <sgmltag class="attribute">frame</sgmltag> attribute is present.
1008 </para>
1009 </section>
1010 <section id="dsssl.expr.loop"><title>Loops</title>
1011 <para>
1012 <indexterm><primary>for loop, DSSSL and</primary></indexterm>
1013 <indexterm><primary>loops, implementing (DSSSL)</primary></indexterm>
1014 <indexterm><primary>tail recursion (DSSSL)</primary></indexterm>
1015
1016 <acronym>DSSSL</acronym> doesn't have any construct that resembles the
1017 <quote>for loop</quote> that occurs in most imperative languages like C
1018 and Java. Instead, <acronym>DSSSL</acronym> employs a common trick in
1019 functional languages for implementing a loop: tail recursion.
1020 </para>
1021 <para>
1022 Loops in <acronym>DSSSL</acronym> use a special form of
1023 <literal>let</literal>. This loop counts from 1 to 10:
1024 <screen>
1025 (let <co id="dl1"/>loopvar <co id="dl2"/>((count 1))
1026 <co id="dl3"/>(if (> count 10)
1027 <co id="dl4"/>#t
1028 (<co id="dl5"/>loopvar <co id="dl6"/>(+ count 1))))</screen></para>
1029 <calloutlist>
1030 <callout arearefs="dl1">
1031 <para>This variable controls the loop. It is declared without an
1032 initial value, immediately after the <literal>let</literal>
1033 operand.</para>
1034 </callout>
1035 <callout arearefs="dl2">
1036 <para>
1037 <indexterm><primary>variables (DSSSL)</primary>
1038 <secondary>local, defining after loop variable</secondary></indexterm>
1039
1040 Any number of additional local variables can be defined after
1041 the loop variable, just as they can in any other
1042 <literal>let</literal> expression.</para>
1043 </callout>
1044 <callout arearefs="dl3">
1045 <para>If you ever want the loop to end, you have to put some sort of a
1046 test in it.</para>
1047 </callout>
1048 <callout arearefs="dl4">
1049 <para>This is the value that will be returned.</para>
1050 </callout>
1051 <callout arearefs="dl5">
1052 <para>Note that you iterate the loop by using the loop variable as if
1053 it was a function name.</para>
1054 </callout>
1055 <callout arearefs="dl6">
1056 <para>The arguments to this <quote>function</quote> are the values that
1057 you want the local variables declared in <xref linkend="dl2"/> to have
1058 in the next iteration.</para>
1059 </callout>
1060 </calloutlist>
1061 </section>
1062 </section>
1063 <section><title>A Closer Look at <xref linkend="dsssl.stylesheet"/></title>
1064 <para>
1065 <indexterm><primary>DSSSL</primary>
1066 <secondary>stylesheets</secondary>
1067 <tertiary>style specifications</tertiary></indexterm>
1068 <xref linkend="dsssl.stylesheet"/> is a style sheet that contains a
1069 style specification. Stylesheets may consist of multiple
1070 specifications, as we'll see in <xref linkend="dsssl.twostylesheets"/>.
1071 </para>
1072 <para>
1073 The actual <acronym>DSSSL</acronym> code goes in the style
1074 specification body, within the style specification. Each construction
1075 rule processes different elements from the source document.
1076 </para>
1077 <section><title>Processing chapters</title>
1078 <para>
1079 <indexterm><primary>chapters, processing (DSSSL)</primary></indexterm>
1080 <indexterm><primary>simple-page-sequence (DSSSL)</primary>
1081 <secondary>chapters, constructing</secondary></indexterm>
1082 <indexterm><primary>printing</primary>
1083 <secondary>stylesheets</secondary></indexterm>
1084
1085 <sgmltag>Chapter</sgmltag>s are processed by the <literal>chapter</literal>
1086 construction rule. Each <sgmltag>Chapter</sgmltag> is formatted as a
1087 <literal>simple-page-sequence</literal>. Every print stylesheet should format
1088 a document as one or more simple page sequences. Characteristics on
1089 the simple page sequence can specify headers and footers as well as
1090 margins and other page parameters.
1091 </para>
1092 <para>
1093 <indexterm><primary>nesting</primary>
1094 <secondary>simple page sequences (DSSSL), not allowed</secondary></indexterm>
1095
1096 One important note about simple page sequences: they cannot nest.
1097 This means that you cannot blindly process divisions
1098 (<sgmltag>Part</sgmltag>s, <sgmltag>Reference</sgmltag>) and the elements they
1099 contain (<sgmltag>Chapter</sgmltag>s, <sgmltag>RefEntry</sgmltag>s) as simple page
1100 sequences. This sometimes involves a little creativity.
1101 </para>
1102 </section>
1103 <section><title>Processing titles</title>
1104 <para>
1105 <indexterm><primary>titles</primary>
1106 <secondary>processing, DSSSL</secondary></indexterm>
1107
1108 The <literal>make</literal> expression in the <literal>title</literal> element rule
1109 ensures that <sgmltag>Title</sgmltag>s are formatted in large, bold print.
1110 </para>
1111 <para>
1112 This construction rule applies equally to <sgmltag>Chapter</sgmltag> titles,
1113 <sgmltag>Figure</sgmltag> titles, and <sgmltag>Book</sgmltag> titles. It's unlikely
1114 that you'd want all of these titles to be presented in the same way,
1115 so a more robust stylesheet would have to arrange the processing of
1116 titles with more context. This might be achieved in the way that
1117 nested <sgmltag>Emphasis</sgmltag> elements are handled in <xref linkend="dsssl.nestedemph"/>.
1118 </para>
1119 </section>
1120 <section><title>Processing paragraphs</title>
1121 <para>
1122 <sgmltag>Para</sgmltag> elements are simply formatted as paragraphs.
1123 <indexterm><primary>Para element</primary>
1124 <secondary>DSSSL, processing</secondary></indexterm>
1125
1126 </para>
1127 </section>
1128 <section id="dsssl.nestedemph"><title>Processing emphasis</title>
1129 <para>
1130 <indexterm><primary>Emphasis element</primary>
1131 <secondary>DSSSL, processing</secondary></indexterm>
1132 <indexterm><primary>nesting</primary>
1133 <secondary>emphasis elements, DSSSL processing</secondary></indexterm>
1134
1135 Processing <sgmltag>Emphasis</sgmltag> elements is made a little more
1136 interesting because we want to consider an attribute value and the
1137 possibility that <sgmltag>Emphasis</sgmltag> elements can be nested.
1138 </para>
1139 <para>
1140 In the simple case, in which we're processing an <sgmltag>Emphasis</sgmltag>
1141 element that is not nested, we begin by testing the value of the
1142 <sgmltag class='attribute'>role</sgmltag> attribute. If the content of that
1143 attribute is the string <literal>strong</literal>, it is formatted
1144 in bold; otherwise, it is formatted in italic.
1145 </para>
1146 <para>
1147 The nested case is handled by the <literal>(emphasis emphasis)</literal>
1148 rule. This rule simply formats the content using an upright
1149 (nonitalic) font. This rule, like the rule for <sgmltag>Title</sgmltag>s, is
1150 not robust. <sgmltag>Emphasis</sgmltag> nested inside
1151 <literal>strong</literal> <sgmltag>Emphasis</sgmltag> won't be distinguished,
1152 for example, and nestings more than two elements deep will be handled
1153 just as nestings that are two deep.
1154 </para>
1155 </section>
1156 <section><title>Processing subscripts and superscripts</title>
1157 <para>
1158 <indexterm><primary>Subscript element</primary>
1159 <secondary>DSSSL, processing</secondary></indexterm>
1160 <indexterm><primary>Superscript element</primary>
1161 <secondary>DSSSL, processing</secondary></indexterm>
1162
1163 Processing <sgmltag>Subscript</sgmltag> and <sgmltag>Superscript</sgmltag> elements
1164 is really handled by the <literal>super-sub-script</literal> function.
1165 There are several interesting things about this function:
1166 </para>
1167 <variablelist>
1168 <varlistentry><term>The <literal>plus-or-minus</literal> argument</term>
1169 <listitem>
1170 <para>
1171 <indexterm><primary>Scheme language</primary>
1172 <secondary>function, passing as argument</secondary></indexterm>
1173 <indexterm><primary>functions</primary>
1174 <secondary>Scheme, passing as argument</secondary></indexterm>
1175
1176 You might ordinarily think of passing a keyword or boolean argument to
1177 the <function>super-sub-script</function> function to indicate whether
1178 subscripts or superscripts are desired. But with Scheme, it's
1179 possible to pass the actual function as an argument!
1180 </para>
1181 <para>
1182 Note that in the element construction rules for
1183 <sgmltag>Superscript</sgmltag> and <sgmltag>Subscript</sgmltag>, we pass the actual
1184 functions <literal>+</literal> and <literal>-</literal>. In the body of
1185 <function>super-sub-script</function>, we use the
1186 <literal>plus-or-minus</literal> argument as a function name (it
1187 appears immediately after an open parenthesis).
1188 </para>
1189 </listitem></varlistentry>
1190 <varlistentry><term>The optional argument</term>
1191 <listitem>
1192 <para>
1193 <indexterm><primary>optional arguments (DSSSL functions)</primary></indexterm>
1194
1195 <literal>optional</literal> arguments are indicated by
1196 <literal>#!optional</literal> in the function declaration. Any number of
1197 <literal>optional</literal> arguments may be given, but each must
1198 specify a default value. This is accomplished by listing each
1199 argument and default value (an expression) as a pair.
1200 </para>
1201 <para>
1202 In <function>super-sub-script</function>, the optional argument
1203 <literal>sosofo</literal> is initialized to <literal>process-children</literal>.
1204 This means that at the point where the function is
1205 <emphasis>called</emphasis>, <literal>process-children</literal> is evaluated and the
1206 resulting <literal>sosofo</literal> is passed to the function.
1207 </para>
1208 </listitem></varlistentry>
1209 <varlistentry><term>Use of inherited characteristics</term>
1210 <listitem>
1211 <para>
1212 <indexterm><primary>inherited characteristics, calculating new values with</primary></indexterm>
1213
1214 It is possible to use the <quote>current</quote> value of an inherited
1215 characteristic to calculate a new value. Using this technique,
1216 superscripts and subscripts will be presented at 80 percent of the
1217 current font size.
1218 </para>
1219 </listitem></varlistentry>
1220 </variablelist>
1221 </section>
1222 </section>
1223 <section><title>Customizing the Stylesheets</title>
1224 <para>
1225 <indexterm><primary>DSSSL</primary>
1226 <secondary>stylesheets</secondary>
1227 <tertiary>customizing</tertiary></indexterm>
1228 <indexterm><primary>customizing</primary>
1229 <secondary>DSSSL stylesheets</secondary></indexterm>
1230 <indexterm><primary>driver files</primary>
1231 <secondary>DSSSL stylsheets, customizing</secondary></indexterm>
1232
1233 The best way to customize the stylesheets is to write your own
1234 <quote>driver</quote> file; this is a stylesheet that contains your
1235 local modifications and then includes the appropriate stylesheet from
1236 the standard distribution by reference. This allows you to make local
1237 changes and extensions without modifying the distributed files, which
1238 makes upgrading to the next release much simpler.
1239 </para>
1240 <section id="dsssl.driver"><title>Writing Your Own Driver</title>
1241 <para>
1242 A basic driver file looks like this:
1243 <screen>
1244 <![CDATA[<!DOCTYPE style-sheet PUBLIC "-//James Clark//DTD DSSSL Style Sheet//EN" [
1245 <!ENTITY dbstyle PUBLIC "-//Norman Walsh//DOCUMENT DocBook Print Stylesheet//EN" CDATA DSSSL>
1246 ]>
1247
1248 <style-sheet>
1249 <style-specification use="docbook">
1250 <style-specification-body>
1251
1252 ;; your changes go here...
1253
1254 </style-specification-body>
1255 </style-specification>
1256 <external-specification id="docbook" document="dbstyle">
1257 </style-sheet>
1258 ]]></screen>
1259 </para>
1260 <para>
1261 <indexterm><primary>public identifiers</primary>
1262 <secondary>Modular DocBook Stylesheets</secondary></indexterm>
1263 <indexterm><primary>Modular DocBook Stylesheets</primary>
1264 <secondary>public identifiers</secondary></indexterm>
1265
1266 There are two public identifiers associated with the Modular DocBook
1267 Stylesheets:
1268 <itemizedlist>
1269 <listitem><para><literal>-//Norman Walsh//DOCUMENT DocBook Print Stylesheet//EN</literal></para>
1270 </listitem>
1271 <listitem><para><literal>-//Norman Walsh//DOCUMENT DocBook HTML Stylesheet//EN</literal></para>
1272 </listitem>
1273 </itemizedlist>
1274 <indexterm><primary>printing</primary>
1275 <secondary>stylesheets</secondary></indexterm>
1276 <indexterm><primary>HTML</primary>
1277 <secondary>stylesheets</secondary>
1278 <tertiary>selecting</tertiary></indexterm>
1279 <indexterm><primary>catalog files</primary>
1280 <secondary>Open (SGML)</secondary></indexterm>
1281
1282 The former selects the print stylesheet and the latter selects the
1283 &HTML; stylesheet. There is an &SGML; Open catalog file in the
1284 distribution that maps these public identifiers to the stylesheet
1285 files.</para> <para>You can add your own definitions, or
1286 redefinitions, of stylesheet rules and parameters so that
1287 <screen>
1288 ;; your changes go here...
1289 </screen>
1290 occurs in the previous example.
1291 </para>
1292 <para>
1293 <indexterm><primary>driver files</primary>
1294 <secondary>example</secondary></indexterm>
1295
1296 For a concrete example of a driver file, see
1297 <filename>plain.dsl</filename> in the
1298 <filename>docbook/print</filename> directory in the stylesheet
1299 distribution (or on the <link linkend="app-cdrom"><acronym>CD-ROM</acronym></link>). This is a
1300 customization of the print stylesheet, which turns off title page and
1301 <acronym>TOC</acronym> generation.
1302 </para>
1303 </section>
1304 <section><title>Changing the Localization</title>
1305 <para>
1306 <indexterm><primary>DocBook DTD</primary>
1307 <secondary>stylesheets</secondary>
1308 <tertiary>languages</tertiary></indexterm>
1309 <indexterm><primary>languages</primary>
1310 <secondary>DocBook stylesheets</secondary></indexterm>
1311
1312 As distributed, the stylesheets use English for all generated text,
1313 but other localization files are also provided. The languages supported
1314 at the time of this writing are summarized in
1315 <xref linkend="tbl.lang"/>.
1316 (If you can write a localization for another language,
1317 <emphasis>please</emphasis> contribute it.)
1318 </para>
1319 <para>
1320 <indexterm><primary>Lang attribute</primary></indexterm>
1321 <indexterm><primary>customizing</primary>
1322 <secondary>DSSSL stylesheets</secondary>
1323 <tertiary>language, changing</tertiary></indexterm>
1324
1325 There are two ways to switch languages: by specifying a <sgmltag class="attribute">lang</sgmltag> attribute, or by changing the
1326 default language in a customization.
1327 </para>
1328 <section><title>Using the <sgmltag class="attribute">lang</sgmltag> attribute</title>
1329 <para>
1330 <indexterm><primary>DocBook DTD</primary>
1331 <secondary>Lang attribute</secondary></indexterm>
1332 <indexterm><primary>languages</primary>
1333 <secondary>codes</secondary></indexterm>
1334
1335 One of the DocBook common attributes is <sgmltag class="attribute">
1336 lang</sgmltag>. If you specify a language, the DocBook stylesheets
1337 will use that language (and all its descendants, if no other language
1338 is specified) for generated text within that element.
1339 </para>
1340 <para>
1341 <xref linkend="tbl.lang"/> summarizes the language codes for the
1342 supported languages.<footnote>
1343 <para>
1344 Language codes should conform to <acronym>IETF</acronym>
1345 <acronym>RFC</acronym> 3066.
1346 </para>
1347 </footnote>
1348 <indexterm><primary>French language chapter, example</primary></indexterm>
1349
1350 The following chapter uses text generated in French:
1351 <screen><![CDATA[
1352 <chapter lang="fr"><title>B]]>&ecirc;<![CDATA[tises</title>
1353 <para>Pierre qui roule n'amasse pas de mousse.</para>
1354 </chapter>
1355 ]]></screen>
1356 </para>
1357
1358 <table id="tbl.lang">
1359 <title>DocBook Stylesheet Language Codes</title>
1360 <tgroup cols="2">
1361 <?dbhtml cellpadding="2px"?>
1362 <colspec colname="COLSPEC0" colwidth="2in" colsep="1"/>
1363 <colspec colname="COLSPEC1"/>
1364 <thead>
1365 <row rowsep="1">
1366 <entry colname="COLSPEC0" rowsep="1" align="left" char="">Language Code</entry>
1367 <entry colname="COLSPEC1" rowsep="1" align="left" char="">Language</entry>
1368 </row>
1369 </thead>
1370 <tbody>
1371 <row>
1372 <entry colname="COLSPEC0"><literal>af</literal></entry>
1373 <entry colname="COLSPEC1">Afrikaans</entry>
1374 </row>
1375 <row>
1376 <entry colname="COLSPEC0"><literal>ca</literal></entry>
1377 <entry colname="COLSPEC1">Catalan</entry>
1378 </row>
1379 <row>
1380 <entry colname="COLSPEC0"><literal>cs</literal></entry>
1381 <entry colname="COLSPEC1">Czech</entry>
1382 </row>
1383 <row>
1384 <entry colname="COLSPEC0"><literal>da</literal></entry>
1385 <entry colname="COLSPEC1">Danish</entry>
1386 </row>
1387 <row>
1388 <entry colname="COLSPEC0"><literal>de</literal></entry>
1389 <entry colname="COLSPEC1">German</entry>
1390 </row>
1391 <row>
1392 <entry colname="COLSPEC0"><literal>el</literal></entry>
1393 <entry colname="COLSPEC1">Greek</entry>
1394 </row>
1395 <row>
1396 <entry colname="COLSPEC0"><literal>en</literal></entry>
1397 <entry colname="COLSPEC1">English</entry>
1398 </row>
1399 <row>
1400 <entry colname="COLSPEC0"><literal>es</literal></entry>
1401 <entry colname="COLSPEC1">Spanish</entry>
1402 </row>
1403 <row>
1404 <entry colname="COLSPEC0"><literal>et</literal></entry>
1405 <entry colname="COLSPEC1">Estonian</entry>
1406 </row>
1407 <row>
1408 <entry colname="COLSPEC0"><literal>eu</literal></entry>
1409 <entry colname="COLSPEC1">Basque</entry>
1410 </row>
1411 <row>
1412 <entry colname="COLSPEC0"><literal>fi</literal></entry>
1413 <entry colname="COLSPEC1">Finnish</entry>
1414 </row>
1415 <row>
1416 <entry colname="COLSPEC0"><literal>fr</literal></entry>
1417 <entry colname="COLSPEC1">French</entry>
1418 </row>
1419 <row>
1420 <entry colname="COLSPEC0"><literal>he</literal></entry>
1421 <entry colname="COLSPEC1">Hebrew</entry>
1422 </row>
1423 <row>
1424 <entry colname="COLSPEC0"><literal>hu</literal></entry>
1425 <entry colname="COLSPEC1">Hungarian</entry>
1426 </row>
1427 <row>
1428 <entry colname="COLSPEC0"><literal>id</literal></entry>
1429 <entry colname="COLSPEC1">Indonesian</entry>
1430 </row>
1431 <row>
1432 <entry colname="COLSPEC0"><literal>it</literal></entry>
1433 <entry colname="COLSPEC1">Italian</entry>
1434 </row>
1435 <row>
1436 <entry colname="COLSPEC0"><literal>ja</literal></entry>
1437 <entry colname="COLSPEC1">Japanese</entry>
1438 </row>
1439 <row>
1440 <entry colname="COLSPEC0"><literal>ko</literal></entry>
1441 <entry colname="COLSPEC1">Korean</entry>
1442 </row>
1443 <row>
1444 <entry colname="COLSPEC0"><literal>nl</literal></entry>
1445 <entry colname="COLSPEC1">Dutch</entry>
1446 </row>
1447 <row>
1448 <entry colname="COLSPEC0"><literal>nn</literal></entry>
1449 <entry colname="COLSPEC1">Nyorsk</entry>
1450 </row>
1451 <row>
1452 <entry colname="COLSPEC0"><literal>no</literal></entry>
1453 <entry colname="COLSPEC1">Norwegian</entry>
1454 </row>
1455 <row>
1456 <entry colname="COLSPEC0"><literal>pl</literal></entry>
1457 <entry colname="COLSPEC1">Polish</entry>
1458 </row>
1459 <row>
1460 <entry colname="COLSPEC0"><literal>pt</literal></entry>
1461 <entry colname="COLSPEC1">Portuguese</entry>
1462 </row>
1463 <row>
1464 <entry colname="COLSPEC0"><literal>pt-br</literal></entry>
1465 <entry colname="COLSPEC1">Portuguese (Brazil)</entry>
1466 </row>
1467 <row>
1468 <entry colname="COLSPEC0"><literal>ro</literal></entry>
1469 <entry colname="COLSPEC1">Romanian</entry>
1470 </row>
1471 <row>
1472 <entry colname="COLSPEC0"><literal>ru</literal></entry>
1473 <entry colname="COLSPEC1">Russian</entry>
1474 </row>
1475 <row>
1476 <entry colname="COLSPEC0"><literal>sk</literal></entry>
1477 <entry colname="COLSPEC1">Slovak</entry>
1478 </row>
1479 <row>
1480 <entry colname="COLSPEC0"><literal>sl</literal></entry>
1481 <entry colname="COLSPEC1">Slovenian</entry>
1482 </row>
1483 <row>
1484 <entry colname="COLSPEC0"><literal>sv</literal></entry>
1485 <entry colname="COLSPEC1">Swedish</entry>
1486 </row>
1487 <row>
1488 <entry colname="COLSPEC0"><literal>th</literal></entry>
1489 <entry colname="COLSPEC1">Thai</entry>
1490 </row>
1491 <row>
1492 <entry colname="COLSPEC0"><literal>tr</literal></entry>
1493 <entry colname="COLSPEC1">Turkish</entry>
1494 </row>
1495 <row>
1496 <entry colname="COLSPEC0"><literal>uk</literal></entry>
1497 <entry colname="COLSPEC1">Ukranian</entry>
1498 </row>
1499 <row>
1500 <entry colname="COLSPEC0"><literal>xh</literal></entry>
1501 <entry colname="COLSPEC1">Xhosa</entry>
1502 </row>
1503 <row>
1504 <entry colname="COLSPEC0"><literal>zh-cn</literal></entry>
1505 <entry colname="COLSPEC1">Chinese (Continental)</entry>
1506 </row>
1507 <row>
1508 <entry colname="COLSPEC0"><literal>zh-tw</literal></entry>
1509 <entry colname="COLSPEC1">Chinese (Traditional)</entry>
1510 </row>
1511 </tbody>
1512 </tgroup>
1513 </table>
1514 </section>
1515 <section><title>Changing the default language</title>
1516 <para>
1517 <indexterm><primary>languages</primary>
1518 <secondary>default, changing</secondary></indexterm>
1519 <indexterm><primary>driver files</primary>
1520 <secondary>language, changing default</secondary></indexterm>
1521
1522 If no <sgmltag class="attribute">lang</sgmltag> attribute is
1523 specified, the default language is used. You can change the default
1524 language with a <link linkend="dsssl.driver">driver</link>.
1525 </para>
1526 <para>
1527 In the driver, define the default language. <xref linkend="tbl.lang"/>
1528 summarizes the language codes for the supported languages. The
1529 following driver makes German the default language:
1530 <screen><![CDATA[
1531 <!DOCTYPE style-sheet PUBLIC "-//James Clark//DTD DSSSL Style Sheet//EN" [
1532 <!ENTITY dbstyle PUBLIC "-//Norman Walsh//DOCUMENT DocBook Print Stylesheet//EN" CDATA DSSSL>
1533 ]>
1534
1535 <style-sheet>
1536 <style-specification use="docbook">
1537 <style-specification-body>
1538
1539 (define %default-language% "dege")
1540
1541 </style-specification-body>
1542 </style-specification>
1543 <external-specification id="docbook" document="dbstyle">
1544 </style-sheet>
1545 ]]></screen>
1546 </para>
1547 <para>
1548 <indexterm><primary>%gentext-language%</primary></indexterm>
1549
1550 There are two other settings that can be changed only in a driver. Both
1551 of these settings are turned off in the distributed stylesheet:
1552 <variablelist>
1553 <varlistentry><term><literal>%gentext-language%</literal></term>
1554 <listitem>
1555 <para>
1556 If a language code is specified in
1557 <literal>%gentext-language%</literal>, then that language will be used
1558 for all generated text, regardless of any <sgmltag class="attribute">lang</sgmltag> attribute settings in the
1559 document.
1560 </para>
1561 </listitem>
1562 </varlistentry>
1563 <varlistentry><term><literal>%gentext-use-xref-language%</literal></term>
1564 <listitem>
1565 <para>
1566 <indexterm><primary>%gentext-use-xref-language%</primary></indexterm>
1567 <indexterm><primary>cross references</primary>
1568 <secondary>languages, changing (stylesheets)</secondary></indexterm>
1569
1570 If turned on (defined as <literal>#t</literal>), then the stylesheets
1571 will generate the text associated with a cross reference using the
1572 language of the target, not the current language. Consider the
1573 following book:
1574 </para>
1575 <screen><![CDATA[
1576 <book><title>A Test Book</title>
1577 <preface>
1578 <para>There are three chapters in this book: <xref linkend="c1">,
1579 <xref linkend="c2">, and <xref linkend="c3">.
1580 </para>
1581 </preface>
1582 <chapter lang="usen"><title>English</title> ... </chapter>
1583 <chapter lang="fr"><title>French</title> ... </chapter>
1584 <chapter lang="dege"><title>Deutsch</title> ... </chapter>
1585 </book>
1586 ]]></screen>
1587 <para>
1588 The standard stylesheets render the Preface as something like this:
1589 </para>
1590 <blockquote>
1591 <para>There are three chapters in this book: Chapter 1, Chapter 2, and Chapter
1592 3.</para>
1593 </blockquote>
1594 <para>
1595 With <literal>%gentext-use-xref-language%</literal> turned on, it
1596 would render like this:
1597 </para>
1598 <blockquote>
1599 <para>There are are three chapters in this book: Chapter 1, Chapitre 2, and
1600 Kapitel 3.</para>
1601 </blockquote>
1602 </listitem>
1603 </varlistentry>
1604 </variablelist></para>
1605 </section>
1606 </section>
1607 <section id="dsssl.twostylesheets"><title>A Single Stylesheet for Both Print and &HTML;</title>
1608 <para>
1609 <indexterm><primary>stylesheets</primary>
1610 <secondary>HTML and print, using single</secondary></indexterm>
1611 <indexterm><primary>printing</primary>
1612 <secondary>stylesheets</secondary></indexterm>
1613 <indexterm><primary>HTML</primary>
1614 <secondary>stylesheets</secondary></indexterm>
1615
1616 A <acronym>DSSSL</acronym> stylesheet consists of one or more
1617 <quote>style specifications.</quote> Using more than one style
1618 specification allows you to build a single stylesheet file that can
1619 format with either the print or &SGML; backends. <xref linkend="ex-twostyles"/> shows a stylesheet with two style
1620 specifications.
1621 <indexterm><primary>DSSSL</primary>
1622 <secondary>stylesheets</secondary>
1623 <tertiary>style specifications, multiple</tertiary></indexterm>
1624 </para>
1625 <example id="ex-twostyles">
1626 <title><filename>both.dsl</filename>: A Stylesheet with Two Style Specifications</title>
1627 <screen>
1628 <inlinegraphic format="linespecific" fileref="examples/dsssl.twostyles"/>
1629 </screen>
1630 </example>
1631 <para>
1632 <indexterm><primary>Jade</primary>
1633 <secondary>style specifications, indicating selected</secondary></indexterm>
1634
1635 Once you have stylesheets with more than one style specification, you
1636 have to be able to indicate which style specification you want to
1637 use. In Jade, you indicate this by providing the <acronym>ID</acronym>
1638 of the style specification after the stylesheet filename, separated
1639 with a hash mark: <literal>#</literal>.
1640 </para>
1641 <para>
1642 Using the code from <xref linkend="ex-twostyles"/>, you can format a
1643 document using the print stylesheet by running:
1644 </para>
1645 <screen>
1646 jade -t rtf -d both.dsl#print file.sgm
1647 </screen>
1648 <para>
1649 and using the &HTML; stylesheet by running:
1650 </para>
1651 <screen>
1652 jade -t sgml -d both.dsl#html file.sgm
1653 </screen>
1654 </section>
1655 </section>
1656 <section id="jademultdecl"><title>Dealing with Multiple Declarations</title>
1657 <para>
1658 <indexterm><primary>declarations</primary>
1659 <secondary>multiple, using</secondary></indexterm>
1660
1661 The DocBook &SGML; &DTD; and the DocBook <acronym>DSSSL</acronym>
1662 Stylesheets happen to use the same &SGML; declaration. This makes it
1663 very easy to run Jade with DocBook. However, you may sometimes wish
1664 to use Jade with other document types, for example the DocBook &XML;
1665 &DTD;, which has a different declaration. There are a couple of ways
1666 to do this.
1667 </para>
1668 <section><title>Pass the Declaration Explicitly</title>
1669 <para>
1670
1671 If your stylesheets parse fine with the default declaration, but you
1672 want to use an alternate declaration with a particular document, just
1673 pass the declaration on the command line:
1674 <screen>
1675 jade <replaceable>options</replaceable> the-declaration the-document
1676 </screen>
1677 Note that there's no option required before the declaration; it simply
1678 occurs before the first filename. Jade concatenates all of the files
1679 that you give it together, and parses them as if they were one
1680 document.
1681 </para>
1682 </section>
1683 <section><title>Use the Catalogs</title>
1684 <para>
1685 The other way to fix this is with a little catalog trickery.
1686 </para>
1687 <para>
1688 <indexterm><primary>catalog files</primary>
1689 <secondary>multiple declarations, using</secondary></indexterm>
1690
1691 First, note that Jade always looks in the file called
1692 <filename>catalog</filename> in the same directory as the document
1693 that it is loading, and uses settings in that file in preference to
1694 settings in other catalogs.
1695 </para>
1696 <para>
1697 With this fact, we can employ the following trick:
1698 </para>
1699 <itemizedlist>
1700 <listitem>
1701 <para>Put a <filename>catalog</filename> file in the directory that
1702 contains your stylesheets, which contain an
1703 <literal>SGMLDECL</literal> directive. Jade understands the
1704 directive, which points to the &SGML; declaration that you should use
1705 when parsing the stylesheets. For the DocBook stylesheets, the DocBook
1706 declaration works fine.</para>
1707 </listitem>
1708 <listitem>
1709 <para>In the directory that contains the document you want to process,
1710 create a <filename>catalog</filename> file that contains an
1711 <literal>SGMLDECL</literal> directive that points to the &SGML;
1712 declaration that should be used when parsing the document.</para>
1713 </listitem>
1714 </itemizedlist>
1715 <para>
1716 There's no easy way to have both the stylesheet and the document in
1717 the same directory if they must be processed with different
1718 declarations. But this is usually not too inconvenient.
1719 </para>
1720 </section>
1721 </section>
1722 <section id="dsssl.arch"><title>The <acronym>DSSSL</acronym> Architecture</title>
1723 <para>
1724 <indexterm><primary>architecture</primary>
1725 <secondary>DSSSL</secondary></indexterm>
1726 <indexterm><primary>DSSSL</primary>
1727 <secondary>architecture</secondary></indexterm>
1728 <indexterm><primary>attributes</primary>
1729 <secondary>DSSSL architecture</secondary></indexterm>
1730 <indexterm><primary>elements</primary>
1731 <secondary>DSSSL architecture and</secondary></indexterm>
1732
1733 The concept of an architecture was promoted by HyTime. In some ways,
1734 it takes the standard &SGML;/&XML; notions of the role of elements and
1735 attributes and inverts them. Instead of relying on the name of an
1736 element to assign its primary semantics, it uses the values of a small
1737 set of fixed attributes.
1738 </para>
1739 <para>
1740 While this may be counterintuitive initially, it has an interesting
1741 benefit. An architecture-aware processor can work transparently with
1742 many different &DTD;s. A small example will help illustrate this
1743 point.
1744 </para>
1745 <note>
1746 <para>The following example demonstrates the concept behind
1747 architectures, but for the sake of simplicity, it does not properly
1748 implement an architecture as defined in HyTime.</para>
1749 </note>
1750 <para>
1751 Imagine that you wrote an application that can read an &SGML;/&XML;
1752 document containing a letter (conforming to some letter &DTD;), and
1753 automatically print an envelope for the letter. It's easy to envision
1754 how this works. The application reads the content of the letter,
1755 extracts the address and return address elements from the source, and
1756 uses them to generate an envelope:
1757 </para>
1758 <screen><![CDATA[<?xml version='1.0'>
1759 <!DOCTYPE letter "/share/sgml/letter/letter.dtd" [
1760 <!ENTITY myaddress "/share/sgml/entities/myaddress.xml">
1761 ]>
1762 <letter>
1763 <returnaddress>&myaddress;</returnaddress>
1764 <address>
1765 <name>Leonard Muellner</name>
1766 <company>O'Reilly &amp; Associates</company>
1767 <street>90 Sherman Street</street>
1768 <city>Cambridge</city><state>MA</state><zip>02140</zip>
1769 </address>
1770 <body>
1771 <salutation>Hi Lenny</salutation>
1772 ...
1773 </body>]]></screen>
1774 <para>
1775 The processor extracts the <sgmltag>Returnaddress</sgmltag> and
1776 <sgmltag role="nolink">Address</sgmltag> elements and their children
1777 and prints the envelope accordingly.
1778 </para>
1779 <para>
1780 Now suppose that a colleague from payroll comes by and asks you to
1781 adapt the application to print envelopes for mailing checks, using the
1782 information in the payroll database, which has a different &DTD;. And
1783 a week later, someone from sales comes by and asks if you can modify
1784 the application to use the contact information &DTD;. After a while,
1785 you would have 11 versions of this program to maintain.
1786 </para>
1787 <para>
1788 <indexterm><primary>fixed attributes</primary></indexterm>
1789
1790 Suppose that instead of using the actual element names to locate the addresses
1791 in the documents, you asked each person to add a few attributes to their &DTD;.
1792 By forcing the attributes to have fixed values, they'd automatically be present
1793 in each document, but authors would never have to worry about them.
1794 </para>
1795 <para>
1796 For example, the address part of the letter &DTD; might look like
1797 this:
1798 </para>
1799 <screen>
1800 <inlinegraphic format="linespecific" fileref="examples/arch-letter.dtd"/>
1801 </screen>
1802 <para>
1803 Effectively, each address in a letter would look like this:
1804 </para>
1805 <screen>
1806 <inlinegraphic format="linespecific" fileref="examples/arch-letter.xml"/>
1807 </screen>
1808 <para>
1809 In practice, the author would not include the <sgmltag class="attribute">ADDRESS</sgmltag> attributes; they are automatically
1810 provided by the &DTD; because they are
1811 <literal>#FIXED</literal>.<footnote>
1812 <para>
1813 The use of uppercase names here is intentional. These are not
1814 attributes that an author is ever expected to type. In &XML;, which is
1815 case-sensitive, using uppercase for things like this reduces the
1816 likelihood of collision with <quote>real</quote> attribute names in the
1817 &DTD;.
1818 </para>
1819 </footnote>
1820 </para>
1821 <para>
1822 Now the address portion of the payroll &DTD; might look like this:
1823 </para>
1824 <screen>
1825 <inlinegraphic format="linespecific" fileref="examples/arch-payroll.dtd"/>
1826 </screen>
1827 <para>
1828 The employee records will look like this:
1829 </para>
1830 <screen>
1831 <inlinegraphic format="linespecific" fileref="examples/arch-payroll.xml"/>
1832 </screen>
1833 <para>
1834 Your application no longer cares about the actual element names. It
1835 simply looks for the elements with the correct attributes and uses
1836 them. This is the power of an architecture: it provides a level of
1837 abstraction that processing applications can use to their
1838 advantage. In practice, architectural forms are a bit more complex to
1839 set up because they have facilities for dealing with attribute name
1840 conflicts, among other things.
1841 </para>
1842 <para>
1843 Why have we told you all this? Because <acronym>DSSSL</acronym> is an
1844 architecture. This means you can modify the stylesheet &DTD; and still
1845 run your stylesheets through Jade.
1846 </para>
1847 <para>
1848 Consider the case presented earlier in <xref linkend="ex-twostyles"/>.
1849 In order to use this stylesheet, you must specify three things: the
1850 backend you want to use, the stylesheet you want to use, and the
1851 style specification you want to use. If you mismatch any of the
1852 parameters, you'll get the wrong results. In practice, the problem is
1853 compounded further:
1854 </para>
1855 <itemizedlist>
1856 <listitem>
1857 <para>Some stylesheets support several backends
1858 (<acronym>RTF</acronym>, &TeX;, and &SGML;).
1859 <indexterm><primary>backends, stylesheet support</primary></indexterm>
1860 <indexterm><primary>stylesheets</primary>
1861 <secondary>backends, supporting</secondary></indexterm>
1862
1863 </para>
1864 </listitem>
1865 <listitem>
1866 <para>Some stylesheets support only some backends
1867 (<acronym>RTF</acronym> and &SGML;, but not &TeX; or
1868 <acronym>MIF</acronym>).</para>
1869 </listitem>
1870 <listitem>
1871 <para>Some stylesheets support multiple outputs using the same
1872 backend (several kinds of &HTML; output, for example, using the
1873 &SGML; backend: &HTML;, &HTML;Help, JavaHelp, and so on).</para>
1874 </listitem>
1875 <listitem>
1876 <para>If you have complex stylesheets, some backends may require
1877 additional options to define parameter entities or stylesheet
1878 options.</para>
1879 </listitem>
1880 </itemizedlist>
1881 <para>
1882 None of this complexity is really necessary, after all, the options
1883 don't change&mdash;you just have to use the correct combinations. The
1884 mental model is really something like this: <quote>I want a certain
1885 kind of output, &TeX; say, so I have to use this combination of
1886 parameters.</quote>
1887 </para>
1888 <para>
1889 You can summarize this information in a table to help keep track of
1890 it:
1891 </para>
1892
1893 <informaltable>
1894 <tgroup cols='5' colsep="1">
1895 <colspec/>
1896 <colspec align="center"/>
1897 <colspec/>
1898 <colspec/>
1899 <colspec align="center"/>
1900 <thead>
1901 <row rowsep="1">
1902 <entry>Desired Output</entry>
1903 <entry>Backend</entry>
1904 <entry>Style specification</entry>
1905 <entry>Options</entry>
1906 <entry>Supported?</entry>
1907 </row>
1908 </thead>
1909 <tbody>
1910 <row>
1911 <entry>rtf</entry>
1912 <entry>rtf</entry>
1913 <entry>print</entry>
1914 <entry>-V rtf-backend</entry>
1915 <entry>yes</entry>
1916 </row>
1917 <row>
1918 <entry>tex</entry>
1919 <entry>tex</entry>
1920 <entry>print</entry>
1921 <entry>-V tex-backend -i tex</entry>
1922 <entry>yes</entry>
1923 </row>
1924 <row>
1925 <entry>html</entry>
1926 <entry>sgml</entry>
1927 <entry>htmlweb</entry>
1928 <entry>-i html</entry>
1929 <entry>yes</entry>
1930 </row>
1931 <row>
1932 <entry>javahelp</entry>
1933 <entry>sgml</entry>
1934 <entry>help</entry>
1935 <entry>-i help</entry>
1936 <entry>yes</entry>
1937 </row>
1938 <row>
1939 <entry>htmlhelp</entry>
1940 <entry></entry>
1941 <entry></entry>
1942 <entry></entry>
1943 <entry>no</entry>
1944 </row>
1945 </tbody>
1946 </tgroup>
1947 </informaltable>
1948 <para>
1949 Putting this information in a table will help you keep track of it,
1950 but it's not the best solution. The ideal solution is to keep this
1951 information on your system, and let the software figure it all
1952 out. You'd like to be able to run a command, tell it what output you
1953 want from what stylesheet, what file you want to process, and then let
1954 it figure everything else out. For example:
1955 </para>
1956 <screen>
1957 format html mybook.dsl mydoc.sgm
1958 </screen>
1959 <para>
1960 <indexterm><primary>stylesheets</primary>
1961 <secondary>configuration data, storing within</secondary></indexterm>
1962 <indexterm><primary>configuration data, storing in stylesheet</primary></indexterm>
1963
1964 One way to do this is to put the configuration data in a separate
1965 file, and have the <command>format</command> command load it out of this other
1966 file. The disadvantage of this solution is that it introduces another
1967 file that you have to maintain and it's independent from the stylesheet so
1968 it isn't easy to keep it up-to-date.
1969 </para>
1970 <para>
1971 In the <acronym>DSSSL</acronym> case, a better alternative is to
1972 modify the stylesheet &DTD; so you can store the configuration
1973 data <emphasis>in the stylesheet</emphasis>. Using this alternate &DTD;, your
1974 <filename>mybook.dsl</filename> stylesheets might look like this:
1975 </para>
1976 <screen>
1977 <inlinegraphic format="linespecific" fileref="examples/dsssl.annot"/>
1978 </screen>
1979 <para>
1980 In this example, the stylesheet has been annotated with a title, a
1981 list of the public <acronym>ID</acronym>s to which it is applicable,
1982 and a table that provides information about the output formats that it
1983 supports.
1984 </para>
1985 <para>
1986 Using this information, the <command>format</command> command can get all the
1987 information it needs to construct the appropriate call to Jade. To
1988 make <acronym>HTML</acronym> from <filename>myfile.sgm</filename>,
1989 <command>format</command> would run the following:
1990 </para>
1991 <screen>
1992 jade -t sgml -d mybook.dsl#htmlweb -i html myfile.sgm
1993 </screen>
1994 <para>
1995 The additional information, titles and public <acronym>ID</acronym>s,
1996 can be used as part of a <acronym>GUI</acronym> interface to simplify
1997 the selection of stylesheets for an author.
1998 </para>
1999 <para>
2000 The complete annotated stylesheet &DTD;, and an example of the
2001 <command>format</command> command script, are provided on <link linkend="app-cdrom">the <acronym>CD-ROM</acronym></link>.
2002 <indexterm startref="DocBookpublishch04" class="endofrange"/>
2003 <indexterm startref="publishDocBookch04" class="endofrange"/>
2004 </para>
2005 </section>
2006 </section>
2007 <section id="xsl">
2008 <sectioninfo>
2009 <author><surname>Stayton</surname>
2010 <firstname>Bob</firstname></author>
2011 <copyright><year>2000</year><holder>Bob Stayton</holder>
2012 </copyright>
2013 </sectioninfo>
2014 <title>A Brief Introduction to XSL</title>
2015 <section>
2016 <title>Using XSL tools to publish DocBook
2017 documents</title>
2018 <para>There is a growing list of tools to process DocBook
2019 documents using XSL stylesheets. Each tool implements parts
2020 or all of the XSL standard, which actually has several
2021 components:
2022 <variablelist>
2023 <varlistentry>
2024 <term>Extensible Stylesheet Language (XSL)</term>
2025 <listitem>
2026 <para>A language for expressing stylesheets written
2027 in &XML;. It includes the formatting object language, but
2028 refers to separate documents for the transformation
2029 language and the path language.</para>
2030 </listitem>
2031 </varlistentry>
2032 <varlistentry>
2033 <term>XSL Transformation (XSLT)</term>
2034 <listitem>
2035 <para>The part of XSL for transforming &XML; documents
2036 into other &XML; documents, HTML, or text. It can be used to
2037 rearrange the content and generate new content.</para>
2038 </listitem>
2039 </varlistentry>
2040 <varlistentry>
2041 <term>XML Path Language (XPath)</term>
2042 <listitem>
2043 <para>A language for addressing parts of an &XML;
2044 document. It is used to find the parts of your document to
2045 apply different styles to. All XSL processors use this
2046 component.</para>
2047 </listitem>
2048 </varlistentry>
2049 </variablelist></para>
2050 <para>To publish HTML from your &XML; documents, you just
2051 need an XSLT engine. To get to print, you need an XSLT
2052 engine to produce formatting objects (FO), which then must
2053 be processed with a formatting object processor to produce
2054 PostScript or PDF output.</para>
2055 <para>James Clark's XT was the first useful XSLT engine,
2056 and it is still in wide use. It is written in Java, so it
2057 runs on many platforms, and it is free (
2058 <ulink url="http://www.jclark.com">http://www.jclark.com</ulink>).
2059 XT comes with James Clark's nonvalidating parser XP, but
2060 you can substitute a different Java parser. Here is a
2061 simple example of using XT from the Unix command line to
2062 produce HTML: You'll need to alter your
2063 <parameter>CLASSPATH</parameter> environment variable to
2064 include the path to where you put the
2065 <filename>.jar</filename> files from the XT
2066 distribution.</para>
2067 <screen>CLASSPATH=xt.jar:xp.jar:sax.jar
2068 export CLASSPATH
2069 java com.jclark.xsl.sax.Driver <replaceable>filename.xml</replaceable> <replaceable>docbook/html/docbook.xsl</replaceable> &gt; <replaceable>output.html</replaceable></screen>
2070 <para>If you replace the HTML stylesheet with a
2071 formatting object stylesheet, XT will produce a formatting
2072 object file. Then you can convert that to PDF using FOP, a
2073 formatting object processor available for free from the
2074 Apache &XML; Project (
2075 <ulink url="http://xml.apache.org">http://xml.apache.org</ulink>).
2076 Here is an example of that two stage processing:</para>
2077 <screen>CLASSPATH=xt.jar:xp.jar:sax.jar:fop.jar
2078 export CLASSPATH
2079 java com.jclark.xsl.sax.Driver <replaceable>filename.xml</replaceable> <replaceable>docbook/fo/docbook.xsl</replaceable> &gt; <replaceable>output.fo</replaceable>
2080 java org.apache.fop.apps.CommandLine <replaceable>output.fo</replaceable> <replaceable>output.pdf</replaceable></screen>
2081 <para>As of this writing, some other XSLT processors to
2082 choose from include:</para>
2083 <itemizedlist>
2084 <listitem>
2085 <para>4XSLT, written in Python, from FourThought LLC (
2086 <ulink url="http://www.fourthought.com">http://www.fourthought.com</ulink>)</para>
2087 </listitem>
2088 <listitem>
2089 <para>Sablotron, written in C++, from Ginger Alliance (
2090
2091 <ulink url="http://www.gingerall.com">http://www.gingerall.com</ulink>)</para>
2092 </listitem>
2093 <listitem>
2094 <para>Saxon, written in Java, from Michael Kay (
2095 <ulink url="http://users.iclway.co.uk/mhkay/saxon">http://users.iclway.co.uk/mhkay/saxon</ulink>)</para>
2096 </listitem>
2097 <listitem>
2098 <para>Xalan, written in Java, from the Apache &XML;
2099 Project (
2100 <ulink url="http://xml.apache.org">http://xml.apache.org</ulink>)</para>
2101 </listitem>
2102 <listitem>
2103 <para>XML::XSLT,written in Perl, from Geert Josten and
2104 Egon Willighagen (
2105 <ulink url="http://www.cpan.org">http://www.cpan.org</ulink>)</para>
2106 </listitem>
2107 </itemizedlist>
2108 <para>For print output, these additional tools are available for processing formatting objects:</para>
2109 <itemizedlist><listitem><para>XEP (written in Java) from
2110 RenderX (
2111 <ulink url="http://www.renderx.com">http://www.renderx.com</ulink>).</para></listitem><listitem><para>PassiveTeX from Sebastian Rahtz (<ulink url="http://users.ox.ac.uk/~rahtz/passivetex/">http://users.ox.ac.uk/~rahtz/passivetex/</ulink>).</para></listitem></itemizedlist></section>
2112 <section>
2113 <title>A brief introduction to XSL</title>
2114 <para>XSL is both a transformation language and a
2115 formatting language. The XSLT transformation part lets you
2116 scan through a document's structure and rearrange its
2117 content any way you like. You can write out the content
2118 using a different set of &XML; tags, and generate text as
2119 needed. For example, you can scan through a document to
2120 locate all headings and then insert a generated table of
2121 contents at the beginning of the document, at the same time
2122 writing out the content marked up as HTML. XSL is also a
2123 rich formatting language, letting you apply typesetting
2124 controls to all components of your output. With a good
2125 formatting backend, it is capable of producing high quality
2126 printed pages.</para>
2127 <para>An XSL stylesheet is written using &XML; syntax, and is
2128 itself a well-formed &XML; document. That makes the basic
2129 syntax familiar, and enables an &XML; processor to check for
2130 basic syntax errors. The stylesheet instructions use
2131 special element names, which typically begin with
2132 <sgmltag role="namespace">xsl:</sgmltag> to distinguish them from any &XML;
2133 tags you want to appear in the output. The XSL namespace is
2134 identified at the top of the stylesheet file. As with other
2135 &XML;, any XSL elements that are not empty will require a
2136 closing tag. And some XSL elements have specific attributes
2137 that control their behavior. It helps to keep a good XSL
2138 reference book handy.</para>
2139 <para>Here is an example of a simple XSL stylesheet applied
2140 to a simple &XML; file to generate HTML output.</para>
2141 <example>
2142 <title>Simple &XML; file</title>
2143 <programlisting><![CDATA[<?xml version="1.0"?>
2144 <document>
2145 <title>Using a mouse</title>
2146 <para>It's easy to use a mouse. Just roll it
2147 around and click the buttons.</para>
2148 </document>]]></programlisting>
2149 </example>
2150 <example>
2151 <title>Simple XSL stylesheet</title>
2152 <programlisting><![CDATA[<?xml version='1.0'?>
2153 <xsl:stylesheet
2154 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'>
2155 <xsl:output method="html"/>
2156
2157 <xsl:template match="document">
2158 <HTML><HEAD><TITLE>
2159 <xsl:value-of select="./title"/>
2160 </TITLE>
2161 </HEAD>
2162 <BODY>
2163 <xsl:apply-templates/>
2164 </BODY>
2165 </HTML>
2166 </xsl:template>
2167
2168 <xsl:template match="title">
2169 <H1><xsl:apply-templates/></H1>
2170 </xsl:template>
2171
2172 <xsl:template match="para">
2173 <P><xsl:apply-templates/></P>
2174 </xsl:template>
2175
2176 </xsl:stylesheet>
2177 ]]></programlisting>
2178 </example>
2179 <example>
2180 <title>HTML output</title>
2181 <programlisting><![CDATA[<HTML>
2182 <HEAD>
2183 <TITLE>Using a mouse</TITLE>
2184 </HEAD>
2185 <BODY>
2186 <H1>Using a mouse</H1>
2187 <P>It's easy to use a mouse. Just roll it
2188 around and click the buttons.</P>
2189 </BODY>
2190 </HTML>
2191 ]]></programlisting>
2192 </example>
2193 </section>
2194 <section>
2195 <title>XSL processing model</title>
2196 <para>XSL is a template language, not a procedural
2197 language. That means a stylesheet specifies a sample of the
2198 output, not a sequence of programming steps to generate it.
2199 A stylesheet consists of a mixture of output samples with
2200 instructions of what to put in each sample. Each bit of
2201 output sample and instructions is called
2202 a <emphasis>template</emphasis>.</para>
2203 <para>In general, you write a template for each element
2204 type in your document. That lets you concentrate on
2205 handling just one element at a time, and keeps a stylesheet
2206 modular. The power of XSL comes from processing the
2207 templates recursively. That is, each template handles the
2208 processing of its own element, and then calls other
2209 templates to process its children, and so on. Since an &XML;
2210 document is always a single root element at the top level
2211 that contains all of the nested descendent elements, the
2212 XSL templates also start at the top and work their way down
2213 through the hierarchy of elements.</para>
2214 <para>Take the
2215 DocBook <sgmltag class="element">&lt;para&gt;</sgmltag> paragraph element as
2216 an example. To convert this to HTML, you want to wrap the
2217 paragraph content with the HTML
2218 tags <sgmltag class="starttag">p</sgmltag> and <sgmltag class="endtag">/p</sgmltag>.
2219 But a DocBook <sgmltag class="element">&lt;para&gt;</sgmltag> can contain
2220 any number of in-line DocBook elements marking up the text.
2221 Fortunately, you can let other templates take care of those
2222 elements, so your XSL template
2223 for <sgmltag class="element">&lt;para&gt;</sgmltag> can be quite
2224 simple:</para>
2225 <programlisting><![CDATA[<xsl:template match="para">
2226 <p>
2227 <xsl:apply-templates/>
2228 </p>
2229 </xsl:template>
2230 ]]></programlisting>
2231 <para>The <sgmltag class="element">&lt;xsl:template&gt;</sgmltag> element
2232 starts a new template, and
2233 its <sgmltag class="attribute">match</sgmltag> attribute indicates where to
2234 apply the template, in this case to
2235 any <sgmltag class="element">&lt;para&gt;</sgmltag> elements. The template
2236 says to output a literal <sgmltag class="starttag">p</sgmltag> string
2237 and then execute
2238 the <sgmltag class="element">&lt;xsl:apply-templates/&gt;</sgmltag> instruction.
2239 This tells the XSL processor to look among all the
2240 templates in the stylesheet for any that should be applied
2241 to the content of the paragraph. If each template in the
2242 stylesheet includes
2243 an <sgmltag class="element">&lt;xsl:apply-templates/&gt;</sgmltag> instruction,
2244 then all descendents will eventually be processed. When it
2245 is through recursively applying templates to the paragraph
2246 content, it outputs the <sgmltag class="endtag">/p</sgmltag> closing
2247 tag.</para>
2248 <section>
2249 <title>Context is important</title>
2250 <para>Since you aren't writing a linear procedure to
2251 process your document, the context of where and how to
2252 apply each modular template is important.
2253 The <sgmltag class="attribute">match</sgmltag> attribute
2254 of <sgmltag class="element">&lt;xsl:template&gt;</sgmltag> provides that
2255 context for most templates. There is an entire expression
2256 language, XPath, for identifying what parts of your
2257 document should be handled by each template. The simplest
2258 context is just an element name, as in the example above.
2259 But you can also specify elements as children of other
2260 elements, elements with certain attribute values, the first
2261 or last elements in a sequence, and so on. Here is how the
2262 DocBook <sgmltag class="element">&lt;formalpara&gt;</sgmltag> element is
2263 handled:</para>
2264 <programlisting><![CDATA[<xsl:template match="formalpara">
2265 <p>
2266 <xsl:apply-templates/>
2267 </p>
2268 </xsl:template>
2269
2270 <xsl:template match="formalpara/title">
2271 <b><xsl:apply-templates/></b>
2272 <xsl:text> </xsl:text>
2273 </xsl:template>
2274
2275 <xsl:template match="formalpara/para">
2276 <xsl:apply-templates/>
2277 </xsl:template>
2278 ]]></programlisting>
2279 <para>There are three templates defined, one for
2280 the <sgmltag class="element">&lt;formalpara&gt;</sgmltag> element itself,
2281 and one for each of its children elements. The <sgmltag class="attribute">match</sgmltag> attribute
2282 value <literal>formalpara/title</literal> in the second
2283 template is an XPath expression indicating
2284 a <sgmltag class="element">&lt;title&gt;</sgmltag> element that is an
2285 immediate child of
2286 a <sgmltag class="element">&lt;formalpara&gt;</sgmltag> element. This
2287 distinguishes such titles from
2288 other <sgmltag class="element">&lt;title&gt;</sgmltag> elements used in
2289 DocBook. XPath expressions are the key to controlling how
2290 your templates are applied.</para>
2291 <para>In general, the XSL processor has internal rules that
2292 apply templates that are more specific before templates
2293 that are less specific. That lets you control the details,
2294 but also provides a fallback mechanism to a less specific
2295 template when you don't supply the full context for every
2296 combination of elements. This feature is illustrated by the
2297 third template, for <literal>formalpara/para</literal>. By
2298 including this template, the stylesheet processes a <sgmltag class="element">&lt;para&gt;</sgmltag> within <sgmltag class="element">&lt;formalpara&gt;</sgmltag> in
2299 a special way, in this case by not outputting the HTML <sgmltag class="element">&lt;p&gt;</sgmltag> tags already output by its parent. If this template had not been included, then the processor would have fallen back to the template
2300 specified by <sgmltag class="attribute">match="para"</sgmltag> described
2301 above, which would have output a second set of <sgmltag class="element">&lt;p&gt;</sgmltag> tags.</para>
2302 <para>You can also control template context with
2303 XSL <emphasis>modes</emphasis>, which are used extensively
2304 in the DocBook stylesheets. Modes let you process the same
2305 input more than once in different ways.
2306 A <sgmltag class="attribute">mode</sgmltag> attribute in
2307 an <sgmltag class="element">&lt;xsl:template&gt;</sgmltag> definition adds a
2308 specific mode name to that template. When the same mode
2309 name is used
2310 in <sgmltag class="element">&lt;xsl:apply-templates/&gt;</sgmltag>, it acts
2311 as a filter to narrow the selection of templates to only
2312 those selected by
2313 the <sgmltag class="attribute">match</sgmltag> expression <emphasis>and</emphasis> that
2314 have that mode name. This lets you define two different
2315 templates for the same element match that are applied under
2316 different contexts. For example, there are two templates
2317 defined for
2318 DocBook <sgmltag class="element">&lt;listitem&gt;</sgmltag> elements:</para>
2319 <programlisting><![CDATA[<xsl:template match="listitem">
2320 <li><xsl:apply-templates/></li>
2321 </xsl:template>
2322
2323 <xsl:template match="listitem" mode="xref">
2324 <xsl:number format="1"/>
2325 </xsl:template>
2326 ]]></programlisting>
2327 <para>The first template is for the normal list item
2328 context where you want to output the
2329 HTML <sgmltag class="element">&lt;li&gt;</sgmltag> tags. The second template
2330 is called with <sgmltag class="element">&lt;xsl:apply-templates
2331 select="$target" mode="xref"/&gt;</sgmltag> in the context
2332 of processing <sgmltag class="element">&lt;xref&gt;</sgmltag> elements. In
2333 this case the <sgmltag class="attribute">select</sgmltag> attribute locates
2334 the ID of the specific list item and
2335 the <sgmltag class="attribute">mode</sgmltag> attribute selects the second
2336 template, whose effect is to output its item number when it
2337 is in an ordered list. Because there are many such special
2338 needs when
2339 processing <sgmltag class="element">&lt;xref&gt;</sgmltag> elements, it is
2340 convenient to define a mode name <literal>xref</literal> to
2341 handle them all. Keep in mind that mode settings
2342 do <emphasis>not</emphasis> automatically get passed down to
2343 other templates
2344 through <sgmltag class="element">&lt;xsl:apply-templates/&gt;</sgmltag>.</para>
2345 </section>
2346 <section>
2347 <title>Programming features</title>
2348 <para>Although XSL is template-driven, it also has some
2349 features of traditional programming languages. Here are
2350 some examples from the DocBook stylesheets. </para>
2351 <programlisting><lineannotation>Assign a value to a variable:</lineannotation>
2352 <![CDATA[<xsl:variable name="refelem" select="name($target)"/>
2353 ]]>
2354 <lineannotation>If statement:</lineannotation>
2355 <![CDATA[<xsl:if test="$show.comments">
2356 <i><xsl:call-template name="inline.charseq"/></i>
2357 </xsl:if>
2358 ]]>
2359 <lineannotation>Case statement:</lineannotation>
2360 <![CDATA[<xsl:choose>
2361 <xsl:when test="@columns">
2362 <xsl:value-of select="@columns"/>
2363 </xsl:when>
2364 <xsl:otherwise>1</xsl:otherwise>
2365 </xsl:choose>
2366 ]]>
2367 <lineannotation>Call a template by name like a subroutine, passing parameter values and accepting a return value:</lineannotation>
2368 <![CDATA[<xsl:call-template name="xref.xreflabel">
2369 <xsl:with-param name="target" select="$target"/>
2370 </xsl:call-template>
2371 ]]></programlisting>
2372 <para>However, you can't always use these constructs as you
2373 do in other programming languages. Variables in particular
2374 have very different behavior.</para>
2375 <section>
2376 <title>Using variables and parameters</title>
2377 <para>XSL provides two elements that let you assign a value
2378 to a
2379 name: <sgmltag class="element">&lt;xsl:variable&gt;</sgmltag> and <sgmltag class="element">&lt;xsl:param&gt;</sgmltag>.
2380 These share the same name space and syntax for assigning
2381 names and values. Both can be referred to using
2382 the <literal>$name</literal> syntax. The main difference
2383 between these two elements is that a param's value acts as
2384 a default value that can be overridden when a template is
2385 called using
2386 a <sgmltag class="element">&lt;xsl:with-param&gt;</sgmltag> element as in the
2387 last example above.</para>
2388 <para>Here are two examples from DocBook:</para>
2389 <programlisting><![CDATA[<xsl:param name="cols">1</xsl:param>
2390 <xsl:variable name="segnum" select="position()"/>
2391 ]]></programlisting>
2392 <para>In both elements, the name of the parameter or
2393 variable is specified with
2394 the <sgmltag class="attribute">name</sgmltag> attribute. So the name of
2395 the <sgmltag class="element">param</sgmltag> here
2396 is <sgmltag class="attvalue">cols</sgmltag> and the name of
2397 the <sgmltag class="element">variable</sgmltag> is <sgmltag class="attvalue">segnum</sgmltag>.
2398 The value of either can be supplied in two ways. The value
2399 of the first example is the text node "1" and is supplied
2400 as the content of the element. The value of the second
2401 example is supplied as the result of the expression in
2402 its <sgmltag class="attribute">select</sgmltag> attribute, and the element
2403 itself has no content.</para>
2404 <para>The feature of XSL variables that is odd to new users
2405 is that once you assign a value to a variable, you cannot
2406 assign a new value within the same scope. Doing so will
2407 generate an error. So variables are not used as dynamic
2408 storage bins they way they are in other languages. They
2409 hold a fixed value within their scope of application, and
2410 then disappear when the scope is exited. This feature is a
2411 result of the design of XSL, which is template-driven and
2412 not procedural. This means there is no definite order of
2413 processing, so you can't rely on the values of changing
2414 variables. To use variables in XSL, you need to understand
2415 how their scope is defined.</para>
2416 <para>Variables defined outside of all templates are
2417 considered global variables, and they are readable within
2418 all templates. The value of a global variable is fixed, and
2419 its global value can't be altered from within any template.
2420 However, a template can create a local variable of the same
2421 name and give it a different value. That local value
2422 remains in effect only within the scope of the local
2423 variable.</para>
2424 <para>Variables defined within a template remain in effect
2425 only within their permitted scope, which is defined as all
2426 following siblings and their descendants. To understand
2427 such a scope, you have to remember that XSL instructions
2428 are true &XML; elements that are embedded in an &XML; family
2429 hierarchy of XSL elements, often referred to as parents,
2430 children, siblings, ancestors and descendants. Taking the
2431 family analogy a step further, think of a variable
2432 assignment as a piece of advice that you are allowed to
2433 give to certain family members. You can give your advice
2434 only to your younger siblings (those that follow you) and
2435 their descendents. Your older siblings won't listen,
2436 neither will your parents or any of your ancestors. To
2437 stretch the analogy a bit, it is an error to try to give
2438 different advice under the same name to the same group of
2439 listeners (in other words, to redefine the variable). Keep
2440 in mind that this family is not the elements of your
2441 document, but just the XSL instructions in your stylesheet.
2442 To help you keep track of such scopes in hand-written
2443 stylesheets, it helps to indent nested XSL elements. Here
2444 is an edited snippet from the DocBook stylesheet
2445 file <filename>pi.xsl</filename> that illustrates different
2446 scopes for two variables:</para>
2447 <programlisting><![CDATA[
2448 1 <xsl:template name="dbhtml-attribute">
2449 2 ...
2450 3 <xsl:choose>
2451 4 <xsl:when test="$count>count($pis)">
2452 5 <!-- not found -->
2453 6 </xsl:when>
2454 7 <xsl:otherwise>
2455 8 <xsl:variable name="pi">
2456 9 <xsl:value-of select="$pis[$count]"/>
2457 10 </xsl:variable>
2458 11 <xsl:choose>
2459 12 <xsl:when test="contains($pi,concat($attribute, '='))">
2460 13 <xsl:variable name="rest" select="substring-after($pi,concat($attribute,'='))"/>
2461 14 <xsl:variable name="quote" select="substring($rest,1,1)"/>
2462 15 <xsl:value-of select="substring-before(substring($rest,2),$quote)"/>
2463 16 </xsl:when>
2464 17 <xsl:otherwise>
2465 18 ...
2466 19 </xsl:otherwise>
2467 20 </xsl:choose>
2468 21 </xsl:otherwise>
2469 22 </xsl:choose>
2470 23 </xsl:template>
2471
2472 ]]></programlisting>
2473 <para>The scope of the variable <varname>pi</varname> begins
2474 on line 8 where it is defined in this template, and ends on
2475 line 20 when its last sibling ends.<footnote><para>Technically, the scope extends to the end tag of the parent of the <sgmltag class="element">&lt;xsl:variable&gt;</sgmltag> element. That is effectively the last sibling.</para></footnote> The scope of the
2476 variable <varname>rest</varname> begins on line 13 and ends
2477 on line 15. Fortunately, line 15 outputs an expression
2478 using the value before it goes out of scope.</para>
2479 <para>What happens when
2480 an <sgmltag class="element">&lt;xsl:apply-templates/&gt;</sgmltag> element
2481 is used within the scope of a local variable? Do the
2482 templates that are applied to the document children get the
2483 variable? The answer is no. The templates that are applied
2484 are not actually within the scope of the variable. They
2485 exist elsewhere in the stylesheet and are not following
2486 siblings or their descendants. </para>
2487 <para>To pass a value to another template, you pass a
2488 parameter using
2489 the <sgmltag class="element">&lt;xsl:with-param&gt;</sgmltag> element. This
2490 parameter passing is usually done with calls to a specific
2491 named template
2492 using <sgmltag class="element">&lt;xsl:call-template&gt;</sgmltag>, although
2493 it works
2494 with <sgmltag class="element">&lt;xsl:apply-templates&gt;</sgmltag> too.
2495 That's because the called template must be expecting the
2496 parameter by defining it using
2497 a <sgmltag class="element">&lt;xsl:param&gt;</sgmltag> element with the same
2498 parameter name. Any passed parameters whose names are not
2499 defined in the called template are ignored.</para>
2500 <para>Here is an example of parameter passing
2501 from <filename>docbook.xsl</filename>:</para>
2502 <programlisting><![CDATA[<xsl:call-template name="head.content">
2503 <xsl:with-param name="node" select="$doc"/>
2504 </xsl:call-template>
2505 ]]></programlisting>
2506 <para>Here a template
2507 named <literal>head.content</literal> is being called and
2508 passed a parameter named <parameter>node</parameter> whose
2509 content is the value of the <varname>$doc</varname> variable
2510 in the current context. The top of that template looks like
2511 this:</para>
2512 <programlisting><![CDATA[<xsl:template name="head.content">
2513 <xsl:param name="node" select="."/>
2514 ]]></programlisting>
2515 <para>The template is expecting the parameter because it
2516 has a <sgmltag class="element">&lt;xsl:param&gt;</sgmltag> defined with the
2517 same name. The value in this definition is the default
2518 value. This would be the parameter value used in the
2519 template if the template was called without passing that
2520 parameter.</para>
2521 </section>
2522 </section>
2523 <section>
2524 <title>Generating HTML output.</title>
2525 <para>You generate HTML from your DocBook &XML; files by
2526 applying the HTML version of the stylesheets. This is done
2527 by using the HTML driver
2528 file <filename>docbook/html/docbook.xsl</filename> as your
2529 stylesheet. That is the master stylesheet file that
2530 uses <sgmltag class="element">&lt;xsl:include&gt;</sgmltag> to pull in the
2531 component files it needs to assemble a complete stylesheet
2532 for producing HTML. </para>
2533 <para>The way the DocBook stylesheet generates HTML is to
2534 apply templates that output a mix of text content and HTML
2535 elements. Starting at the top level in the main
2536 file <filename>docbook.xsl</filename>:</para>
2537 <programlisting><![CDATA[<xsl:template match="/">
2538 <xsl:variable name="doc" select="*[1]"/>
2539 <html>
2540 <head>
2541 <xsl:call-template name="head.content">
2542 <xsl:with-param name="node" select="$doc"/>
2543 </xsl:call-template>
2544 </head>
2545 <body>
2546 <xsl:apply-templates/>
2547 </body>
2548 </html>
2549 </xsl:template>
2550 ]]></programlisting>
2551 <para>This template matches the root element of your input
2552 document, and starts the process of recursively applying
2553 templates. It first defines a variable
2554 named <varname>doc</varname> and then outputs two literal
2555 HTML elements <sgmltag class="element">&lt;html&gt;</sgmltag> and <sgmltag class="element">&lt;head&gt;</sgmltag>.
2556 Then it calls a named
2557 template <literal>head.content</literal> to process the
2558 content of the HTML <sgmltag class="element">&lt;head&gt;</sgmltag>, closes
2559 the <sgmltag class="element">&lt;head&gt;</sgmltag> and starts
2560 the <sgmltag class="element">&lt;body&gt;</sgmltag>. There it
2561 uses <sgmltag class="emptytag">&lt;xsl:apply-templates/&gt;</sgmltag> to
2562 recursively process the entire input document. Then it just
2563 closes out the HTML file.</para>
2564 <para>Simple HTML elements can generated as literal
2565 elements as shown here. But if the HTML being output
2566 depends on the context, you need something more powerful to
2567 select the element name and possibly add attributes and
2568 their values. Here is a fragment
2569 from <filename>sections.xsl</filename> that shows how a
2570 heading tag is generated using
2571 the <sgmltag class="element">&lt;xsl:element&gt;</sgmltag> and <sgmltag class="element">&lt;xsl:attribute&gt;</sgmltag> elements:</para>
2572 <programlisting><![CDATA[
2573 1 <xsl:element name="h{$level}">
2574 2 <xsl:attribute name="class">title</xsl:attribute>
2575 3 <xsl:if test="$level<3">
2576 4 <xsl:attribute name="style">clear: all</xsl:attribute>
2577 5 </xsl:if>
2578 6 <a>
2579 7 <xsl:attribute name="name">
2580 8 <xsl:call-template name="object.id"/>
2581 9 </xsl:attribute>
2582 10 <b><xsl:copy-of select="$title"/></b>
2583 11 </a>
2584 12 </xsl:element>
2585 ]]></programlisting>
2586 <para>This whole example is generating a single HTML
2587 heading element. Line 1 begins the HTML element definition
2588 by identifying the name of the element. In this case, the
2589 name is an expression that includes the
2590 variable <parameter>$level</parameter> passed as a parameter to
2591 this template. Thus a single template can
2592 generate <sgmltag class="element">&lt;h1&gt;</sgmltag>, <sgmltag class="element">&lt;h2&gt;</sgmltag>,
2593 etc. depending on the context in which it is called. Line 2
2594 defines a <sgmltag class="attribute">class="title"</sgmltag> attribute that is
2595 added to this element. Lines 3 to 5 add
2596 a <sgmltag class="attribute">style="clear all"</sgmltag> attribute, but only
2597 if the heading level is less than 3. Line 6 opens
2598 an <sgmltag class="element">&lt;a&gt;</sgmltag> anchor element. Although this
2599 looks like a literal output string, it is actually modified
2600 by lines 7 to 9 that insert
2601 the <sgmltag class="attribute">name</sgmltag> attribute into
2602 the <sgmltag class="element">&lt;a&gt;</sgmltag> element. This illustrates
2603 that XSL is managing output elements as active element
2604 nodes, not just text strings. Line 10 outputs the text of
2605 the heading title, also passed as a parameter to the
2606 template, enclosed in HTML boldface tags. Line 11 closes
2607 the anchor tag with the
2608 literal <sgmltag class="endtag">/a</sgmltag> syntax, while line 12
2609 closes the heading tag by closing the element definition.
2610 Since the actual element name is a variable, it couldn't
2611 use the literal syntax.</para>
2612 <para>As you follow the sequence of nested templates
2613 processing elements, you might be wondering how the
2614 ordinary text of your input document gets to the output. In
2615 the file <filename>docbook.xsl</filename> you will find
2616 this template that handles any text not processed by any
2617 other template:</para>
2618 <programlisting><![CDATA[<xsl:template match="text()">
2619 <xsl:value-of select="."/>
2620 </xsl:template>
2621 ]]></programlisting>
2622 <para>This template's body consists of the "value" of the text node,
2623 which is just its text. In general, all XSL processors have
2624 some built-in templates to handle any content for which
2625 your stylesheet doesn't supply a matching template. This
2626 template serves the same function but appears explicitly in
2627 the stylesheet.</para>
2628 </section>
2629 <section>
2630 <title>Generating formatting objects.</title>
2631 <para>You generate formatting objects from your DocBook &XML;
2632 files by applying the fo version of the stylesheets. This
2633 is done by using the fo driver
2634 file <filename>docbook/fo/docbook.xsl</filename> as your
2635 stylesheet. That is the master stylesheet file that
2636 uses <sgmltag class="element">&lt;xsl:include&gt;</sgmltag> to pull in the
2637 component files it needs to assemble a complete stylesheet
2638 for producing formatting objects. Generating a formatting
2639 objects file is only half the process of producing typeset
2640 output. You also need a formatting object processor such as
2641 the Apache &XML; Project's FOP as described in an earlier
2642 section.</para>
2643 <para>The DocBook fo stylesheet works in a similar manner
2644 to the HTML stylesheet. Instead of outputting HTML tags, it
2645 outputs text marked up
2646 with <sgmltag class="element">&lt;fo:<replaceable>something</replaceable>&gt;</sgmltag> tags.
2647 For example, to indicate that some text should be kept
2648 in-line and typeset with a monospace font, it might look
2649 like this:</para>
2650 <programlisting><![CDATA[<fo:inline-sequence font-family="monospace">/usr/man</fo:inline-sequence>]]></programlisting>
2651 <para>The templates
2652 in <filename>docbook/fo/inline.xsl</filename> that produce
2653 this output for a
2654 DocBook <sgmltag class="element">&lt;filename&gt;</sgmltag> element look
2655 like this:</para>
2656 <programlisting><![CDATA[<xsl:template match="filename">
2657 <xsl:call-template name="inline.monoseq"/>
2658 </xsl:template>
2659
2660 <xsl:template name="inline.monoseq">
2661 <xsl:param name="content">
2662 <xsl:apply-templates/>
2663 </xsl:param>
2664 <fo:inline-sequence font-family="monospace">
2665 <xsl:copy-of select="$content"/>
2666 </fo:inline-sequence>
2667 </xsl:template>
2668 ]]></programlisting>
2669 <para>There are dozens of fo tags and attributes specified
2670 in the XSL standard. It is beyond the scope of this
2671 document to cover how all of them are used in the DocBook
2672 stylesheets. Fortunately, this is only an intermediate
2673 format that you probably won't have to deal with very much
2674 directly unless you are writing your own
2675 stylesheets.</para>
2676 </section>
2677 </section>
2678 <section>
2679 <title>Customizing DocBook XSL stylesheets</title>
2680 <para>The DocBook XSL stylesheets are written in a modular
2681 fashion. Each of the HTML and FO stylesheets starts with a
2682 driver file that assembles a collection of component files
2683 into a complete stylesheet. This modular design puts similar things together into smaller files that are easier to write and maintain than one big stylesheet. The modular stylesheet files
2684 are distributed among four directories:</para>
2685
2686 <variablelist>
2687 <varlistentry><term>common/</term>
2688 <listitem>
2689 <para>contains code common to both stylesheets, including localization data
2690 </para>
2691 </listitem>
2692 </varlistentry>
2693 <varlistentry><term>fo/</term>
2694 <listitem>
2695 <para>a stylesheet that produces XSL FO result trees
2696 </para>
2697 </listitem>
2698 </varlistentry>
2699 <varlistentry><term>html/</term>
2700 <listitem>
2701 <para>a stylesheet that produces HTML/XHTML result trees
2702 </para>
2703 </listitem>
2704 </varlistentry>
2705 <varlistentry><term>lib/</term>
2706 <listitem>
2707 <para>contains schema-independent functions
2708 </para>
2709 </listitem>
2710 </varlistentry>
2711 </variablelist>
2712
2713 <para>The driver files for each of HTML and FO stylesheets
2714 are <filename>html/docbook.xsl</filename> and <filename>fo/docbook.xsl</filename>,
2715 respectively. A driver file consists mostly of a bunch
2716 of <sgmltag class="element">&lt;xsl:include&gt;</sgmltag> instructions to
2717 pull in the component templates, and then defines some
2718 top-level templates. For example:</para>
2719 <programlisting><![CDATA[<xsl:include href="../VERSION"/>
2720 <xsl:include href="../lib/lib.xsl"/>
2721 <xsl:include href="../common/l10n.xsl"/>
2722 <xsl:include href="../common/common.xsl"/>
2723 <xsl:include href="autotoc.xsl"/>
2724 <xsl:include href="lists.xsl"/>
2725 <xsl:include href="callout.xsl"/>
2726 ...
2727 <xsl:include href="param.xsl"/>
2728 <xsl:include href="pi.xsl"/>
2729 ]]></programlisting>
2730 <para>The first four modules are shared with the FO
2731 stylesheet and are referenced using relative pathnames to
2732 the common directories. Then the long list of component
2733 stylesheets starts. Pathnames in include statements are
2734 always taken to be relative to the including file. Each
2735 included file must be a valid XSL stylesheet, which means
2736 its root element must
2737 be <sgmltag class="element">&lt;xsl:stylesheet&gt;</sgmltag>.</para>
2738 <section>
2739 <title>Stylesheet inclusion vs. importing</title>
2740 <para>XSL actually provides two inclusion
2741 mechanisms: <sgmltag class="element">&lt;xsl:include&gt;</sgmltag> and <sgmltag class="element">&lt;xsl:import&gt;</sgmltag>.
2742 Of the two, <sgmltag class="element">&lt;xsl:include&gt;</sgmltag> is
2743 the simpler. It treats the included content as if it were
2744 actually typed into the file at that point, and doesn't
2745 give it any more or less precedence relative to the
2746 surrounding text. It is best used when assembling
2747 dissimilar templates that don't overlap what they match.
2748 The DocBook driver files use this instruction to assemble a
2749 set of modules into a stylesheet.</para>
2750 <para>In contrast, <sgmltag class="element">&lt;xsl:import&gt;</sgmltag> lets
2751 you manage the precedence of templates and variables. It is
2752 the preferred mode of customizing another stylesheet because
2753 it lets you override definitions in the distributed
2754 stylesheet with your own, without altering the distribution
2755 files at all. You simply import the whole stylesheet and
2756 add whatever changes you want.</para>
2757 <para>The precedence rules for import are detailed and
2758 rigorously defined in the XSL standard. The basic rule is
2759 that any templates and variables in the importing
2760 stylesheet have precedence over equivalent templates and
2761 variables in the imported stylesheet. Think of the imported stylesheet elements as a fallback collection, to be used only if a match is not found in the current stylesheet. You can customize the templates you want to change in your stylesheet file, and let the imported stylesheet handle the rest.</para>
2762 <note>
2763 <para>Customizing a DocBook XSL stylesheet is the opposite
2764 of customizing a DocBook DTD. When you customize a DocBook
2765 DTD, the rules of &XML; and &SGML; dictate that
2766 the <emphasis>first</emphasis> of any duplicate declarations
2767 wins. Any subsequent declarations of the same element or
2768 entity are ignored. The architecture of the DTD provides
2769 slots for inserting your own custom declarations early
2770 enough in the DTD for them to override the standard
2771 declarations. In contrast, customizing an XSL stylesheet is
2772 simpler because your definitions have precedence over imported ones.</para>
2773 </note>
2774 <para>You can carry modularization to deeper levels because
2775 module files can also include or import other modules.
2776 You'll need to be careful to maintain the precedence that
2777 you want as the modules get rolled up into a complete
2778 stylesheet. </para>
2779 </section>
2780 <section>
2781 <title>Customizing
2782 with <sgmltag class="element">&lt;xsl:import&gt;</sgmltag></title>
2783 <para>There is currently one example of customizing
2784 with <sgmltag class="element">&lt;xsl:import&gt;</sgmltag> in the HTML
2785 version of the DocBook stylesheets.
2786 The <filename>xtchunk.xsl</filename> stylesheet modifies the
2787 HTML processing to output many smaller HTML files rather
2788 than a single large file per input document. It uses XSL
2789 extensions defined only in the XSL
2790 processor <command>XT</command>. In the driver
2791 file <filename>xtchunk.xsl</filename>, the first instruction
2792 is <sgmltag class="element">&lt;xsl:import
2793 href="docbook.xsl"/&gt;</sgmltag>. That instruction imports
2794 the original driver file, which in turn uses
2795 many <sgmltag class="element">&lt;xsl:include&gt;</sgmltag> instructions to
2796 include all the modules. That single import instruction
2797 gives the new stylesheet the complete set of DocBook
2798 templates to start with.</para>
2799 <para>After the
2800 import, <filename>xtchunk.xsl</filename> redefines some of
2801 the templates and adds some new ones. Here is one example
2802 of a redefined template:</para>
2803 <programlisting><lineannotation>Original template in autotoc.xsl</lineannotation>
2804 <![CDATA[<xsl:template name="href.target">
2805 <xsl:param name="object" select="."/>
2806 <xsl:text>#</xsl:text>
2807 <xsl:call-template name="object.id">
2808 <xsl:with-param name="object" select="$object"/>
2809 </xsl:call-template>
2810 </xsl:template>]]>
2811
2812 <lineannotation>New template in xtchunk.xsl</lineannotation>
2813 <![CDATA[<xsl:template name="href.target">
2814 <xsl:param name="object" select="."/>
2815 <xsl:variable name="ischunk">
2816 <xsl:call-template name="chunk">
2817 <xsl:with-param name="node" select="$object"/>
2818 </xsl:call-template>
2819 </xsl:variable>
2820
2821 <xsl:apply-templates mode="chunk-filename" select="$object"/>
2822
2823 <xsl:if test="$ischunk='0'">
2824 <xsl:text>#</xsl:text>
2825 <xsl:call-template name="object.id">
2826 <xsl:with-param name="object" select="$object"/>
2827 </xsl:call-template>
2828 </xsl:if>
2829 </xsl:template>]]>
2830 </programlisting>
2831 <para>The new template handles the more complex processing
2832 of HREFs when the output is split into many HTML files.
2833 Where the old template could simply
2834 output <literal>#<replaceable>object.id</replaceable></literal>,
2835 the new one outputs <literal><replaceable>filename</replaceable>#<replaceable>object.id</replaceable></literal>.</para>
2836 </section>
2837 <section>
2838 <title>Setting stylesheet variables</title>
2839 <para>You may not have to define any new templates,
2840 however. The DocBook stylesheets are parameterized using
2841 XSL variables rather than hard-coded values for many of the
2842 formatting features. Since
2843 the <sgmltag class="element">&lt;xsl:import&gt;</sgmltag> mechanism also
2844 lets you redefine global variables, this gives you an easy
2845 way to customize many features of the DocBook
2846 stylesheets. Over time, more features will be parameterized to permit customization. If you find hardcoded values in the stylesheets that would be useful to customize, please let the maintainer know.</para>
2847 <para>Near the end of the list of includes in the main
2848 DocBook driver file is the
2849 instruction <sgmltag class="element">&lt;xsl:include
2850 href="param.xsl"/&gt;</sgmltag>.
2851 The <filename>param.xsl</filename> file is the most
2852 important module for customizing a DocBook XSL stylesheet.
2853 This module contains no templates, only definitions of
2854 stylesheet variables. Since these variables are defined
2855 outside of any template, they are global variables and
2856 apply to the entire stylesheet. By redefining these
2857 variables in an importing stylesheet, you can change the
2858 behavior of the stylesheet.</para>
2859 <para>To create a customized DocBook stylesheet, you simply
2860 create a new stylesheet file such
2861 as <filename>mystyle.xsl</filename> that imports the standard
2862 stylesheet and adds your own new variable definitions. Here
2863 is an example of a complete custom stylesheet that changes
2864 the depth of sections listed in the table of contents from
2865 two to three:</para>
2866 <programlisting><![CDATA[<?xml version='1.0'?>
2867 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
2868 version='1.0'
2869 xmlns="http://www.w3.org/TR/xhtml1/transitional"
2870 exclude-result-prefixes="#default">
2871
2872 <xsl:import href="docbook.xsl"/>
2873
2874 <xsl:variable name="toc.section.depth">3</xsl:variable>
2875 <!-- Add other variable definitions here -->
2876
2877 </xsl:stylesheet>
2878 ]]></programlisting>
2879 <para>Following the opening stylesheet element are the
2880 import instruction and one variable definition. The
2881 variable <varname>toc.section.depth</varname> was defined
2882 in <filename>param.xsl</filename> with value "2", and here
2883 it is defined as "3". Since the importing stylesheet takes
2884 precedence, this new value is used. Thus documents
2885 processed with <filename>mystyle.xsl</filename> instead
2886 of <filename>docbook.xsl</filename> will have three levels
2887 of sections in the tables of contents, and all other
2888 processing will be the same.</para>
2889 <para>Use the list of variables
2890 in <filename>param.xsl</filename> as your guide for creating
2891 a custom stylesheet. If the changes you want are controlled
2892 by a variable there, then customizing is easy. </para>
2893
2894 </section>
2895 <section>
2896 <title>Writing your own templates</title>
2897 <para>If the changes you want are more extensive than what
2898 is supported by variables, you can write new templates. You
2899 can put your new templates directly in your importing
2900 stylesheet, or you can modularize your importing stylesheet
2901 as well. You can write your own stylesheet module
2902 containing a collection of templates for processing lists,
2903 for example, and put them in a file
2904 named <filename>mylists.xsl</filename>. Then your importing
2905 stylesheet can pull in your list templates with
2906 a <sgmltag class="element">&lt;xsl:include
2907 href="mylists.xsl"/&gt;</sgmltag> instruction. Since your
2908 included template definitions appear after the main import
2909 instruction, your templates will take precedence.</para>
2910 <para>You'll need to make sure your new templates are
2911 compatible with the remaining modules, which means:</para>
2912 <itemizedlist>
2913 <listitem>
2914 <para>Any named templates should use the same name so
2915 calling templates in other modules can find them.</para>
2916 </listitem>
2917 <listitem>
2918 <para>Your template set should process the same elements
2919 matched by templates in the original module, to ensure
2920 complete coverage.</para>
2921 </listitem>
2922 <listitem>
2923 <para>Include the same set
2924 of <sgmltag class="element">&lt;xsl:param&gt;</sgmltag> elements in each
2925 template to interface properly with any calling templates,
2926 although you can set different values for your
2927 parameters.</para>
2928 </listitem>
2929 <listitem>
2930 <para>Any templates that are used like subroutines to
2931 return a value should return the same data type.</para>
2932 </listitem>
2933 </itemizedlist>
2934 </section>
2935 <section>
2936 <title>Writing your own driver</title>
2937 <para>Another approach to customizing the stylesheets is to
2938 write your own driver file. Instead of
2939 using <sgmltag class="element">&lt;xsl:import
2940 href="docbook.xsl"/&gt;</sgmltag>, you copy that file to a
2941 new name and rewrite any of
2942 the <sgmltag class="element">&lt;xsl:include/&gt;</sgmltag> instructions to
2943 assemble a custom collection of stylesheet modules. One
2944 reason to do this is to speed up processing by reducing the
2945 size of the stylesheet. If you are using a customized
2946 DocBook DTD that omits many elements you never use, you
2947 might be able to omit those modules of the
2948 stylesheet.</para>
2949 </section>
2950 <section>
2951 <title>Localization</title>
2952 <para>The DocBook stylesheets include features for
2953 localizing generated text, that is, printing any generated
2954 text in a language other than the default English. In
2955 general, the stylesheets will switch to the language
2956 identified by a <sgmltag class="attribute">lang</sgmltag> attribute when
2957 processing elements in your documents. If your documents
2958 use the <sgmltag class="attribute">lang</sgmltag> attribute, then you don't
2959 need to customize the stylesheets at all for
2960 localization.</para>
2961 <para>As far as the stylesheets go,
2962 a <sgmltag class="attribute">lang</sgmltag> attribute is inherited by the
2963 descendents of a document element. The stylesheet searches
2964 for a <sgmltag class="attribute">lang</sgmltag> attribute using this XPath
2965 expression:</para>
2966 <programlisting><![CDATA[<xsl:variable name="lang-attr"
2967 select="($target/ancestor-or-self::*/@lang
2968 |$target/ancestor-or-self::*/@xml:lang)[last()]"/>]]></programlisting>
2969
2970 <para>This locates the attribute on the current element or
2971 its most recent ancestor. Thus
2972 a <sgmltag class="attribute">lang</sgmltag> attribute is in effect for an
2973 element and all of its descendents, unless it is reset in
2974 one of those descendents. If you define it in only your
2975 document root element, then it applies to the whole
2976 document:</para>
2977 <programlisting><![CDATA[<?xml version="1.0"?>
2978 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.0//EN" "docbook.dtd">
2979 <book lang="fr">
2980 ...
2981 </book>]]></programlisting>
2982 <para>When text is being generated, the stylesheet checks
2983 the most recent <sgmltag class="attribute">lang</sgmltag> attribute and looks
2984 up the generated text strings for that language in a
2985 localization &XML; file. These are located in
2986 the <filename>common</filename> directory of the
2987 stylesheets, one file per language. Here is the top of the
2988 file <filename>fr.xml</filename>:</para>
2989 <programlisting><![CDATA[<localization language="fr">
2990
2991 <gentext key="abstract" text="R]]>&amp;#x00E9;sum&amp;#x00E9;<![CDATA["/>
2992 <gentext key="answer" text="R:"/>
2993 <gentext key="appendix" text="Annexe"/>
2994 <gentext key="article" text="Article"/>
2995 <gentext key="bibliography" text="Bibliographie"/>
2996 ...
2997 ]]></programlisting>
2998 <para>The stylesheet templates use the gentext key names,
2999 and then the stylesheet looks up the associated text value
3000 when the document is processed with that lang setting. The
3001 file <filename>l10n.xml</filename> (note
3002 the <filename>.xml</filename> suffix) lists the filenames of
3003 all the supported languages.</para>
3004 <para>You can also create a custom stylesheet that sets the
3005 language. That might be useful if your documents don't make
3006 appropriate use of the <sgmltag class="attribute">lang</sgmltag> attribute.
3007 The module <filename>l10n.xsl</filename> defines two global
3008 variables that can be overridden with an importing
3009 stylesheet as described above. Here are their default
3010 definitions:</para>
3011 <programlisting><![CDATA[<xsl:variable name="l10n.gentext.language"></xsl:variable>
3012 <xsl:variable name="l10n.gentext.default.language">en</xsl:variable>
3013 ]]></programlisting>
3014 <para>The first one sets the language for all elements,
3015 regardless of an element's <sgmltag class="attribute">lang</sgmltag> attribute
3016 value. The second just sets a default language for any
3017 elements that haven't got a <sgmltag class="attribute">lang</sgmltag> setting
3018 of their own (or their ancestors).</para>
3019 </section>
3020 </section>
3021 </section>
3022 </chapter>
3023
3024 <!--
3025 Local Variables:
3026 mode: xml
3027 sgml-parent-document: ("book.xml" "chapter")
3028 End:
3029 -->