Difficulty: ★★☆ (medium)
Keywords: TOC, table of contents

Problem

You have a book and want the table of contents appear after the main content.

Solution

The processing of a book is handled in the xhtml/division.xsl file. The template needs to be copied and inserted in your customization layer. To move your table of contents, do the following:

  1. Create a customization layer as shown in Section 2.3, “Writing Customization Layers”.

  2. Create a file move-toc.xsl with the following content:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet  version="1.0"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:d="http://docbook.org/ns/docbook"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      exclude-result-prefixes="d">
      
      <xsl:template match="d:book">
        <xsl:call-template name="id.warning"/>
        <div>
          <xsl:apply-templates select="." mode="common.html.attributes"/>
          <xsl:if test="$generate.id.attributes != 0">
            <xsl:attribute name="id">
              <xsl:call-template name="object.id"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:call-template name="book.titlepage"/>
          
          <xsl:apply-templates select="d:dedication" 
            mode="dedication"/>
          <xsl:apply-templates select="d:acknowledgements"
            mode="acknowledgements"/>
          
          <xsl:variable name="toc.params">
            <xsl:call-template name="find.path.params">
              <xsl:with-param name="table" 
                select="normalize-space($generate.toc)"/> 
            </xsl:call-template>
          </xsl:variable>
          
          <xsl:apply-templates/>
    
          <xsl:call-template name="make.lots">
            <xsl:with-param name="toc.params" select="$toc.params"/>
            <xsl:with-param name="toc">
              <xsl:call-template name="division.toc">
                <xsl:with-param name="toc.title.p"
                  select="contains($toc.params, 'title')"/>
              </xsl:call-template>
            </xsl:with-param>
          </xsl:call-template>
        </div>
      </xsl:template>
      
    </xsl:stylesheet>

    1

    The xsl:apply-templates creates first all the content

    2

    Create the table of content

  3. Include move-toc.xsl into your customization layer from Step 1:

    <xsl:include href="move-toc.xsl"/>
  4. Build your document with your customization layer.

Discussion

The same principle applies to other structural elements, although the handling is a bit different.


Project@GitHubIssue#10