xml排序和计数不正确

时间:2012-02-28 05:55:44

标签: xml xslt

我的xml文件或xsl文件似乎都有问题。这是应该发生的事情 XMl文件在显示时包含投票,它应该按照投票等级和百分比对结果进行排序和排序,并显示结果。除了投票的百分比之外,这一切看起来都是正确的。 我做错了什么?

XMl代码

<?xml-stylesheet type="text/xsl" href="os.xsl" ?> 

<poll>
<ballot id="b1">

   <os>Windows Server 2003 Standard</os>
   <os>Suse Linux</os>
</ballot>
<ballot id="b2">
     <os>Windows Server 2003 Standard</os>
       <os>Ubuntu Linux</os>
       </ballot>
<ballot id="b3">

</ballot>
<ballot id="b4">
    <os>Windows Server 2003 Standard</os>
    <os>Debin Linux</os>
</ballot>
<ballot id="b5">
   <os>Suse Linux</os>
   <os>Windows Server 2003 Standard</os>
   <os>Debin Linux</os>
</ballot>
<ballot id="b6">
   <os>Suse Linux</os>
   <os>Ubuntu Linux</os>
   <os>Windows Server 2008 Standard</os>
   <os>Debin Linux</os>
</ballot>
<ballot id="b7">
   <os>Debin Linux</os>
   <os>Ubuntu Linux</os>
      <os>Debin Linux</os>
</ballot>
<ballot id="b8">
   <os>Windows Server 2008 Standard</os>
   <os>Debin Linux</os>
    <os>Ubuntu Linux</os>
    <os>Suse Linux</os>
     <os>Debin Linux</os>
    </ballot>
</poll>

xsl代码从

开始
<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--

-->


        <xsl:key name="oss" match="os" use="."/> 

    <xsl:variable name="single-os" select="//os[generate-id(.) = generate-id(key('oss', .))]/."/>
        <xsl:template match="/">
    <html>
    <head>
        <title>Top Customer Server OS</title>
        <link href="os.css" rel="stylesheet" type="text/css" /> 
    </head>
    <body>
        <h2>Top Customer Server OS </h2>
        <table border="0" width="550">
        <tr><td colspan="4">Number of the Ballots: <xsl:value-of select="count(poll/ballot)"/></td></tr>

        <tr>
        <th>Rank</th>
        <th>os</th>
        <th>Votes</th>
        <th>%</th>
        </tr>


    <xsl:for-each select="$single-os">

    <xsl:sort select="count(key('oss', current()))" order="descending" data-type="number" />
        <xsl:variable select="count(key('oss', current()))" name="votes" />
        <tr>
        <td><xsl:value-of select="position()" />.</td>
        <td>
        <xsl:value-of select="." /> 
        </td>
        <td align="right"><xsl:value-of select="$votes" /></td>
        <td align="right"><xsl:value-of select="format-number($votes div count(//ballot), '#.00%')" /></td>
        </tr>
        <xsl:if test="position() mod 10 = 0">
        <tr>
        <td colspan="4"><hr /></td>
        </tr>
    </xsl:if>
    </xsl:for-each>

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

1 个答案:

答案 0 :(得分:1)

当您执行百分比时,您将 os 的投票数除以选票元素的总数:

<xsl:value-of select="format-number($votes div count(//ballot), '#.00%')" />

您应该做的是将 os 的投票数除以所有 os 元素的总投票数

<xsl:value-of select="format-number($votes div count(//os), '#.00%')"/>

这可以为您提供所需的结果。