<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:variable name="topiclist">
    <topiclist>
      <xsl:for-each select="/topicmap/topics/topic[@domain='wwww']">
        <topic id="{@TID}" name="{translate(name,'abcdefghijklmnopqrstuvwxyz./-+','ABCDEFGHIJKLMNOPQRSTUVWXYZ')}"/>
      </xsl:for-each>
    </topiclist>
  </xsl:variable>
  <xsl:variable name="sortedtopiclist">
    <sortedtopiclist>
      <xsl:for-each select="$topiclist/topiclist/topic">
        <xsl:sort select="@name"/>
        <topic id="{@id}" name="{@name}"/>
      </xsl:for-each>
    </sortedtopiclist>
  </xsl:variable>
  <xsl:template match="/">
    <xsl:variable name="document" select="current()"/>
    <html>
      <body>
        <h1>Topicliste</h1>
        <xsl:for-each select="$sortedtopiclist/sortedtopiclist/topic">
          <xsl:variable name="topic" select="$document/topicmap/topics/topic[@TID=current()/@id]"/>
          <a href="#{@id}"><xsl:value-of select="$topic/name"/></a>
          <xsl:text> </xsl:text>
        </xsl:for-each>
        
        <hr />
          
          <xsl:for-each select="$sortedtopiclist/sortedtopiclist/topic">
            <xsl:variable name="topic" select="$document/topicmap/topics/topic[@TID=current()/@id]"/>
            <h2><a name="{@id}"><xsl:value-of select="$topic/name"/></a></h2>
            <p><xsl:apply-templates select="$topic/desc"/></p>
            <hr />
          </xsl:for-each>
          
        </body>
      </html>
    </xsl:template>

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

    <xsl:template match="topicref">
      <xsl:variable name="topic" select="/topicmap/topics/topic[@TID=current()/@TID]"/>
      <xsl:choose>
        <xsl:when test="$topiclist/topiclist/topic[@id=current()/@TID]">
          <a href="#{@TID}"><xsl:value-of select="$topic/name"/></a>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$topic/name"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

