XSL-FO creating Dynamic Table Of Contents

2019-03-02 09:09发布

问题:

How we can create a table of contents page dynamically in XSL-FO?

回答1:

What I've done in the past is use <fo:page-number-citation> for each entry in the TOC (Table of Contents). I do the table of contents as an <fo:table>.

The <fo:page-number-citation> has a ref-id attribute that should contain the id of the location you're referencing. It should generate the PDF page number where that id is located.

For example, if you wanted each <chapter> referenced in your TOC, you would use <fo:page-number-citation> with a ref-id that matched the id of where the <chapter> was output (like fo:page-sequence, fo:block, etc.).

Here's an example. I'm basing the id attribute on the value of an existing attribute, but you can generate an id if you need to.

Example Chapter XML:

<chapter foo="CHAP-1">
...
</chapter>

Example entry in the TOC table:

<fo:table-cell>
  <fo:block>
    <fo:page-number-citation ref-id="CHAP-1"/>
  </fo:block>
</fo:table-cell>

Example of outputting the <chapter>:

<fo:page-sequence id="CHAP-1">
...
</fo:page-sequence>

You can also wrap the fo:page-number-citation in an fo:basic-link to link the page number in the TOC to the actual page.