<?xml version="1.0" encoding="UTF-8"?>
<!--
By: Neil Salmond
On: Summer 2004
To: output a simple user dictionary Ega-French based on the ega lexicon xml file
-->

<!-- this is a stylesheet -->
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:field="http://www.emeld.org/field/namespace"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- This stylesheet outputs xhtml (which is a dialect of xml) in unicode. In fact, this line puts the xhtml strict doctype into the output file, namely <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> -->
<xsl:output method="xml"
encoding="utf-8"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
indent="yes"
omit-xml-declaration="no"
media-type="application/xhtml+xml" />

<!-- The following line means 'begin at the beginning' - the slash indicates the root element of the xml document.  This is kind of like an initialisation subroutine. -->
<xsl:template match="/">
	<!-- some regular html -->
	<html>
		<head>
			<title>Ega Lexicon - Non Linguist</title>
			<!-- some styles to make it look pretty -->
			<style type="text/css" media="screen">
				body {
				font-family: "Arial Unicode MS", "Lucida Sans Unicode";
				color:#666699;
				}
				
				.ega {
				font-weight: bold;
				}
				
				.pos {
				font-style: italic;
				}
				
				.french {
				font-size: 70%;
				}
				
				h1{
				font-size: large;
				font-weight: bold;
				}
				
				h2{
				font-size: small;
				font-weight: bold;				
				}
				
				.lexeme{
				margin: 1em;
				}
			</style>
			<meta http-equiv="Content-type" content="text/html; charset=utf-8" />			
		</head>
		<body>
			<h1>Ega</h1>
			<h2>Field Linguists: Dafydd Gibbon (Bielefeld), Bruce Connell (Oxford), and Firmin Ahoua (Cocody)</h2>
			<!-- With the following line, the xsl parser will look at the current node in the xml and see if there are any templates below that match that node -->
			<xsl:apply-templates />	
		</body>
	</html>		
</xsl:template>

<!-- This template tells the parser what to do with each <lexeme /> element -->
<xsl:template match="lexeme">
	<!-- Each lexeme should have an ega form, a part of speech and a french form.  However the xml data are somewhat incomplete, and we want this output to look nice 'n' pretty, so we have to check we've got all three before displaying any of an entry. -->
	<!-- We therefore create three variables -->
	<xsl:variable name="ega">
		<!-- the template 'tidytext' works like subroutine that replaces some xsampa characters with their ipa equivalents and trims whitespace -->
		<xsl:call-template name="tidytext">
			<!-- pass this lexeme's <orthography /> element's text content to the tidytext template as a parameter -->
			<xsl:with-param name="tidyme" select="descendant::orthography/text()" />
			<!-- also pass the parameter 'translate' set to 'true' because we want to translate xsampa to ipa -->
			<xsl:with-param name="translate" select="'true'" />
		</xsl:call-template>
	</xsl:variable>
	<xsl:variable name="pos">
		<xsl:call-template name="tidytext">
			<!-- the xpath in this parameter finds a descendant <POS type="traditional" /> element's content -->
			<xsl:with-param name="tidyme" select="descendant::POS[@type='TRADITIONAL']/text()" />
		</xsl:call-template>
	</xsl:variable>
	<xsl:variable name="french">
		<xsl:call-template name="tidytext">
		    <!-- the xpath in this parameter finds a descendant <gloss xml:lang="fr" /> element's content -->
			<xsl:with-param name="tidyme" select="descendant::gloss[@xml:lang='fr']/text()" />
		</xsl:call-template>
	</xsl:variable>
	
	<xsl:choose>		
		<!-- if it's all there, then we'll display it -->
		<xsl:when test="$ega !='' and $pos !='' and $french !=''">
			<!-- This is the xsl way to create html, which we have to do because the id attribute is a variable.-->
			<!-- note that the spans inside are literal, however
			<div class="lexeme" id="[someuniquekey]">
				<span class="ega">[egatext]</span>
				etc.
			</div>
			-->
			<xsl:element name="div">
				<xsl:attribute name="class">lexeme</xsl:attribute>
				<xsl:attribute name="id"><xsl:text>lex</xsl:text><xsl:call-template name="tidytext"><xsl:with-param name="tidyme" select="uniqueKey/text()" /></xsl:call-template></xsl:attribute>
				<span class="ega"><xsl:value-of select="$ega" /></span>
				<span class="pos"><xsl:value-of select="$pos" /></span>
				<span class="french"><xsl:value-of select="$french" /></span>
			</xsl:element>
		</xsl:when>
		
	</xsl:choose>
</xsl:template>

<!-- This template recursively matches all elements in the xml file, and then does nothing.  However, the other templates in this stylesheet match elements directly and the parser will apply the most specific ones it finds.  The upshot of this is that the parser won't display anything - unless we so, as above -->
<xsl:template match="*">
	<xsl:apply-templates select="*" />
</xsl:template>

<!-- This is the 'subroutine' template that tidies up the text -->
<xsl:template name="tidytext">
	<!-- it takes two optional parameters that default to '' -->
	<xsl:param name="tidyme" select="''" />
	<xsl:param name="translate" select="''" />

	<xsl:choose>
		<!-- if we want to replace sampa with ipa... -->
		<xsl:when test="$translate != ''">
			<!-- Do the conversion and pass the converted string back into this subroutine -->
			<xsl:call-template name="tidytext">
				<xsl:with-param name="tidyme" select="translate($tidyme,'IUEOJN ','&#618;&#650;&#603;&#596;&#658;&#331;')" />
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<!-- 'normalize-space' basically trims a string -->
			<xsl:variable name="trimmed" select="normalize-space($tidyme)" />
			<xsl:variable name="start" select="substring($trimmed,1,1)" />
			<xsl:variable name="end" select="substring($trimmed,string-length($trimmed),string-length($trimmed))" />
			<xsl:choose>
				<!-- if the resultant string starts and ends with '_' then it's not real data, it's a placeholder such as _PRONIPAKIEL_ -->
				<xsl:when test="$start='_' and $end='_'"><!-- we have no real data --></xsl:when>
				<!-- otherwise we're good, so output the value of the trimmed string -->
				<xsl:otherwise>
					<xsl:value-of select="$trimmed" />
				</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>
