<!-- 
XSL stylesheet to format TEI XML documents to HTML

Many basic ideas and many details taken from the stylesheets
written by Sebastian Rahtz. I gratefully reproduce his copyright statement:

 Copyright 1999 Sebastian Rahtz/Oxford University  <sebastian.rahtz@oucs.ox.ac.uk>

 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and any associated documentation files (the
 ``Software''), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to
 the following conditions:
 
 The above copyright notice and this permission notice shall be included
 in all copies or substantial portions of the Software.
--> 
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="div" mode="depth">
	<xsl:value-of select="count(ancestor::div)"/>
</xsl:template>

<xsl:template match="div"> 
	<xsl:variable name="ident">
		<xsl:apply-templates select="." mode="ident"/>
	</xsl:variable>
	<xsl:variable name="depth">
		<xsl:apply-templates select="." mode="depth"/>
	</xsl:variable>
	<a name="{$ident}"/>
	<xsl:element name="h{$depth + 2}"><xsl:apply-templates mode="plain" select="head"/></xsl:element>
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="div" mode="header"> 
	<xsl:apply-templates mode="plain" select="head"/>
</xsl:template>

<!-- headings etc -->
<xsl:template match="head">
	<xsl:variable name="parent" select="name(..)"/>
	<xsl:if test="not($parent='div')">
		<xsl:choose >
			<xsl:when test="$parent='body'">
 				<h1><xsl:apply-templates/></h1>
			</xsl:when>
			<xsl:when test="$parent='lg'">
				<h4><xsl:apply-templates/></h4>
			</xsl:when >
			<xsl:otherwise >
				<xsl:apply-templates/>
			</xsl:otherwise >
		</xsl:choose >
	</xsl:if>
</xsl:template>

<xsl:template mode="plain" match="head">
     <xsl:if test="preceding-sibling::head">
	<xsl:text> </xsl:text>
     </xsl:if>
   <xsl:apply-templates/>
</xsl:template>

<xsl:template name="header">
	<xsl:apply-templates mode="plain" select="head"/>
</xsl:template>

<xsl:template name="headerNum">
	<xsl:number level="multiple" count="div"/>
	<xsl:text>. </xsl:text>
	<xsl:apply-templates mode="plain" select="head"/>
</xsl:template>

<!-- work out an ID -->
<xsl:template match="div|bibl|biblStruct|l|lg|note|anchor|divGen|name" mode="ident">
	<xsl:choose>
		<xsl:when test="@id">
			<xsl:value-of select="@id"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="generate-id()"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- build href attributes -->
<xsl:template mode="xrefheader" match="div|bibl|biblStruct|l|lg|note|anchor|divGen|name">
	<xsl:variable name="ident">
		<xsl:apply-templates select="." mode="ident"/>
	</xsl:variable>
	<xsl:variable name="parent">
    	<xsl:call-template name="find-parent"/>
	</xsl:variable>
	<xsl:value-of select="concat(concat($parent,'.htm#'),$ident)"/>
</xsl:template>

<!-- find id of top division -->
<xsl:template name="find-parent">
	<xsl:choose>
		<xsl:when test="ancestor-or-self::div">
			<xsl:apply-templates select="ancestor-or-self::div[last()]" mode="ident"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:message>Didn't cater for this one</xsl:message>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- crossreferences etc. -->
<xsl:template match="anchor">
	<xsl:variable name="ident">
		<xsl:apply-templates select="." mode="ident"/>
	</xsl:variable>
	<a name="{$ident}"></a>
</xsl:template>

<xsl:template match="ref">
	<a>
		<xsl:attribute name="href">
			<xsl:apply-templates mode="xrefheader" select="id(@target)"/>
		</xsl:attribute>
		<xsl:attribute name="target">
			<xsl:apply-templates mode="targframe" select="id(@target)"/>
		</xsl:attribute>
		<xsl:apply-templates/>
	</a>
</xsl:template>

<xsl:template match="ptr">
	<a>
		<xsl:attribute name="href">
			<xsl:apply-templates mode="xrefheader" select="id(@target)"/>
		</xsl:attribute>
		<xsl:attribute name="target">
			<xsl:apply-templates mode="targframe" select="id(@target)"/>
		</xsl:attribute>
		<xsl:apply-templates mode="header" select="id(@target)"/>
	</a>	
</xsl:template>

<xsl:template match="xref[@doc]">
	<xsl:variable name="uri">
		<xsl:value-of select="unparsed-entity-uri(@doc)"/>
	</xsl:variable>
	<a>
		<xsl:attribute name="href">
			<xsl:choose>
				<xsl:when test="starts-with($uri,'http')">
					<xsl:value-of select="$uri"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="filename">
						<xsl:with-param name="x" select="$uri"/>
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
		<xsl:attribute name="target">
			<xsl:text>extern</xsl:text>
		</xsl:attribute>
		<xsl:apply-templates/>
	</a>
</xsl:template>

<xsl:template match="xptr[@doc]">
	<a href="{unparsed-entity-uri(@doc)}" target="extern">
		<tt>
			<xsl:value-of select="unparsed-entity-uri(@doc)"/>
		</tt>
	</a>
</xsl:template>

<!-- generated divisions -->
<xsl:template match="divGen">
	<xsl:choose>
		<xsl:when test="@type='appList'">
			<xsl:call-template name="appList"/>
		</xsl:when>
		<xsl:when test="@type='bsv'">
			<xsl:call-template name="bsv"/>
		</xsl:when>
		<xsl:when test="@type='changes'">
			<xsl:call-template name="changes"/>
		</xsl:when>
		<xsl:when test="@type='corrList'">
			<xsl:call-template name="corrList"/>
		</xsl:when>
		<xsl:when test="@type='globtoc'">
			<xsl:call-template name="globtoc"/>
		</xsl:when>
		<xsl:when test="@type='ixnaam'">
			<xsl:call-template name="ixnaam"/>
		</xsl:when>
		<xsl:when test="@type='notes'">
			<xsl:call-template name="notes"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:message>Onvoorzien divGen type </xsl:message>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>


