<!-- 
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">

<!-- rendering -->
<xsl:template name="rendering">
	<xsl:choose>
		<xsl:when test="@rend='bold'">
			<b><xsl:apply-templates/></b>
		</xsl:when>
		<xsl:when test="@rend='center'">
			<center><xsl:apply-templates/></center>
		</xsl:when>
		<xsl:when test="@rend='code'">
			<tt><xsl:apply-templates/></tt>
		</xsl:when>
		<xsl:when test="@rend='ital'">
			<i><xsl:apply-templates/></i>
		</xsl:when>
		<xsl:when test="@rend='plain'">
			<xsl:apply-templates/>
		</xsl:when>
		<xsl:when test="@rend='quoted'">
			<xsl:text>'</xsl:text><xsl:apply-templates/><xsl:text>'</xsl:text>
		</xsl:when>
		<xsl:when test="@rend='sup'">
			<sup><xsl:apply-templates/></sup>
		</xsl:when>
		<xsl:otherwise>
			<xsl:message>Onvoorzien rend attribuut <xsl:value-of select="@rend"/></xsl:message>
			<xsl:apply-templates/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="gap">
	[...]<xsl:apply-templates/>
</xsl:template>

<xsl:template match="supplied">
	[<xsl:apply-templates/>]
</xsl:template>

<xsl:template match="term|mentioned|soCalled">
	<xsl:choose>
		<xsl:when test="@rend">
			<xsl:call-template name="rendering"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:apply-templates/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="emph">
	<xsl:choose>
		<xsl:when test="@rend">
			<xsl:call-template name="rendering"/>
		</xsl:when>
		<xsl:otherwise>
			<em><xsl:apply-templates/></em>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="hi">
	<xsl:choose>
		<xsl:when test="@rend">
			<xsl:call-template name="rendering"/>
		</xsl:when>
		<xsl:otherwise>
			<strong><xsl:apply-templates/></strong>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- notes -->
<xsl:template name="notes">
	<div class="notes">
		<xsl:apply-templates select="/TEI.2/text//note" mode="printnotes"/>
	</div>
</xsl:template>

<xsl:template match="note">
	<xsl:variable name="ident">
		<xsl:apply-templates select="." mode="ident"/>
	</xsl:variable>
	<a name="{$ident}"></a>
	<xsl:variable name="identifier">
		<xsl:number level="any" from="front"/>
	</xsl:variable>
	<a name="{concat('Note',$identifier)}" href="{concat($notesFile,concat('#Note',$identifier))}" target="{$rightFrame}"><sup><font size="-1"><xsl:value-of select="$identifier"/></font></sup></a>
</xsl:template>

<xsl:template match="note" mode="printnotes">
	<xsl:variable name="identifier">
		<xsl:number level="any" from="front"/>
	</xsl:variable>
	<xsl:variable name="pointer">
		<xsl:apply-templates mode="xrefheader" select="."/>
	</xsl:variable>
	<p>
		<a class="notes" name="{concat('Note',$identifier)}" href="{$pointer}" target="{$leftFrame}">
			<xsl:value-of select="$identifier"/>
		</a>. 
		<xsl:apply-templates/>
	</p>
</xsl:template>

<!-- verwerken link-elementen  -->
<xsl:template name="linkMaken">
	<xsl:param name="identifier"/>
	<xsl:for-each select="//link">
		<xsl:call-template name="extractLink">
			<xsl:with-param name="identifier" select="$identifier"/>
			<xsl:with-param name="targets" select="@targets"/>
		</xsl:call-template>
	</xsl:for-each>
</xsl:template>

<xsl:template name="extractLink">
	<xsl:param name="identifier"/>
	<xsl:param name="targets"/>
	<xsl:variable name="linkb" select="substring-before($targets,' ')"/>
	<xsl:choose>
		<xsl:when test="$linkb=$identifier">
			(<a>
			<xsl:attribute name="href">
				<xsl:apply-templates mode="xrefheader" select="id(substring-after($targets,' '))"/>
			</xsl:attribute>
			<xsl:attribute name="target">
				<xsl:apply-templates mode="targframe" select="id(substring-after($targets,' '))"/>
			</xsl:attribute>
			<xsl:value-of select="substring-after(ancestor::linkGrp/@targFunc,' ')"/>
			</a>)
		</xsl:when>
		<xsl:otherwise>
			<xsl:variable name="linka" select="substring-after($targets,' ')"/>
			<xsl:if test="$linka=$identifier">
				(<a>
				<xsl:attribute name="href">
					<xsl:apply-templates mode="xrefheader" select="id(substring-before($targets,' '))"/>
				</xsl:attribute>
				<xsl:attribute name="target">
					<xsl:apply-templates mode="targframe" select="id(substring-before($targets,' '))"/>
				</xsl:attribute>
				<xsl:value-of select="substring-before(ancestor::linkGrp/@targFunc,' ')"/>
				</a>)
			</xsl:if>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template mode="targframe" match="div|bibl|biblStruct|l|lg|note|anchor">
	<xsl:variable name="parent">
		<xsl:call-template name="find-parent"/>
	</xsl:variable>
	<xsl:choose>
		<xsl:when test="contains($rightFrameFiles,$parent)">
			<xsl:value-of select="$rightFrame"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$leftFrame"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- quoting -->
<xsl:template match="cit">
  <xsl:apply-templates select="quote"/>
</xsl:template>

<xsl:template match="quote[@rend='display']">
	<blockquote>
		<xsl:apply-templates/>
		<xsl:if test="following-sibling::bibl">
			<div align="right"><font size="-1">(<xsl:apply-templates select="following-sibling::bibl"/>)</font></div>
		</xsl:if>
	</blockquote>
</xsl:template>

<xsl:template match="quote[@rend='quoted']">
	<xsl:text>'</xsl:text><xsl:apply-templates/><xsl:text>' </xsl:text>
	<xsl:if test="following-sibling::bibl">
		<font size="-1">(<xsl:apply-templates select="following-sibling::bibl"/>)</font>
	</xsl:if>
</xsl:template>

<!-- copyOf handling -->
<xsl:template match="l[@copyOf]|lg[@copyOf]">
	<xsl:apply-templates select="id(@copyOf)" mode="aanhaling"/>
</xsl:template>

<xsl:template match="lg" mode="aanhaling">
	<xsl:apply-templates select="l"/>
</xsl:template>

<xsl:template match="l" mode="aanhaling">
	<xsl:apply-templates/>
</xsl:template>

<!-- page breaks -->
<xsl:template match="pb">
	<xsl:choose>
		<xsl:when test="preceding::pb">
			<div align="center">
				<table width="30" border="0">
					<tr>
						<td><xsl:value-of select="substring-before(preceding::pb[position()=1]/@n,'=')"/></td>
					</tr>
				</table>
			</div>			
			<hr/>
			<div align="right">
				<table width="50" columns="2" border="0">
					<tr>
						<td><xsl:value-of select="substring-after(@n,'=')"/></td>
						<td>
							<a href="#top">
								<img src="arrowtop.gif" border="0"/>
							</a>
						</td>
					</tr>
				</table>
			</div>			
		</xsl:when>
	</xsl:choose>
</xsl:template>

<!-- sic elements -->
<xsl:template name="corrList">
	<table width="95%" columns="3" border="1" cellspacing="0">
		<tr>
			<td width="33%"><xsl:text>Regel</xsl:text></td>
			<td width="33%"><xsl:text>Druk 1656</xsl:text></td>
			<td width="34%"><xsl:text>Correctie</xsl:text></td>
		</tr>
		<xsl:for-each select="//sic">
			<xsl:variable name="parent">
				<xsl:call-template name="find-parent"/>
			</xsl:variable>
			<tr>
				<xsl:variable name="lineno">
					<xsl:value-of select="ancestor::l/attribute::id"/>
				</xsl:variable>
				<td width="33%">
					<a name="{@id}" href="{concat(concat($parent,'.htm#'),$lineno)}" target="{$leftFrame}">
						<xsl:value-of select="substring($lineno,2,6)"/>
					</a>
				</td>
				<td width="33%"><xsl:value-of select="."/></td>
				<td width="34%"><xsl:value-of select="@corr"/></td>
			</tr>
		</xsl:for-each>
	</table>
</xsl:template>

<!-- sic elements in text -->
<xsl:template match="sic">
    <xsl:value-of select="@corr"/><a href="{concat(concat($corrFile,'#'),ancestor::l/@id)}" target="{$rightFrame}"><sup><font color="red"><font size="-1">*</font></font></sup></a>
</xsl:template>

<!-- sic elements in apparatus -->
<xsl:template match="sic" mode="printapp">
    <xsl:value-of select="text()"/>
</xsl:template>

<!-- change log -->
<xsl:template name="changes">
	<center>
		<table width="95%" border="1" cols="2" cellspacing="0">
			<tr>
				<td width="15%"><xsl:text>Datum</xsl:text></td>
				<td width="85%"><xsl:text>Wijziging</xsl:text></td>
			</tr>
			<xsl:apply-templates select="/TEI.2/teiHeader/revisionDesc/change"/>
		</table>
	</center>
</xsl:template>

<xsl:template match="change">
	<tr>
		<td width="15%" valign="top">
			<xsl:value-of select="./date"/>
		</td>
		<td width="85%"><xsl:value-of select="./item"/>
		</td>
	</tr>
</xsl:template>

<!-- index of proper names -->
<xsl:template name="ixnaam">
	<center>
		<table width="95%" border="0" cols="2" cellspacing="0">
			<tr>
				<td width="30%"><b><xsl:text>Naam</xsl:text></b></td>
				<td width="70%"><b><xsl:text>Komt voor in:</xsl:text></b></td>
			</tr>
			<xsl:for-each select="//name[ancestor::text]">
				<xsl:sort select="@reg"/>
				<tr>
					<td width="30%" valign="top">
						<xsl:value-of select="@reg"/>
					</td>
					<td width="70%" valign="top">
						<xsl:variable name="divident" select="./ancestor::div[position()=1]/@id"/>
						<xsl:choose>
							<xsl:when test="ancestor::note">
								<xsl:apply-templates mode="header" select="id($divident)"/>
								<xsl:text>: </xsl:text>
								<xsl:apply-templates mode="index" select="ancestor::note"/>
							</xsl:when>
							<xsl:when test="ancestor::biblStruct">
								<xsl:apply-templates mode="header" select="id($divident)"/>
								<xsl:text>: </xsl:text>
								<br/>
								<a>
									<xsl:attribute name="href">
										<xsl:apply-templates mode="xrefheader" select="ancestor::biblStruct"/>
									</xsl:attribute>
									<xsl:apply-templates mode="header" select="ancestor::biblStruct"/>
								</a>
							</xsl:when>
							<xsl:otherwise>
								<a>
									<xsl:attribute name="href">
										<xsl:apply-templates mode="xrefheader" select="."/>
									</xsl:attribute>
									<xsl:apply-templates mode="header" select="id($divident)"/>
								</a>
							</xsl:otherwise>
						</xsl:choose>
					</td>
				</tr>
			</xsl:for-each>
		</table>
	</center>
</xsl:template>

<xsl:template match="note" mode="index">
	<xsl:variable name="identifier">
		<xsl:number level="any" from="front"/>
	</xsl:variable>
	<a href="{concat($notesFile,concat('#Note',$identifier))}" target="{$rightFrame}">
		<xsl:text>Noot </xsl:text>
		<xsl:value-of select="$identifier"/>
	</a>
</xsl:template>

<xsl:template match="name">
	<xsl:variable name="ident">
		<xsl:apply-templates select="." mode="ident"/>
	</xsl:variable>
	<a name="{$ident}"></a><xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>


