xsl-list
[Top] [All Lists]

RE: Producing a Table of Contents

2004-03-26 03:57:30

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


<Prev in Thread] Current Thread [Next in Thread>