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

<!--  covered by the W3C copyright policy http://www.w3.org/Consortium/Copyright -->
<!--  based on http://www.w3.org/WAI/AU/sources/toc.xsl -->

<!--  This is for generating the Authoring Tool Accessibility Guidelines -->
<!--  For more information on the way this is done see heetp://www.w3.org/WAI/AU/sources -->

<!--  Todo: make a makefile / imports structure, to reuse pieces better -->


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


  <!-- stuff to go to a makefile ASAP -->
  <xsl:variable name="draftDate">today</xsl:variable>
  <xsl:variable name="thisVersion">today</xsl:variable>

  <!-- start with the root element, to get everything in a default namespace? -->
  <xsl:output method="xml" encoding="iso-8859-1" indent="yes"/>
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>

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

  <!-- put in the title, status and version information -->
  <xsl:template match="html:p[@id='titleStatus']">
    <h1 class="notoc">Authoring Tool Accessibility Guidelines</h1>
    <h2 class="notoc">Working Draft <xsl:value-of select="$draftDate"/></h2>
    <dl>
      <dt>This Version:</dt>
      <dd><a href="{$thisVersion}"><xsl:value-of select="$thisVersion"/></a></dd>
    </dl>
    <h2>Warning. This is a test document</h2>
    <p>This document has been generated as a test
    for an XSLT stylesheet, and does not represent anything much.</p>
  </xsl:template>

  <!-- remove anything that is techniques-specific, or source-doc specific -->
  <xsl:template match="*[starts-with(@class,'techniques')]"/>
  <xsl:template match="*[@class='source']"/>
  <xsl:template match="@class[.='guidelines']"/>
  <xsl:template match="html:style"/>

  <!-- if headers have an id use it as a link destination -->
  <!-- otherwise generate link destinations for headers -->
  <xsl:template match="html:h2|html:h3|html:h4|html:h5|html:h7">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
        <xsl:choose>
        <xsl:when test="@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. This is the bit I sort of kept from MaxF's XSLT - toc.xsl -->

  <!-- there's a bug somewhere - if there isn't a next -->
  <!-- Hn element it inserts an empty ol element instead of closing a list -->

  <!-- when we meet div with id="toc", we include the toc -->
  <!-- we keep the id in it so we can keep doing this -->
  <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 the toc line for h2 -->
  <xsl:template match="html:h2" mode="toc">
    <li>
      <a href="#{@id}"><xsl:value-of select="."/></a>
      <xsl:variable name="children" select="following::html:h3[not(@class='notoc')][preceding::html:h2[1] = current()]"/>
      <xsl:if test="count($children)>0">
        <ol><xsl:apply-templates select="$children" mode="toc"/></ol>
      </xsl:if>
    </li>
  </xsl:template> 

  <!-- generate the toc line for h3 -->
  <xsl:template match="html:h3" mode="toc">
    <li>
      <a href="#{@id}"><xsl:value-of select="."/></a>
      <xsl:variable name="children" select="following::html:h4[@class='toc'][preceding::html:h3[1] = current()]"/>
      <xsl:if test="count($children)>0">
        <ol><xsl:apply-templates select="." mode="toc"/></ol>
      </xsl:if>
    </li>
  </xsl:template>    

  <!-- etc. -->
  <xsl:template match="html:h4" mode="toc">
    <ol>
      <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"/>
    </ol>
  </xsl:template>    

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

  <xsl:template match="html:h6" mode="toc">
 
    <xsl:number format="1. "/>
    <a href="#{generate-id()}"><xsl:value-of select="."/></a>    
    <br/>
  </xsl:template>  
  
  <xsl:template match="ol[not(./*)]"/>

</xsl:stylesheet>

