Using the mode attribute of apply-templates allows you to select
particular elements and transform them different for each type of mode.
Using "//hi" will select all h1 elements from your source. Using
mode="toc" allows you to match each of these elements to the template
with the match attribute set to "h1" and the mode set to "toc".
So this XML...
<?xml version="1.0"?>
<html>
<h1>this is heading 1</h1>
<p>this is paragraph one <b>with bold text <i>with bold and
italicized text</i></b><i> with italicized text</i></p>
<h1>this is heading 2</h1>
<p>this is paragraph one <b>with bold text <i>with bold and
italicized text</i></b><i> with italicized text</i></p>
<h1>this is heading 3</h1>
<p>this is paragraph one <b>with bold text <i>with bold and
italicized text</i></b><i> with italicized text</i></p>
<h1>this is heading 4</h1>
<p>this is paragraph one <b>with bold text <i>with bold and
italicized text</i></b><i> with italicized text</i></p>
</body>
</html>
Transformed by this XSL...
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output method="html"/>
<xsl:template match="html">
Table of Contents: <br/>
<xsl:element name="ol">
<xsl:apply-templates select="//h1" mode="toc"/>
</xsl:element>
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="h1 | p | b | i">
<xsl:copy>
<xsl:element name="name()">
<xsl:apply-templates/>
</xsl:element>
</xsl:copy>
</xsl:template>
<xsl:template match="h1" mode="toc">
<xsl:element name="li">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Gives you this output:
Table of Contents: <br>
<ol>
<li>this is heading 1</li>
<li>this is heading 2</li>
<li>this is heading 3</li>
<li>this is heading 4</li>
</ol>
<h1>this is heading 1</h1>
<p>this is paragraph one <b>with bold text <i>with bold and italicized
text</i></b><i> with italicized text</i>
</p>
<h1>this is heading 2</h1>
<p>this is paragraph one <b>with bold text <i>with bold and italicized
text</i></b><i> with italicized text</i>
</p>
<h1>this is heading 3</h1>
<p>this is paragraph one <b>with bold text <i>with bold and italicized
text</i></b><i> with italicized text</i>
</p>
<h1>this is heading 4</h1>
<p>this is paragraph one <b>with bold text <i>with bold and italicized
text</i></b><i> with italicized text</i>
</p>
Best of luck!
<M:D/>
-----Original Message-----
From: Julian Voelcker [mailto:asp(_at_)tvw(_dot_)net]
Sent: Friday, March 26, 2004 2:56 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Producing a Table of Contents
When doing a transformation, is there any 'clever' way of generating a
table of contents in the XSLT based on the actual content of the
template?
For example, the template has lots of sections of text preceded with
headings (using <h1></h1> tags). Using XSLT is it possible to search
through the template to draw up a list of the text between these tags?
--
Cheers,
Julian Voelcker
United Kingdom
--+------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
You are subscribed as: m(_dot_)david(_at_)mdptws(_dot_)com
To unsubscribe, go to:
http://lists.mulberrytech.com/unsub.php/xsl-list/m(_dot_)david(_at_)mdptws(_dot_)com
or e-mail:
<mailto:xsl-list-unsubscribe-m(_dot_)david=mdptws(_dot_)com(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--