When creating a template sometimes a table of content is needed. Below you can find the steps to follow in order to insert it in your document:
A. Create the Page Set for the Table of Contents
B. Create the Page Set for the actual content
The next step is to add another Page Set for the actual content of the template, and this will be a Repeating Page Set (instructions here). In the attached sample, we iterated through all "cd" nodes.
C. Add the Main Template Content
Once your template structure is finished, add the desired information for each "cd" node, for example, the title of the CD:
- Go to the Main Content Page Set
- Insert a field containing the position() function to display the position of the elements;
- Insert a field containing the "@title" elements that were added in the TOC;
- Add an id to mark each future TOC topic on the first block of the page set:
- Go to XML View
- On the <fo:block> element containing the two fields inserted above, add the fo:id="topicTitle" so you can link them with the items inserted in the Table of Contents.
D. Start working on the table of content section
After adding the desired content, your next step is to define the table of content, which is created in two steps:
- Adding the topics in the TOC;
- Adding the references that point to the correct items in the template.
D.1. How to add the topics of the TOC
- Go to the first page of your document (select the TOC Page Set);
- Go to "Insert" tab in the ribbon and select "Repeating Paragraph";
- Select "Create Expression" and insert the node you want to repeat by and then choose "Insert";
- In the newly created repeating paragraph, insert a table without borders, containing one row and two columns.
D.2. Add correct TOC references
In order to link the items of your Table Of contents with the pages they point to, you will need to follow these steps:
- Switch to "XML View" (View tab -> XML View)
- After the <xf:stylesheet/> element add the following lines of code:
<xfd:attribute-value object="basicLink" attribute="fo:internal-destination" dynamic-value="generate-id(@title)" />
<xfd:attribute-value object="indexCitation" attribute="fo:ref-index-key" dynamic-value="generate-id(@title)" />
<xfd:attribute-value object="topicTitle" attribute="fo:id" dynamic-value="generate-id(@title)" />
<xfd:attribute-value object="topicTitle" attribute="fo:index-key" dynamic-value="generate-id(@title)" />
- Object defines the name of the element to be changed
- Attribute is the name of the property that will be modified
- Dynamic-value is the new value assigned to each object.
Note: The generate-id function is used to generate an ID for each particular element. We used the “@title” attribute because in our case we want to generate a Table of Contents for the “@title” elements of our <CD> nodes;
With regards to the elements modified, the following information should be known:
- fo:internal-destination - Specifies the destination flow object within the formatting object tree.
- fo:ref-index-key - Specifies the "index-key" of objects in the formatting object tree.
- fo:id - An identifier unique within all objects in the result tree with the fo: namespace. It allows references to this formatting object by other objects.
- fo:index-key - Associates an index key with the formatting object on which it is specified.
D.3. Design the Table of Contents
- Add the numbering of the TOC elements
- Go back to Design View
- Click on the area of the first column of the table
- Go to Insert tab in the Ribbon and select the Field button
- Insert the following expression in order to give numbering to your elements:
- The expression above will concatenate the position of the element in the list with a “.”, to obtain the numbering in the following format: 1. , 2., 3. etc.
- Add the topics of the Table of Contents
- Click on the area of the second column of the table
- Go to the View tab in the Ribbon and select “XML View” (use Auto Format feature if needed);
- Inside the <fo:block> element insert a basic link element that contains two elements: an fo:leader , and an fo:index-page-citation-list.
The content of the <fo:basic-link> element should look like this: <fo:basic-link id="basicLink">
<fo:leader leader-pattern="dots" leader-length="50%"/>
<fo:index-key-reference font-weight="bold" id="indexCitation"/>
- The <fo:basic-link> element is necessary to create a link. it should reference the before created attribute id="basicLink";
- The <fo:leader> represents the line between the topic subject and the topic page.
Attributes: leader-pattern="dots" , leader-length="50%" ;
- The <fo:index-page-citation-list> element creates the numbering. It requires a child <fo:index-key-reference> element to reference the index keys of each element. On the <fo:index-key-reference> add the id="indexCitation"
- Go back to Design View, and add the dynamic field that represents the topic subject:
- Click in the second column of the table
- Press "Home" to move the caret before the dotted area
- Add the field containing the elements you wanted to be contained in the TOC. In our case, it is "@title".
- Click on the area of the second column of the table
Optional Style Adjustments:
- Remove additional space at the end of the repeating paragraph:
- Click at the end of the table:
- Press "Delete"
- Add formatting.
Attached you will find two samples, one containing the behavior described above and one modified to display the TOC in a justified manner.