<?xml version="1.0"?>

<!-- apply template with for-each loop and choose condition -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

	<!--================= root template ========================================-->
	<xsl:template match="/">
		<html>
		<head> 
		<title>choose from population numbers</title>
		</head> 
		<body bgcolor="white">

		<xsl:apply-templates select="endangered_species/animal"/>

		</body>
		</html>
	</xsl:template>

	<!--================= animal template =====================================-->
	<xsl:template match="animal">
		<p align="center">
		<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[population <= 3000]">		<!--filter-->

			<xsl:sort select="population"       data-type="number"/>	<!--sort-->
			<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>

		</table>
		<p>The mighty <b> 
			<xsl:value-of select="name[@language='English']"/> 
		</b> faces numerous threats. For more information
		<a href="http://www.worldwildlife.org/"> click</a>
		</p><hr/>
	</xsl:template>

	<!--=============== English name template ==================================-->
	<xsl:template match="name[@language='English']">
		<nobr><b>
			<xsl:value-of select="."/>: 
		</b></nobr>
	</xsl:template>

	<!--================ Latin name template ===================================-->
	<xsl:template match="name[@language='Latin']">
		<nobr><i>
			<xsl:value-of select="."/>
		</i></nobr>
	</xsl:template>
	
	<!--================ population template with choose test =================-->
	<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:when test=". > 0 and . < 50">
				<font color="blue" title="they're almost gone"><xsl:value-of select="."/></font>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>