<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:param name="id" select="/*/INDI[1]/@ID"/>
<!-- define keys to allow records to be found by their id -->
<xsl:key name="IndividualForID" match="IndividualRec" use="@Id"/>
<xsl:key name="IndivNameForID" match="IndividualRec/IndivName" use="@Id"/>
<xsl:key name="EventForID" match="EventRec" use="@Id"/>
<xsl:key name="EventForIndividualID" match="EventRec" use="Participant/Link/@Ref"/>
<xsl:key name="EventRoleForID" match="Role" use="../Link/@Ref"/>
<xsl:key name="FamilyForID" match="FamilyRec" use="@Id"/>
<xsl:key name="ParentForID" match="FamilyRec" use="Child/Link/@Ref"/>
<xsl:key name="FamilyForFatherID" match="FamilyRec" use="HusbFath/Link/@Ref"/>
<xsl:key name="FamilyForMotherID" match="FamilyRec" use="WifeMoth/Link/@Ref"/>



<xsl:template match="/">
	<html>
		<head>
			<xsl:call-template name="css-style"/>
			<xsl:variable name="name">Boisen Genealogy</xsl:variable>
			<title>
				<xsl:value-of select="$name"/>
			</title>
		</head>
		<body>
			<xsl:apply-templates select="/GEDCOM/IndividualRec"/>
		</body>
	</html>
</xsl:template>

<xsl:template match="IndividualRec">
	<!-- Show name and parentage -->
	<h1>
		<a name="#{@Id}">
			<xsl:apply-templates select="IndivName"/>
		</a>
	</h1>
	<xsl:call-template name="show-parents"/>
	<xsl:call-template name="show-spouse"/>
	<xsl:call-template name="show-children"/>
	<xsl:call-template name="show-events"/>
	<hr/>
	<!-- Show notes -->
	<xsl:for-each select="NOTE">
		<p class="text">
			<xsl:apply-templates/>
		</p>
	</xsl:for-each>
	<table width="100%" bgcolor="blue">
		<tr>
			<td>
				<xsl:text>&#xa0;</xsl:text>
			</td>
		</tr>
	</table>
</xsl:template>

<xsl:template name="css-style">
	<style type="text/css">

	H1 {
	    font-family: Verdana, Helvetica, sans-serif;
	    font-size: 14pt;
	    font-weight: bold;
	    <!-- color: "#FF0080" -->
	}

	H2 {
	    font-family: Verdana, Helvetica, sans-serif;
	    font-size: 14pt;
	    font-weight: bold;
	    color: black;
	}

	H3 {
	    font-family: Lucida Sans, Helvetica, sans-serif;
	    font-size: 11pt;
	    font-weight: bold;
	    color: black;
	}

	SPAN.label {
	    font-family: Lucida Sans, Helvetica, sans-serif;
	    font-size: 10pt;
	    font-weight: normal;
        font-style: italic;
	    color: black;
	}

    P,LI,TD {
	    font-family: Lucida Sans, Helvetica, sans-serif;
	    font-size: 10pt;
	    font-weight: normal;
	    color: black;       
	}

    P.text {
	    font-family: Comic Sans MS, Helvetica, sans-serif;
	    font-size: 10pt;
	    font-weight: normal;
	    color: black;       
	}

   </style>
</xsl:template>

<xsl:template name="show-parents">
	<xsl:variable name="family" select="key('ParentForID', @Id)"/>
	<xsl:variable name="father" select="key('IndividualForID', $family/HusbFath/Link/@Ref)"/>
	<xsl:variable name="mother" select="key('IndividualForID', $family/WifeMoth/Link/@Ref)"/>
	<p>
		<xsl:if test="$father">
			<span class="label">Father: </span>
			<xsl:apply-templates select="$father/IndivName" mode="link"/>&#xa0;
		</xsl:if>
		<xsl:if test="$mother">
			<span class="label">Mother: </span>
			<xsl:apply-templates select="$mother/IndivName" mode="link"/>&#xa0; 
		</xsl:if>
	</p>
</xsl:template>

<xsl:template name="show-spouse">
	<xsl:variable name="familyforFather" select="key('FamilyForFatherID', @Id)"/>
	<xsl:variable name="familyforMother" select="key('FamilyForMotherID', @Id)"/>
	<p>
		<xsl:if test="$familyforFather">
			<xsl:variable name="wife" select="key('IndividualForID', $familyforFather/WifeMoth/Link/@Ref)"/>
			<span class="label">Wife: </span>
			<xsl:apply-templates select="$wife/IndivName" mode="link"/>
		</xsl:if>
		<xsl:if test="$familyforMother">
			<xsl:variable name="husband" select="key('IndividualForID', $familyforMother/HusbFath/Link/@Ref)"/>
			<span class="label">Husband: </span>
			<xsl:apply-templates select="$husband/IndivName" mode="link"/>
		</xsl:if>
	</p>
</xsl:template>

<xsl:template name="show-children">
	<xsl:variable name="familyforFather" select="key('FamilyForFatherID', @Id)"/>
	<xsl:variable name="familyforMother" select="key('FamilyForMotherID', @Id)"/>
	<p>
		<xsl:if test="$familyforFather or $familyforMother">
			<span class="label">Children: </span>
			<xsl:for-each select="$familyforFather/Child">
				<xsl:variable name="child" select="key('IndividualForID', Link/@Ref)"/>
				<xsl:apply-templates select="$child/IndivName" mode="link"/>&#xa0;
			</xsl:for-each>
			<xsl:for-each select="$familyforMother/Child">
				<xsl:variable name="child" select="key('IndividualForID', Link/@Ref)"/>
				<xsl:apply-templates select="$child/IndivName" mode="link"/>&#xa0;
			</xsl:for-each>
		</xsl:if>
	</p>
</xsl:template>

<xsl:template name="show-events">
	<xsl:variable name="id" select="@Id"/>
	<xsl:variable name="event" select="key('EventForIndividualID', $id)"/>
	<xsl:variable name="birth" select="$event[@Type = 'birth']"/>
	<xsl:variable name="death" select="$event[@Type = 'death']"/>
	<xsl:variable name="event-role" select="key('EventRoleForID', $id)"/>
	<xsl:if test="string($event) and string($birth) and $event-role = 'child' ">
		<p>Born 
			<xsl:call-template name="event-date-place">
				<xsl:with-param name="event" select="$birth"/>
			</xsl:call-template>
		</p>
	</xsl:if>
	<xsl:if test="string($event) and string($death) and $event-role = 'principle' ">
		<p>Died  
			<xsl:call-template name="event-date-place">
				<xsl:with-param name="event" select="$death"/>
			</xsl:call-template>
		</p>
	</xsl:if>
	<!-- 
			<xsl:for-each select="NOTE">
				<p class="text"><xsl:apply-templates/></p>
			</xsl:for-each>
	-->
</xsl:template>

<xsl:template match="MARR" mode="expand">Marriage</xsl:template>
<xsl:template match="*" mode="expand"/>

<xsl:template match="IndivName" mode="link">
	<a>
		<xsl:attribute name="href"><xsl:call-template name="make-href"/></xsl:attribute>
		<xsl:apply-templates select="."/>
	</a>
</xsl:template>

<xsl:template match="IndivName">
	<xsl:value-of select="NamePart[@Level=3]"/>&#xa0;
	<xsl:if test="NamePart/@Level = 2">(<xsl:value-of select="NamePart[@Level=2]"/>) </xsl:if>
	<xsl:value-of select="NamePart[@Level=1]"/>
</xsl:template>

<xsl:template name="event-date-place">
	<xsl:param name="event"/>
	<xsl:if test="$event/Date">
		<xsl:value-of select="$event/Date"/>&#xa0;
	</xsl:if>
	<xsl:if test="$event/Place">
		in <xsl:value-of select="$event/Place"/><br/>
	</xsl:if>
</xsl:template>

<xsl:template name="make-href">
	<xsl:value-of select="concat('#', ../@Id)"/>
</xsl:template>

</xsl:transform>
