dbtoepub
[platform/upstream/docbook-xsl-stylesheets.git] / common / labels.xsl
1 <?xml version='1.0'?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                 xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
4                 exclude-result-prefixes="doc"
5                 version='1.0'>
6
7 <!-- ********************************************************************
8      $Id: labels.xsl 9706 2013-01-16 18:56:16Z bobstayton $
9      ********************************************************************
10
11      This file is part of the XSL DocBook Stylesheet distribution.
12      See ../README or http://docbook.sf.net/release/xsl/current/ for
13      copyright and other information.
14
15      ******************************************************************** -->
16
17 <!-- ==================================================================== -->
18
19 <!-- label markup -->
20
21 <doc:mode mode="label.markup" xmlns="">
22 <refpurpose>Provides access to element labels</refpurpose>
23 <refdescription id="label.markup-desc">
24 <para>Processing an element in the
25 <literal role="mode">label.markup</literal> mode produces the
26 element label.</para>
27 <para>Trailing punctuation is not added to the label.
28 </para>
29 </refdescription>
30 </doc:mode>
31
32 <xsl:template match="*" mode="intralabel.punctuation">
33   <xsl:text>.</xsl:text>
34 </xsl:template>
35
36 <xsl:template match="*" mode="label.markup">
37   <xsl:param name="verbose" select="1"/>
38   <xsl:if test="$verbose">
39     <xsl:message>
40       <xsl:text>Request for label of unexpected element: </xsl:text>
41       <xsl:value-of select="local-name(.)"/>
42     </xsl:message>
43   </xsl:if>
44 </xsl:template>
45
46 <xsl:template match="set|book" mode="label.markup">
47   <xsl:if test="@label">
48     <xsl:value-of select="@label"/>
49   </xsl:if>
50 </xsl:template>
51
52 <xsl:template match="part" mode="label.markup">
53   <xsl:choose>
54     <xsl:when test="@label">
55       <xsl:value-of select="@label"/>
56     </xsl:when>
57     <xsl:when test="string($part.autolabel) != 0">
58       <xsl:variable name="format">
59         <xsl:call-template name="autolabel.format">
60           <xsl:with-param name="format" select="$part.autolabel"/>
61         </xsl:call-template>
62       </xsl:variable>
63       <xsl:number from="book" count="part" format="{$format}"/>
64     </xsl:when>
65   </xsl:choose>
66 </xsl:template>
67
68 <xsl:template match="partintro" mode="label.markup">
69   <!-- no label -->
70 </xsl:template>
71
72 <xsl:template match="preface" mode="label.markup">
73   <xsl:choose>
74     <xsl:when test="@label">
75       <xsl:value-of select="@label"/>
76     </xsl:when>
77     <xsl:when test="string($preface.autolabel) != 0">
78       <xsl:if test="$component.label.includes.part.label != 0 and
79                       ancestor::part">
80         <xsl:variable name="part.label">
81           <xsl:apply-templates select="ancestor::part" 
82                                mode="label.markup"/>
83         </xsl:variable>
84         <xsl:if test="$part.label != ''">
85           <xsl:value-of select="$part.label"/>
86           <xsl:apply-templates select="ancestor::part" 
87                                mode="intralabel.punctuation"/>
88         </xsl:if>
89       </xsl:if>
90       <xsl:variable name="format">
91         <xsl:call-template name="autolabel.format">
92           <xsl:with-param name="format" select="$preface.autolabel"/>
93         </xsl:call-template>
94       </xsl:variable>
95       <xsl:choose>
96         <xsl:when test="$label.from.part != 0 and ancestor::part">
97           <xsl:number from="part" count="preface" format="{$format}" level="any"/>
98         </xsl:when>
99         <xsl:otherwise>
100           <xsl:number from="book" count="preface" format="{$format}" level="any"/>
101         </xsl:otherwise>
102       </xsl:choose>
103     </xsl:when>
104   </xsl:choose>
105 </xsl:template>
106
107 <xsl:template match="chapter" mode="label.markup">
108   <xsl:choose>
109     <xsl:when test="@label">
110       <xsl:value-of select="@label"/>
111     </xsl:when>
112     <xsl:when test="string($chapter.autolabel) != 0">
113       <xsl:if test="$component.label.includes.part.label != 0 and
114                       ancestor::part">
115         <xsl:variable name="part.label">
116           <xsl:apply-templates select="ancestor::part" 
117                                mode="label.markup"/>
118         </xsl:variable>
119         <xsl:if test="$part.label != ''">
120           <xsl:value-of select="$part.label"/>
121           <xsl:apply-templates select="ancestor::part" 
122                                mode="intralabel.punctuation"/>
123         </xsl:if>
124       </xsl:if>
125       <xsl:variable name="format">
126         <xsl:call-template name="autolabel.format">
127           <xsl:with-param name="format" select="$chapter.autolabel"/>
128         </xsl:call-template>
129       </xsl:variable>
130       <xsl:choose>
131         <xsl:when test="$label.from.part != 0 and ancestor::part">
132           <xsl:number from="part" count="chapter" format="{$format}" level="any"/>
133         </xsl:when>
134         <xsl:otherwise>
135           <xsl:number from="book" count="chapter" format="{$format}" level="any"/>
136         </xsl:otherwise>
137       </xsl:choose>
138     </xsl:when>
139   </xsl:choose>
140 </xsl:template>
141
142 <xsl:template match="appendix" mode="label.markup">
143   <xsl:choose>
144     <xsl:when test="@label">
145       <xsl:value-of select="@label"/>
146     </xsl:when>
147     <xsl:when test="string($appendix.autolabel) != 0">
148       <xsl:if test="$component.label.includes.part.label != 0 and
149                       ancestor::part">
150         <xsl:variable name="part.label">
151           <xsl:apply-templates select="ancestor::part" 
152                                mode="label.markup"/>
153         </xsl:variable>
154         <xsl:if test="$part.label != ''">
155           <xsl:value-of select="$part.label"/>
156           <xsl:apply-templates select="ancestor::part" 
157                                mode="intralabel.punctuation"/>
158         </xsl:if>
159       </xsl:if>
160       <xsl:variable name="format">
161         <xsl:call-template name="autolabel.format">
162           <xsl:with-param name="format" select="$appendix.autolabel"/>
163         </xsl:call-template>
164       </xsl:variable>
165       <xsl:choose>
166         <xsl:when test="$label.from.part != 0 and ancestor::part">
167           <xsl:number from="part" count="appendix" format="{$format}" level="any"/>
168         </xsl:when>
169         <xsl:otherwise>
170           <xsl:number from="book|article"
171                       count="appendix" format="{$format}" level="any"/>
172         </xsl:otherwise>
173       </xsl:choose>
174     </xsl:when>
175   </xsl:choose>
176 </xsl:template>
177
178 <xsl:template match="article" mode="label.markup">
179   <xsl:if test="@label">
180     <xsl:value-of select="@label"/>
181   </xsl:if>
182 </xsl:template>
183
184 <xsl:template match="dedication|colophon" mode="label.markup">
185   <xsl:if test="@label">
186     <xsl:value-of select="@label"/>
187   </xsl:if>
188 </xsl:template>
189
190 <xsl:template match="reference" mode="label.markup">
191   <xsl:choose>
192     <xsl:when test="@label">
193       <xsl:value-of select="@label"/>
194     </xsl:when>
195     <xsl:when test="string($reference.autolabel) != 0">
196       <xsl:if test="$component.label.includes.part.label != 0 and
197                       ancestor::part">
198         <xsl:variable name="part.label">
199           <xsl:apply-templates select="ancestor::part" 
200                                mode="label.markup"/>
201         </xsl:variable>
202         <xsl:if test="$part.label != ''">
203           <xsl:value-of select="$part.label"/>
204           <xsl:apply-templates select="ancestor::part" 
205                                mode="intralabel.punctuation"/>
206         </xsl:if>
207       </xsl:if>
208       <xsl:variable name="format">
209         <xsl:call-template name="autolabel.format">
210           <xsl:with-param name="format" select="$reference.autolabel"/>
211         </xsl:call-template>
212       </xsl:variable>
213       <xsl:choose>
214         <xsl:when test="$label.from.part != 0 and ancestor::part">
215           <xsl:number from="part" count="reference" format="{$format}" level="any"/>
216         </xsl:when>
217         <xsl:otherwise>
218           <xsl:number from="book" count="reference" format="{$format}" level="any"/>
219         </xsl:otherwise>
220       </xsl:choose>
221     </xsl:when>
222   </xsl:choose>
223 </xsl:template>
224
225 <xsl:template match="refentry" mode="label.markup">
226   <xsl:if test="@label">
227     <xsl:value-of select="@label"/>
228   </xsl:if>
229 </xsl:template>
230
231 <xsl:template match="section" mode="label.markup">
232   <!-- if this is a nested section, label the parent -->
233   <xsl:if test="local-name(..) = 'section'">
234     <xsl:variable name="parent.section.label">
235       <xsl:call-template name="label.this.section">
236         <xsl:with-param name="section" select=".."/>
237       </xsl:call-template>
238     </xsl:variable>
239     <xsl:if test="$parent.section.label != '0'">
240       <xsl:apply-templates select=".." mode="label.markup"/>
241       <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
242     </xsl:if>
243   </xsl:if>
244
245   <!-- if the parent is a component, maybe label that too -->
246   <xsl:variable name="parent.is.component">
247     <xsl:call-template name="is.component">
248       <xsl:with-param name="node" select=".."/>
249     </xsl:call-template>
250   </xsl:variable>
251
252   <!-- does this section get labelled? -->
253   <xsl:variable name="label">
254     <xsl:call-template name="label.this.section">
255       <xsl:with-param name="section" select="."/>
256     </xsl:call-template>
257   </xsl:variable>
258
259   <xsl:if test="$section.label.includes.component.label != 0
260                 and $parent.is.component != 0">
261     <xsl:variable name="parent.label">
262       <xsl:apply-templates select=".." mode="label.markup"/>
263     </xsl:variable>
264     <xsl:if test="$parent.label != ''">
265       <xsl:apply-templates select=".." mode="label.markup"/>
266       <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
267     </xsl:if>
268   </xsl:if>
269
270 <!--
271   <xsl:message>
272     test: <xsl:value-of select="$label"/>, <xsl:number count="section"/>
273   </xsl:message>
274 -->
275
276   <xsl:choose>
277     <xsl:when test="@label">
278       <xsl:value-of select="@label"/>
279     </xsl:when>
280     <xsl:when test="$label != 0">      
281       <xsl:variable name="format">
282         <xsl:call-template name="autolabel.format">
283           <xsl:with-param name="format" select="$section.autolabel"/>
284         </xsl:call-template>
285       </xsl:variable>
286       <xsl:number format="{$format}" count="section"/>
287     </xsl:when>
288   </xsl:choose>
289 </xsl:template>
290
291 <xsl:template match="sect1" mode="label.markup">
292   <!-- if the parent is a component, maybe label that too -->
293   <xsl:variable name="parent.is.component">
294     <xsl:call-template name="is.component">
295       <xsl:with-param name="node" select=".."/>
296     </xsl:call-template>
297   </xsl:variable>
298
299   <xsl:variable name="component.label">
300     <xsl:if test="$section.label.includes.component.label != 0
301                   and $parent.is.component != 0">
302       <xsl:variable name="parent.label">
303         <xsl:apply-templates select=".." mode="label.markup"/>
304       </xsl:variable>
305       <xsl:if test="$parent.label != ''">
306         <xsl:apply-templates select=".." mode="label.markup"/>
307         <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
308       </xsl:if>
309     </xsl:if>
310   </xsl:variable>
311
312
313   <xsl:variable name="is.numbered">
314     <xsl:call-template name="label.this.section"/>
315   </xsl:variable>
316
317   <xsl:choose>
318     <xsl:when test="@label">
319       <xsl:value-of select="@label"/>
320     </xsl:when>
321     <xsl:when test="$is.numbered != 0">
322       <xsl:variable name="format">
323         <xsl:call-template name="autolabel.format">
324           <xsl:with-param name="format" select="$section.autolabel"/>
325         </xsl:call-template>
326       </xsl:variable>
327       <xsl:copy-of select="$component.label"/>
328       <xsl:number format="{$format}" count="sect1"/>
329     </xsl:when>
330   </xsl:choose>
331 </xsl:template>
332
333 <xsl:template match="sect2|sect3|sect4|sect5" mode="label.markup">
334   <!-- label the parent -->
335   <xsl:variable name="parent.section.label">
336     <xsl:call-template name="label.this.section">
337       <xsl:with-param name="section" select=".."/>
338     </xsl:call-template>
339   </xsl:variable>
340   <xsl:if test="$parent.section.label != '0'">
341     <xsl:apply-templates select=".." mode="label.markup"/>
342     <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
343   </xsl:if>
344
345   <xsl:variable name="is.numbered">
346     <xsl:call-template name="label.this.section"/>
347   </xsl:variable>
348
349   <xsl:choose>
350     <xsl:when test="@label">
351       <xsl:value-of select="@label"/>
352     </xsl:when>
353     <xsl:when test="$is.numbered != 0">
354       <xsl:variable name="format">
355         <xsl:call-template name="autolabel.format">
356           <xsl:with-param name="format" select="$section.autolabel"/>
357         </xsl:call-template>
358       </xsl:variable>
359       <xsl:choose>
360         <xsl:when test="local-name(.) = 'sect2'">
361           <xsl:number format="{$format}" count="sect2"/>
362         </xsl:when>
363         <xsl:when test="local-name(.) = 'sect3'">
364           <xsl:number format="{$format}" count="sect3"/>
365         </xsl:when>
366         <xsl:when test="local-name(.) = 'sect4'">
367           <xsl:number format="{$format}" count="sect4"/>
368         </xsl:when>
369         <xsl:when test="local-name(.) = 'sect5'">
370           <xsl:number format="{$format}" count="sect5"/>
371         </xsl:when>
372         <xsl:otherwise>
373           <xsl:message>label.markup: this can't happen!</xsl:message>
374         </xsl:otherwise>
375       </xsl:choose>
376     </xsl:when>
377   </xsl:choose>
378 </xsl:template>
379
380 <xsl:template match="bridgehead" mode="label.markup">
381   <!-- bridgeheads are not normally numbered -->
382 </xsl:template>
383
384 <xsl:template match="refsect1" mode="label.markup">
385   <xsl:choose>
386     <xsl:when test="@label">
387       <xsl:value-of select="@label"/>
388     </xsl:when>
389     <xsl:when test="$section.autolabel != 0">
390       <xsl:variable name="format">
391         <xsl:call-template name="autolabel.format">
392           <xsl:with-param name="format" select="$section.autolabel"/>
393         </xsl:call-template>
394       </xsl:variable>
395       <xsl:number count="refsect1" format="{$format}"/>
396     </xsl:when>
397   </xsl:choose>
398 </xsl:template>
399
400 <xsl:template match="refsect2|refsect3" mode="label.markup">
401   <!-- label the parent -->
402   <xsl:variable name="parent.label">
403     <xsl:apply-templates select=".." mode="label.markup"/>
404   </xsl:variable>
405   <xsl:if test="$parent.label != ''">
406     <xsl:apply-templates select=".." mode="label.markup"/>
407     <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
408   </xsl:if>
409
410   <xsl:choose>
411     <xsl:when test="@label">
412       <xsl:value-of select="@label"/>
413     </xsl:when>
414     <xsl:when test="$section.autolabel != 0">
415       <xsl:variable name="format">
416         <xsl:call-template name="autolabel.format">
417           <xsl:with-param name="format" select="$section.autolabel"/>
418         </xsl:call-template>
419       </xsl:variable>
420       <xsl:choose>
421         <xsl:when test="local-name(.) = 'refsect2'">
422           <xsl:number count="refsect2" format="{$format}"/>
423         </xsl:when>
424         <xsl:otherwise>
425           <xsl:number count="refsect3" format="{$format}"/>
426         </xsl:otherwise>
427       </xsl:choose>
428     </xsl:when>
429   </xsl:choose>
430 </xsl:template>
431
432 <xsl:template match="simplesect" mode="label.markup">
433   <!-- if this is a nested section, label the parent -->
434   <xsl:if test="local-name(..) = 'section'
435                 or local-name(..) = 'sect1'
436                 or local-name(..) = 'sect2'
437                 or local-name(..) = 'sect3'
438                 or local-name(..) = 'sect4'
439                 or local-name(..) = 'sect5'">
440     <xsl:variable name="parent.section.label">
441       <xsl:apply-templates select=".." mode="label.markup"/>
442     </xsl:variable>
443     <xsl:if test="$parent.section.label != ''">
444       <xsl:apply-templates select=".." mode="label.markup"/>
445       <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
446     </xsl:if>
447   </xsl:if>
448
449   <!-- if the parent is a component, maybe label that too -->
450   <xsl:variable name="parent.is.component">
451     <xsl:call-template name="is.component">
452       <xsl:with-param name="node" select=".."/>
453     </xsl:call-template>
454   </xsl:variable>
455
456   <!-- does this section get labelled? -->
457   <xsl:variable name="label">
458     <xsl:call-template name="label.this.section">
459       <xsl:with-param name="section" select="."/>
460     </xsl:call-template>
461   </xsl:variable>
462
463   <xsl:if test="$section.label.includes.component.label != 0
464                 and $parent.is.component != 0">
465     <xsl:variable name="parent.label">
466       <xsl:apply-templates select=".." mode="label.markup"/>
467     </xsl:variable>
468     <xsl:if test="$parent.label != ''">
469       <xsl:apply-templates select=".." mode="label.markup"/>
470       <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
471     </xsl:if>
472   </xsl:if>
473
474   <xsl:choose>
475     <xsl:when test="@label">
476       <xsl:value-of select="@label"/>
477     </xsl:when>
478     <xsl:when test="$label != 0">
479       <xsl:variable name="format">
480         <xsl:call-template name="autolabel.format">
481           <xsl:with-param name="format" select="$section.autolabel"/>
482         </xsl:call-template>
483       </xsl:variable>
484       <xsl:number format="{$format}" count="simplesect"/>
485     </xsl:when>
486   </xsl:choose>
487 </xsl:template>
488
489 <xsl:template match="topic" mode="label.markup">
490   <!-- topics are not numbered by default -->
491 </xsl:template>
492
493 <xsl:template match="qandadiv" mode="label.markup">
494   <xsl:variable name="lparent" select="(ancestor::set
495                                        |ancestor::book
496                                        |ancestor::chapter
497                                        |ancestor::appendix
498                                        |ancestor::preface
499                                        |ancestor::section
500                                        |ancestor::simplesect
501                                        |ancestor::sect1
502                                        |ancestor::sect2
503                                        |ancestor::sect3
504                                        |ancestor::sect4
505                                        |ancestor::sect5
506                                        |ancestor::refsect1
507                                        |ancestor::refsect2
508                                        |ancestor::refsect3)[last()]"/>
509
510   <xsl:variable name="lparent.prefix">
511     <xsl:apply-templates select="$lparent" mode="label.markup"/>
512   </xsl:variable>
513
514   <xsl:variable name="prefix">
515     <xsl:if test="$qanda.inherit.numeration != 0">
516       <xsl:if test="$lparent.prefix != ''">
517         <xsl:apply-templates select="$lparent" mode="label.markup"/>
518         <xsl:apply-templates select="$lparent" mode="intralabel.punctuation"/>
519       </xsl:if>
520     </xsl:if>
521   </xsl:variable>
522
523   <xsl:choose>
524     <xsl:when test="$qandadiv.autolabel != 0">
525       <xsl:variable name="format">
526         <xsl:call-template name="autolabel.format">
527           <xsl:with-param name="format" select="$qandadiv.autolabel"/>
528         </xsl:call-template>
529       </xsl:variable>
530       <xsl:value-of select="$prefix"/>
531       <xsl:number level="multiple" count="qandadiv" format="{$format}"/>
532     </xsl:when>
533   </xsl:choose>
534 </xsl:template>
535
536 <xsl:template match="question|answer" mode="label.markup">
537   <xsl:variable name="lparent" select="(ancestor::set
538                                        |ancestor::book
539                                        |ancestor::chapter
540                                        |ancestor::appendix
541                                        |ancestor::preface
542                                        |ancestor::section
543                                        |ancestor::simplesect
544                                        |ancestor::sect1
545                                        |ancestor::sect2
546                                        |ancestor::sect3
547                                        |ancestor::sect4
548                                        |ancestor::sect5
549                                        |ancestor::refsect1
550                                        |ancestor::refsect2
551                                        |ancestor::refsect3)[last()]"/>
552
553   <xsl:variable name="lparent.prefix">
554     <xsl:apply-templates select="$lparent" mode="label.markup"/>
555   </xsl:variable>
556
557   <xsl:variable name="prefix">
558     <xsl:if test="$qanda.inherit.numeration != 0">
559       <xsl:choose>
560         <xsl:when test="ancestor::qandadiv">
561           <xsl:variable name="div.label">
562             <xsl:apply-templates select="ancestor::qandadiv[1]" mode="label.markup"/>
563           </xsl:variable>
564           <xsl:if test="string-length($div.label) != 0">
565             <xsl:copy-of select="$div.label"/>
566             <xsl:apply-templates select="ancestor::qandadiv[1]"
567                                  mode="intralabel.punctuation"/>
568           </xsl:if>
569         </xsl:when>
570         <xsl:when test="$lparent.prefix != ''">
571           <xsl:apply-templates select="$lparent" mode="label.markup"/>
572           <xsl:apply-templates select="$lparent" mode="intralabel.punctuation"/>
573         </xsl:when>
574       </xsl:choose>
575     </xsl:if>
576   </xsl:variable>
577
578   <xsl:variable name="inhlabel"
579                 select="ancestor-or-self::qandaset/@defaultlabel[1]"/>
580
581   <xsl:variable name="deflabel">
582     <xsl:choose>
583       <xsl:when test="$inhlabel != ''">
584         <xsl:value-of select="$inhlabel"/>
585       </xsl:when>
586       <xsl:otherwise>
587         <xsl:value-of select="$qanda.defaultlabel"/>
588       </xsl:otherwise>
589     </xsl:choose>
590   </xsl:variable>
591
592   <xsl:variable name="label" select="label"/>
593
594   <xsl:choose>
595     <xsl:when test="count($label)>0">
596       <xsl:apply-templates select="$label"/>
597     </xsl:when>
598
599     <xsl:when test="$deflabel = 'qanda' and self::question">
600       <xsl:call-template name="gentext">
601         <xsl:with-param name="key" select="'Question'"/>
602       </xsl:call-template>
603     </xsl:when>
604
605     <xsl:when test="$deflabel = 'qanda' and self::answer">
606       <xsl:call-template name="gentext">
607         <xsl:with-param name="key" select="'Answer'"/>
608       </xsl:call-template>
609     </xsl:when>
610
611     <xsl:when test="($deflabel = 'qnumber' or
612                      $deflabel = 'qnumberanda') and self::question">
613       <xsl:call-template name="gentext">
614         <xsl:with-param name="key" select="'Question'"/>
615       </xsl:call-template>
616       <xsl:text>&#xA0;</xsl:text>
617       <xsl:value-of select="$prefix"/>
618       <xsl:number level="multiple" count="qandaentry" format="1"/>
619     </xsl:when>
620
621     <xsl:when test="$deflabel = 'qnumberanda' and self::answer">
622       <xsl:call-template name="gentext">
623         <xsl:with-param name="key" select="'Answer'"/>
624       </xsl:call-template>
625     </xsl:when>
626
627     <xsl:when test="$deflabel = 'number' and self::question">
628       <xsl:value-of select="$prefix"/>
629       <xsl:number level="multiple" count="qandaentry" format="1"/>
630     </xsl:when>
631   </xsl:choose>
632 </xsl:template>
633
634 <xsl:template match="bibliography|glossary|
635                      qandaset|index|setindex" mode="label.markup">
636   <xsl:if test="@label">
637     <xsl:value-of select="@label"/>
638   </xsl:if>
639 </xsl:template>
640
641 <xsl:template match="figure|table|example" mode="label.markup">
642   <xsl:variable name="pchap"
643                 select="(ancestor::chapter
644                         |ancestor::appendix
645                         |ancestor::article[ancestor::book])[last()]"/>
646
647   <xsl:variable name="prefix">
648     <xsl:if test="count($pchap) &gt; 0">
649       <xsl:apply-templates select="$pchap" mode="label.markup"/>
650     </xsl:if>
651   </xsl:variable>
652
653   <xsl:choose>
654     <xsl:when test="@label">
655       <xsl:value-of select="@label"/>
656     </xsl:when>
657     <xsl:otherwise>
658       <xsl:choose>
659         <xsl:when test="$prefix != ''">
660             <xsl:apply-templates select="$pchap" mode="label.markup"/>
661             <xsl:apply-templates select="$pchap" mode="intralabel.punctuation"/>
662           <xsl:number format="1" from="chapter|appendix" level="any"/>
663         </xsl:when>
664         <xsl:otherwise>
665           <xsl:number format="1" from="book|article" level="any"/>
666         </xsl:otherwise>
667       </xsl:choose>
668     </xsl:otherwise>
669   </xsl:choose>
670 </xsl:template>
671
672 <xsl:template match="procedure" mode="label.markup">
673   <xsl:variable name="pchap"
674                 select="ancestor::chapter
675                         |ancestor::appendix
676                         |ancestor::article[ancestor::book]"/>
677
678   <xsl:variable name="prefix">
679     <xsl:if test="count($pchap) &gt; 0">
680       <xsl:apply-templates select="$pchap" mode="label.markup"/>
681     </xsl:if>
682   </xsl:variable>
683
684   <xsl:choose>
685     <xsl:when test="@label">
686       <xsl:value-of select="@label"/>
687     </xsl:when>
688     <xsl:when test="$formal.procedures = 0">
689       <!-- No label -->
690     </xsl:when>
691     <xsl:otherwise>
692       <xsl:choose>
693         <xsl:when test="count($pchap)>0">
694           <xsl:if test="$prefix != ''">
695             <xsl:apply-templates select="$pchap" mode="label.markup"/>
696             <xsl:apply-templates select="$pchap" mode="intralabel.punctuation"/>
697           </xsl:if>
698           <xsl:number count="procedure[title]" format="1" 
699                       from="chapter|appendix" level="any"/>
700         </xsl:when>
701         <xsl:otherwise>
702           <xsl:number count="procedure[title]" format="1" 
703                       from="book|article" level="any"/>
704         </xsl:otherwise>
705       </xsl:choose>
706     </xsl:otherwise>
707   </xsl:choose>
708 </xsl:template>
709
710 <xsl:template match="equation" mode="label.markup">
711   <xsl:variable name="pchap"
712                 select="ancestor::chapter
713                         |ancestor::appendix
714                         |ancestor::article[ancestor::book]"/>
715
716   <xsl:variable name="prefix">
717     <xsl:if test="count($pchap) &gt; 0">
718       <xsl:apply-templates select="$pchap" mode="label.markup"/>
719     </xsl:if>
720   </xsl:variable>
721
722   <xsl:choose>
723     <xsl:when test="@label">
724       <xsl:value-of select="@label"/>
725     </xsl:when>
726     <xsl:otherwise>
727       <xsl:choose>
728         <xsl:when test="count($pchap)>0">
729           <xsl:if test="$prefix != ''">
730             <xsl:apply-templates select="$pchap" mode="label.markup"/>
731             <xsl:apply-templates select="$pchap" mode="intralabel.punctuation"/>
732           </xsl:if>
733           <xsl:number format="1" count="equation" 
734                       from="chapter|appendix" level="any"/>
735         </xsl:when>
736         <xsl:otherwise>
737           <xsl:number format="1" count="equation" 
738                       from="book|article" level="any"/>
739         </xsl:otherwise>
740       </xsl:choose>
741     </xsl:otherwise>
742   </xsl:choose>
743 </xsl:template>
744
745 <xsl:template match="orderedlist/listitem" mode="label.markup">
746   <xsl:variable name="numeration">
747     <xsl:call-template name="list.numeration">
748       <xsl:with-param name="node" select="parent::orderedlist"/>
749     </xsl:call-template>
750   </xsl:variable>
751
752   <xsl:variable name="type">
753     <xsl:choose>
754       <xsl:when test="$numeration='arabic'">1</xsl:when>
755       <xsl:when test="$numeration='loweralpha'">a</xsl:when>
756       <xsl:when test="$numeration='lowerroman'">i</xsl:when>
757       <xsl:when test="$numeration='upperalpha'">A</xsl:when>
758       <xsl:when test="$numeration='upperroman'">I</xsl:when>
759       <!-- What!? This should never happen -->
760       <xsl:otherwise>
761         <xsl:message>
762           <xsl:text>Unexpected numeration: </xsl:text>
763           <xsl:value-of select="$numeration"/>
764         </xsl:message>
765         <xsl:value-of select="1."/>
766       </xsl:otherwise>
767     </xsl:choose>
768   </xsl:variable>
769
770   <xsl:variable name="item-number">
771     <xsl:call-template name="orderedlist-item-number"/>
772   </xsl:variable>
773
774   <xsl:number value="$item-number" format="{$type}"/>
775 </xsl:template>
776
777 <xsl:template match="production" mode="label.markup">
778   <xsl:number count="production" level="any"/>
779 </xsl:template>
780
781 <xsl:template match="abstract" mode="label.markup">
782   <!-- nop -->
783 </xsl:template>
784
785 <xsl:template match="sidebar" mode="label.markup">
786   <!-- nop -->
787 </xsl:template>
788
789 <xsl:template match="glossdiv|glosslist" mode="label.markup">
790   <!-- nop -->
791 </xsl:template>
792
793 <xsl:template match="glossentry" mode="label.markup">
794   <!-- nop -->
795 </xsl:template>
796
797 <!-- ============================================================ -->
798
799 <xsl:template name="label.this.section">
800   <xsl:param name="section" select="."/>
801
802   <xsl:variable name="level">
803     <xsl:call-template name="section.level"/>
804   </xsl:variable>
805
806   <xsl:choose>
807     <!-- bridgeheads are not numbered -->
808     <xsl:when test="$section/self::bridgehead">0</xsl:when>
809     <xsl:when test="$level &lt;= $section.autolabel.max.depth">      
810       <xsl:value-of select="$section.autolabel"/>
811     </xsl:when>
812     <xsl:otherwise>0</xsl:otherwise>
813   </xsl:choose>
814 </xsl:template>
815
816 <doc:template name="label.this.section" xmlns="">
817 <refpurpose>Returns true if $section should be labelled</refpurpose>
818 <refdescription id="label.this.section-desc">
819 <para>Returns true if the specified section should be labelled.
820 By default, this template returns zero unless 
821 the section level is less than or equal to the value of the
822 <literal>$section.autolabel.max.depth</literal> parameter, in
823 which case it returns
824 <literal>$section.autolabel</literal>.
825 Custom stylesheets may override it to get more selective behavior.</para>
826 </refdescription>
827 </doc:template>
828
829 <!-- ============================================================ -->
830
831 <xsl:template name="default.autolabel.format">
832   <xsl:param name="context" select="."/>
833   <xsl:choose>
834     <xsl:when test="local-name($context) = 'appendix'">
835       <xsl:value-of select="'A'"/>
836     </xsl:when>
837     <xsl:when test="local-name($context) = 'part'">
838       <xsl:value-of select="'I'"/>
839     </xsl:when>
840     <xsl:otherwise>1</xsl:otherwise>
841   </xsl:choose>  
842 </xsl:template>
843   
844 <xsl:template name="autolabel.format">
845   <xsl:param name="context" select="."/>
846   <xsl:param name="format"/>
847
848   <xsl:choose>
849     <xsl:when test="string($format) != 0">
850       <xsl:choose>
851         <xsl:when test="string($format)='arabic' or $format='1'">1</xsl:when>
852         <xsl:when test="$format='loweralpha' or $format='a'">
853           <xsl:value-of select="'a'"/>
854         </xsl:when>
855         <xsl:when test="$format='lowerroman' or $format='i'">
856           <xsl:value-of select="'i'"/>
857         </xsl:when>
858         <xsl:when test="$format='upperalpha' or $format='A'">
859           <xsl:value-of select="'A'"/>
860         </xsl:when>
861         <xsl:when test="$format='upperroman' or $format='I'">
862           <xsl:value-of select="'I'"/>
863         </xsl:when>      
864         <xsl:when test="$format='arabicindic' or $format='&#x661;'">
865           <xsl:value-of select="'&#x661;'"/>
866         </xsl:when>
867         <xsl:otherwise>
868           <xsl:message>
869             <xsl:text>Unexpected </xsl:text><xsl:value-of select="local-name(.)"/><xsl:text>.autolabel value: </xsl:text>
870             <xsl:value-of select="$format"/><xsl:text>; using default.</xsl:text>
871           </xsl:message>
872           <xsl:call-template name="default.autolabel.format"/>
873         </xsl:otherwise>
874       </xsl:choose>
875     </xsl:when>
876   </xsl:choose>
877 </xsl:template>
878
879 <doc:template name="autolabel.format" xmlns="">
880 <refpurpose>Returns format for autolabel parameters</refpurpose>
881 <refdescription id="autolabel.format-desc">
882 <para>Returns format passed as parameter if non zero. Supported
883   format are 'arabic' or '1', 'loweralpha' or 'a', 'lowerroman' or 'i', 
884   'upperlapha' or 'A', 'upperroman' or 'I', 'arabicindic' or '&#x661;'.
885   If its not one of these then 
886   returns the default format.</para>
887 </refdescription>
888 </doc:template>
889
890 <!-- ============================================================ -->
891
892 </xsl:stylesheet>