使用 DocBook/XSLT-FO 生成每章下的章节目录

Generate a TOC of sections under each chapter with DocBook/XSLT-FO

我使用的是 FOP 2.3,我有以下内容:

<book>
  <chapter>
    <title>ChA</title>
    <section>
      <title>Structures</title>
      <xi:include href="struct1.xml"/>
    </section>
    <section>
      <title>Functions</title>
      <xi:include href="function1.xml"/>
    </section>
  </chapter>
</book>

对于function1.xml(例如),我有:

<refentry xml:id="some_func" version="5.0">
  <refmeta>
    <refentrytitle>SomeFunc</refentrytitle>
  </refmeta>
  ...
</refentry>

是否可以...

  1. 不列出 refentry 之间的部分,而是仅将它们列在章节标题下作为迷你目录?
  2. 列出每个部分下的 refentry/refentrytitle 个值?

例如,ChA 章开头的输出应如下所示:

Page X
       ChA
         Structures
           SomeStruct
         Functions
           SomeFunc

Page X+1
       SomeStruct page

Page X+2
       SomeFunc page

Page X+3
       ChB
         Structures
           SomeStruct2
         Functions
           SomeFunc2
...

下一个 new 页面按顺序呈现第 ChA 章所有部分的每个 refentry,并且不按部分分解结构函数

编辑 1

我能够防止在手册页之间打印这些部分。我复制了 section.content 模板:

<xsl:template name="section.content">
  <!-- Don't display section -->
  <!-- <xsl:call-template name="section.titlepage"/> -->
  ...
</xsl:template>

关于列出这些部分,我发现了这个:

<xsl:template match="chapter">
  ...
  <fo:page-sequence hyphenate="{$hyphenate}"
                    master-reference="{$master-reference}">
    ...
    <fo:flow flow-name="xsl-region-body">
      <xsl:call-template name="set.flow.properties">
        <xsl:with-param name="element" select="local-name(.)"/>
        <xsl:with-param name="master-reference" select="$master-reference"/>
      </xsl:call-template>

      <fo:block id="{$id}"
                xsl:use-attribute-sets="component.titlepage.properties">
        <xsl:call-template name="chapter.titlepage"/>
        <!-- === HERE IS WHERE I AM INSERTING MY CUSTOM TABLE OF CONTENTS === -->
        <xsl:for-each select="self::*/section">
          <fo:block>
            <xsl:message>DEBUG><xsl:value-of select="title"/></xsl:message>
            <xsl:value-of select="title"/>

            <xsl:for-each select="self::*/refentry/refmeta">
              <fo:block>
                <xsl:value-of select="refentrytitle"/>
              </fo:block>
            </xsl:for-each>
          </fo:block>
        </xsl:for-each>

      </fo:block>

      <xsl:call-template name="make.component.tocs"/>

      <xsl:apply-templates/>
    </fo:flow>
  </fo:page-sequence>
</xsl:template>

花了一段时间。

首先,必须在您的自定义样式文件中覆盖 chapter 模板:

<xsl:template match="chapter">
  ...
    <fo:flow flow-name="xsl-region-body">
      <xsl:call-template name="set.flow.properties">
        <xsl:with-param name="element" select="local-name(.)"/>
        <xsl:with-param name="master-reference" select="$master-reference"/>
      </xsl:call-template>

      <xsl:call-template name="chapter.titlepage"/>

      <!-- Call Table of contents name here -->
      <xsl:call-template name="table.of.contents.titlepage.recto.sony"/>

      <fo:block id="{$id}" xsl:use-attribute-sets="component.titlepage.properties">
        <xsl:for-each select="self::*/section">
          <fo:block start-indent="{count(ancestor::*) + 2}pc">
            <xsl:variable name="section.id">
              <xsl:call-template name="object.id"/>
            </xsl:variable>

            <!-- XXX: Error if "title" element doesn't exist -->
            <xsl:value-of select="title"/>
          </fo:block>

          <!-- XXX: Error if "refnamediv" doesn't exist -->
          <xsl:for-each select="self::*/refentry/refnamediv">
            <fo:block xsl:use-attribute-sets="toc.line.properties" start-indent="{count(ancestor::*) + 2}pc">
              <xsl:variable name="refname.id">
                <xsl:call-template name="object.id"/>
              </xsl:variable>

              <!-- XXX: Error if "refname" doesn't exist -->
              <fo:inline font-family="Courier" keep-with-next.within-line="always">
                <fo:basic-link internal-destination="{$refname.id}">
                  <xsl:value-of select="refname"/>
                </fo:basic-link>
              </fo:inline>

              <fo:inline keep-together.within-line="always">
                <fo:leader leader-pattern="space"
                           keep-with-next.within-line="always"/>
                <fo:basic-link internal-destination="{$id}">
                  <fo:page-number-citation ref-id="{$refname.id}"/>
                </fo:basic-link>
              </fo:inline>

            </fo:block>
          </xsl:for-each>

        </xsl:for-each>
      </fo:block>

      <xsl:call-template name="make.component.tocs"/>

      <xsl:apply-templates/>
    </fo:flow>
  </fo:page-sequence>
</xsl:template>

以上将显示每章 table 的内容。需要省略的部分:

<xsl:template name="section.content">
  <!-- Don't display section -->
  <!-- <xsl:call-template name="section.titlepage"/> -->
  ...
</xsl:template>