RE: [xsl] Producing a Table of Contents

Subject: RE: [xsl] Producing a Table of Contents
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Fri, 26 Mar 2004 03:43:48 -0700
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@xxxxxxx] 
Sent: Friday, March 26, 2004 2:56 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
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

Current Thread