<xsl:transform version="1.0"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://www.w3.org/1999/xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes='h'>
<xsl:output method="xml" encoding='utf-8' indent='yes'
  doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
/>

<!-- This started out as a simple fix to the stylesheet, but has
become the place for generic fix-up to the result from xmlspec
processing. -->

<!-- stylesheet fix-up -->

<xsl:template match="h:style">
  <style type="text/css">
        <xsl:apply-templates select="node()"/>

	[id]:hover:after { 
	  content: "   #" attr(id) " "; 
	  font-size: 80%;
	  color: #ccc;
	  text-decoration: none;
	}
	
	a.borken {
	  background: #f99;
	  color:      #000;
	  font-weight: bold;
	}

	p[id]:hover:after {
			  content: "";
	}

	a.anchor {
	  color: inherit;
	  font-weight: inherit;
	  text-decoration: none;
	  font-style: inherit;
	}

	p[id]:hover:after {
	  content: " #" attr(id) " ¶ "; 
	  font-size: 80%;
	  color: #ccc;
	  text-decoration: none;
	}

	div.note {
	  font-weight: bold;
	  font-style: italic;
	  color: #008000;
	  border-left: 2px solid #008000;
	  margin-left: 0;
	  padding-left: 2em;
	}

	span.sqbrackets {
	  font-style: italic;
	  color: #005000;
	}

	div.exampleOuter {
	  padding-left: 2em;
	  padding-right: 2em;
	  border: 1px solid black;
	  background: #ffa;
	}
	
  </style>
</xsl:template>

<!-- Fix note formatting -->

<xsl:template match="h:div[@class='note']">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <p>
      <xsl:apply-templates select="node()"/>
    </p>
  </xsl:copy>
</xsl:template>

<xsl:template match="h:div[@class='note']/h:p[@class='prefix']"/>

<xsl:template match="h:span[@class='sqbrackets']">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <b>[[</b> <xsl:apply-templates select="node()"/> <b>]]</b>
  </xsl:copy>
</xsl:template>
  


<!-- Move the text of a section *inside* the <a> tags -->

  <xsl:template match="h:h2[starts-with(., &quot;Editor's Draft&quot;)]">
    <xsl:apply-templates select="." mode="fix-headers"/>
    <h2>$Revision: 1.15 $ $Date: 2008/02/27 13:45:00 $</h2>
  </xsl:template>

  <xsl:template match="h:h1|h:h2|h:h3|h:h4|h:h5">
    <xsl:apply-templates select="." mode="fix-headers"/>
  </xsl:template>
  
  
  <xsl:template match="h:h1|h:h2|h:h3|h:h4|h:h5" mode="fix-headers">
    <xsl:choose>
      <xsl:when test="h:a">
	<xsl:copy>
	  <a href="#{h:a/@id}" id="{h:a/@id}" name="{h:a/@id}" class="anchor">
	    <xsl:apply-templates select="@*|node()" mode="noa"/>
	  </a>
	</xsl:copy>
      </xsl:when>
      <xsl:otherwise>
	<xsl:copy>
	  <xsl:apply-templates select="@*|node()"/>
	</xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <xsl:template match="h:a" mode="noa"/>
  <xsl:template match="@*|node()" mode="noa">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Automatically add navigation links -->

 <xsl:template match="h:title">
   <title><xsl:apply-templates select="node()"/></title>

   <xsl:call-template name="addlinkrel">
     <xsl:with-param name="title">
       <xsl:text>Top</xsl:text>
     </xsl:with-param>
     <xsl:with-param name="rel" select="'home'"/>
     <xsl:with-param name="id" select="'title'"/>
   </xsl:call-template>

   <xsl:apply-templates select="/" mode="linkrel"/>
 </xsl:template>

 <xsl:template name="addlinkrel">
   <xsl:param name="title"/>
   <xsl:param name="rel" select="'section'"/>
   <xsl:param name="id" select="h:a/@id"/>

   <xsl:text>
   </xsl:text>
   <xsl:element name="link">
     <xsl:attribute name="rel">
       <xsl:value-of select="$rel"/>
     </xsl:attribute>
     <xsl:attribute name="title">
       <xsl:value-of select="$title"/>
<!--       <xsl:apply-templates select="$node" mode="copytext"/> -->
     </xsl:attribute>
     <xsl:attribute name="href">
       <xsl:text>#</xsl:text><xsl:value-of select="$id"/>
     </xsl:attribute>
   </xsl:element>
 </xsl:template>

 <xsl:template match="*|/" mode="copytext">
   <xsl:apply-templates mode="copytext" select="node()"/>
 </xsl:template>
 <xsl:template match="text()" mode="copytext">
   <xsl:copy/>
 </xsl:template>

<!--  
   <xsl:template match="h:h1" mode="linkrel">
    <xsl:call-template name="addlinkrel">
      <xsl:with-param name="rel" select="'chapter'"/>
    </xsl:call-template>
  </xsl:template>
-->

  <xsl:template match="h:div[@class='head']" mode="linkrel"/>
  
  <xsl:template match="h:div[@class='toc']" mode="linkrel">
    <xsl:call-template name="addlinkrel">
      <xsl:with-param name="rel" select="'contents'"/>
      <xsl:with-param name="title" select="'Table of Contents'"/>
      <xsl:with-param name="id">
	<xsl:value-of select="h:h2/h:a/@id"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="h:h2" mode="linkrel">
    <xsl:call-template name="addlinkrel">
      <xsl:with-param name="rel" select="'chapter'"/>
      <xsl:with-param name="title">
	<xsl:apply-templates select="." mode="copytext"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="h:h3" mode="linkrel">
    <xsl:call-template name="addlinkrel">
      <xsl:with-param name="rel" select="'section'"/>
      <xsl:with-param name="title">
	<xsl:apply-templates select="." mode="copytext"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="h:h4" mode="linkrel">
    <xsl:call-template name="addlinkrel">
      <xsl:with-param name="rel" select="'subsection'"/>
      <xsl:with-param name="title">
	<xsl:apply-templates select="." mode="copytext"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="h:p[@id]|h:li[@id]|h:p/h:a[@id]" mode="linkrel">
    <xsl:call-template name="addlinkrel">
      <xsl:with-param name="rel"  select="'bookmark'"/>
      <xsl:with-param name="id"   select="@id"/>
      <xsl:with-param name="title">
	<xsl:value-of select="@id"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="text()" mode="linkrel"/>

  <xsl:template match="*" mode="linkrel">
    <xsl:apply-templates mode="linkrel" select="node()"/>
  </xsl:template>


  <!-- mark broken as designed references  -->

  <xsl:template match="h:a[@href = '#']">
    <xsl:copy>
      <xsl:attribute name="class">borken</xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@*|node()">
     <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
  </xsl:template>

</xsl:transform>

