<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:output method="xml" omit-xml-declaration="no" standalone="no"/>
<xsl:output indent="yes" doctype-system="http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" doctype-public="-//W3C//DTD SVG 1.1//EN"/>







<xsl:template match="/jeneo">

<!-- find the extents of document -->
<xsl:variable name="max_x">
	<xsl:for-each select="/jeneo/*/*/pos/x|/jeneo/*/*/rect/x">
		<xsl:sort data-type="number" order="descending"/>
		<xsl:if test="position()=1">
			<xsl:value-of select="."/>
		</xsl:if>
	</xsl:for-each>
</xsl:variable>

<xsl:variable name="max_y">
	<xsl:for-each select="/jeneo/*/*/pos/y|/jeneo/*/*/rect/y">
		<xsl:sort data-type="number" order="descending"/>
		<xsl:if test="position()=1">
			<xsl:value-of select="."/>
		</xsl:if>
	</xsl:for-each>
</xsl:variable>

<xsl:variable name="min_x">
	<xsl:for-each select="/jeneo/*/*/pos/x|/jeneo/*/*/rect/x">
		<xsl:sort data-type="number" order="ascending"/>
		<xsl:if test="position()=1">
			<xsl:value-of select="."/>
		</xsl:if>
	</xsl:for-each>
</xsl:variable>

<xsl:variable name="min_y">
	<xsl:for-each select="/jeneo/*/*/pos/y|/jeneo/*/*/rect/y">
		<xsl:sort data-type="number" order="ascending"/>
		<xsl:if test="position()=1">
			<xsl:value-of select="."/>
		</xsl:if>
	</xsl:for-each>
</xsl:variable>



<svg width="{number($max_x)-number($min_x)+60}" height="{number($max_y)-number($min_y)+80}" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">

	<defs>
		<!-- All individual have the same shapes: rects, ellpses, etc. We define a template which we will use anywhen we want to insert an individual shape -->
		<rect    id="male_shape"     x="-15" y="-20" width="30" height="40"/>
		<ellipse id="female_shape"   cx="0"  cy="0"  rx="15"    ry="20"/>
		<rect    id="dontknow_shape" x="-15" y="-20" width="30" height="40" rx="8" ry="8"/>

		<clipPath id="male_clippath">    <use xlink:href="#male_shape"/>    </clipPath>		
		<clipPath id="female_clippath">  <use xlink:href="#female_shape"/>  </clipPath>
		<clipPath id="dontknow_clippath"><use xlink:href="#dontknow_shape"/></clipPath>
	</defs>

	<g transform="translate({30-number($min_x)},{40-number($min_y)})"  stroke="Black" stroke-width="1.0px" fill="none" font-family="Arial"  font-size="8px" >
		<xsl:apply-templates select="pictures/picture"/>
		<xsl:apply-templates select="texts/text"/>

		<xsl:apply-templates select="families/family"/>
		<xsl:apply-templates select="pedigrees/pedigree"/>

		<xsl:apply-templates select="individuals/individual"/>
	</g>
</svg>
</xsl:template>







<xsl:template match="individuals/individual" xmlns="http://www.w3.org/2000/svg"  >
	<!-- Template for displaying individuals. -->
	<g transform="translate({pos/x}, {pos/y}) scale({size})">
		<!-- We use the same coordinates in individuals' drawing, but we transform the group according to individual's position and size -->
		<xsl:choose>
			<xsl:when test="gender='0'">
				<use xlink:href="#male_shape" fill="white"/>	
				<image x="-15" y="-20" width="30" height="40" clip-path="url(#male_clippath)" xlink:href="{photo}"/>
				<use xlink:href="#male_shape" fill="none"/>
			</xsl:when>
			<xsl:when test="gender='1'">
				<use xlink:href="#female_shape" fill="white"/>	
				<image x="-15" y="-20" width="30" height="40" clip-path="url(#female_clippath)" xlink:href="{photo}"/>
				<use xlink:href="#female_shape" fill="none"/>
			</xsl:when>
			<xsl:otherwise>
				<use xlink:href="#dontknow_shape" fill="white"/>	
				<image x="-15" y="-20" width="30" height="40" clip-path="url(#dontknow_clippath)" xlink:href="{photo}"/>
				<use xlink:href="#dontknow_shape" fill="none"/>
			</xsl:otherwise>
		</xsl:choose>

		<text x="0" y="-16" text-anchor="middle" fill="black" stroke="none">
			<xsl:if test="death">
				<tspan x="0" dy="-7">
					<xsl:value-of select="death"/>
				</tspan>
			</xsl:if>
			<xsl:if test="birth">
				<tspan x="0" dy="-7">
					<xsl:value-of select="birth"/>
				</tspan>
			</xsl:if>
		</text>

		<text x="0" y="21" text-anchor="middle" fill="black" stroke="none">
			<xsl:if test="first">
				<tspan x="0" dy="7"><xsl:value-of select="first"/></tspan>
			</xsl:if>
			<xsl:if test="middle">
				<tspan x="0" dy="7"><xsl:value-of select="middle"/></tspan>
			</xsl:if>
			<xsl:if test="last">
				<tspan x="0" dy="7"><xsl:value-of select="last"/></tspan>
			</xsl:if>
		</text>
	</g>
</xsl:template>









<xsl:template match="families/family" xmlns="http://www.w3.org/2000/svg"  >
	<xsl:variable name="father"><xsl:value-of select="father"/></xsl:variable>
	<xsl:variable name="mother"><xsl:value-of select="mother"/></xsl:variable>
	<xsl:variable name="this">  <xsl:value-of select="id"/>    </xsl:variable>

	<xsl:variable name="father_x" select="/jeneo/individuals/individual[id=$father]/pos/x"/>
	<xsl:variable name="father_y" select="/jeneo/individuals/individual[id=$father]/pos/y"/>
	<xsl:variable name="mother_x" select="/jeneo/individuals/individual[id=$mother]/pos/x"/>
	<xsl:variable name="mother_y" select="/jeneo/individuals/individual[id=$mother]/pos/y"/>

	<xsl:variable name="family_x" select="(number($father_x) + number($mother_x)) div 2"/>
	<xsl:variable name="family_y" select="(number($father_y) + number($mother_y)) div 2"/>

	<g line-width="{size}">
		<xsl:attribute name="transform">scale(<!--<xsl:value-of select="size"/>-->1.0)</xsl:attribute>
	
		<line x1="{$father_x}" y1="{pos/y}" x2="{$mother_x}" y2="{pos/y}"/>
		<line x1="{$father_x}" y1="{$father_y}" x2="{$father_x}" y2="{pos/y}"/>
		<line x1="{$mother_x}" y1="{$mother_y}" x2="{$mother_x}" y2="{pos/y}"/>

		<xsl:if test="date">
			<text x="{pos/x}" y="{pos/y}" text-anchor="middle" fill="black" stroke="none">			
				<tspan dy="-2"><xsl:value-of select="date"/></tspan>
			</text>
		</xsl:if>

	</g>
</xsl:template>









<xsl:template match="points/point">
	<xsl:value-of select="x"/>,<xsl:value-of select="y"/><xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="pedigrees/pedigree" xmlns="http://www.w3.org/2000/svg"  >

	<xsl:variable name="source"><xsl:value-of select="source"/></xsl:variable>
	<xsl:variable name="target"><xsl:value-of select="target"/></xsl:variable>
	<xsl:variable name="this">  <xsl:value-of select="id"/>    </xsl:variable>

	<xsl:variable name="source_x" select="/jeneo/*/*[id=$source]/pos/x"/>
	<xsl:variable name="source_y" select="/jeneo/*/*[id=$source]/pos/y"/>
	<xsl:variable name="target_x" select="/jeneo/*/*[id=$target]/pos/x"/>
	<xsl:variable name="target_y" select="/jeneo/*/*[id=$target]/pos/y"/>

	<g>
		<polyline>
			<xsl:attribute name="points">
				<xsl:choose>
					<xsl:when test="points/point[position()=1]/x">
						<xsl:value-of select="points/point[position()=1]/x"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$target_x"/>
					</xsl:otherwise>					
				</xsl:choose>
				,<xsl:value-of select="$source_y"/><xsl:text> </xsl:text>
				<xsl:apply-templates select="points/point"/>
				<xsl:value-of select="$target_x"/>,<xsl:value-of select="$target_y"/>
			</xsl:attribute>
		</polyline>
	</g>
			
</xsl:template>








<xsl:template match="pictures/picture" xmlns="http://www.w3.org/2000/svg"  >
	<g>
		<image x="{rect/x}" y="{rect/y}" width="{rect/width}" height="{rect/height}" xlink:href="{picture}"/>
		<rect  x="{rect/x}" y="{rect/y}" width="{rect/width}" height="{rect/height}" fill="none"/>
	</g>
</xsl:template>







<xsl:template match="texts/text" xmlns="http://www.w3.org/2000/svg"  >
	<g transform="translate({rect/x}, {rect/y})">

		<g transform="translate({number(rect/width) div 2}, 5)">
			<text x="0" y="0" text-anchor="middle" fill="black" stroke="none">
				<xsl:call-template name="textwrap">
					<xsl:with-param name="text" select="text"/>
					<xsl:with-param name="chars" select="number(rect/width) div 4.5"/>
				</xsl:call-template>
			</text>
		</g>

		<rect x="0" y="0" width="{rect/width}" height="{rect/height}" fill="none"/>
	</g>
</xsl:template>


<xsl:template name="textwrap" xmlns="http://www.w3.org/2000/svg" >
	<xsl:param name="text"/>
	<xsl:param name="chars"/>
	<xsl:choose>
		<xsl:when test="string-length($text) &gt; number($chars)">
			<tspan x="0" dy="13px"><xsl:value-of select="substring($text, 1, number($chars))"/></tspan>
			<xsl:call-template name="textwrap">
				<xsl:with-param name="text" select="substring($text, number($chars+1))" />
				<xsl:with-param name="chars" select="$chars" />
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<tspan x="0" dy="13px"><xsl:value-of select="$text"/></tspan>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>








</xsl:stylesheet>



