Tables of contents

Here, tables of contents with numbering of the entries are generated and reference is made to the page numbers of the content.

This example demonstrates how a table of contents is generated from the contents. The table of contents contains the automatic numbering of the entries of the table of contents and the referencing to the page numbers of the content. The following stylesheet is based upon the premise that an empty table of contents element is defined in the DTD. The example refers to the simple requirement of displaying only entries of one level in the table of contents.

Excerpt from the DTD:

<!ELEMENT Work (Title, ..., Content, Section+)>       ❶ 
<!ELEMENT Content EMPTY>                              ❷ 
...
<!ELEMENT Section (Title, ...)>                       ❸ 

❶ The <Content> element indicates the place which the table of contents shall occupy in the element sequence.

❷ The <Content> tag is only a placeholder in the XML instance without any substructure. The table of contents is completely specified in the stylesheet.

❸ For the generation of the table of contents it is important that each section has a title. What else should be entered in the table of contents for this section?

Stylesheet:

...
<xsl:template match="Section">
   <fo:block id="{generate-id()}">
      <xsl:apply-templates/>                                                            ❶
   </fo:block>
</xsl:template>
...
<xsl:template match="Content">                                                          ❷
   <fo:block font-size="18pt" font-weight="bold" text-align="center" space-after="6mm"> ❸
    Table of contents
   </fo:block>
   <xsl:for-each select="Section">
      <xsl:call-template name="irc.line"/>                                              ❹
   </xsl:for-each>                                                                      ❺
</xsl:template>
...
<xsl:template name="irc.line">                                                          ❺
   <fo:block text-align-last="justify">                                                 ❻
      <fo:table width="160mm">                                                          ❼
         <fo:table-column column-number="1" column-width="10mm"/>                       ❼ 
         <fo:table-column column-number="2" column-width="150mm"/>                      ❼ 
         <fo:table-body>
            <fo:table-row> 
               <fo:table-cell column-number="1">
                  <fo:block>
                     <xsl:number level="single" count="Section" format="1."/>           ❽ 
                  </fo:block>
               </fo:table-cell>
               <fo:table-cell column-number="2">
                  <fo:block> 
                     <xsl:value-of select="Title"/>                                     ❾ 
                     <fo:inline>
                        <fo:leader leader-pattern="dots"/>                              ❾
                     </fo:inline>
                     <fo:page-number-citation ref-id="{generate-id()}"/>                ❾
                  </fo:block>
               </fo:table-cell>
            </fo:table-row>
         </fo:table-body>
      </fo:table>
   </fo:block>
</xsl:template>

❶ In the XSLT process an unique identification is generated for each section. The formatter accesses these identifiers if it makes reference to the sections with the <fo:page-number-citation> element in order to copy the page numbers for the beginning of the sections into the table of contents.

❷ This template specifies the generation of the table of contents. If the DTD does not contain a placeholder element for the table of contents, the content of this template (not the template itself!) would be displayed in a page sequence (<fo:page-sequence>) and within this page sequence in <fo:flow>.

❸ This is the block for the headline of the table of contents including the headline formulation.

❹ For each section shall apply what is specified in <xsl:for-each>.

❺ In place of the specification for each entry of the table of contents, a named template (<xsl:call-template>) with the name irc.line is called up. The contents of this template can also be set instead of the template call. The separated template specification shall only create a better overview in the stylesheet since this specification can be very extensive and therefore, the focus on larger contexts can be made more difficult when treated as an integrated part.

❻ The block which encloses an entry of the table of contents with the page number shall set justify in the last line of the entry, which means the page number shall be set to the right print space margin. The text-align-last attribute is passed on the substructures.

❼ For each entry of the table of contents a single-line table is generated which includes the data of the entry. The table has two columns, one column for the numbering of the entries (sections), a second for the headlines of the sections. The numberings are cropped and the headlines are also uniformly left-aligned in case of surrounding lines.

❽ The <xsl:number> element generates the numbering of the section headlines.

❾ In the second column the following is inserted within a block: At first, the content of <Title> of the given section (without any substructures!). In the last line of this block, between the end of the headline text and the page number, a dotted line is generated by the <fo:leader> element. At the right margin the page number appears, which means the number of the page in which the block with the corresponding generated identifier begins. The formatter resolves the reference of the id-ref-id relationship by inserting the page number.

 

<< back next >>

Copyright © dpunkt.verlag GmbH 2004
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "XSL-FO in der Praxis" ("XSL-FO in practice") is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.

dpunkt.verlag GmbH, Ringstraße 19B, 69115 Heidelberg, fon 06221-14830, fax 06221-148399, hallo@dpunkt.de