Imported Upstream version 1.77.1
[platform/upstream/docbook-xsl.git] / html / toc.xsl
1 <?xml version='1.0'?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                 version='1.0'>
4
5 <!-- ********************************************************************
6      $Id: toc.xsl 9297 2012-04-22 03:56:16Z bobstayton $
7      ********************************************************************
8
9      This file is part of the XSL DocBook Stylesheet distribution.
10      See ../README or http://docbook.sf.net/release/xsl/current/ for
11      copyright and other information.
12
13      ******************************************************************** -->
14
15 <!-- ==================================================================== -->
16
17 <xsl:template match="set/toc | book/toc | part/toc">
18   <xsl:variable name="toc.params">
19     <xsl:call-template name="find.path.params">
20       <xsl:with-param name="node" select="parent::*"/>
21       <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
22     </xsl:call-template>
23   </xsl:variable>
24
25   <!-- Do not output the toc element if one is already generated
26        by the use of $generate.toc parameter, or if
27        generating a source toc is turned off -->
28   <xsl:if test="not(contains($toc.params, 'toc')) and
29                 ($process.source.toc != 0 or $process.empty.source.toc != 0)">
30     <xsl:variable name="content">
31       <xsl:choose>
32         <xsl:when test="* and $process.source.toc != 0">
33           <xsl:apply-templates />
34         </xsl:when>
35         <xsl:when test="count(*) = 0 and $process.empty.source.toc != 0">
36           <!-- trick to switch context node to parent element -->
37           <xsl:for-each select="parent::*">
38             <xsl:choose>
39               <xsl:when test="self::set">
40                 <xsl:call-template name="set.toc">
41                   <xsl:with-param name="toc.title.p" 
42                                   select="contains($toc.params, 'title')"/>
43                 </xsl:call-template>
44               </xsl:when>
45               <xsl:when test="self::book">
46                 <xsl:call-template name="division.toc">
47                   <xsl:with-param name="toc.title.p" 
48                                   select="contains($toc.params, 'title')"/>
49                 </xsl:call-template>
50               </xsl:when>
51               <xsl:when test="self::part">
52                 <xsl:call-template name="division.toc">
53                   <xsl:with-param name="toc.title.p" 
54                                   select="contains($toc.params, 'title')"/>
55                 </xsl:call-template>
56               </xsl:when>
57             </xsl:choose>
58           </xsl:for-each>
59         </xsl:when>
60       </xsl:choose>
61     </xsl:variable>
62
63     <xsl:if test="string-length(normalize-space($content)) != 0">
64       <xsl:copy-of select="$content"/>
65     </xsl:if>
66   </xsl:if>
67 </xsl:template>
68   
69 <xsl:template match="chapter/toc | appendix/toc | preface/toc | article/toc">
70   <xsl:variable name="toc.params">
71     <xsl:call-template name="find.path.params">
72       <xsl:with-param name="node" select="parent::*"/>
73       <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
74     </xsl:call-template>
75   </xsl:variable>
76
77   <!-- Do not output the toc element if one is already generated
78        by the use of $generate.toc parameter, or if
79        generating a source toc is turned off -->
80   <xsl:if test="not(contains($toc.params, 'toc')) and
81                 ($process.source.toc != 0 or $process.empty.source.toc != 0)">
82     <xsl:choose>
83       <xsl:when test="* and $process.source.toc != 0">
84         <div>
85           <xsl:apply-templates select="." mode="common.html.attributes"/>
86           <xsl:call-template name="id.attribute"/>
87           <xsl:apply-templates select="title"/> 
88           <dl>
89             <xsl:apply-templates select="." mode="common.html.attributes"/>
90             <xsl:apply-templates select="*[not(self::title)]"/> 
91           </dl>
92         </div>
93         <xsl:call-template name="component.toc.separator"/>
94       </xsl:when>
95       <xsl:when test="count(*) = 0 and $process.empty.source.toc != 0">
96         <!-- trick to switch context node to section element -->
97         <xsl:for-each select="parent::*">
98           <xsl:call-template name="component.toc">
99             <xsl:with-param name="toc.title.p" 
100                             select="contains($toc.params, 'title')"/>
101           </xsl:call-template>
102         </xsl:for-each>
103         <xsl:call-template name="component.toc.separator"/>
104       </xsl:when>
105     </xsl:choose>
106   </xsl:if>
107 </xsl:template>
108
109 <xsl:template match="section/toc
110                     |sect1/toc
111                     |sect2/toc
112                     |sect3/toc
113                     |sect4/toc
114                     |sect5/toc">
115
116   <xsl:variable name="toc.params">
117     <xsl:call-template name="find.path.params">
118       <xsl:with-param name="node" select="parent::*"/>
119       <xsl:with-param name="table" select="normalize-space($generate.toc)"/>
120     </xsl:call-template>
121   </xsl:variable>
122
123   <!-- Do not output the toc element if one is already generated
124        by the use of $generate.toc parameter, or if
125        generating a source toc is turned off -->
126   <xsl:if test="not(contains($toc.params, 'toc')) and
127                 ($process.source.toc != 0 or $process.empty.source.toc != 0)">
128     <xsl:choose>
129       <xsl:when test="* and $process.source.toc != 0">
130         <div>
131           <xsl:apply-templates select="." mode="common.html.attributes"/>
132           <xsl:call-template name="id.attribute"/>
133           <xsl:apply-templates select="title"/> 
134           <dl>
135             <xsl:apply-templates select="." mode="common.html.attributes"/>
136             <xsl:apply-templates select="*[not(self::title)]"/> 
137           </dl>
138         </div>
139         <xsl:call-template name="section.toc.separator"/>
140       </xsl:when>
141       <xsl:when test="count(*) = 0 and $process.empty.source.toc != 0">
142         <!-- trick to switch context node to section element -->
143         <xsl:for-each select="parent::*">
144           <xsl:call-template name="section.toc">
145             <xsl:with-param name="toc.title.p" 
146                             select="contains($toc.params, 'title')"/>
147           </xsl:call-template>
148         </xsl:for-each>
149         <xsl:call-template name="section.toc.separator"/>
150       </xsl:when>
151     </xsl:choose>
152   </xsl:if>
153 </xsl:template>
154
155 <!-- ==================================================================== -->
156
157 <xsl:template match="tocpart|tocchap
158                      |toclevel1|toclevel2|toclevel3|toclevel4|toclevel5">
159   <xsl:variable name="sub-toc">
160     <xsl:if test="tocchap|toclevel1|toclevel2|toclevel3|toclevel4|toclevel5">
161       <xsl:choose>
162         <xsl:when test="$toc.list.type = 'dl'">
163           <dd>
164             <xsl:apply-templates select="." mode="common.html.attributes"/>
165             <xsl:element name="{$toc.list.type}">
166               <xsl:apply-templates select="." mode="common.html.attributes"/>
167               <xsl:apply-templates select="tocchap|toclevel1|toclevel2|
168                                            toclevel3|toclevel4|toclevel5"/>
169             </xsl:element>
170           </dd>
171         </xsl:when>
172         <xsl:otherwise>
173           <xsl:element name="{$toc.list.type}">
174             <xsl:apply-templates select="." mode="common.html.attributes"/>
175             <xsl:apply-templates select="tocchap|toclevel1|toclevel2|
176                                          toclevel3|toclevel4|toclevel5"/>
177           </xsl:element>
178         </xsl:otherwise>
179       </xsl:choose>
180     </xsl:if>
181   </xsl:variable>
182
183   <xsl:apply-templates select="tocentry[position() != last()]"/>
184
185   <xsl:choose>
186     <xsl:when test="$toc.list.type = 'dl'">
187       <dt>
188         <xsl:apply-templates select="." mode="common.html.attributes"/>
189         <xsl:apply-templates select="tocentry[position() = last()]"/>
190       </dt>
191       <xsl:copy-of select="$sub-toc"/>
192     </xsl:when>
193     <xsl:otherwise>
194       <li>
195         <xsl:apply-templates select="." mode="common.html.attributes"/>
196         <xsl:apply-templates select="tocentry[position() = last()]"/>
197         <xsl:copy-of select="$sub-toc"/>
198       </li>
199     </xsl:otherwise>
200   </xsl:choose>
201 </xsl:template>
202
203 <xsl:template match="tocentry|tocdiv|lotentry|tocfront|tocback">
204   <xsl:choose>
205     <xsl:when test="$toc.list.type = 'dl'">
206       <dt>
207         <xsl:apply-templates select="." mode="common.html.attributes"/>
208         <xsl:call-template name="tocentry-content"/>
209       </dt>
210     </xsl:when>
211     <xsl:otherwise>
212       <li>
213         <xsl:apply-templates select="." mode="common.html.attributes"/>
214         <xsl:call-template name="tocentry-content"/>
215       </li>
216     </xsl:otherwise>
217   </xsl:choose>
218 </xsl:template>
219
220 <xsl:template match="tocentry[position() = last()]" priority="2">
221   <xsl:call-template name="tocentry-content"/>
222 </xsl:template>
223
224 <xsl:template name="tocentry-content">
225   <xsl:variable name="targets" select="key('id',@linkend)"/>
226   <xsl:variable name="target" select="$targets[1]"/>
227
228   <xsl:choose>
229     <xsl:when test="@linkend">
230       <xsl:call-template name="check.id.unique">
231         <xsl:with-param name="linkend" select="@linkend"/>
232       </xsl:call-template>
233       <a>
234         <xsl:attribute name="href">
235           <xsl:call-template name="href.target">
236             <xsl:with-param name="object" select="$target"/>
237           </xsl:call-template>
238         </xsl:attribute>
239         <xsl:apply-templates/>
240       </a>
241     </xsl:when>
242     <xsl:otherwise>
243       <xsl:apply-templates/>
244     </xsl:otherwise>
245   </xsl:choose>
246 </xsl:template>
247
248 <xsl:template match="toc/title">
249   <div>
250     <xsl:apply-templates select="." mode="common.html.attributes"/>
251     <xsl:apply-templates/>
252   </div>
253 </xsl:template>
254
255 <xsl:template match="toc/subtitle">
256   <div>
257     <xsl:apply-templates select="." mode="common.html.attributes"/>
258     <xsl:apply-templates/>
259   </div>
260 </xsl:template>
261
262 <xsl:template match="toc/titleabbrev">
263 </xsl:template>
264
265 <!-- ==================================================================== -->
266
267 <!-- A lot element must have content, because there is no attribute
268      to select what kind of list should be generated -->
269 <xsl:template match="book/lot | part/lot">
270   <!-- Don't generate a page sequence unless there is content -->
271   <xsl:variable name="content">
272     <xsl:choose>
273       <xsl:when test="* and $process.source.toc != 0">
274         <div>
275           <xsl:apply-templates select="." mode="common.html.attributes"/>
276           <xsl:apply-templates />
277         </div>
278       </xsl:when>
279       <xsl:when test="not(child::*) and $process.empty.source.toc != 0">
280         <xsl:call-template name="process.empty.lot"/>
281       </xsl:when>
282     </xsl:choose>
283   </xsl:variable>
284
285   <xsl:if test="string-length(normalize-space($content)) != 0">
286     <xsl:copy-of select="$content"/>
287   </xsl:if>
288 </xsl:template>
289   
290 <xsl:template match="chapter/lot | appendix/lot | preface/lot | article/lot">
291   <xsl:choose>
292     <xsl:when test="* and $process.source.toc != 0">
293       <div>
294         <xsl:apply-templates select="." mode="common.html.attributes"/>
295         <xsl:apply-templates />
296       </div>
297       <xsl:call-template name="component.toc.separator"/>
298     </xsl:when>
299     <xsl:when test="not(child::*) and $process.empty.source.toc != 0">
300       <xsl:call-template name="process.empty.lot"/>
301     </xsl:when>
302   </xsl:choose>
303 </xsl:template>
304
305 <xsl:template match="section/lot
306                     |sect1/lot
307                     |sect2/lot
308                     |sect3/lot
309                     |sect4/lot
310                     |sect5/lot">
311   <xsl:choose>
312     <xsl:when test="* and $process.source.toc != 0">
313       <div>
314         <xsl:apply-templates select="." mode="common.html.attributes"/>
315         <xsl:apply-templates/>
316       </div>
317       <xsl:call-template name="section.toc.separator"/>
318     </xsl:when>
319     <xsl:when test="not(child::*) and $process.empty.source.toc != 0">
320       <xsl:call-template name="process.empty.lot"/>
321     </xsl:when>
322   </xsl:choose>
323 </xsl:template>
324
325 <xsl:template name="process.empty.lot">
326   <!-- An empty lot element does not provide any information to indicate
327        what should be included in it.  You can customize this
328        template to generate a lot based on @role or something -->
329   <xsl:message>
330     <xsl:text>Warning: don't know what to generate for </xsl:text>
331     <xsl:text>lot that has no children.</xsl:text>
332   </xsl:message>
333 </xsl:template>
334
335 <xsl:template match="lot/title">
336   <div>
337     <xsl:apply-templates select="." mode="common.html.attributes"/>
338     <xsl:apply-templates/>
339   </div>
340 </xsl:template>
341
342 <xsl:template match="lot/subtitle">
343   <div>
344     <xsl:apply-templates select="." mode="common.html.attributes"/>
345     <xsl:apply-templates/>
346   </div>
347 </xsl:template>
348
349 <xsl:template match="lot/titleabbrev">
350 </xsl:template>
351
352 </xsl:stylesheet>