<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xlink="http://www.w3.org/1999/xlink"
	xmlns:mods="http://www.loc.gov/mods/v3"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	exclude-result-prefixes="mods">
	<xsl:output method="html" indent="yes" encoding="UTF-8"
		doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
		doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />
	<!-- MODS3 records to html -->

	<xsl:variable name="dictionary"
		select="document('http://www.loc.gov/standards/mods/modsDictionary.xml')/dictionary" />

	<xsl:template match="/">

		<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
				<title>
					<xsl:value-of select="mods:mods/mods:titleInfo" />
				</title>
				<style type="text/css">
					TD { vertical-align:top; margin-top:0em;} 
					.level1 { margin-top:1em; font-weight: bold; } 
					.value1 { margin-top:1em; }
					.value { }
					.level2 { margin-left:1em; font-style:italic; }
					.level3 { margin-left:2em; font-style:italic; }
					.level4 { margin-left:3em; font-style:italic; }
				</style>
			</head>
			<body>
				<h2>
					MODS Record:
				</h2>
				<h1>
					<xsl:value-of select="mods:mods/mods:titleInfo" />
				</h1>
				<xsl:choose>
					<xsl:when test="mods:modsCollection">
						<xsl:apply-templates
							select="mods:modsCollection" />
					</xsl:when>
					<xsl:when test="mods:mods">
						<xsl:apply-templates select="mods:mods" />
					</xsl:when>
				</xsl:choose>
			</body>
		</html>
	</xsl:template>

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

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

	<xsl:template match="*">

		<xsl:choose>

			<xsl:when test="child::*">
				<tr>
					<td colspan="2">
						<div class="level1">
							<xsl:call-template name="longName">
								<xsl:with-param name="name">
									<xsl:value-of select="local-name()" />
								</xsl:with-param>
							</xsl:call-template>

							<xsl:call-template name="attr" />
						</div>
					</td>
				</tr>
				<xsl:apply-templates mode="level2" />
			</xsl:when>

			<xsl:otherwise>
				<tr>
					<td width="300px">
						<div class="level1">
							<xsl:call-template name="longName">
								<xsl:with-param name="name">
									<xsl:value-of select="local-name()" />
								</xsl:with-param>
							</xsl:call-template>

							<xsl:call-template name="attr" />
						</div>
					</td>
					<td>
						<div class="value1">
							<xsl:call-template name="formatValue" />
						</div>
					</td>
				</tr>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="formatValue">

		<xsl:choose>

			<xsl:when test="@type='uri'">
					<a href="{text()}">
						<xsl:value-of select="text()" />
					</a>
			</xsl:when>

			<xsl:otherwise>
					<xsl:value-of select="text()" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="*" mode="level2">

		<xsl:choose>

			<xsl:when test="child::*">
				<tr>
					<td colspan="2">
						<div class="level2">
							<xsl:call-template name="longName">
								<xsl:with-param name="name">
									<xsl:value-of select="local-name()" />
								</xsl:with-param>
							</xsl:call-template>

							<xsl:call-template name="attr" />
						</div>
					</td>
				</tr>
				<xsl:apply-templates mode="level3" />
			</xsl:when>

			<xsl:otherwise>
				<tr>
					<td>
						<div class="level2">
							<xsl:call-template name="longName">
								<xsl:with-param name="name">
									<xsl:value-of select="local-name()" />
								</xsl:with-param>
							</xsl:call-template>

							<xsl:call-template name="attr" />
						</div>
					</td>
					<td>
						<div class="value">
							<xsl:call-template name="formatValue" />
						</div>
					</td>
				</tr>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="*" mode="level3">

		<xsl:choose>

			<xsl:when test="child::*">
				<tr>
					<td colspan="2">
						<div class="level3">
							<xsl:call-template name="longName">
								<xsl:with-param name="name">
									<xsl:value-of select="local-name()" />
								</xsl:with-param>
							</xsl:call-template>

							<xsl:call-template name="attr" />
						</div>
					</td>
				</tr>
				<xsl:apply-templates mode="level4" />
			</xsl:when>

			<xsl:otherwise>
				<tr>
					<td>
						<div class="level3">
							<xsl:call-template name="longName">
								<xsl:with-param name="name">
									<xsl:value-of select="local-name()" />
								</xsl:with-param>
							</xsl:call-template>

							<xsl:call-template name="attr" />
						</div>
					</td>
					<td>
						<div class="value">
							<xsl:call-template name="formatValue" />
						</div>
					</td>
				</tr>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="*" mode="level4">
		<tr>
			<td>
						<div class="level4">
					<xsl:call-template name="longName">
						<xsl:with-param name="name">
							<xsl:value-of select="local-name()" />
						</xsl:with-param>
					</xsl:call-template>

					<xsl:call-template name="attr" />
				</div>
			</td>
			<td>
				<div class="value">
					<xsl:call-template name="formatValue" />
				</div>
			</td>
		</tr>
	</xsl:template>


	<xsl:template name="longName">
		<xsl:param name="name" />

		<xsl:choose>

			<xsl:when test="$dictionary/entry[@key=$name]">
				<xsl:value-of select="$dictionary/entry[@key=$name]" />
			</xsl:when>

			<xsl:otherwise>
				<xsl:value-of select="$name" />
			</xsl:otherwise>

		</xsl:choose>

	</xsl:template>

	<xsl:template name="attr">

		<xsl:for-each select="@type|@point">
			:
			<xsl:call-template name="longName">
				<xsl:with-param name="name">
					<xsl:value-of select="." />
				</xsl:with-param>
			</xsl:call-template>
		</xsl:for-each>

		<xsl:if test="@authority or @edition">

			<xsl:for-each select="@authority">
				(
				<xsl:call-template name="longName">
					<xsl:with-param name="name">
						<xsl:value-of select="." />
					</xsl:with-param>
				</xsl:call-template>
			</xsl:for-each>
			<xsl:if test="@edition">
				Edition
				<xsl:value-of select="@edition" />
			</xsl:if>
			)
		</xsl:if>

		<xsl:variable name="attrStr">

			<xsl:for-each
				select="@*[local-name()!='edition' and local-name()!='type' and local-name()!='authority' and local-name()!='point']">

				<xsl:value-of select="local-name()" />
				="
				<xsl:value-of select="." />
				",
			</xsl:for-each>
		</xsl:variable>

		<xsl:variable name="nattrStr"
			select="normalize-space($attrStr)" />

		<xsl:if test="string-length($nattrStr)">
			(
			<xsl:value-of
				select="substring($nattrStr,1,string-length($nattrStr)-1)" />
			)
		</xsl:if>
	</xsl:template>

</xsl:stylesheet>
