<?xml version="1.0" encoding="utf-8"?>


<!-- This stylesheet was written by Max Froumentin, and modified by chaals -->
<!--  covered by the W3C copyright policy http://www.w3.org/Consortium/Copyright -->
<!--  for details of how it works see http://www.w3.org/WAI/AU/sources -->

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

  <xsl:output method="xml" encoding="iso-8859-1"/>

  <!-- by default, copy everything verbatim -->
  <xsl:template match="@*|node()"> 
    <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
  </xsl:template>

  <!-- when we meet div id="toc", we include the toc -->
  <xsl:template match="html:div[@id='toc']">
    <div id="toc">
      <h2><a name="contents" id="contents" accesskey="c">Table of Contents</a></h2>
        <ol>
        <xsl:apply-templates select="following::html:h2[not(@class='notoc')]" mode="toc"/>
        </ol>
    </div>
  </xsl:template>

  <!-- generate link destinations for header content from their id or automagically -->
  <xsl:template match="html:h2|html:h3|html:h4|html:h5|html:h7">
    <xsl:copy>
      <xsl:apply-templates select="@*[not(name(.) = id)]"/>
      <xsl:choose>
        <xsl:when select="@id">
          <a name="{@id}" id="{@id}"><xsl:apply-templates/></a>
        </xsl:when>
        <xsl:otherwise>
          <a name="{generate-id()}"><xsl:apply-templates/></a>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:copy>
  </xsl:template>

  <!-- generate the toc line for h2 - make the first level a list -->
  <xsl:template match="html:h2" mode="toc">
    <li><a href="#{generate-id()}"><xsl:value-of select="."/></a></li>
    <!-- select all the h3s before the next h2 -->
    <xsl:apply-templates select="following::html:h3[not(@class='notoc')][preceding::html:h2[1] = current()]" mode="toc"/>
  </xsl:template> 

  <!-- generate the toc line for h3 -->
  <xsl:template match="html:h3" mode="toc">
    <xsl:text>&#160;&#160;&#160;&#160;</xsl:text>
    <xsl:number format="1. "/>
    <a href="#{generate-id()}"><xsl:value-of select="."/></a>    
    <br/>
    <xsl:apply-templates select="following::html:h4[@class='toc'][preceding::html:h3[1] = current()]" mode="toc"/>
  </xsl:template>    

  <!-- etc. -->
  <xsl:template match="html:h4" mode="toc">
    <xsl:text>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</xsl:text>
    <xsl:number format="1. "/>
    <a href="#{generate-id()}"><xsl:value-of select="."/></a>    
    <br/>
    <xsl:apply-templates select="following::html:h5[@class='toc'][preceding::html:h4[1] = current()]" mode="toc"/>
  </xsl:template>    

  <xsl:template match="html:h5" mode="toc">
    <xsl:text>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</xsl:text>
    <xsl:number format="1. "/>
    <a href="#{generate-id()}"><xsl:value-of select="."/></a>    
    <br/>
    <xsl:apply-templates select="following::html:h6[@class='toc'][preceding::html:h5[1] = current()]" mode="toc"/>
  </xsl:template>    

  <xsl:template match="html:h6" mode="toc">
    <xsl:text>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</xsl:text>
    <xsl:number format="1. "/>
    <a href="#{generate-id()}"><xsl:value-of select="."/></a>    
    <br/>
  </xsl:template>    

</xsl:stylesheet>

