recursive templates in XSLT

webmasterbeta

New Member
I am new to XSLT and am trying to grasp recursive templates. The "showbar" template should output a bar graph (red, blue or green) for the party affiliation based on the percent of votes. As my code is, I am getting no output and do not know what is missing in the recursive template or the template call.

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

<xsl:variable name="redcell">
<td bgcolor="red" width="1"> </td>
</xsl:variable>

<xsl:variable name="bluecell">
<td bgcolor="blue" width="1"> </td>
</xsl:variable>

<xsl:variable name="greencell">
<td bgcolor="green" width="1"> </td>
</xsl:variable>

<xsl:template match="/">
<html>
<head>
<title>Election Night Results</title>
<link href=http://www.webdeveloper.com/forum/archive/index.php/"polls.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Election Night Results</h1>
<xsl:apply-templates select="polls/race" />
</body>
</html>
</xsl:template>

<xsl:template match="polls/race">
<h2><xsl:value-of select="title" /></h2>
<table cellpadding="3" cellspacing="0">
<xsl:apply-templates select="candidate" />

</table>
</xsl:template>

<xsl:template match="candidate">
<xsl:variable name="percent" select="format-number(votes div sum(..//votes),'#0%')"/>
<tr>
<td width="120"><xsl:value-of select="name" /> (<xsl:value-of select="party" />)</td>
<td width="80" align="right"><xsl:value-of select="format-number(votes, '#,##0') " /></td>
<td>(<xsl:copy-of select="$percent"/>)</td>
<td
<xsl:call-template name="showbar">
<xsl:with-param name="cells" select="$percent - 1"/>
<xsl:with-param name="party_type" select="party"/>
</xsl:call-template>
</td>
</tr>
</xsl:template>

<xsl:template name="showbar">
<xsl:param name="cells" select="0" />
<xsl:param name="party_type" select="party" />
<xsl:if test="$cells > 0">
<xsl:choose>
<xsl:when test="$party_type = 'R'"><xsl:copy-of select="$redcell"/></xsl:when>
<xsl:when test="$party_type = 'D'"><xsl:copy-of select="$bluecell"/></xsl:when>
<xsl:otherwise><xsl:copy-of select="$greencell"/></xsl:otherwise>
</xsl:choose>
<xsl:call-template name="showbar">
<xsl:with-param name="cells" select="$cells - 1"/>
<xsl:with-param name="party_type" select="$party_type"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

</xsl:stylesheet>
 
Back
Top