<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:h="http://www.w3.org/1999/xhtml"
                exclude-result-prefixes="h"
                version="1.0">

<!-- ====================================================================== 
     links-to-unordered-lists.xsl                                                   

     This stylesheet creates one, or more, unordered lists based on the
     link elements in the source document.

     List items are grouped according to either their rev or rel attribute. 
     Elements whose rel attribute is 'stylesheet' are explictly excluded.

     Version : 1.0
     Date    : $Date: 2003/01/03 23:36:23 $

     Copyright (c) 2002 Aaron Straup Cope. All Rights Reserved.

     Documentation: http://www.aaronland.info/xsl/xhtml/links-to-unordered-lists

     Permission to use, copy, modify and distribute this stylesheet and its 
     accompanying documentation for any purpose and without fee is hereby 
     granted in perpetuity, provided that the above copyright notice and 
     this paragraph appear in all copies.  The copyright holders make no 
     representation about the suitability of the stylesheet for any purpose.

     It is provided "as is" without expressed or implied warranty.

     ====================================================================== 
     Changes
     
     1.0     December 14, 2002

             - Initial release

     ====================================================================== -->

     <xsl:key name = "rev" match = "/h:html/h:head/h:link" use = "@rev" />
     <xsl:key name = "rel" match = "/h:html/h:head/h:link" use = "@rel" />

<!-- ======================================================================

     ListAllLinks
     ====================================================================== -->

     <xsl:template name = "ListAllLinks">
      <xsl:call-template name = "ListAllRevLinks" />
      <xsl:call-template name = "ListAllRelLinks" />
     </xsl:template>

<!-- ======================================================================

     ListAllRevLinks
     ====================================================================== -->

     <xsl:template name = "ListAllRevLinks">
      <div>
        <xsl:attribute name = "class">reverse-links</xsl:attribute>
        <xsl:for-each select = "/h:html/h:head/h:link[count(.|key('rev',@rev)[1]) = 1]">
        <xsl:variable name = "rev" select = "@rev" />         

        <xsl:if test = "$rev != '' and $rev != 'stylesheet'">
         <xsl:call-template name = "ListOneRev">
          <xsl:with-param name = "rev" select = "$rev" />
         </xsl:call-template>
        </xsl:if>
       </xsl:for-each>
      </div>       
     </xsl:template>

<!-- ======================================================================

     ListRevGroup
     ====================================================================== -->

     <xsl:template name = "ListOneRev">
      <xsl:param name = "rev" />
        
      <xsl:call-template name = "_ListGroup">
       <xsl:with-param name = "group" select = "$rev" />
       <xsl:with-param name = "direction">rev</xsl:with-param>
      </xsl:call-template>

     </xsl:template>

<!-- ======================================================================

     ListAllRelLinks
     ====================================================================== -->

     <xsl:template name = "ListAllRelLinks">
      <div>
        <xsl:attribute name = "class">forward-links</xsl:attribute>
        <xsl:for-each select = "/h:html/h:head/h:link[count(.|key('rel',@rel)[1]) = 1]">
        <xsl:variable name = "rel" select = "@rel" />         

        <xsl:if test = "$rel != '' and $rel != 'stylesheet'">
         <xsl:call-template name = "ListOneRel">
          <xsl:with-param name = "rel" select = "$rel" />
         </xsl:call-template>
        </xsl:if>
       </xsl:for-each>
      </div>  
     </xsl:template>

<!-- ======================================================================

     ListRelGroup
     ====================================================================== -->

     <xsl:template name = "ListOneRel">
      <xsl:param name = "rel" />
        
      <xsl:call-template name = "_ListGroup">
       <xsl:with-param name = "group" select = "$rel" />
       <xsl:with-param name = "direction">rel</xsl:with-param>
      </xsl:call-template>

     </xsl:template>

<!-- ======================================================================
     _ListGroup
     ====================================================================== -->

     <xsl:template name = "_ListGroup">
      <xsl:param name = "group" />
      <xsl:param name = "direction" />

      <div>
       <xsl:attribute name = "id"><xsl:value-of select = "$direction" />.<xsl:value-of select = "translate($group,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" /></xsl:attribute>
       <xsl:attribute name = "class"><xsl:value-of select = "$direction" />-group</xsl:attribute>

       <h2><xsl:value-of select = "$group" /></h2>

       <ul>

        <xsl:choose>
         <xsl:when test = "$direction = 'rev'">
          <xsl:for-each select = "/h:html/h:head/h:link[@rev = $group]">
           <xsl:call-template name = "_ListItem" />
          </xsl:for-each>
         </xsl:when>

         <xsl:when test = "$direction = 'rel'">
          <xsl:for-each select = "/h:html/h:head/h:link[@rel = $group]">
           <xsl:call-template name = "_ListItem" />
          </xsl:for-each>
         </xsl:when>

         <xsl:otherwise />
        </xsl:choose>
       </ul>

      </div>

     </xsl:template>

<!-- ======================================================================
     _ListItem
     ====================================================================== -->

     <xsl:template name = "_ListItem">
      <li>
       <a>
        <xsl:attribute name = "href">
         <xsl:value-of select = "@href" />
        </xsl:attribute>
        <xsl:attribute name = "title">
         <xsl:value-of select = "@title" />
        </xsl:attribute>
        <xsl:value-of select = "@title" />
       </a>
      </li>       
     </xsl:template>

<!-- ======================================================================
     FIN
     ====================================================================== -->

</xsl:stylesheet>
