<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" />

<xsl:key name="person" match="person" use="@id"/>
<xsl:key name="org" match="org" use="@id"/>
<xsl:key name="pub" match="pub" use="@id"/>
<xsl:key name="event" match="event" use="@id"/>
<xsl:key name="journal" match="journal" use="@id"/>

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

<xsl:template match="cv"><xsl:apply-templates select="pubs" /></xsl:template>

<xsl:template name="org">
	<xsl:for-each select="key('org', @org)">
		<xsl:value-of select="name" />
		<xsl:for-each select="location">, <xsl:value-of select="text()" />
		</xsl:for-each>
		<xsl:for-each select="note"><xsl:text> </xsl:text><xsl:value-of select="text()" />
		</xsl:for-each>
	</xsl:for-each>
</xsl:template>

<xsl:template match="author" name="author">
	<xsl:variable name="authorElem" select="." />
	<xsl:for-each select="key('person', @person)">
		<xsl:choose>
			<xsl:when test="$authorElem/@as">
				<xsl:value-of select="$authorElem/@as" />
			</xsl:when>
			<xsl:when test="citeName">
				<xsl:value-of select="citeName" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="name" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:for-each>
</xsl:template>

<xsl:template match="literal">{<xsl:value-of select="text()" />}</xsl:template>

<xsl:template match="pub">
	<xsl:if test="not(../dontShow)">
	<xsl:for-each select="report">@TechReport</xsl:for-each><xsl:for-each select="proceedings">@InProceedings</xsl:for-each><xsl:for-each select="journal">@Article</xsl:for-each>{<xsl:value-of select="@id" />,
author = {<xsl:for-each select="author[1]"><xsl:call-template name="author" /></xsl:for-each><xsl:for-each select="author[position() > 1]"> and <xsl:call-template name="author" /></xsl:for-each>},
title = {<xsl:for-each select="title"><xsl:apply-templates /></xsl:for-each>},
<xsl:for-each select="journal">volume = {<xsl:value-of select="@volume"/>},
pages = {<xsl:value-of select="@pages"/>},
year = {<xsl:value-of select="@year"/>},
<xsl:for-each select="key('journal', @journal)">journal = {<xsl:value-of select="name" />},</xsl:for-each></xsl:for-each><xsl:for-each select="proceedings"><xsl:for-each select="key('event', @event)">booktitle = {<xsl:value-of select="abbrev" />: Proceedings of the <xsl:value-of select="name" />},
month = <xsl:value-of select="month/@month" />,
year = {<xsl:value-of select="year" />},
location = {<xsl:value-of select="location" />},</xsl:for-each></xsl:for-each><xsl:for-each select="report">type = {{Technical Report}},
number = {<xsl:value-of select="../report" />},
<xsl:for-each select="../atOrg"><xsl:for-each select="key('org', @org)">institution = {<xsl:value-of select="name" />},</xsl:for-each></xsl:for-each>
year = {<xsl:value-of select="../year" />},
</xsl:for-each><xsl:for-each select="status">
note = {<xsl:value-of select="text()"/>},</xsl:for-each>
url = {http://adam.chlipala.net/papers/<xsl:value-of select="@id" />/}
}</xsl:if></xsl:template>

</xsl:stylesheet>
