Monday, December 12, 2011

Table of Contents with subsections using XSL-FO

In Table of Contents using XSL-FO I explored creating table of contents using XSL-FO.

I needed to list subsections too. The way to do it is to loop through all subsections after you have listed the section in the TOC:
<xsl:for-each select="section">
<!-- Code to show TOC line for section -->
<xsl:for-each select="subsection">
<fo:block margin-left="10pt" text-align-last="justify">
<xsl:value-of select="subsectionheader" />
<fo:leader leader-pattern="dots" leader-length.maximum="100%" />
<fo:page-number-citation ref-id="{@subsectionid}" />
</fo:block>
</xsl:for-each>
</xsl:for-each>


Notice the margin-left="10pt" which makes an indent to show that the line represents a subsection.

Also remember to add a block in the subsection with id corresponding to the @subsectionid attribute which XSL 'retrieves' from XML above.
<fo:block id="{@subsectionid}" />

If you have subsections that you dont want to list you can wrap the block above in the following code:
<xsl:if test="@subsectionid">
...
</xsl:if>

which checks if the 'subsection' attribute has been set in XML, if not, it enter a line in the TOC.


See http://wiki.apache.org/xmlgraphics-fop/IndentInheritance
http://www.mail-archive.com/fop-users@xmlgraphics.apache.org/msg00300.html
http://www.herikstad.net/2011/12/table-of-contents-using-xsl-fo.html

No comments:

Post a Comment