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 8540 2009-12-02 05:28:09Z 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 <!-- FIXME: could we do a better job here? -->
382 <xsl:variable name="contsec"
383 select="(ancestor::section
384 |ancestor::simplesect
395 |ancestor::preface)[last()]"/>
397 <xsl:apply-templates select="$contsec" mode="label.markup"/>
400 <xsl:template match="refsect1" mode="label.markup">
402 <xsl:when test="@label">
403 <xsl:value-of select="@label"/>
405 <xsl:when test="$section.autolabel != 0">
406 <xsl:variable name="format">
407 <xsl:call-template name="autolabel.format">
408 <xsl:with-param name="format" select="$section.autolabel"/>
411 <xsl:number count="refsect1" format="{$format}"/>
416 <xsl:template match="refsect2|refsect3" mode="label.markup">
417 <!-- label the parent -->
418 <xsl:variable name="parent.label">
419 <xsl:apply-templates select=".." mode="label.markup"/>
421 <xsl:if test="$parent.label != ''">
422 <xsl:apply-templates select=".." mode="label.markup"/>
423 <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
427 <xsl:when test="@label">
428 <xsl:value-of select="@label"/>
430 <xsl:when test="$section.autolabel != 0">
431 <xsl:variable name="format">
432 <xsl:call-template name="autolabel.format">
433 <xsl:with-param name="format" select="$section.autolabel"/>
437 <xsl:when test="local-name(.) = 'refsect2'">
438 <xsl:number count="refsect2" format="{$format}"/>
441 <xsl:number count="refsect3" format="{$format}"/>
448 <xsl:template match="simplesect" mode="label.markup">
449 <!-- if this is a nested section, label the parent -->
450 <xsl:if test="local-name(..) = 'section'
451 or local-name(..) = 'sect1'
452 or local-name(..) = 'sect2'
453 or local-name(..) = 'sect3'
454 or local-name(..) = 'sect4'
455 or local-name(..) = 'sect5'">
456 <xsl:variable name="parent.section.label">
457 <xsl:apply-templates select=".." mode="label.markup"/>
459 <xsl:if test="$parent.section.label != ''">
460 <xsl:apply-templates select=".." mode="label.markup"/>
461 <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
465 <!-- if the parent is a component, maybe label that too -->
466 <xsl:variable name="parent.is.component">
467 <xsl:call-template name="is.component">
468 <xsl:with-param name="node" select=".."/>
472 <!-- does this section get labelled? -->
473 <xsl:variable name="label">
474 <xsl:call-template name="label.this.section">
475 <xsl:with-param name="section" select="."/>
479 <xsl:if test="$section.label.includes.component.label != 0
480 and $parent.is.component != 0">
481 <xsl:variable name="parent.label">
482 <xsl:apply-templates select=".." mode="label.markup"/>
484 <xsl:if test="$parent.label != ''">
485 <xsl:apply-templates select=".." mode="label.markup"/>
486 <xsl:apply-templates select=".." mode="intralabel.punctuation"/>
491 <xsl:when test="@label">
492 <xsl:value-of select="@label"/>
494 <xsl:when test="$label != 0">
495 <xsl:variable name="format">
496 <xsl:call-template name="autolabel.format">
497 <xsl:with-param name="format" select="$section.autolabel"/>
500 <xsl:number format="{$format}" count="simplesect"/>
505 <xsl:template match="qandadiv" mode="label.markup">
506 <xsl:variable name="lparent" select="(ancestor::set
512 |ancestor::simplesect
520 |ancestor::refsect3)[last()]"/>
522 <xsl:variable name="lparent.prefix">
523 <xsl:apply-templates select="$lparent" mode="label.markup"/>
526 <xsl:variable name="prefix">
527 <xsl:if test="$qanda.inherit.numeration != 0">
528 <xsl:if test="$lparent.prefix != ''">
529 <xsl:apply-templates select="$lparent" mode="label.markup"/>
530 <xsl:apply-templates select="$lparent" mode="intralabel.punctuation"/>
536 <xsl:when test="$qandadiv.autolabel != 0">
537 <xsl:variable name="format">
538 <xsl:call-template name="autolabel.format">
539 <xsl:with-param name="format" select="$qandadiv.autolabel"/>
542 <xsl:value-of select="$prefix"/>
543 <xsl:number level="multiple" count="qandadiv" format="{$format}"/>
548 <xsl:template match="question|answer" mode="label.markup">
549 <xsl:variable name="lparent" select="(ancestor::set
555 |ancestor::simplesect
563 |ancestor::refsect3)[last()]"/>
565 <xsl:variable name="lparent.prefix">
566 <xsl:apply-templates select="$lparent" mode="label.markup"/>
569 <xsl:variable name="prefix">
570 <xsl:if test="$qanda.inherit.numeration != 0">
572 <xsl:when test="ancestor::qandadiv">
573 <xsl:variable name="div.label">
574 <xsl:apply-templates select="ancestor::qandadiv[1]" mode="label.markup"/>
576 <xsl:if test="string-length($div.label) != 0">
577 <xsl:copy-of select="$div.label"/>
578 <xsl:apply-templates select="ancestor::qandadiv[1]"
579 mode="intralabel.punctuation"/>
582 <xsl:when test="$lparent.prefix != ''">
583 <xsl:apply-templates select="$lparent" mode="label.markup"/>
584 <xsl:apply-templates select="$lparent" mode="intralabel.punctuation"/>
590 <xsl:variable name="inhlabel"
591 select="ancestor-or-self::qandaset/@defaultlabel[1]"/>
593 <xsl:variable name="deflabel">
595 <xsl:when test="$inhlabel != ''">
596 <xsl:value-of select="$inhlabel"/>
599 <xsl:value-of select="$qanda.defaultlabel"/>
604 <xsl:variable name="label" select="label"/>
607 <xsl:when test="count($label)>0">
608 <xsl:apply-templates select="$label"/>
611 <xsl:when test="$deflabel = 'qanda' and self::question">
612 <xsl:call-template name="gentext">
613 <xsl:with-param name="key" select="'Question'"/>
617 <xsl:when test="$deflabel = 'qanda' and self::answer">
618 <xsl:call-template name="gentext">
619 <xsl:with-param name="key" select="'Answer'"/>
623 <xsl:when test="($deflabel = 'qnumber' or
624 $deflabel = 'qnumberanda') and self::question">
625 <xsl:call-template name="gentext">
626 <xsl:with-param name="key" select="'Question'"/>
628 <xsl:text> </xsl:text>
629 <xsl:value-of select="$prefix"/>
630 <xsl:number level="multiple" count="qandaentry" format="1"/>
633 <xsl:when test="$deflabel = 'qnumberanda' and self::answer">
634 <xsl:call-template name="gentext">
635 <xsl:with-param name="key" select="'Answer'"/>
639 <xsl:when test="$deflabel = 'number' and self::question">
640 <xsl:value-of select="$prefix"/>
641 <xsl:number level="multiple" count="qandaentry" format="1"/>
646 <xsl:template match="bibliography|glossary|
647 qandaset|index|setindex" mode="label.markup">
648 <xsl:if test="@label">
649 <xsl:value-of select="@label"/>
653 <xsl:template match="figure|table|example" mode="label.markup">
654 <xsl:variable name="pchap"
655 select="(ancestor::chapter
657 |ancestor::article[ancestor::book])[last()]"/>
659 <xsl:variable name="prefix">
660 <xsl:if test="count($pchap) > 0">
661 <xsl:apply-templates select="$pchap" mode="label.markup"/>
666 <xsl:when test="@label">
667 <xsl:value-of select="@label"/>
671 <xsl:when test="$prefix != ''">
672 <xsl:apply-templates select="$pchap" mode="label.markup"/>
673 <xsl:apply-templates select="$pchap" mode="intralabel.punctuation"/>
674 <xsl:number format="1" from="chapter|appendix" level="any"/>
677 <xsl:number format="1" from="book|article" level="any"/>
684 <xsl:template match="procedure" mode="label.markup">
685 <xsl:variable name="pchap"
686 select="ancestor::chapter
688 |ancestor::article[ancestor::book]"/>
690 <xsl:variable name="prefix">
691 <xsl:if test="count($pchap) > 0">
692 <xsl:apply-templates select="$pchap" mode="label.markup"/>
697 <xsl:when test="@label">
698 <xsl:value-of select="@label"/>
700 <xsl:when test="$formal.procedures = 0">
705 <xsl:when test="count($pchap)>0">
706 <xsl:if test="$prefix != ''">
707 <xsl:apply-templates select="$pchap" mode="label.markup"/>
708 <xsl:apply-templates select="$pchap" mode="intralabel.punctuation"/>
710 <xsl:number count="procedure[title]" format="1"
711 from="chapter|appendix" level="any"/>
714 <xsl:number count="procedure[title]" format="1"
715 from="book|article" level="any"/>
722 <xsl:template match="equation" mode="label.markup">
723 <xsl:variable name="pchap"
724 select="ancestor::chapter
726 |ancestor::article[ancestor::book]"/>
728 <xsl:variable name="prefix">
729 <xsl:if test="count($pchap) > 0">
730 <xsl:apply-templates select="$pchap" mode="label.markup"/>
735 <xsl:when test="@label">
736 <xsl:value-of select="@label"/>
740 <xsl:when test="count($pchap)>0">
741 <xsl:if test="$prefix != ''">
742 <xsl:apply-templates select="$pchap" mode="label.markup"/>
743 <xsl:apply-templates select="$pchap" mode="intralabel.punctuation"/>
745 <xsl:number format="1" count="equation"
746 from="chapter|appendix" level="any"/>
749 <xsl:number format="1" count="equation"
750 from="book|article" level="any"/>
757 <xsl:template match="orderedlist/listitem" mode="label.markup">
758 <xsl:variable name="numeration">
759 <xsl:call-template name="list.numeration">
760 <xsl:with-param name="node" select="parent::orderedlist"/>
764 <xsl:variable name="type">
766 <xsl:when test="$numeration='arabic'">1</xsl:when>
767 <xsl:when test="$numeration='loweralpha'">a</xsl:when>
768 <xsl:when test="$numeration='lowerroman'">i</xsl:when>
769 <xsl:when test="$numeration='upperalpha'">A</xsl:when>
770 <xsl:when test="$numeration='upperroman'">I</xsl:when>
771 <!-- What!? This should never happen -->
774 <xsl:text>Unexpected numeration: </xsl:text>
775 <xsl:value-of select="$numeration"/>
777 <xsl:value-of select="1."/>
782 <xsl:variable name="item-number">
783 <xsl:call-template name="orderedlist-item-number"/>
786 <xsl:number value="$item-number" format="{$type}"/>
789 <xsl:template match="abstract" mode="label.markup">
793 <xsl:template match="sidebar" 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 <xsl:when test="$level <= $section.autolabel.max.depth">
808 <xsl:value-of select="$section.autolabel"/>
810 <xsl:otherwise>0</xsl:otherwise>
814 <doc:template name="label.this.section" xmlns="">
815 <refpurpose>Returns true if $section should be labelled</refpurpose>
816 <refdescription id="label.this.section-desc">
817 <para>Returns true if the specified section should be labelled.
818 By default, this template returns zero unless
819 the section level is less than or equal to the value of the
820 <literal>$section.autolabel.max.depth</literal> parameter, in
821 which case it returns
822 <literal>$section.autolabel</literal>.
823 Custom stylesheets may override it to get more selective behavior.</para>
827 <!-- ============================================================ -->
829 <xsl:template name="default.autolabel.format">
830 <xsl:param name="context" select="."/>
832 <xsl:when test="local-name($context) = 'appendix'">
833 <xsl:value-of select="'A'"/>
835 <xsl:when test="local-name($context) = 'part'">
836 <xsl:value-of select="'I'"/>
838 <xsl:otherwise>1</xsl:otherwise>
842 <xsl:template name="autolabel.format">
843 <xsl:param name="context" select="."/>
844 <xsl:param name="format"/>
847 <xsl:when test="string($format) != 0">
849 <xsl:when test="string($format)='arabic' or $format='1'">1</xsl:when>
850 <xsl:when test="$format='loweralpha' or $format='a'">
851 <xsl:value-of select="'a'"/>
853 <xsl:when test="$format='lowerroman' or $format='i'">
854 <xsl:value-of select="'i'"/>
856 <xsl:when test="$format='upperalpha' or $format='A'">
857 <xsl:value-of select="'A'"/>
859 <xsl:when test="$format='upperroman' or $format='I'">
860 <xsl:value-of select="'I'"/>
862 <xsl:when test="$format='arabicindic' or $format='١'">
863 <xsl:value-of select="'١'"/>
867 <xsl:text>Unexpected </xsl:text><xsl:value-of select="local-name(.)"/><xsl:text>.autolabel value: </xsl:text>
868 <xsl:value-of select="$format"/><xsl:text>; using default.</xsl:text>
870 <xsl:call-template name="default.autolabel.format"/>
877 <doc:template name="autolabel.format" xmlns="">
878 <refpurpose>Returns format for autolabel parameters</refpurpose>
879 <refdescription id="autolabel.format-desc">
880 <para>Returns format passed as parameter if non zero. Supported
881 format are 'arabic' or '1', 'loweralpha' or 'a', 'lowerroman' or 'i',
882 'upperlapha' or 'A', 'upperroman' or 'I', 'arabicindic' or '١'.
883 If its not one of these then
884 returns the default format.</para>
888 <!-- ============================================================ -->