2012-02-28 117 views
0

我似乎在我的xml文件或xsl文件中有问题。这是应该发生的事情 XMl文件在显示时包含投票,它应该按照投票次数和百分比对结果进行排序和排序,并显示结果。这一切看起来是正确的,除了百分比的选票是离开的。 我在做什么错?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

当你正在做的百分比,你是潜水的票数为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%')"/> 

这应该给你你需要的结果。

+0

这就是我所需要的非常感谢你 – Jon 2012-02-28 22:22:33