RE: [xsl] Producing a Table of Contents

Subject: RE: [xsl] Producing a Table of Contents
From: "Andrew Welch" <AWelch@xxxxxxxxxxxxxxx>
Date: Fri, 26 Mar 2004 10:57:30 -0000
> 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?

You can use the fact that calling document('') with no parameter will
return the stylesheet, so you can then iterate through the templates
extracting the elements you need.

This stylesheet:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:template match="/">
  <xsl:for-each select="document('')/*/xsl:template/h1">
    <xsl:element name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:for-each>
</xsl:template>

<xsl:template match="one">
  <h1>heading one</h1>
</xsl:template>

<xsl:template match="two">
  <h1>heading two</h1>
</xsl:template>

</xsl:stylesheet>

When applied to itself, produces:

<h1>heading one</h1><h1>heading two</h1>

cheers
andrew

Current Thread