"Alex Geller" <> wrote in message news:35gpjb-...
> Hi Dimitre,
> Dimitre Novatchev wrote:
>
> > Excuse me, but it is not clear what exactly you want to produce from your
> > source xhtml -- how the output is related to the input (they seem
> > essentially to have the same structure),
> Well, not quite. The input H?s have a flat structure (siblings) while in the
> output they are nested (Hn+1 become descendands of Hn). Maybe you were
> fooled by the indentation of the input HTML.
> >how the output must be structured
> Exactly as shown (best is, you view both the input and the output in a
> browser).
> > and what requirements it must satisfy.
>
> >
> > In other words, can you define what you mean by "index"?
>
> I want an automatic generation of a table of contents up front of an
> arbitrary HTML document where chapters and subchapters are denoted using H?
> tags. The style should search for these tags in the document, create the
> table of contents from those tags and then copy the document itself. The
> items in the table of contents should be linked vi <a href=.." to their
> respective chapters in the document. The table of content should have a
> hirachical structure using numbered lists as shown in the example. The
> rules for the structure could be defined as follows:
> Let v be a vector of all H? elements found in a pre order traversal of the
> document tree.
> For example:
> v=H1,H2,H2,H3,H2,H3,H1,H1,H2,H3,H2,H3
> We call n of a Hn element, it's hierarchy value.
> Create a resulttree r so that it contains all nodes from the source vector
> v. In this resulttree r every node vn from the source vector v
> becomes the child of it's preceding sibling vn-1 if the hierarchy value of
> vn is lower than the hirarchy value of vn-1
> r=H1(H2,H2(H3),H2(H3)),H1,H1(H2(H3),H2(H3).
>
> Thank you,
> Alex
Hi Alex,
The following transformation implements all your requirements,
including the one that different Hx may not always be siblings, but
may be children of other elements, e.g. div.
<xsl:stylesheet version="1.0"
xmlns

sl="http://www.w3.org/1999/XSL/Transform">
<xsl

utput omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kChildren"
match="H2"
use="generate-id(
(ancestor::H1 | preceding::H1)
[last()]
)"/>
<xsl:key name="kChildren"
match="H3"
use="generate-id(
(ancestor::H2 | preceding::H2)
[last()]
)"/>
<xsl:key name="kChildren"
match="H4"
use="generate-id(
(ancestor::H3 | preceding::H3)
[last()]
)"/>
<xsl:key name="kChildren"
match="H5"
use="generate-id(
(ancestor::H4 | preceding::H4)
[last()]
)"/>
<xsl:key name="kChildren"
match="H6"
use="generate-id(
(ancestor::H5 | preceding::H5)
[last()]
)"/>
<xsl:template match="/">
<html>
<xsl:apply-templates select="/*/*/H1" mode="TOC"/>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="H1 | H2 | H3 | H4 | H5 | H6"
mode="TOC">
<LI><a href="#{.}"><xsl:value-of select="."/></a>
<OL>
<xsl:apply-templates
select="key('kChildren', generate-id())"
mode="TOC"/>
</OL>
</LI>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="H1 | H2 | H3 | H4 | H5 | H6">
<a name="#{.}"/>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When applied on this source.xhtml:
<HTML>
<BODY>
<H1>H1 1</H1>
<H2>H2 1.1</H2>
<div>
<H3>H3 1.1.1</H3>
<H3>H3 1.1.2</H3>
</div>
<H3>H3 1.1.3</H3>
<H2>H2 1.2</H2>
<H3>H3 1.2.1</H3>
<H3>H3 1.2.2</H3>
<H3>H3 1.2.3</H3>
<H1>H1 2</H1>
<H2>H2 2.1</H2>
<H3>H3 2.1.1</H3>
<H3>H3 2.1.2</H3>
<H3>H3 2.1.3</H3>
<H2>H2 2.2</H2>
<H3>H3 2.2.1</H3>
<H3>H3 2.2.2</H3>
<H3>H3 2.2.3</H3>
</BODY>
</HTML>
the wanted result is produced:
<html>
<LI><a href="#H1 1">H1 1</a><OL>
<LI><a href="#H2 1.1">H2 1.1</a><OL>
<LI><a href="#H3 1.1.1">H3 1.1.1</a><OL></OL>
</LI>
<LI><a href="#H3 1.1.2">H3 1.1.2</a><OL></OL>
</LI>
<LI><a href="#H3 1.1.3">H3 1.1.3</a><OL></OL>
</LI>
</OL>
</LI>
<LI><a href="#H2 1.2">H2 1.2</a><OL>
<LI><a href="#H3 1.2.1">H3 1.2.1</a><OL></OL>
</LI>
<LI><a href="#H3 1.2.2">H3 1.2.2</a><OL></OL>
</LI>
<LI><a href="#H3 1.2.3">H3 1.2.3</a><OL></OL>
</LI>
</OL>
</LI>
</OL>
</LI>
<LI><a href="#H1 2">H1 2</a><OL>
<LI><a href="#H2 2.1">H2 2.1</a><OL>
<LI><a href="#H3 2.1.1">H3 2.1.1</a><OL></OL>
</LI>
<LI><a href="#H3 2.1.2">H3 2.1.2</a><OL></OL>
</LI>
<LI><a href="#H3 2.1.3">H3 2.1.3</a><OL></OL>
</LI>
</OL>
</LI>
<LI><a href="#H2 2.2">H2 2.2</a><OL>
<LI><a href="#H3 2.2.1">H3 2.2.1</a><OL></OL>
</LI>
<LI><a href="#H3 2.2.2">H3 2.2.2</a><OL></OL>
</LI>
<LI><a href="#H3 2.2.3">H3 2.2.3</a><OL></OL>
</LI>
</OL>
</LI>
</OL>
</LI>
<HTML>
<BODY>
<a name="#H1 1"></a><H1>H1 1</H1>
<a name="#H2 1.1"></a><H2>H2 1.1</H2>
<div>
<a name="#H3 1.1.1"></a><H3>H3 1.1.1</H3>
<a name="#H3 1.1.2"></a><H3>H3 1.1.2</H3>
</div>
<a name="#H3 1.1.3"></a><H3>H3 1.1.3</H3>
<a name="#H2 1.2"></a><H2>H2 1.2</H2>
<a name="#H3 1.2.1"></a><H3>H3 1.2.1</H3>
<a name="#H3 1.2.2"></a><H3>H3 1.2.2</H3>
<a name="#H3 1.2.3"></a><H3>H3 1.2.3</H3>
<a name="#H1 2"></a><H1>H1 2</H1>
<a name="#H2 2.1"></a><H2>H2 2.1</H2>
<a name="#H3 2.1.1"></a><H3>H3 2.1.1</H3>
<a name="#H3 2.1.2"></a><H3>H3 2.1.2</H3>
<a name="#H3 2.1.3"></a><H3>H3 2.1.3</H3>
<a name="#H2 2.2"></a><H2>H2 2.2</H2>
<a name="#H3 2.2.1"></a><H3>H3 2.2.1</H3>
<a name="#H3 2.2.2"></a><H3>H3 2.2.2</H3>
<a name="#H3 2.2.3"></a><H3>H3 2.2.3</H3>
</BODY>
</HTML>
</html>
Hope this helped.
=====
Cheers,
Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL