【问题标题】:I'm having trouble displaying a horizontal bar chart using a result tree fragment我无法使用结果树片段显示水平条形图
【发布时间】:2012-02-04 21:43:09
【问题描述】:

我是 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>

【问题讨论】:

  • 一些示例输入 XML 和期望输出的 XML 会更容易帮助您
  • 如果您遇到问题,了解问题的性质也会有所帮助。仅仅向我们展示一些代码并说它不起作用是不够的(代码中没有任何明显错误的地方)。
  • 我应该为每个候选人设置一系列条形图。这就是 redcell/bluecell/greencell 片段中的代码。出于某种原因,这些栏根本没有出现。

标签: xml xslt fragment


【解决方案1】:

问题出在这一行:

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

您不想将模板应用于showBar 元素。您想调用命名模板“showBar”。使用这个:

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

同时更改结束标记。

顺便说一句,你的空间会在那些 td 元素中折叠。您可以使用 xsl:text 来防止这种情况,例如

<xsl:text>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多