Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6534633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:16:26+00:00 2026-05-25T10:16:26+00:00

I have the following document structure and I want to generate a table of

  • 0

I have the following document structure and I want to generate a table of contents which would fit to my XSLT transformation. I was trying many things but none of them worked for me. Could anybody help me with this?

With the following transformation I get this WARNING and empty page number in the place.

 WARNING: Page 2: Unresolved id reference "N65898" found.

Document structure

<article>
    <chapter>
        <title>Chapter 1</title>
        <para>chapter 1 text</para>
        <sect1>
            <title>Section1 1.1</title>
            <para>text 1.1</para>
            <sect2>
                <title>Section2 1.1.1</title>
                <para>text 1.1.1</para>
            </sect2>
            <sect2>
                <title>Section2 1.1.2</title>
                <para>text 1.1.2</para>
            </sect2>
            <sect2>
                <title>Section2 1.1.3</title>
                <para>text 1.1.3</para>
            </sect2>
            <sect2>
                <title>Section2 1.1.4</title>
                <para>text 1.1.4</para>
            </sect2>
        </sect1>
        <sect1>
            <title>Section1 1.2</title>
            <sect2>
                <title>Section2 1.2.1</title>
            </sect2>
            <sect2>
                <title>Section2 1.2.2</title>
                <sect3>
                    <title>Section3 1.2.2.1</title>
                </sect3>
                <sect3>
                    <title>Section3 1.2.2.2</title>
                </sect3>
            </sect2>
        </sect1>
    </chapter>
    <chapter>
        <title>Chapter 2</title>
        <sect1>
            <title>Section1 2.1</title>
            <sect2>
                <title>Section2 2.1.1</title>
            </sect2>
            <sect2>
                <title>Section2 2.1.2</title>
                <sect3>
                    <title>Section3 2.1.2.1</title>
                </sect3>
                <sect3>
                    <title>Section3 2.1.2.2</title>
                </sect3>
            </sect2>
        </sect1>
        <sect1>
            <title>Section1 2.2</title>
            <sect2>
                <title>Section2 2.2.1</title>
            </sect2>
            <sect2>
                <title>Section2 2.2.2</title>
                <sect3>
                    <title>Section3 2.2.2.1</title>
                </sect3>
                <sect3>
                    <title>Section3 2.2.2.2</title>
                </sect3>
            </sect2>
        </sect1>
    </chapter>
    <chapter>
        <title>Chapter 3</title>
        <sect1>
            <title>Section1 3.1</title>
            <sect2>
                <title>Section2 3.1.1</title>
            </sect2>
            <sect2>
                <title>Section2 3.1.2</title>
                <sect3>
                    <title>Section3 3.1.2.1</title>
                </sect3>
                <sect3>
                    <title>Section3 3.1.2.2</title>
                </sect3>
            </sect2>
        </sect1>
        <sect1>
            <title>Section1 3.2</title>
            <sect2>
                <title>Section2 3.2.1</title>
            </sect2>
            <sect2>
                <title>Section2 3.2.2</title>
                <sect3>
                    <title>Section3 3.2.2.1</title>
                </sect3>
                <sect3>
                    <title>Section3 3.2.2.2</title>
                </sect3>
            </sect2>
        </sect1>
    </chapter>
</article>

Transformation

<!-- table of contents -->
<fo:block break-before='page'>
    <fo:block font-size="16pt" font-weight="bold">TABLE OF CONTENTS</fo:block>
    <xsl:for-each select="//chapter">
        <fo:block text-align-last="justify">
            <fo:basic-link internal-destination="{generate-id(.)}">
                <xsl:value-of select="count(preceding::chapter) + 1" />
                <xsl:text> </xsl:text>
                <xsl:value-of select="title" />
                <fo:leader leader-pattern="dots" />
                <fo:page-number-citation ref-id="{generate-id(.)}" />
            </fo:basic-link>
        </fo:block>
    </xsl:for-each>
</fo:block>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:16:27+00:00Added an answer on May 25, 2026 at 10:16 am

    This will only work if you output an ID when you are outputting <chapter>. The @ref-id in <fo:page-number-citation> needs something to point to.

    See my answer here.


    EDIT – Example of generated ID

    Here’s an example stylesheet. It will generate a PDF with a working TOC from your input XML. I tested with Saxon 6.5.5 and FOP.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <xsl:output indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="node()|@*">
        <xsl:apply-templates select="node()|@*"/>
      </xsl:template>
      
      <xsl:template match="/article">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
           <fo:layout-master-set>
              <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
                 <fo:region-body margin="1in" margin-top="1.5in"/>
              </fo:simple-page-master>
           </fo:layout-master-set>
           <fo:page-sequence master-reference="my-page">
             <fo:flow flow-name="xsl-region-body">
               <xsl:call-template name="genTOC"/>
             </fo:flow>
            </fo:page-sequence>
            <xsl:apply-templates/>
        </fo:root>
      </xsl:template>
    
      <xsl:template name="genTOC">
        <fo:block break-before='page'>
          <fo:block font-size="16pt" font-weight="bold">TABLE OF CONTENTS</fo:block>
          <xsl:for-each select="//chapter">
            <fo:block text-align-last="justify">
              <fo:basic-link internal-destination="{generate-id(.)}">
                <xsl:value-of select="count(preceding::chapter) + 1" />
                <xsl:text> </xsl:text>
                <xsl:value-of select="title" />
                <fo:leader leader-pattern="dots" />
                <fo:page-number-citation ref-id="{generate-id(.)}" />
              </fo:basic-link>
            </fo:block>
          </xsl:for-each>
        </fo:block>
      </xsl:template>
      
      <xsl:template match="title|para">
        <fo:block><xsl:value-of select="."/></fo:block>
      </xsl:template>
      
      <xsl:template match="chapter">
        <fo:page-sequence master-reference="my-page" id="{generate-id(.)}">
           <fo:flow flow-name="xsl-region-body">
             <xsl:apply-templates/>
           </fo:flow>
        </fo:page-sequence>
      </xsl:template>
    
    </xsl:stylesheet>
    

    EDIT 2023-07-27 – Noticed a deleted answer that was actually a question from 2014…

    This is the best example I have found on the inet to create a toc. HOwever, this works for me, but can’t figure out how to generate the sect1, sect2, etc headings in in toc with page numbers. tried repeating the fo:basic link code for section, but doesn’t work. Please help. – Lori Boyters

    Here’s an updated XSLT 1.0 stylesheet that will also create TOC entries for sect1, sect2, and sect3.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
        
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
        
        <xsl:template match="/article">
            <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
                <fo:layout-master-set>
                    <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
                        <fo:region-body margin="1in" margin-top="1.5in"/>
                    </fo:simple-page-master>
                </fo:layout-master-set>
                <fo:page-sequence master-reference="my-page">
                    <fo:flow flow-name="xsl-region-body">
                        <xsl:call-template name="genTOC"/>
                    </fo:flow>
                </fo:page-sequence>
                <xsl:apply-templates/>
            </fo:root>
        </xsl:template>
        
        <xsl:template name="genTOC">
            <fo:block break-before='page'>
                <fo:block font-size="16pt" font-weight="bold" text-align="center">TABLE OF CONTENTS</fo:block>
                <xsl:for-each select="//chapter|//sect1|//sect2|//sect3">
                    <fo:block text-align-last="justify" text-indent="{count(ancestor::chapter|ancestor::sect1|ancestor::sect2|ancestor::sect3) * 18}pt">
                        <fo:basic-link internal-destination="{generate-id(.)}">
                            <xsl:number count="chapter|sect1|sect2|sect3" 
                                level="multiple" format="1.1.1.1."/>
                            <xsl:value-of select="concat(' ',title,' ')"/>
                            <fo:leader leader-pattern="dots" />
                            <fo:page-number-citation ref-id="{generate-id(.)}" />
                        </fo:basic-link>
                    </fo:block>
                </xsl:for-each>
            </fo:block>
        </xsl:template>
        
        <xsl:template match="title|para">
            <fo:block><xsl:value-of select="."/></fo:block>
        </xsl:template>
        
        <xsl:template match="chapter">
            <fo:page-sequence master-reference="my-page" id="{generate-id(.)}">
                <fo:flow flow-name="xsl-region-body">
                    <xsl:apply-templates/>
                </fo:flow>
            </fo:page-sequence>
        </xsl:template>
        
        <xsl:template match="sect1|sect2|sect3">
            <fo:block-container id="{generate-id()}">
                <xsl:apply-templates/>
            </fo:block-container>
        </xsl:template>
        
    </xsl:stylesheet>
    

    Here’s a working fiddle: http://xsltfiddle.liberty-development.net/bFD9uum

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following structure: class User include Mongoid::Document end class Resource include Mongoid::Document
I have a table with following structure : id, - Primary key minlatitude -
I have a document with the following structure: { tags: ['bbb', 'aaa', 'ddd', 'ccc']
I have an loaded an XML document with the following structure: <?xml version=1.0 encoding=UTF-8
I have a reasonably complex XML document which I want to flatten down to
I have an xml file with following structure: <table name=tblcats> <row> <Id>3680</Id> <Industry>Associations</Industry> <ParentId>1810</ParentId>
I have a table with the following basic structure. unique_id | name | original
I have an xml file with following structure: <table name=tblcats> <row> <Id>3680</Id> <Industry>Associations</Industry> <ParentId>1810</ParentId>
I'm trying to design an XML document structure for my application. I want to
Lets say we have the following document structure: class BlogPost { [MongoIdentifier] public Guid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.