<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://www.w3.org/1999/xhtml"
  >

  <xsl:output
    method="xml"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
    indent="yes"
    encoding="iso-8859-1"
    />

<!--  exclude-result-prefixes="h" -->


  <!-- xhtml-toc-ns.xsl Stylesheet to create a TOC client-side for h1 and h2 XHTML elements
       Daniel Schneider from tecfa.unig.ch (feb 2007). This is freeware.
       Modified April 2009: 
         - commented  exclude-result-prefixes="h" ... doesn't exist anymore .

       Get it from: http://tecfa.unige.ch/guides/xml/examples/xsl-toc/

       Tested with Firefox 1.5x (linux) and 2.0x (win). Does not work with IE 6 (win)
       If you change encoding you have to fix this file, i.e replace the &#160 and § characters
       XHTML should be real XML like below and h1 and h2 headers can be any place I think
       - - - - -
      <?xml version="1.0" encoding="ISO-8859-1" ?>
      <?xml-stylesheet href="xhtml-toc.xsl" version="1.0" type="text/xsl"?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

        <html xmlns:h="http://www.w3.org/1999/xhtml">
          <head> ... </head><body> ... </body> ... </html>
       - - - - -
       Notes: 
       Carefully look at the namespace declarations in both
       xsl:stylesheet AND the template for the root element

       The stylesheet is supposed to generate XHTML output with result
       elements in the XHTML namespace so the stylesheet should have
       the XHTML namespace as the default namespace. Then for XPath
       expressions and match patterns to be able to select/match
       elements in the XHTML namespace you need a declaration binding
       a prefix to the namespace too. To avoid that the prefix is
       later appearing in the output and making the markup invalid in
       terms of (rather limited) DTD validation use
       exclude-result-prefixes. Thanx to Martin Honnen and David Carlisle
       (XSL mailing list)
       -->    

  <xsl:template match="/h:html">
  <html xmlns:h="http://www.w3.org/1999/xhtml">
    <head>
      <title><xsl:value-of select="h:title"/></title>
    </head>
    <body bgcolor="#FFFFFF">
      <xsl:apply-templates select="h:body"/>
    </body>
  </html>
</xsl:template>

<xsl:template match="h:body">
  <strong><a name="toc">Contents</a></strong>
  <xsl:apply-templates select="h:h1|h:h2" mode="toc"/>
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="h:h1" mode="toc">
  <br/>
  <a href="#h1_{generate-id(.)}"><xsl:value-of select="."/></a><br/>
</xsl:template>

<xsl:template match="h:h2" mode="toc">
  <span style="font-size:small;">
    <xsl:text> &#160;&#160;§ </xsl:text>
    <a href="#h2_{generate-id(.)}"><xsl:value-of select="."/></a>
  </span>
</xsl:template>

<xsl:template match="h:h1">
  <h1><a name="h1_{generate-id(.)}"><xsl:value-of select="."/></a> (<a href="#toc">&#171;up</a>)</h1>
</xsl:template>

<xsl:template match="h:h2">
  <h2><a name="h2_{generate-id(.)}"><xsl:value-of select="."/></a> (<a href="#toc">&#171;up</a>)</h2>
</xsl:template>


<!-- A default rule will just copy all the rest -->

<xsl:template match="*">
     <xsl:copy>
         <xsl:copy-of select="@*"/>
       <xsl:apply-templates/>
     </xsl:copy>
   </xsl:template>

</xsl:stylesheet>
