<?xml version="1.0" encoding="UTF-8"?>
<!-- XSLT header and selection output in the form of HTML etc. -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:saxon="http://saxon.sf.net/" exclude-result-prefixes="saxon">
  <xsl:output method="html" version="4.01" encoding="UTF-8" indent="yes" doctype-public="-//W3C//DTD HTML 4.01//EN" 
    doctype-system="http://www.w3.org/TR/html4/strict.dtd" saxon:character-representation="native;entity;decimal" />

  <!-- XSLT will operate on elements <page> of source XML -->
    <xsl:template match="page"> 
      <!-- For debugging purposes, the name of the actual processed page is dispplayed -->
      <xsl:message>Processing: <xsl:value-of select="@file"/></xsl:message>
      <!-- Set the output document according to the attribute file, the referred xpath expression is in curly braces -->
      <xsl:result-document href="{@file}">
	<!-- The usual html follows with xsl elements for inserting contents from the source XSML file -->
	<html>
	  <head>
	    <title>
	      <!-- The title of the page is inserted by the xsl element <value-of> with the attribute select containing appropriate xpath expression -->
		<xsl:value-of select="@title"/>
	      </title>
	    </head>
	    <body>
	      <div style="float:left; margin-right:5ex">
		Table of contents
		<ul>
		  <!-- The whole XML file will be processed independently in the mode "tableofcontents" -->
		  <xsl:apply-templates select="/" mode="tableofcontents">
		    <!-- in order to highlight the actual page, its name is given as a parameter -->
		    <xsl:with-param name="actpage">
		      <xsl:value-of select="@file"/>
		    </xsl:with-param>
		  </xsl:apply-templates>

		</ul>
	      </div>
	      <h1>
		<xsl:value-of select="@title"/>
	      </h1>
	      <!-- the body of the actual <page> element is inserted by a function node() -->
		<xsl:copy-of select="node()"/>
	      </body>
	    </html>
	  </xsl:result-document>
	</xsl:template>

	<!-- this template is active only in the toc mode -->
	<xsl:template match="page" mode="tableofcontents">
	  <!-- declare the accepted parameter -->
	  <xsl:param name="actpage"/>
	  <li>
	    <!-- distinguish the active and inactive page according to the parameter -->
	    <xsl:choose>
	      <xsl:when test="@file=$actpage">
		<b>
		  <xsl:value-of select="@title"/>
		</b>
	      </xsl:when>
	      <xsl:otherwise>
		<a href="{@file}">
		  <xsl:value-of select="@title"/>
		</a>
	      </xsl:otherwise>
	    </xsl:choose>
	  </li>
	</xsl:template>

      </xsl:stylesheet>



