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