<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

<xsl:output method="text" encoding="utf-8"/>

<xsl:strip-space elements="authors editors"/>

<xsl:param name="showall" select="0"/>
<xsl:param name="showlocalfile" select="0"/>
<xsl:variable name="tab" select="'&#9;'"/>
<xsl:variable name="space" select="'&#32;'"/>
<xsl:variable name="newline" select="'&#10;'"/>
<xsl:variable name="liopen" select="'&lt;li&gt;'"/>
<xsl:variable name="liclose" select="'&lt;/li&gt;'"/>
<xsl:variable name="emopen" select="'&lt;em&gt;'"/>
<xsl:variable name="emclose" select="'&lt;/em&gt;'"/>
<xsl:variable name="strongopen" select="'&lt;strong&gt;'"/>
<xsl:variable name="strongclose" select="'&lt;/strong&gt;'"/>

<xsl:template match="bibliography">
	<xsl:variable name="templatecode" select="document($template)"/>
	<xsl:copy-of select="$templatecode/template/headcode/node()"/>
	<xsl:apply-templates select="article[@a1='yes']"/>
	<xsl:copy-of select="$templatecode/template/section1code/node()"/>
	<xsl:apply-templates select="article[not(@a1='yes')]"/>
	<xsl:copy-of select="$templatecode/template/section2code/node()"/>
	<xsl:apply-templates select="incollection"/>
	<xsl:copy-of select="$templatecode/template/section3code/node()"/>
	<xsl:apply-templates select="inproceedings[@p1='yes' and not(@abstract='yes')]"/>
	<xsl:copy-of select="$templatecode/template/section4code/node()"/>
	<xsl:apply-templates select="inproceedings[not(@p1='yes') and not(@abstract='yes')]"/>
	<xsl:copy-of select="$templatecode/template/section5code/node()"/>
	<xsl:apply-templates select="inproceedings[@abstract='yes']"/>
	<xsl:copy-of select="$templatecode/template/section6code/node()"/>
	<xsl:apply-templates select="inproceedings[@participation='yes']|atconference" mode="participation"/>
	<xsl:copy-of select="$templatecode/template/footcode/node()"/>
</xsl:template>

<xsl:template match="article">
	<xsl:if test="not(@show='no') or $showall">
		<xsl:value-of select="concat($newline, '          ', $liopen)"/>
		<xsl:apply-templates select="author"/>
		<xsl:apply-templates select="authors"/>
		<xsl:apply-templates select="title"/>
		<xsl:if test="./child::journal">
			<xsl:text>, </xsl:text>
			<xsl:value-of select="$emopen"/>
			<xsl:apply-templates select="journal"/>
			<xsl:value-of select="$emclose"/>
		</xsl:if>
		<xsl:if test="./child::volume">
			<xsl:text> </xsl:text>
			<xsl:value-of select="$strongopen"/>
			<xsl:apply-templates select="volume"/>
			<xsl:value-of select="$strongclose"/>
			<xsl:text>, </xsl:text>
			<xsl:apply-templates select="number"/>
		</xsl:if>
		<xsl:if test="./child::year">
			<xsl:text> (</xsl:text>
			<xsl:apply-templates select="year"/>
			<xsl:text>)</xsl:text>
		</xsl:if>
		<xsl:if test="./child::pages">
			<xsl:text> </xsl:text>
			<xsl:apply-templates select="pages"/>
		</xsl:if>
		<xsl:apply-templates select="note"/>
		<xsl:text>.</xsl:text>
		<xsl:if test="./child::file and $showlocalfile">
			<xsl:text>&lt;br />[</xsl:text>
			<xsl:text>&lt;a href="</xsl:text>
				<xsl:apply-templates select="file"/>
			<xsl:text>"></xsl:text>
			<xsl:text>file&lt;/a>]</xsl:text>
		</xsl:if>
		<xsl:value-of select="$liclose"/>
	</xsl:if>
</xsl:template>

<xsl:template match="incollection|inproceedings">
	<xsl:if test="not(@show='no') or $showall">
		<xsl:value-of select="concat($newline, '          ', $liopen)"/>
		<xsl:apply-templates select="author"/>
		<xsl:apply-templates select="authors"/>
		<xsl:apply-templates select="title"/>
		<xsl:text>, in: </xsl:text>
		<xsl:value-of select="$emopen"/>
		<xsl:choose>
			<xsl:when test="./child::booktitle">
				<xsl:apply-templates select="booktitle"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>Proceedings of the </xsl:text>
				<xsl:apply-templates select="conference"/>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:value-of select="$emclose"/>
		<xsl:if test="./child::editor|editors">
			<xsl:text> (</xsl:text>
			<xsl:apply-templates select="editor"/>
			<xsl:apply-templates select="editors"/>
			<xsl:choose>
				<xsl:when test="./child::editor[2]|./child::editors/editor[2]">
					<xsl:text>, eds.)</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>, ed.)</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
		<xsl:apply-templates select="publisher"/>
		<xsl:apply-templates select="address"/>
		<xsl:text>, </xsl:text>
		<xsl:apply-templates select="year"/>
		<xsl:if test="./child::pages">
			<xsl:text>, pp. </xsl:text>
			<xsl:apply-templates select="pages"/>
		</xsl:if>
		<xsl:if test="./child::note">
			<xsl:apply-templates select="note"/>
		</xsl:if>
		<xsl:text>.</xsl:text>
		<xsl:if test="./child::file and $showlocalfile">
			<xsl:text>&lt;br />[</xsl:text>
			<xsl:text>&lt;a href="</xsl:text>
				<xsl:apply-templates select="file"/>
			<xsl:text>"></xsl:text>
			<xsl:text>file&lt;/a>]</xsl:text>
		</xsl:if>
		<xsl:value-of select="$liclose"/>
	</xsl:if>
</xsl:template>

<xsl:template match="inproceedings|atconference" mode="participation">
	<xsl:variable name="tagname" select="name()"/>
	<xsl:variable name="conferencename" select="./conference[text()]"/>
	<xsl:if test="not(@show='no') or $showall">
		<xsl:if test="not(./preceding-sibling::*[1][name() = $tagname] and ./preceding-sibling::*[1]/conference[text() = $conferencename])">
			<xsl:value-of select="concat($newline, '          ', $liopen)"/>
			<xsl:apply-templates select="conference"/>
			<xsl:text>, </xsl:text>
			<xsl:apply-templates select="location"/>
			<xsl:text>, </xsl:text>
			<xsl:apply-templates select="day"/>
			<xsl:text> </xsl:text>
			<xsl:apply-templates select="month"/>
			<xsl:text> </xsl:text>
			<xsl:apply-templates select="year"/>
			<xsl:apply-templates select="session"/>
		</xsl:if>
		<xsl:choose>
			<!-- TODO: check if this works when 3 papers are presented -->
			<xsl:when test="./preceding-sibling::*[1][name() = $tagname] and ./preceding-sibling::*[1]/conference[text() = $conferencename]">
				<xsl:text>, "</xsl:text>
				<xsl:apply-templates select="title" mode="participation"/>
				<xsl:text>"</xsl:text>
			</xsl:when>
			<xsl:when test="./following-sibling::*[1][name() = $tagname] and ./following-sibling::*[1]/conference[text() = $conferencename] and ./following-sibling::*[1][@participation='yes']">
				<xsl:text>, paper presentations "</xsl:text>
				<xsl:apply-templates select="title" mode="participation"/>
				<xsl:text>"</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:if test="./title">
					<xsl:text>, paper presentation "</xsl:text>
					<xsl:apply-templates select="title" mode="participation"/>
					<xsl:text>"</xsl:text>
				</xsl:if>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:if test="not(./following-sibling::*[1][name() = $tagname] and ./following-sibling::*[1]/conference[text() = $conferencename] and ./following-sibling::*[1][@participation='yes'])">
			<xsl:text>.</xsl:text>
			<xsl:value-of select="$liclose"/>
		</xsl:if>
	</xsl:if>
</xsl:template>

<xsl:template match="authors">
	<xsl:apply-templates select="author"/>
</xsl:template>

<xsl:template match="editors">
	<xsl:apply-templates select="editor"/>
</xsl:template>

<xsl:template match="author|editor">
	<xsl:variable name="tagname" select="name()"/>
	<xsl:if test="not(@id)">
		<xsl:if test="./preceding-sibling::*[name() = $tagname]">
			<xsl:choose>
				<xsl:when test="not(./following-sibling::*[name() = $tagname])">
					<xsl:text> and </xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>, </xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:if>
	<xsl:choose>
		<xsl:when test="not(@link)">
			<xsl:call-template name="printvalue"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:variable name="link" select="@link"/>
			<xsl:apply-templates select="/bibliography/author[@id=$link]"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="printvalue">
	<xsl:choose>
		<xsl:when test="@xml:lang">
			<xsl:text>&lt;span xml:lang="</xsl:text>
				<xsl:value-of select="@xml:lang"/>
			<xsl:text>"></xsl:text>
			<xsl:call-template name="printvalue2"/>
			<xsl:text>&lt;/span></xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template name="printvalue2"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="printvalue2">
	<xsl:choose>
		<xsl:when test="@url">
			<xsl:text>&lt;a href="</xsl:text>
				<xsl:value-of select="@url"/>
			<xsl:text>"></xsl:text>
			<xsl:apply-templates/>
			<xsl:text>&lt;/a></xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="name() = 'author' or name() = 'editor'">
					<xsl:apply-templates select="firstname"/>
					<xsl:text> </xsl:text>
					<xsl:apply-templates select="lastname"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="title|journal|booktitle|conference|publisher|address|note">
	<xsl:variable name="tagname" select="name()"/>
	<xsl:if test="not(name() = 'journal' or name() = 'booktitle' or name() = 'conference' or @id)">
		<xsl:text>, </xsl:text>
	</xsl:if>
	<xsl:choose>
		<xsl:when test="not(@link)">
			<xsl:call-template name="printvalue"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:variable name="link" select="@link"/>
			<xsl:apply-templates select="/bibliography/*[name()=$tagname and @id=$link]"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="title" mode="participation">
	<xsl:call-template name="printvalue"/>
</xsl:template>

<xsl:template match="volume|number|year|pages">
	<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="month">
	<xsl:choose>
		<xsl:when test="text() = 1"><xsl:text>January</xsl:text></xsl:when>
		<xsl:when test="text() = 2"><xsl:text>February</xsl:text></xsl:when>
		<xsl:when test="text() = 3"><xsl:text>March</xsl:text></xsl:when>
		<xsl:when test="text() = 4"><xsl:text>April</xsl:text></xsl:when>
		<xsl:when test="text() = 5"><xsl:text>May</xsl:text></xsl:when>
		<xsl:when test="text() = 6"><xsl:text>June</xsl:text></xsl:when>
		<xsl:when test="text() = 7"><xsl:text>July</xsl:text></xsl:when>
		<xsl:when test="text() = 8"><xsl:text>August</xsl:text></xsl:when>
		<xsl:when test="text() = 9"><xsl:text>September</xsl:text></xsl:when>
		<xsl:when test="text() = 10"><xsl:text>October</xsl:text></xsl:when>
		<xsl:when test="text() = 11"><xsl:text>November</xsl:text></xsl:when>
		<xsl:when test="text() = 12"><xsl:text>December</xsl:text></xsl:when>
		<xsl:otherwise>
			<xsl:apply-templates/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="session">
	<xsl:choose>
		<xsl:when test="@coorganized='yes' and @chaired='yes'">
			<xsl:text>, co-organizer and chair of the </xsl:text>
		</xsl:when>
		<xsl:when test="@organized='yes' and @chaired='yes'">
			<xsl:text>, organizer and chair of the </xsl:text>
		</xsl:when>
		<xsl:when test="@coorganized='yes' and not(@chaired='yes')">
			<xsl:text>, co-organizer of the </xsl:text>
		</xsl:when>
		<xsl:when test="@organized='yes' and not(@chaired='yes')">
			<xsl:text>, organizer of the </xsl:text>
		</xsl:when>
		<xsl:when test="not(@coorganized='yes') and not(@organized='yes') and @chaired='yes'">
			<xsl:text>, chair of the </xsl:text>
		</xsl:when>
	</xsl:choose>
	<xsl:text>"</xsl:text>
	<xsl:apply-templates/>
	<xsl:text>"</xsl:text>
</xsl:template>

<xsl:template match="Lstar"><xsl:text>L*</xsl:text></xsl:template>
<xsl:template match="LI"><xsl:text>L</xsl:text><sup>I</sup></xsl:template>
<xsl:template match="aacute"><xsl:text>&#225;</xsl:text></xsl:template>
<xsl:template match="eacute"><xsl:text>&#233;</xsl:text></xsl:template>
<xsl:template match="iacute"><xsl:text>&#237;</xsl:text></xsl:template>
<xsl:template match="nacute"><xsl:text>&#324;</xsl:text></xsl:template>
<xsl:template match="oacute"><xsl:text>&#243;</xsl:text></xsl:template>
<xsl:template match="uacute"><xsl:text>&#250;</xsl:text></xsl:template>
<xsl:template match="yacute"><xsl:text>&#253;</xsl:text></xsl:template>
<xsl:template match="uuml"><xsl:text>&#252;</xsl:text></xsl:template>
<xsl:template match="atilde"><xsl:text>&#227;</xsl:text></xsl:template>
<xsl:template match="Scaron"><xsl:text>&#352;</xsl:text></xsl:template>
<xsl:template match="ccaron"><xsl:text>&#269;</xsl:text></xsl:template>
<xsl:template match="ecaron"><xsl:text>&#283;</xsl:text></xsl:template>
<xsl:template match="ml"><xsl:text>l'</xsl:text></xsl:template>
<xsl:template match="LL"><xsl:text>&#321;</xsl:text></xsl:template>
<xsl:template match="ll"><xsl:text>&#322;</xsl:text></xsl:template>
<xsl:template match="ndash"><xsl:text>&#8211;</xsl:text></xsl:template>
<xsl:template match="mdash"><xsl:text>&#8212;</xsl:text></xsl:template>
<xsl:template match="lsquo"><xsl:text>&#8216;</xsl:text></xsl:template>
<xsl:template match="rsquo"><xsl:text>&#8217;</xsl:text></xsl:template>
<xsl:template match="ldquo"><xsl:text>&#8220;</xsl:text></xsl:template>
<xsl:template match="rdquo"><xsl:text>&#8221;</xsl:text></xsl:template>
<xsl:template match="newline"><xsl:value-of select="$newline"/></xsl:template>

</xsl:stylesheet>

