<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<!-- This code transform <daogrp> elements into HTML based on an interpretation of XLink as applied to EAD Version 2002.  
This interpretation is laid out in the document "Mapping XLink to HTML in EAD version 2002" by Mark Carlson 
http://staff.washington.edu/carlsonm/XLinkToEAD.pdf

It currently only supports the use of standard hyperlinks and <img> tag links.  

This code currently only processes <daogrp>, <daoloc> (including <daodesc>), <arc>, and <resource> elements.
If you use <daodesc>, this template only processes it in these circumstances:  
1) It must be a child element of <daoloc>, not <daogrp>
2) It is only displayed on the first traversal when the "show" attribute of <arc> is set to "embed". 
The reason for this is that you can't send caption information and open a link to a remote resource in the
same link.  So it can only be displayed in an embedded image.  

Formatting for display and positioning is limited in this template so as to be as generic as possible.  Depending 
on the structure of your stylesheet, you may need or want to add additional formatting.

The order of elements is important.  Put elements in the following order: <resource>, <daoloc>, <arc>. 
The first <arc> element should contain data for the first traversal, the second
<arc> for the second traveral.

-->

<!-- Document variables -->

<!-- These variables set the path to an icon file that is used when the "show" attribute is set to "new" or "replace"
, the "actuate" attribute is set to "onreqest" and no data is present in the <resource> element (which indicates that you
do not want a textual link) -->
<!-- Set this variable to the server/folder path that points to the icon image file on your server.  
This should end with a forward /, e.g. http://myserver.com/images/ -->
<xsl:variable name="pathToIcon"><!-- put data here --></xsl:variable>
<!-- Set this variable to the filename of the icon image, e.g. icon.jpg -->
<xsl:variable name="iconFilename"><!-- put data here --></xsl:variable>

<!-- This is currently setup as a named template, but you could easily just copy the part starting with
<xsl:template match="daogrp"> if you don't want to call it as a named template -->

<xsl:template name="daoloc">
<xsl:apply-templates select="daogrp | ../daogrp"/>
</xsl:template>


<xsl:template match="daogrp">


<xsl:choose>
<!-- First, check whether we are dealing with one or two <arc> elements -->
	<xsl:when test="arc[2]">
	<a>
	<xsl:if test="arc[2]/@show='new'">
	<xsl:attribute name="target">_blank</xsl:attribute>
	</xsl:if>

	<xsl:for-each select="daoloc">
	<!-- This selects the <daoloc> element that matches the @label attribute from <daoloc> and the @to attribute
	from the second <arc> element -->
	<xsl:if test="@label = following::arc[2]/@to">
	<xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
	</xsl:if>
	</xsl:for-each>

	<xsl:for-each select="daoloc">
	<xsl:if test="@label = following::arc[1]/@to">
	<img src="{@href}" class="daoimage" bolder="0">
<xsl:if test="following::arc[1]/@title">
					<xsl:attribute name="title"><xsl:value-of select="following::arc[1]/@title"/></xsl:attribute>
					<xsl:attribute name="alt"><xsl:value-of select="following::arc[1]/@title"/></xsl:attribute>					
					</xsl:if>
					</img>	
		<xsl:if test="string(daodesc)">
		<br/><span class="daodesc"><xsl:apply-templates/></span>
		</xsl:if>
	</xsl:if>
	</xsl:for-each>
	</a>

	</xsl:when>
	<!-- i.e. no second <arc> element -->
	<xsl:otherwise>
		<xsl:choose>
			<xsl:when test="arc[1][@show='embed'] and arc[1][@actuate='onload']">
			<xsl:for-each select="daoloc">
			<xsl:if test="@label = following-sibling::arc[1]/@to">
			<img src="{@href}" class="daoimage" border="0">
					<xsl:if test="following::arc[1]/@title">
					<xsl:attribute name="title"><xsl:value-of select="following::arc[1]/@title"/></xsl:attribute>
					<xsl:attribute name="alt"><xsl:value-of select="following::arc[1]/@title"/></xsl:attribute>					
					</xsl:if>			
			</img>
				<xsl:if test="string(daodesc)">
				<br/><span class="daodesc"><xsl:apply-templates/></span>
				</xsl:if>
			</xsl:if>
			</xsl:for-each>
			</xsl:when>
			<xsl:when test="(arc[1]/@show='replace' or arc[1]/@show='new') and arc[1]/@actuate='onrequest'">
		<a>
				<xsl:choose>
				<!-- when a textual hyperlink is desired, i.e. <resource> element contains data -->
					<xsl:when test="string(resource)">
					<xsl:for-each select="daoloc">
					<xsl:if test="@label = following::arc[1]/@to">
					<xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
						<xsl:if test="following::arc[1]/@show='new'">
						<xsl:attribute name="target">_blank</xsl:attribute>
						</xsl:if>
					</xsl:if>
					</xsl:for-each>
					<xsl:apply-templates/>
					</xsl:when>
					<xsl:otherwise>
				<!-- if <resource> element is empty, produce an icon that can be used to traverse the link -->
					<xsl:for-each select="daoloc">
					<xsl:if test="@label = following::arc[1]/@to">
					<xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
						<xsl:if test="following::arc[1]/@show='new'">
						<xsl:attribute name="target">_blank</xsl:attribute>
						</xsl:if>
					</xsl:if>
					<img src="{$pathToIcon}{$iconFilename}" border="0">
					<xsl:if test="following::arc[1]/@title">
					<xsl:attribute name="title"><xsl:value-of select="following::arc[1]/@title"/></xsl:attribute>
					<xsl:attribute name="alt"><xsl:value-of select="following::arc[1]/@title"/></xsl:attribute>					
					</xsl:if>
					</img>					
					</xsl:for-each>
					</xsl:otherwise>
			</xsl:choose>
	</a>				
		</xsl:when>
	</xsl:choose>
</xsl:otherwise>
</xsl:choose>

</xsl:template>
</xsl:stylesheet>
