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"
7 <!-- ********************************************************************
8 $Id: labels.xsl 9706 2013-01-16 18:56:16Z bobstayton $
9 ********************************************************************
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.
15 ******************************************************************** -->
17 <!-- ==================================================================== -->
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
27 <para>Trailing punctuation is not added to the label.
32 <xsl:template match="*" mode="intralabel.punctuation">
33 <xsl:text>.</xsl:text>
36 <xsl:template match="*" mode="label.markup">
37 <xsl:param name="verbose" select="1"/>
38 <xsl:if test="$verbose">
40 <xsl:text>Request for label of unexpected element: </xsl:text>
41 <xsl:value-of select="local-name(.)"/>
46 <xsl:template match="set|book" mode="label.markup">
47 <xsl:if test="@label">
48 <xsl:value-of select="@label"/>
52 <xsl:template match="part" mode="label.markup">
54 <xsl:when test="@label">
55 <xsl:value-of select="@label"/>
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"/>
63 <xsl:number from="book" count="part" format="{$format}"/>
68 <xsl:template match="partintro" mode="label.markup">
72 <xsl:template match="preface" mode="label.markup">
74 <xsl:when test="@label">
75 <xsl:value-of select="@label"/>
77 <xsl:when test="string($preface.autolabel) != 0">
78 <xsl:if test="$component.label.includes.part.label != 0 and
80 <xsl:variable name="part.label">
81 <xsl:apply-templates select="ancestor::part"
84 <xsl:if test="$part.label != ''">
85 <xsl:value-of select="$part.label"/>
86 <xsl:apply-templates select="ancestor::part"
87 mode="intralabel.punctuation"/>
90 <xsl:variable name="format">
91 <xsl:call-template name="autolabel.format">
92 <xsl:with-param name="format" select="$preface.autolabel"/>
96 <xsl:when test="$label.from.part != 0 and ancestor::part">
97 <xsl:number from="part" count="preface" format="{$format}" level="any"/>
100 <xsl:number from="book" count="preface" format="{$format}" level="any"/>
107 <xsl:template match="chapter" mode="label.markup">
109 <xsl:when test="@label">
110 <xsl:value-of select="@label"/>
112 <xsl:when test="string($chapter.autolabel) != 0">
113 <xsl:if test="$component.label.includes.part.label != 0 and
115 <xsl:variable name="part.label">
116 <xsl:apply-templates select="ancestor::part"
117 mode="label.markup"/>
119 <xsl:if test="$part.label != ''">
120 <xsl:value-of select="$part.label"/>
121 <xsl:apply-templates select="ancestor::part"
122 mode="intralabel.punctuation"/>
125 <xsl:variable name="format">
126 <xsl:call-template name="autolabel.format">
127 <xsl:with-param name="format" select="$chapter.autolabel"/>
131 <xsl:when test="$label.from.part != 0 and ancestor::part">
132 <xsl:number from="part" count="chapter" format="{$format}" level="any"/>
135 <xsl:number from="book" count="chapter" format="{$format}" level="any"/>
142 <xsl:template match="appendix" mode="label.markup">
144 <xsl:when test="@label">
145 <xsl:value-of select="@label"/>
147 <xsl:when test="string($appendix.autolabel) != 0">
148 <xsl:if test="$component.label.includes.part.label != 0 and
150 <xsl:variable name="part.label">
151 <xsl:apply-templates select="ancestor::part"
152 mode="label.markup"/>
154 <xsl:if test="$part.label != ''">
155 <xsl:value-of select="$part.label"/>
156 <xsl:apply-templates select="ancestor::part"
157 mode="intralabel.punctuation"/>
160 <xsl:variable name="format">
161 <xsl:call-template name="autolabel.format">
162 <xsl:with-param name="format" select="$appendix.autolabel"/>
166 <xsl:when test="$label.from.part != 0 and ancestor::part">
167 <xsl:number from="part" count="appendix" format="{$format}" level="any"/>
170 <xsl:number from="book|article"
171 count="appendix" format="{$format}" level="any"/>
178 <xsl:template match="article" mode="label.markup">
179 <xsl:if test="@label">
180 <xsl:value-of select="@label"/>
184 <xsl:template match="dedication|colophon" mode="label.markup">
185 <xsl:if test="@label">
186 <xsl:value-of select="@label"/>
190 <xsl:template match="reference" mode="label.markup">
192 <xsl:when test="@label">
193 <xsl:value-of select="@label"/>
195 <xsl:when test="string($reference.autolabel) != 0">
196 <xsl:if test="$component.label.includes.part.label != 0 and
198 <xsl:variable name="part.label">
199 <xsl:apply-templates select="ancestor::part"
200 mode="label.markup"/>
202 <xsl:if test="$part.label != ''">
203 <xsl:value-of select="$part.label"/>
204 <xsl:apply-templates select="ancestor::part"
205 mode="intralabel.punctuation"/>
208 <xsl:variable name="format">
209 <xsl:call-template name="autolabel.format">
210 <xsl:with-param name="format" select="$reference.autolabel"/>
214 <xsl:when test="$label.from.part != 0 and ancestor::part">
215 <xsl:number from="part" count="reference" format="{$format}" level="any"/>
218 <xsl:number from="book" count="reference" format="{$format}" level="any"/>
225 <xsl:template match="refentry" mode="label.markup">
226 <xsl:if test="@label">
227 <xsl:value-of select="@label"/>
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=".."/>
239 <xsl:if test="$parent.section.label != '0'">
240 <xsl:apply-templates select=".." mode="label.markup"/>
241 <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
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=".."/>
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="."/>
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"/>
264 <xsl:if test="$parent.label != ''">
265 <xsl:apply-templates select=".." mode="label.markup"/>
266 <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
272 test: <xsl:value-of select="$label"/>, <xsl:number count="section"/>
277 <xsl:when test="@label">
278 <xsl:value-of select="@label"/>
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"/>
286 <xsl:number format="{$format}" count="section"/>
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=".."/>
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"/>
305 <xsl:if test="$parent.label != ''">
306 <xsl:apply-templates select=".." mode="label.markup"/>
307 <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
313 <xsl:variable name="is.numbered">
314 <xsl:call-template name="label.this.section"/>
318 <xsl:when test="@label">
319 <xsl:value-of select="@label"/>
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"/>
327 <xsl:copy-of select="$component.label"/>
328 <xsl:number format="{$format}" count="sect1"/>
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=".."/>
340 <xsl:if test="$parent.section.label != '0'">
341 <xsl:apply-templates select=".." mode="label.markup"/>
342 <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
345 <xsl:variable name="is.numbered">
346 <xsl:call-template name="label.this.section"/>
350 <xsl:when test="@label">
351 <xsl:value-of select="@label"/>
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"/>
360 <xsl:when test="local-name(.) = 'sect2'">
361 <xsl:number format="{$format}" count="sect2"/>
363 <xsl:when test="local-name(.) = 'sect3'">
364 <xsl:number format="{$format}" count="sect3"/>
366 <xsl:when test="local-name(.) = 'sect4'">
367 <xsl:number format="{$format}" count="sect4"/>
369 <xsl:when test="local-name(.) = 'sect5'">
370 <xsl:number format="{$format}" count="sect5"/>
373 <xsl:message>label.markup: this can't happen!</xsl:message>
380 <xsl:template match="bridgehead" mode="label.markup">
381 <!-- bridgeheads are not normally numbered -->
384 <xsl:template match="refsect1" mode="label.markup">
386 <xsl:when test="@label">
387 <xsl:value-of select="@label"/>
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"/>
395 <xsl:number count="refsect1" format="{$format}"/>
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"/>
405 <xsl:if test="$parent.label != ''">
406 <xsl:apply-templates select=".." mode="label.markup"/>
407 <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
411 <xsl:when test="@label">
412 <xsl:value-of select="@label"/>
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"/>
421 <xsl:when test="local-name(.) = 'refsect2'">
422 <xsl:number count="refsect2" format="{$format}"/>
425 <xsl:number count="refsect3" format="{$format}"/>
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"/>
443 <xsl:if test="$parent.section.label != ''">
444 <xsl:apply-templates select=".." mode="label.markup"/>
445 <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
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=".."/>
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="."/>
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"/>
468 <xsl:if test="$parent.label != ''">
469 <xsl:apply-templates select=".." mode="label.markup"/>
470 <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
475 <xsl:when test="@label">
476 <xsl:value-of select="@label"/>
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"/>
484 <xsl:number format="{$format}" count="simplesect"/>
489 <xsl:template match="topic" mode="label.markup">
490 <!-- topics are not numbered by default -->
493 <xsl:template match="qandadiv" mode="label.markup">
494 <xsl:variable name="lparent" select="(ancestor::set
500 |ancestor::simplesect
508 |ancestor::refsect3)[last()]"/>
510 <xsl:variable name="lparent.prefix">
511 <xsl:apply-templates select="$lparent" mode="label.markup"/>
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"/>
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"/>
530 <xsl:value-of select="$prefix"/>
531 <xsl:number level="multiple" count="qandadiv" format="{$format}"/>
536 <xsl:template match="question|answer" mode="label.markup">
537 <xsl:variable name="lparent" select="(ancestor::set
543 |ancestor::simplesect
551 |ancestor::refsect3)[last()]"/>
553 <xsl:variable name="lparent.prefix">
554 <xsl:apply-templates select="$lparent" mode="label.markup"/>
557 <xsl:variable name="prefix">
558 <xsl:if test="$qanda.inherit.numeration != 0">
560 <xsl:when test="ancestor::qandadiv">
561 <xsl:variable name="div.label">
562 <xsl:apply-templates select="ancestor::qandadiv[1]" mode="label.markup"/>
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"/>
570 <xsl:when test="$lparent.prefix != ''">
571 <xsl:apply-templates select="$lparent" mode="label.markup"/>
572 <xsl:apply-templates select="$lparent" mode="intralabel.punctuation"/>
578 <xsl:variable name="inhlabel"
579 select="ancestor-or-self::qandaset/@defaultlabel[1]"/>
581 <xsl:variable name="deflabel">
583 <xsl:when test="$inhlabel != ''">
584 <xsl:value-of select="$inhlabel"/>
587 <xsl:value-of select="$qanda.defaultlabel"/>
592 <xsl:variable name="label" select="label"/>
595 <xsl:when test="count($label)>0">
596 <xsl:apply-templates select="$label"/>
599 <xsl:when test="$deflabel = 'qanda' and self::question">
600 <xsl:call-template name="gentext">
601 <xsl:with-param name="key" select="'Question'"/>
605 <xsl:when test="$deflabel = 'qanda' and self::answer">
606 <xsl:call-template name="gentext">
607 <xsl:with-param name="key" select="'Answer'"/>
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'"/>
616 <xsl:text> </xsl:text>
617 <xsl:value-of select="$prefix"/>
618 <xsl:number level="multiple" count="qandaentry" format="1"/>
621 <xsl:when test="$deflabel = 'qnumberanda' and self::answer">
622 <xsl:call-template name="gentext">
623 <xsl:with-param name="key" select="'Answer'"/>
627 <xsl:when test="$deflabel = 'number' and self::question">
628 <xsl:value-of select="$prefix"/>
629 <xsl:number level="multiple" count="qandaentry" format="1"/>
634 <xsl:template match="bibliography|glossary|
635 qandaset|index|setindex" mode="label.markup">
636 <xsl:if test="@label">
637 <xsl:value-of select="@label"/>
641 <xsl:template match="figure|table|example" mode="label.markup">
642 <xsl:variable name="pchap"
643 select="(ancestor::chapter
645 |ancestor::article[ancestor::book])[last()]"/>
647 <xsl:variable name="prefix">
648 <xsl:if test="count($pchap) > 0">
649 <xsl:apply-templates select="$pchap" mode="label.markup"/>
654 <xsl:when test="@label">
655 <xsl:value-of select="@label"/>
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"/>
665 <xsl:number format="1" from="book|article" level="any"/>
672 <xsl:template match="procedure" mode="label.markup">
673 <xsl:variable name="pchap"
674 select="ancestor::chapter
676 |ancestor::article[ancestor::book]"/>
678 <xsl:variable name="prefix">
679 <xsl:if test="count($pchap) > 0">
680 <xsl:apply-templates select="$pchap" mode="label.markup"/>
685 <xsl:when test="@label">
686 <xsl:value-of select="@label"/>
688 <xsl:when test="$formal.procedures = 0">
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"/>
698 <xsl:number count="procedure[title]" format="1"
699 from="chapter|appendix" level="any"/>
702 <xsl:number count="procedure[title]" format="1"
703 from="book|article" level="any"/>
710 <xsl:template match="equation" mode="label.markup">
711 <xsl:variable name="pchap"
712 select="ancestor::chapter
714 |ancestor::article[ancestor::book]"/>
716 <xsl:variable name="prefix">
717 <xsl:if test="count($pchap) > 0">
718 <xsl:apply-templates select="$pchap" mode="label.markup"/>
723 <xsl:when test="@label">
724 <xsl:value-of select="@label"/>
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"/>
733 <xsl:number format="1" count="equation"
734 from="chapter|appendix" level="any"/>
737 <xsl:number format="1" count="equation"
738 from="book|article" level="any"/>
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"/>
752 <xsl:variable name="type">
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 -->
762 <xsl:text>Unexpected numeration: </xsl:text>
763 <xsl:value-of select="$numeration"/>
765 <xsl:value-of select="1."/>
770 <xsl:variable name="item-number">
771 <xsl:call-template name="orderedlist-item-number"/>
774 <xsl:number value="$item-number" format="{$type}"/>
777 <xsl:template match="production" mode="label.markup">
778 <xsl:number count="production" level="any"/>
781 <xsl:template match="abstract" mode="label.markup">
785 <xsl:template match="sidebar" mode="label.markup">
789 <xsl:template match="glossdiv|glosslist" mode="label.markup">
793 <xsl:template match="glossentry" mode="label.markup">
797 <!-- ============================================================ -->
799 <xsl:template name="label.this.section">
800 <xsl:param name="section" select="."/>
802 <xsl:variable name="level">
803 <xsl:call-template name="section.level"/>
807 <!-- bridgeheads are not numbered -->
808 <xsl:when test="$section/self::bridgehead">0</xsl:when>
809 <xsl:when test="$level <= $section.autolabel.max.depth">
810 <xsl:value-of select="$section.autolabel"/>
812 <xsl:otherwise>0</xsl:otherwise>
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>
829 <!-- ============================================================ -->
831 <xsl:template name="default.autolabel.format">
832 <xsl:param name="context" select="."/>
834 <xsl:when test="local-name($context) = 'appendix'">
835 <xsl:value-of select="'A'"/>
837 <xsl:when test="local-name($context) = 'part'">
838 <xsl:value-of select="'I'"/>
840 <xsl:otherwise>1</xsl:otherwise>
844 <xsl:template name="autolabel.format">
845 <xsl:param name="context" select="."/>
846 <xsl:param name="format"/>
849 <xsl:when test="string($format) != 0">
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'"/>
855 <xsl:when test="$format='lowerroman' or $format='i'">
856 <xsl:value-of select="'i'"/>
858 <xsl:when test="$format='upperalpha' or $format='A'">
859 <xsl:value-of select="'A'"/>
861 <xsl:when test="$format='upperroman' or $format='I'">
862 <xsl:value-of select="'I'"/>
864 <xsl:when test="$format='arabicindic' or $format='١'">
865 <xsl:value-of select="'١'"/>
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>
872 <xsl:call-template name="default.autolabel.format"/>
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 '١'.
885 If its not one of these then
886 returns the default format.</para>
890 <!-- ============================================================ -->