<?xml version=„1.0“ encoding=„utf-8“?> <xsl:stylesheet

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

<xsl:template match="/">
	<!-- this allows nesting results element arbitrarily -->
	<xsl:apply-templates select="//results"></xsl:apply-templates>
</xsl:template>

<xsl:template match="results">
	<xsl:apply-templates select="cumulative"></xsl:apply-templates>
</xsl:template>

<xsl:template match="cumulative">
	<xsl:apply-templates select="category">
		<xsl:sort select="@id" order="descending"/>
	</xsl:apply-templates>
</xsl:template>
<xsl:template match="detail">
	<xsl:apply-templates select="category">
		<xsl:sort select="@id" order="descending"/>
	</xsl:apply-templates>
</xsl:template>

  <xsl:template match="category">
	<h2>Kategorie <xsl:value-of select="@id"/>. ročníků</h2><!-- here should be a named template -->
	<table class="table table-hover table-striped tablesorter">
		<col align="right" class="poradi" />
		<col class="jmeno" />
		<col class="skola" />
			<xsl:element name="col">
				<xsl:attribute name="span"><xsl:value-of select="count(column-definitions/column-definition)-3"/></xsl:attribute>
				<xsl:attribute name="align">right</xsl:attribute>
				<xsl:attribute name="class">body</xsl:attribute>
			</xsl:element>
			<col align="right" class="procenta" />
			<col align="right" class="procenta" />
			<col align="right" class="celkem" />
		<thead><tr>
       	<th></th><th>Jméno</th><th>Škola</th>
       	<xsl:apply-templates select="column-definitions/column-definition" mode="header"></xsl:apply-templates>
		</tr></thead>
		
		<tr class="student_pilny">
        <td></td><td>Student Pilný</td><td>MFF UK</td>
        <xsl:apply-templates select="column-definitions/column-definition" mode="limit"></xsl:apply-templates>
		</tr>
		
		<xsl:apply-templates select="data/contestant"></xsl:apply-templates>
	</table>
  </xsl:template>
  
<xsl:template match="column-definition" mode="header">
	<th>
	<xsl:choose>
		<xsl:when test="@label = 'percent'">%</xsl:when>
		<xsl:when test="@label = 'total-percent'">#</xsl:when>
		<xsl:when test="@label = 'sum'">&#8721;</xsl:when>						
		<xsl:otherwise><xsl:value-of select="@label"/></xsl:otherwise>
	</xsl:choose>
	</th>
</xsl:template>

<xsl:template match="column-definition" mode="limit">
	<td>
		<xsl:choose>
			<xsl:when test="position() = last()">
				<b><xsl:call-template name="nullable"><xsl:with-param name="value" select="@limit"/></xsl:call-template></b>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="nullable"><xsl:with-param name="value" select="@limit"/></xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</td>
</xsl:template>

<xsl:template match="column" mode="contestant">
	<td>
		<xsl:choose>
			<xsl:when test="position() = last()">
				<b><xsl:call-template name="nullable"><xsl:with-param name="value" select="text()"/></xsl:call-template></b>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="nullable"><xsl:with-param name="value" select="text()"/></xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</td>
</xsl:template>

<xsl:template match="contestant">
	<xsl:if test="column[last()]/text()">
	<tr>
		<td align='right'>
			<xsl:if test="rank/@to"><xsl:value-of select="rank/@from"/>.&#8211;<xsl:value-of select="rank/@to"/>.</xsl:if>
			<xsl:if test="not(rank/@to)"><xsl:value-of select="rank/@from"/>.</xsl:if>
		</td>
		<td>
			<xsl:value-of select="@name"/>
		</td>
		<td>
			<xsl:value-of select="@school"/>
		</td>
		<xsl:apply-templates select="column" mode="contestant"></xsl:apply-templates>
	</tr>
</xsl:if>
</xsl:template>

<xsl:template name="nullable">
	<xsl:param name="value"/>
	<xsl:if test="not($value)">&#8211;</xsl:if>
	<xsl:if test="$value"><xsl:value-of select="$value"/></xsl:if>
</xsl:template>

</xsl:stylesheet>