<?xml version="1.0"?>
<xsl:stylesheet 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0"
><xsl:template match="/"><html><head><title>Endangered Species</title></head>
<body bgcolor="white">
	<xsl:apply-templates select="endangered_species/animal"/>
</body>
</html>
</xsl:template>


<xsl:template match="animal">
<p align="center"><xsl:apply-templates select="picture"/>
	<br/><font size="+3"><xsl:apply-templates select="name" /></font></p>
	
	<table width="100%" border="2">
<tr><th>Subspecies</th><th>Region</th><th>Number Left</th><th>As Of</th></tr>
	
	<xsl:for-each select="subspecies">	
	<xsl:sort select="population" data-type="number"/>
		<xsl:sort select="population/@year" data-type="number"/>
		<tr><td><xsl:apply-templates select="name"/></td>
		<td><xsl:value-of select="region"/></td>
		<td><xsl:apply-templates select="population"/></td>
		<td><xsl:value-of select="population/@year"/></td></tr>
		</xsl:for-each>
		<tr><td align="right"><b>Total:</b></td><td><xsl:value-of select="count(subspecies)"/> subspecies</td><td><xsl:value-of select="sum(subspecies/population)"/></td><td><br/></td></tr>
		</table>
	<xsl:apply-templates select="threats"/>

</xsl:template>

<xsl:template match="name[@language='English']">
	<nobr><b><xsl:value-of select="."/>: </b></nobr>
	</xsl:template>

<xsl:template match="name[@language='Latin']">
	<nobr><i><xsl:value-of select="."/></i></nobr>
</xsl:template>
<xsl:template match="subspecies/name[@language='Latin']">
	<nobr><i><xsl:value-of select="substring(../../name[@language='Latin'],1,1)"/>.<xsl:value-of select="substring(substring-after(../../name[@language='Latin'],' '),1,1)"/>. <xsl:value-of select="."/></i></nobr>
</xsl:template>

<xsl:template match="population">
	<xsl:choose>
		<xsl:when test=". = 0">
			<font color="red" title="that means there are no more left">Extinct</font>
		</xsl:when>
		
		<xsl:otherwise>
			<xsl:value-of select="."/> (<xsl:value-of select="format-number(. div sum(../../subspecies/population), '##0.0%')"/>)
		</xsl:otherwise>
	</xsl:choose>
	</xsl:template>

<xsl:template match="threats">
<p>The mighty <xsl:value-of select="../name[@language='English']"/> faces numerous threats, among them <xsl:for-each select="threat">
	<xsl:value-of select="."/>
	<xsl:choose>
	<xsl:when test="position()=last()">.</xsl:when>
	<xsl:when test="position()=last()-1">, and </xsl:when>
	<xsl:otherwise>, </xsl:otherwise>
	</xsl:choose>
	
	</xsl:for-each></p>
	

	</xsl:template>
	
	<xsl:template match="picture">
	<img>
	<xsl:attribute name="src"><xsl:value-of select="./@filename"/></xsl:attribute>
	<xsl:attribute name="width"><xsl:value-of select="ceiling(./@x div 2)"/></xsl:attribute>
	<xsl:attribute name="height"><xsl:value-of select="ceiling (./@y div 2)"/></xsl:attribute>
	</img>
	</xsl:template>
	
	
</xsl:stylesheet>

