<?xml version="1.0"?>

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

  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>

  <!--
    This is the identity template - it will produce a copy of 
    any node that it matches (and it will match all nodes in the
    input document, unless its overridden by other templates in
    this document).
  -->

  <xsl:template match="node()|@*">			<!-- match any node or attribute -->
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>		<!-- apply matching template if any --> 
    </xsl:copy>
  </xsl:template>

  <xsl:template match="body">
    <body>
      <!-- Begin standard header -->
      <table style="border: solid thin black">
        <tr>
          <td><a href="mammoth.html">Visit the Mammoth zone!</a> - </td>
          <td><a href="play.html">Play Pteranodon Poker</a></td>
        </tr>
      </table>
      <!-- End standard header -->
    
      <!-- 
        Build a table of content with internal hyperlinks 
        Example: This will create an <a href="#Stegosaurus">Stegosaurus</a> element
      -->
      <!-- Build a table of content with internal hyperlinks -->
      <h3>Table of Contents</h3>
      <ul>
        <!-- select each "h2" tag, and create an internal hyperlink for it --> 
        <xsl:for-each select="h2">
          <li>
            <a>
              <!-- 
                The <xsl:attribute> element adds an attribute with the 
                specified name to the tag that it is inside - in this case,
                adding an "href" attribute to the <a> tag.
              -->
              <xsl:attribute name="href">
                #<xsl:value-of select="text()"/>	<!-- notice the # for internal href -->
              </xsl:attribute>
              <!-- 
                We reference the text inside the "h2" again to provide
                the clickable text content of our link.
              -->
              <xsl:value-of select="text()"/>
            </a>
          </li>
        </xsl:for-each>
      </ul>  
    
      <!-- 
        Apply templates to everything inside the body - in this case, it will 
        be the identity template since there are no others defined in the file.
      -->

      <xsl:apply-templates />

      <!-- Begin standard footer -->
      <hr/>
      Copyright 2003 DinosaurOrg.
      <!-- End standard footer -->

    </body>
  </xsl:template>

  <!--
    Override the normal handling of "h2" to add in a link anchor.
    Example: This will create an <a name="Stegosaurus"></a> element
  -->

  <xsl:template match="h2">
    <a>
      <xsl:attribute name="name">
         <xsl:value-of select="text()"/>
      </xsl:attribute>
      <!-- This appears as the contents of the <a>..</a> tag --> 
      <h2><xsl:apply-templates/></h2> 
    </a>
  </xsl:template>

</xsl:stylesheet>