<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="html" />

<xsl:key name="indilist" match="indi" use="./@id" />
<xsl:key name="famlist" match="fam" use="./@id" />

<xsl:template match="/">
<html>
<head><title>Genealogie</title></head>
<body style="font-family: times; ">
<xsl:apply-templates select="genea"/>
</body>
</html>
</xsl:template>

<xsl:template match="genea">
<h1>Genealogie</h1>

<h2>Familles</h2>
<div>
<xsl:apply-templates select="//fam" />
</div>
<h2>Individus</h2>
<div>
<xsl:apply-templates select="indi">
	<xsl:sort select="name"/>
</xsl:apply-templates>
</div>
</xsl:template>

<xsl:template match="fam">
<p/>
<hr/>
<p/>
<xsl:element name="a">
<xsl:attribute name="name"><xsl:value-of select="@id" /></xsl:attribute>
</xsl:element>
<div>
<table width="100%" style="border-collapse:collapse;">
<xsl:comment>Famille ID <xsl:value-of select="@id" /></xsl:comment>
<tr><td width="50%" style="
					background-color:rgb(164,100,200);
					vertical-align: middle;
					border:1pt solid lightblue;
					text-align: center;
					padding-bottom: 10px; 
					padding-top: 10px; 
					">
	<xsl:call-template name="indiphoto">
		<xsl:with-param name="id" select="father/@id"/>
	</xsl:call-template>
	
	<xsl:call-template name="shortindiname-with-link">
		<xsl:with-param name="id" select="father/@id"/>
	</xsl:call-template>

</td><td width="50%" style="
					background-color:rgb(100,164,200);
					vertical-align: middle;
					border:1pt solid lightblue;
					text-align: center;
					padding-bottom: 10px; 
					padding-top: 10px; 
					">
	<xsl:call-template name="indiphoto">
		<xsl:with-param name="id" select="mother/@id"/>
	</xsl:call-template>
	
	<xsl:call-template name="shortindiname-with-link">
		<xsl:with-param name="id" select="mother/@id"/>
	</xsl:call-template>

</td></tr></table>

<xsl:if test="children">
<div align="center"><table width="80%" style="border-collapse:collapse;">
<caption>Enfants</caption><tr>
<xsl:for-each select="key('indilist', children/@id)">
	<xsl:element name="td">
	<xsl:attribute name="style">
	
	
	<xsl:if test="position()mod 2 = 1">
			background-color:azure;
			border:1pt solid lightblue;
			vertical-align: middle;
			text-align: center;
			padding-bottom: 10px; 
			padding-top: 10px; 
		</xsl:if>
	<xsl:if test="position() mod 2 = 0">
			background-color:powderblue;
			border:1pt solid lightblue;
			vertical-align: middle;
			text-align: center;
			padding-bottom: 10px; 
			padding-top: 10px; 
		</xsl:if>
	</xsl:attribute>
	<xsl:call-template name="indiphoto">
		<xsl:with-param name="id" select="@id"/>
	</xsl:call-template>
	
	<xsl:call-template name="shortindiname-with-link">
		<xsl:with-param name="id" select="@id"/>
	</xsl:call-template>
	</xsl:element>
</xsl:for-each>
</tr></table></div>
</xsl:if>


<xsl:apply-templates select="text"/>


</div><p/>
</xsl:template>


	
<xsl:template match="indi">
<p/>
<hr/>
<p/>
<div align="center">
<table width="80%" ><tr>
<td align="center" style="font-size:18pt;"><xsl:call-template name="shortindiname-with-anchor">
	<xsl:with-param name="id" select="@id"/>
</xsl:call-template></td>
<td align="right">
	<xsl:call-template name="indiphoto">
		<xsl:with-param name="id" select="@id"/>
	</xsl:call-template>
</td>
</tr></table><p />
<xsl:call-template name="seek-fam-as-child">
	<xsl:with-param name="indi" select="@id"/>
</xsl:call-template>
<xsl:call-template name="seek-fam-as-parent">
	<xsl:with-param name="indi" select="@id"/>
</xsl:call-template>

<xsl:apply-templates select="text"/>
</div>
</xsl:template>

<xsl:template match="text">
	<xsl:copy-of select="."/>
</xsl:template>

<xsl:template name="seek-fam-as-parent">
	<xsl:param name="indi" />
	<xsl:for-each select="/genea/fam">
			<xsl:if test="father/@id=$indi or mother/@id=$indi" >
				<p />
				<table width="100%" border="1" cellspacing="0">
				<tr>
				<th align="center">Union: 
				<xsl:call-template name="shortfam-with-link">
					<xsl:with-param name="id" select="@id"/>
				</xsl:call-template>
				</th>
				<xsl:for-each select="children">
				<td align="center">
					<xsl:call-template name="indiphoto">
						<xsl:with-param name="id" select="@id"/>
					</xsl:call-template>
					<xsl:call-template name="shortindiname-with-link">
						<xsl:with-param name="id" select="@id"/>
					</xsl:call-template>
				</td>
				</xsl:for-each>
				</tr>
				</table>
			</xsl:if>
	</xsl:for-each>
</xsl:template>

<xsl:template name="seek-fam-as-child">
	<xsl:param name="indi" />
	<xsl:for-each select="/genea/fam">
			<xsl:if test="children/@id=$indi" >
				<table width="100%" border="0" cellspacing="1">
				<caption> Enfant de la Famille
					<xsl:call-template name="shortfam-with-link">
						<xsl:with-param name="id" select="@id"/>
					</xsl:call-template>
				</caption>
				<tr><td width="50%" align="right">
					<xsl:call-template name="indiphoto">
						<xsl:with-param name="id" select="father/@id"/>
					</xsl:call-template>
				</td><td width="50%"  align="left">
					<xsl:call-template name="indiphoto">
						<xsl:with-param name="id" select="mother/@id"/>
					</xsl:call-template>
				</td></tr></table>
			</xsl:if>
	</xsl:for-each>
</xsl:template>



<xsl:template name="shortfam-with-link">
	<xsl:param name="id" />
	<xsl:element name="a">
	<xsl:attribute name="href">#<xsl:value-of select="$id" /></xsl:attribute>
	<xsl:for-each select="key('famlist', $id )">
	
		<xsl:for-each select="key('indilist', father/@id )">
		<xsl:call-template name="shortindiname">
			<xsl:with-param name="id" select="@id"/>
		</xsl:call-template>
		</xsl:for-each>
		<xsl:text> </xsl:text>
		<xsl:for-each select="key('indilist',  mother/@id)">
		<xsl:call-template name="shortindiname">
			<xsl:with-param name="id" select="@id"/>
		</xsl:call-template>
		</xsl:for-each>
	
	</xsl:for-each>
	</xsl:element>

</xsl:template>

<xsl:template name="shortfam-with-anchor">
	<xsl:param name="id" />
	<xsl:element name="a">
	<xsl:attribute name="name"><xsl:value-of select="$id" /></xsl:attribute>
	<xsl:for-each select="key('famlist', $id )">
	
		<xsl:for-each select="key('indilist', father/@id )">
		<xsl:call-template name="shortindiname-with-link">
			<xsl:with-param name="id" select="@id"/>
		</xsl:call-template>
		</xsl:for-each>
		<xsl:text> </xsl:text>
		<xsl:for-each select="key('indilist',  mother/@id)">
		<xsl:call-template name="shortindiname-with-link">
			<xsl:with-param name="id" select="@id"/>
		</xsl:call-template>
		</xsl:for-each>
	
	</xsl:for-each>
	</xsl:element>
</xsl:template>

<xsl:template name="shortindiname-with-anchor">
	<xsl:param name="id" />
	<xsl:element name="a">
	<xsl:attribute name="name"><xsl:value-of select="$id" /></xsl:attribute>
	<xsl:call-template name="shortindiname">
		<xsl:with-param name="id" select="$id"/>
	</xsl:call-template>	
	</xsl:element>
</xsl:template>


<xsl:template name="shortindiname-with-link">
	<xsl:param name="id" />
	<xsl:element name="a">
	<xsl:attribute name="href">#<xsl:value-of select="$id" /></xsl:attribute>
	<xsl:call-template name="shortindiname">
		<xsl:with-param name="id" select="$id"/>
	</xsl:call-template>	
	</xsl:element>
</xsl:template>

<xsl:template name="indiphoto">
	<xsl:param name="id" />
	<xsl:for-each select="key('indilist', $id)">
		<xsl:choose>
			<xsl:when test="photo">
			<xsl:element name="a">
			<xsl:attribute name="href">#<xsl:value-of select="$id" /></xsl:attribute>
			<xsl:element name="img">
				<xsl:attribute name="src"><xsl:value-of select="photo/@src" /></xsl:attribute>
			</xsl:element>
			</xsl:element>
			<br />
			</xsl:when>
			<xsl:otherwise>
				<xsl:element name="img">
					<xsl:attribute name="src">00.JPG</xsl:attribute>
				</xsl:element>
				<br />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:for-each>
</xsl:template>



<xsl:template name="shortindiname">
	<xsl:param name="id" />
	<xsl:for-each select="key('indilist', $id)">
	<b>
	<xsl:call-template name="titleize">
		<xsl:with-param name="s" select="name"/>
	</xsl:call-template>	
	</b>
	<xsl:if test="surname">
		<xsl:text> </xsl:text>
		<xsl:call-template name="titleize">
			<xsl:with-param name="s" select="surname"/>
		</xsl:call-template>	
	</xsl:if>
	<xsl:if test="born or dead">
		<i>(
		<xsl:if test="born"><xsl:value-of select="born" /></xsl:if>
		-
		<xsl:if test="dead"><xsl:value-of select="dead" /></xsl:if>
		)</i>
	</xsl:if>
	</xsl:for-each>

</xsl:template>

<xsl:template name="titleize">
	<xsl:param name="s" />
	<xsl:call-template name="toupper">
		<xsl:with-param name="s" select="substring($s,1,1)"/>
	</xsl:call-template>	
	<xsl:call-template name="tolower">
		<xsl:with-param name="s" select="substring($s,2)"/>
	</xsl:call-template>	
</xsl:template>

<xsl:template name="toupper">
	<xsl:param name="s" />
	<xsl:value-of select="translate($s,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
</xsl:template>

<xsl:template name="tolower">
	<xsl:param name="s" />
	<xsl:value-of select="translate($s,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
</xsl:template>


</xsl:stylesheet>

