Re: [xsl] Generate nested TOC from flat XHTMTL

Subject: Re: [xsl] Generate nested TOC from flat XHTMTL
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 05 Sep 2006 16:52:22 -0400
Jesper,

With XSLT2.0, your best bet is to use the native grouping construct xsl:for-each-group, in particular with the group-starting-with attribute.

First, select groups of elements starting with an h1 element. Within each of these, process its first element (the h1) and then create groups starting with h2 elements. Then ... etc. As you build the ToC, you won't actually output anything for elements other than the headers themselves; but the groups will allow you to construct the necessary nesting of lists.

This can be done recursively, although you'd need in that case to parameterize the heading level of the element names and to test for a stop condition (no more headers). It's easier to build the first time if you hard-code it ... which requires you to know how many levels deep you wish your code to go (which you probably do). A further complication might arise if you don't always have "cleanly nested" heading levels (for example if you skip straight from an h1 to an h3).

Unfortunately I don't have time to demonstrate this, although maybe someone else is up to the challenge. In any case, if you look up xsl:for-each-grou/@group-starting-with that might give you some hints on how to proceed.

There are a couple of trickier methods (this is called "positional grouping") available in XSLT 1.0.

Cheers,
Wendell

At 06:58 AM 9/4/2006, you wrote:

How can I generate a TOC in correctly nested ordered XHTML list elements
from the flat heading structure in XHTML 1.0?
I am only interested in correctly nested ordered lists as output, like this:

<ol>
        <li>
                <ol>
                        <li/>
                </ol>
        </li>
        <li/>
</ol>

XSLT 2.0 will do. I have no problems with the first level.


Best regards, Jesper Tverskov

Current Thread