Imported Upstream version 1.79.0
[platform/upstream/docbook-xsl-stylesheets.git] / fo / docbook.xsl
1 <?xml version='1.0'?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                 xmlns:exsl="http://exslt.org/common"
4                 xmlns:fo="http://www.w3.org/1999/XSL/Format"
5                 xmlns:ng="http://docbook.org/docbook-ng"
6                 xmlns:db="http://docbook.org/ns/docbook"
7                 exclude-result-prefixes="db ng exsl"
8                 version='1.0'>
9
10 <!-- It is important to use indent="no" here, otherwise verbatim -->
11 <!-- environments get broken by indented tags...at least when the -->
12 <!-- callout extension is used...at least with some processors -->
13 <xsl:output method="xml" indent="no"/>
14
15 <!-- ********************************************************************
16      $Id: docbook.xsl 9988 2015-09-17 20:22:22Z bobstayton $
17      ********************************************************************
18
19      This file is part of the XSL DocBook Stylesheet distribution.
20      See ../README or http://docbook.sf.net/release/xsl/current/ for
21      copyright and other information.
22
23      ******************************************************************** -->
24
25 <!-- ==================================================================== -->
26
27
28 <xsl:include href="../VERSION.xsl"/>
29 <xsl:include href="param.xsl"/>
30 <xsl:include href="../lib/lib.xsl"/>
31 <xsl:include href="../common/l10n.xsl"/>
32 <xsl:include href="../common/common.xsl"/>
33 <xsl:include href="../common/utility.xsl"/>
34 <xsl:include href="../common/labels.xsl"/>
35 <xsl:include href="../common/titles.xsl"/>
36 <xsl:include href="../common/subtitles.xsl"/>
37 <xsl:include href="../common/gentext.xsl"/>
38 <xsl:include href="../common/olink.xsl"/>
39 <xsl:include href="../common/targets.xsl"/>
40 <xsl:include href="../common/pi.xsl"/>
41 <xsl:include href="autotoc.xsl"/>
42 <xsl:include href="autoidx.xsl"/>
43 <xsl:include href="lists.xsl"/>
44 <xsl:include href="callout.xsl"/>
45 <xsl:include href="verbatim.xsl"/>
46 <xsl:include href="graphics.xsl"/>
47 <xsl:include href="xref.xsl"/>
48 <xsl:include href="formal.xsl"/>
49 <xsl:include href="table.xsl"/>
50 <xsl:include href="htmltbl.xsl"/>
51 <xsl:include href="sections.xsl"/>
52 <xsl:include href="inline.xsl"/>
53 <xsl:include href="footnote.xsl"/>
54 <xsl:include href="fo.xsl"/>
55 <xsl:include href="fo-rtf.xsl"/>
56 <xsl:include href="info.xsl"/>
57 <xsl:include href="keywords.xsl"/>
58 <xsl:include href="division.xsl"/>
59 <xsl:include href="index.xsl"/>
60 <xsl:include href="toc.xsl"/>
61 <xsl:include href="refentry.xsl"/>
62 <xsl:include href="math.xsl"/>
63 <xsl:include href="admon.xsl"/>
64 <xsl:include href="component.xsl"/>
65 <xsl:include href="biblio.xsl"/>
66 <xsl:include href="biblio-iso690.xsl"/>
67 <xsl:include href="glossary.xsl"/>
68 <xsl:include href="block.xsl"/>
69 <xsl:include href="task.xsl"/>
70 <xsl:include href="qandaset.xsl"/>
71 <xsl:include href="synop.xsl"/>
72 <xsl:include href="titlepage.xsl"/>
73 <xsl:include href="titlepage.templates.xsl"/>
74 <xsl:include href="pagesetup.xsl"/>
75 <xsl:include href="pi.xsl"/>
76 <xsl:include href="spaces.xsl"/>
77 <xsl:include href="ebnf.xsl"/>
78 <xsl:include href="../html/chunker.xsl"/>
79 <xsl:include href="annotations.xsl"/>
80 <xsl:include href="publishers.xsl"/>
81 <xsl:include href="../common/stripns.xsl"/>
82
83 <xsl:include href="fop.xsl"/>
84 <xsl:include href="fop1.xsl"/>
85 <xsl:include href="xep.xsl"/>
86 <xsl:include href="axf.xsl"/>
87 <xsl:include href="ptc.xsl"/>
88
89 <xsl:param name="stylesheet.result.type" select="'fo'"/>
90
91 <!-- ==================================================================== -->
92
93 <xsl:key name="id" match="*" use="@id|@xml:id"/>
94
95 <!-- ==================================================================== -->
96
97 <xsl:template match="*">
98   <xsl:message>
99     <xsl:text>Element </xsl:text>
100     <xsl:value-of select="local-name(.)"/>
101     <xsl:text> in namespace '</xsl:text>
102     <xsl:value-of select="namespace-uri(.)"/>
103     <xsl:text>' encountered</xsl:text>
104     <xsl:if test="parent::*">
105       <xsl:text> in </xsl:text>
106       <xsl:value-of select="name(parent::*)"/>
107     </xsl:if>
108     <xsl:text>, but no template matches.</xsl:text>
109   </xsl:message>
110   
111   <fo:block color="red">
112     <xsl:text>&lt;</xsl:text>
113     <xsl:value-of select="name(.)"/>
114     <xsl:text>&gt;</xsl:text>
115     <xsl:apply-templates/> 
116     <xsl:text>&lt;/</xsl:text>
117     <xsl:value-of select="name(.)"/>
118     <xsl:text>&gt;</xsl:text>
119   </fo:block>
120 </xsl:template>
121
122 <!-- Update this list if new root elements supported -->
123 <xsl:variable name="root.elements" select="' appendix article bibliography book chapter colophon dedication glossary index part preface qandaset refentry reference sect1 section set setindex '"/>
124
125 <xsl:template match="/">
126   <!-- * Get a title for current doc so that we let the user -->
127   <!-- * know what document we are processing at this point. -->
128   <xsl:variable name="doc.title">
129     <xsl:call-template name="get.doc.title"/>
130   </xsl:variable>
131   <xsl:choose>
132     <!-- fix namespace if necessary -->
133     <xsl:when test="$exsl.node.set.available != 0 and 
134                   namespace-uri(/*) = 'http://docbook.org/ns/docbook'">
135       <xsl:variable name="no.namespace">
136         <xsl:apply-templates select="/*" mode="stripNS"/>
137       </xsl:variable>
138
139       <xsl:call-template name="log.message">
140         <xsl:with-param name="level">Note</xsl:with-param>
141         <xsl:with-param name="source" select="$doc.title"/>
142         <xsl:with-param name="context-desc">
143           <xsl:text>namesp. cut</xsl:text>
144         </xsl:with-param>
145         <xsl:with-param name="message">
146           <xsl:text>stripped namespace before processing</xsl:text>
147         </xsl:with-param>
148       </xsl:call-template>
149       <!-- DEBUG: uncomment to save namespace-fixed document.
150       <xsl:message>Saving namespace-fixed document.</xsl:message>
151       <xsl:call-template name="write.chunk">
152         <xsl:with-param name="filename" select="'namespace-fixed.debug.xml'"/>
153         <xsl:with-param name="method" select="'xml'"/>
154         <xsl:with-param name="content">
155           <xsl:copy-of select="exsl:node-set($no.namespace)"/>
156         </xsl:with-param>
157       </xsl:call-template>
158       -->
159       <xsl:apply-templates select="exsl:node-set($no.namespace)"/>
160     </xsl:when>
161     <!-- Can't process unless namespace fixed with exsl node-set()-->
162     <xsl:when test="namespace-uri(/*) = 'http://docbook.org/ns/docbook'">
163       <xsl:message terminate="yes">
164         <xsl:text>Unable to strip the namespace from DB5 document,</xsl:text>
165         <xsl:text> cannot proceed.</xsl:text>
166       </xsl:message>
167     </xsl:when>
168     <xsl:otherwise>
169       <xsl:choose>
170         <xsl:when test="$rootid != ''">
171           <xsl:variable name="root.element" select="key('id', $rootid)"/>
172           <xsl:choose>
173             <xsl:when test="count($root.element) = 0">
174               <xsl:message terminate="yes">
175                 <xsl:text>ID '</xsl:text>
176                 <xsl:value-of select="$rootid"/>
177                 <xsl:text>' not found in document.</xsl:text>
178               </xsl:message>
179             </xsl:when>
180             <xsl:when test="not(contains($root.elements, concat(' ', local-name($root.element), ' ')))">
181               <xsl:message terminate="yes">
182                 <xsl:text>ERROR: Document root element ($rootid=</xsl:text>
183                 <xsl:value-of select="$rootid"/>
184                 <xsl:text>) for FO output </xsl:text>
185                 <xsl:text>must be one of the following elements:</xsl:text>
186                 <xsl:value-of select="$root.elements"/>
187               </xsl:message>
188             </xsl:when>
189             <!-- Otherwise proceed -->
190             <xsl:otherwise>
191               <xsl:if test="$collect.xref.targets = 'yes' or
192                             $collect.xref.targets = 'only'">
193                 <xsl:apply-templates select="$root.element"
194                                      mode="collect.targets"/>
195               </xsl:if>
196               <xsl:if test="$collect.xref.targets != 'only'">
197                 <xsl:apply-templates select="$root.element"
198                                      mode="process.root"/>
199               </xsl:if>
200             </xsl:otherwise>
201           </xsl:choose>
202         </xsl:when>
203         <!-- Otherwise process the document root element -->
204         <xsl:otherwise>
205           <xsl:variable name="document.element" select="*[1]"/>
206           <xsl:choose>
207             <xsl:when test="not(contains($root.elements,
208                      concat(' ', local-name($document.element), ' ')))">
209               <xsl:message terminate="yes">
210                 <xsl:text>ERROR: Document root element for FO output </xsl:text>
211                 <xsl:text>must be one of the following elements:</xsl:text>
212                 <xsl:value-of select="$root.elements"/>
213               </xsl:message>
214             </xsl:when>
215             <!-- Otherwise proceed -->
216             <xsl:otherwise>
217               <xsl:if test="$collect.xref.targets = 'yes' or
218                             $collect.xref.targets = 'only'">
219                 <xsl:apply-templates select="/"
220                                      mode="collect.targets"/>
221               </xsl:if>
222               <xsl:if test="$collect.xref.targets != 'only'">
223                 <xsl:apply-templates select="/"
224                                      mode="process.root"/>
225               </xsl:if>
226             </xsl:otherwise>
227           </xsl:choose>
228         </xsl:otherwise>
229       </xsl:choose>
230     </xsl:otherwise>
231   </xsl:choose>
232 </xsl:template>
233
234 <xsl:template match="*" mode="process.root">
235   <xsl:variable name="document.element" select="self::*"/>
236
237   <xsl:call-template name="root.messages"/>
238
239   <xsl:variable name="title">
240     <xsl:choose>
241       <xsl:when test="$document.element/title | $document.element/info/title">
242         <xsl:value-of 
243              select="($document.element/title | $document.element/info/title)[1]"/>
244       </xsl:when>
245       <xsl:otherwise>[could not find document title]</xsl:otherwise>
246     </xsl:choose>
247   </xsl:variable>
248   
249   <!-- Include all id values in XEP output -->
250   <xsl:if test="$xep.extensions != 0">
251     <xsl:processing-instruction 
252      name="xep-pdf-drop-unused-destinations">false</xsl:processing-instruction>
253   </xsl:if>
254
255   <fo:root xsl:use-attribute-sets="root.properties">
256     <xsl:attribute name="language">
257       <xsl:call-template name="l10n.language">
258         <xsl:with-param name="target" select="/*[1]"/>
259       </xsl:call-template>
260     </xsl:attribute>
261
262     <xsl:if test="$xep.extensions != 0">
263       <xsl:call-template name="xep-pis"/>
264       <xsl:call-template name="xep-document-information"/>
265     </xsl:if>
266     <xsl:if test="$axf.extensions != 0">
267       <xsl:call-template name="axf-document-information"/>
268     </xsl:if>
269
270     <xsl:call-template name="setup.pagemasters"/>
271
272     <xsl:if test="$fop1.extensions != 0">
273       <xsl:call-template name="fop1-document-information"/>
274       <xsl:apply-templates select="$document.element" mode="fop1.foxdest"/>
275     </xsl:if>
276
277     <!-- generate bookmark tree -->
278     <xsl:call-template name="generate.bookmarks"/>
279
280     <xsl:apply-templates select="$document.element"/>
281   </fo:root>
282 </xsl:template>
283
284 <xsl:template name="generate.bookmarks">
285   <xsl:variable name="document.element" select="self::*"/>
286   <xsl:choose>
287     <xsl:when test="$show.bookmarks = 0">
288       <!-- omit bookmarks  -->
289     </xsl:when>
290     <xsl:when test="$xsl1.1.bookmarks != 0">
291       <!-- use standard bookmark elements -->
292       <xsl:variable name="bookmarks">
293         <xsl:apply-templates select="$document.element" 
294                              mode="bookmark"/>
295       </xsl:variable>
296       <xsl:if test="string($bookmarks) != ''">
297         <fo:bookmark-tree>
298           <xsl:copy-of select="$bookmarks"/>
299         </fo:bookmark-tree>
300       </xsl:if>
301     </xsl:when>
302     <xsl:otherwise>
303       <xsl:choose>
304         <!-- pre FOP 1.0 -->
305         <xsl:when test="$fop.extensions != 0">
306           <xsl:apply-templates select="$document.element" mode="fop.outline"/>
307         </xsl:when>
308     
309         <!-- FOP 1.0 -->
310         <xsl:when test="$fop1.extensions != 0">
311           <xsl:variable name="bookmarks">
312             <xsl:apply-templates select="$document.element" 
313                                  mode="fop1.outline"/>
314           </xsl:variable>
315           <xsl:if test="string($bookmarks) != ''">
316             <fo:bookmark-tree>
317               <xsl:copy-of select="$bookmarks"/>
318             </fo:bookmark-tree>
319           </xsl:if>
320         </xsl:when>
321   
322         <!-- RenderX XEP 4.6 and earlier -->
323         <xsl:when test="$xep.extensions != 0">
324           <xsl:variable name="bookmarks">
325             <xsl:apply-templates select="$document.element" mode="xep.outline"/>
326           </xsl:variable>
327           <xsl:if test="string($bookmarks) != ''">
328             <rx:outline xmlns:rx="http://www.renderx.com/XSL/Extensions">
329               <xsl:copy-of select="$bookmarks"/>
330             </rx:outline>
331           </xsl:if>
332         </xsl:when>
333   
334         <!-- PTC Arbortext -->
335         <xsl:when test="$arbortext.extensions != 0">
336           <xsl:variable name="bookmarks">
337             <xsl:apply-templates select="$document.element"
338                                  mode="ati.xsl11.bookmarks"/>
339           </xsl:variable>
340           <xsl:if test="string($bookmarks) != ''">
341             <fo:bookmark-tree>
342               <xsl:copy-of select="$bookmarks"/>
343             </fo:bookmark-tree>
344           </xsl:if>
345         </xsl:when>
346       </xsl:choose>
347     </xsl:otherwise>
348   </xsl:choose>
349 </xsl:template>
350
351 <xsl:variable name="bookmarks.state">
352   <xsl:choose>
353     <xsl:when test="$bookmarks.collapse != 0">hide</xsl:when>
354     <xsl:otherwise>show</xsl:otherwise>
355   </xsl:choose>
356 </xsl:variable>
357
358 <xsl:template match="*" mode="bookmark">
359   <xsl:apply-templates select="*" mode="bookmark"/>
360 </xsl:template>
361
362 <xsl:template match="set|book|part|reference|
363                      preface|chapter|appendix|article|topic
364                      |glossary|bibliography|index|setindex
365                      |refentry
366                      |sect1|sect2|sect3|sect4|sect5|section"
367               mode="bookmark">
368
369   <xsl:variable name="id">
370     <xsl:call-template name="object.id"/>
371   </xsl:variable>
372   <xsl:variable name="bookmark-label">
373     <xsl:apply-templates select="." mode="object.title.markup"/>
374   </xsl:variable>
375
376   <!-- Put the root element bookmark at the same level as its children -->
377   <!-- If the object is a set or book, generate a bookmark for the toc -->
378
379   <xsl:choose>
380     <xsl:when test="self::index and $generate.index = 0"/>
381     <xsl:when test="parent::*">
382       <fo:bookmark internal-destination="{$id}">
383         <xsl:attribute name="starting-state">
384           <xsl:value-of select="$bookmarks.state"/>
385         </xsl:attribute>
386         <fo:bookmark-title>
387           <xsl:value-of select="normalize-space($bookmark-label)"/>
388         </fo:bookmark-title>
389         <xsl:apply-templates select="*" mode="bookmark"/>
390       </fo:bookmark>
391     </xsl:when>
392     <xsl:otherwise>
393       <fo:bookmark internal-destination="{$id}">
394         <xsl:attribute name="starting-state">
395           <xsl:value-of select="$bookmarks.state"/>
396         </xsl:attribute>
397         <fo:bookmark-title>
398           <xsl:value-of select="normalize-space($bookmark-label)"/>
399         </fo:bookmark-title>
400       </fo:bookmark>
401
402       <xsl:variable name="toc.params">
403         <xsl:call-template name="find.path.params">
404           <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
405         </xsl:call-template>
406       </xsl:variable>
407
408       <xsl:if test="contains($toc.params, 'toc')
409                     and (book|part|reference|preface|chapter|appendix|article|topic
410                          |glossary|bibliography|index|setindex
411                          |refentry
412                          |sect1|sect2|sect3|sect4|sect5|section)">
413         <fo:bookmark internal-destination="toc...{$id}">
414           <fo:bookmark-title>
415             <xsl:call-template name="gentext">
416               <xsl:with-param name="key" select="'TableofContents'"/>
417             </xsl:call-template>
418           </fo:bookmark-title>
419         </fo:bookmark>
420       </xsl:if>
421       <xsl:apply-templates select="*" mode="bookmark"/>
422     </xsl:otherwise>
423   </xsl:choose>
424 </xsl:template>
425
426 <xsl:template name="root.messages">
427   <!-- redefine this any way you'd like to output messages -->
428   <!-- DO NOT OUTPUT ANYTHING FROM THIS TEMPLATE -->
429   <xsl:message>
430     <xsl:text>Making </xsl:text>
431     <xsl:value-of select="$page.orientation"/>
432     <xsl:text> pages on </xsl:text>
433     <xsl:value-of select="$paper.type"/>
434     <xsl:text> paper (</xsl:text>
435     <xsl:value-of select="$page.width"/>
436     <xsl:text>x</xsl:text>
437     <xsl:value-of select="$page.height"/>
438     <xsl:text>)</xsl:text>
439   </xsl:message>
440 </xsl:template>
441
442 <!-- ==================================================================== -->
443
444 </xsl:stylesheet>