<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:cinclude="http://apache.org/cocoon/include/1.0"
		xmlns:zip="http://apache.org/cocoon/zip-archive/1.0">

<!-- gds.xsl: pull together html display for indexing
                @author <a href="http://librarycog.uwindsor.ca">art rhyno</a>
-->

<xsl:param name="next_url"/>
<xsl:param name="gds_url"/>
<xsl:param name="bib_url"/>

<xsl:include href="common.xsl"/>

<xsl:template match="text()"/>

<xsl:template match="/records">
	<xsl:if test="count(//controlfield[@tag = '001']) &gt; 0">
		<zip:archive>
		<xsl:for-each select="record">

			<xsl:variable name="bibid">
				<xsl:value-of select="controlfield[@tag = '001']"/>
			</xsl:variable>

			<xsl:variable name="theTitle">
			<xsl:for-each select="datafield[@tag = '245']/subfield">
				<xsl:value-of select="."/>
				<xsl:if test="not(position() = last())">
					<xsl:text> </xsl:text>
				</xsl:if>
			</xsl:for-each>
			</xsl:variable>

			<zip:entry serializer="html">

			<xsl:attribute name="name">
			<xsl:value-of select="$gds_url"/>
			<xsl:text>/</xsl:text>
			<xsl:value-of select="$bibid"/>
			<xsl:text>.html</xsl:text>
			</xsl:attribute>

        		<html>
                	<head>
                        <title>Library-
                        <xsl:value-of select="$bibid"/></title>
                	</head>
                	<body>
                	<h3><xsl:value-of select="$theTitle"/></h3>

			<xsl:if test="count(datafield[starts-with(@tag,'1') and string-length(@tag) = 3]) &gt; 0">
			<xsl:text> </xsl:text>
			<h4>
			<xsl:for-each select="datafield[starts-with(@tag,'1') and string-length(@tag) = 3]">
			<xsl:for-each select="subfield">
				<xsl:if test="not(contains($theTitle,.)) and not(@code='d')">
				<xsl:value-of select="."/>
				<xsl:if test="not(position() = last())">
					<xsl:text> </xsl:text>
				</xsl:if>
				</xsl:if>
			</xsl:for-each>
			<br/>
			</xsl:for-each>
			</h4>
			</xsl:if>

			<!-- notes fields -->
			<xsl:call-template name="deal-with-tags">
                	<xsl:with-param name="theTag"
                        	select="'5'"/>
                	<xsl:with-param name="theSep"
                        	select="' '"/>
                	<xsl:with-param name="theLabel"
                        	select="'Notes: '"/>
			</xsl:call-template>

			<!-- subject fields -->
			<xsl:call-template name="deal-with-tags">
                	<xsl:with-param name="theTag"
                        	select="'6'"/>
                	<xsl:with-param name="theSep"
                        	select="' --- '"/>
                	<xsl:with-param name="theLabel"
                        	select="'Subjects: '"/>
			</xsl:call-template>

			<!-- additional access fields -->
			<xsl:call-template name="deal-with-tags">
                	<xsl:with-param name="theTag"
                        	select="'7'"/>
                	<xsl:with-param name="theSep"
                        	select="' '"/>
                	<xsl:with-param name="theLabel"
                        	select="'Misc: '"/>
			</xsl:call-template>

			<xsl:variable name="pubdate">
			<xsl:value-of 
			select="substring(controlfield[@tag='008'],8,4)"/>
			</xsl:variable>

			<xsl:if test="number($pubdate) &gt; 0">
			<date>
				<xsl:value-of select="$pubdate"/>
			</date>
			</xsl:if>

			<xsl:if test="not($next_url = '0')">
			<xsl:if test="position() = last()">
				<status>
                        	<xsl:value-of select="$gds_url"/>
				<xsl:text>/index-</xsl:text>
                        	<xsl:value-of select="$next_url"/>
				</status>
			</xsl:if>
			</xsl:if>
                	</body>
        		</html>

			</zip:entry>
		</xsl:for-each>
		</zip:archive>
	</xsl:if>
</xsl:template>

<xsl:template name="deal-with-260">
	<tr>
	<td>
	<i>
	<xsl:value-of select="datafield[@tag='260']/subfield[@code='a']"/>
	<xsl:text> </xsl:text>
	<xsl:value-of select="datafield[@tag='260']/subfield[@code='b']"/>
	<xsl:text> </xsl:text>
	<xsl:value-of select="datafield[@tag='260']/subfield[@code='c']"/>
	</i>
	</td>
	</tr>
</xsl:template>

<xsl:template name="deal-with-tags">
	<xsl:param name="theTag"/>
	<xsl:param name="theSep"/>
	<xsl:param name="theLabel"/>

	<xsl:variable name="numTags">
		<xsl:value-of select="count(datafield[starts-with(@tag,$theTag) and string-length(@tag) = 3])"/>
	</xsl:variable>

	<xsl:if test="number($numTags) &gt; 0">
		<strong>
			<xsl:value-of select="$theLabel"/>
		</strong>
		<xsl:for-each select="datafield[starts-with(@tag,$theTag) and string-length(@tag) = 3]">
			<xsl:for-each select="subfield">
				<xsl:value-of select="."/>
				<xsl:if test="not(position() = last())">
					<xsl:value-of select="$theSep"/>
				</xsl:if>
			</xsl:for-each>
			<xsl:if test="number($numTags) &gt; 1 and not(position() = last())">
				<br/>
			</xsl:if>
		</xsl:for-each>
	</xsl:if>
</xsl:template>

</xsl:stylesheet>
