2012-02-04 8 views
0

XSLが初めてのので、ここで間違っていることはわかりません。ページには選挙結果が表示され、各候補者の投票率に対応する横棒グラフが表示されます。結果ツリーの断片を使って横棒グラフを表示するのに問題があります。結果ツリーのフラグメントを使用して横棒グラフを表示するのに問題があります

私の研究を試みましたが、私は立ち往生しています。誰かが助けてくれますか?ここに私のコードは次のとおりです。

<?xml version="1.0" encoding="UTF-8" ?> 

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

<xsl:variable name="redcell"> 
<td class="red"> </td> 
</xsl:variable> 

<xsl:variable name="bluecell"> 
<td class="blue"> </td> 
</xsl:variable> 

<xsl:variable name="greencell"> 
<td class="green"> </td> 
</xsl:variable> 

<xsl:template match="/"> 
<html> 
<head> 
<title>Election Night Results</title> 
<link href="polls.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 
<div id="intro"> 
<p><img src="logo.jpg" alt="Election Day Results" /></p> 
<a href="#">Election Home Page</a> 
<a href="#">President</a> 
<a href="#">Senate Races</a> 
<a href="#">Congressional Races</a> 
<a href="#">State Senate</a> 
<a href="#">State House</a> 
<a href="#">Local Races</a> 
<a href="#">Judicial</a> 
<a href="#">Referendums</a> 
</div> 

<div id="results"> 
<h1>Congressional Races</h1> 
<xsl:apply-templates select="polls/race" /> 
</div> 
</body> 
</html> 
</xsl:template> 



<xsl:template match="candidate"> 
<xsl:variable name="percent" select="votes div sum(..//votes)" /> 
<tr> 
<td class="name"><xsl:value-of select="name" /> (<xsl:value-of select="party" />)</td> 
<td class="num"><xsl:value-of select="format-number(votes, '#,##0')" /> (<xsl:value-of select="format-number($percent, '#0%')" />)</td> 
<xsl:apply-templates select="showBar"> 
<xsl:with-param name="cells" select="$percent * 100" /> 
<xsl:with-param name="partyType" select="party" /> 
</xsl:apply-templates> 
</tr> 
</xsl:template> 

<xsl:template match="race"> 
<h2><xsl:value-of select="title" /></h2> 
<table cellpadding="1" cellspacing="1"> 
<tr><th>Candidate</th><th class="num">Votes</th></tr> 
<xsl:apply-templates select="candidate" /> 
</table> 
</xsl:template> 

<xsl:template name="showBar"> 
<xsl:param name="cells" select="0" /> 
<xsl:param name="partyType" /> 

<xsl:if test="$cells > 0"> 

<xsl:choose> 
<xsl:when test="$partyType='R'"> 
<xsl:copy-of select="$redcell" /> 
</xsl:when> 

<xsl:when test="$partyType='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="partyType" select="party" /> 
</xsl:call-template> 

</xsl:if> 
</xsl:template> 

</xsl:stylesheet> 
+2

は、あなたが問題を抱えている場合、それはまた、あなたの悩みの本質が何であるか、知って役立つだろう、それが簡単にあなたの – Kevan

+1

を助けることになるだろう。ちょうど私達にいくつかのコードを示して、それがうまくいかないと言っても十分ではありません(明らかに間違っていると叫ぶコードには何もありません)。 –

+0

私は候補者ごとに一連のバーを用意しています。これは、redcell/bluecell/greencellの断片のコードです。なんらかの理由で、これらのバーはまったく表示されません。 –

答えて

0

問題は、この行です:

<xsl:apply-templates select="showBar"> 

あなたはshowBar要素にテンプレートを適用する必要はありません。名前付きテンプレート "showBar"を呼び出すとします。これを使用してください:

<xsl:call-template name="showBar"> 

また、終了タグも変更してください。

ところで、あなたのスペースはそれらの要素で崩壊するでしょう。 xsl:textを使用すると、それを防ぐことができます。いくつかの例入力XMLと出たいXML

<xsl:text> 
関連する問題