在XSLT 1.0中排序的问题

时间:2012-02-02 12:04:53

标签: xslt

为了学习XSLT,我有以下XML文件和XSLT样式表。我想制作一个在得分3*goals + 2*assists之后排名的球员名单。

<?xml-stylesheet type="text/xsl" href="player_ranking.xsl"?>
<soccer xmlns="http://mysoccer.it"> 
  <players>
    <player>
      <name>Pelé</name>
      <goals>21</goals>
      <assists>9</assists>
    </player>
    <player>
      <name>Beckenbauer</name>
        <goals>7</goals>
        <assists>18</assists>
      </player>
      <player>
        <name>Spiderman</name>
        <goals>27</goals>
        <assists>38</assists>
      </player>
      <player>
        <name>Hagi</name>
        <goals>13</goals>
        <assists>14</assists>
      </player>
      <player>
        <name>Laudrup</name>
        <goals>11</goals>
        <assists>25</assists>
      </player>
      <player>
        <name>Gullit</name>
        <goals>17</goals>
        <assists>15</assists>
      </player>
    </players>
  </soccer>

样式表

 <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:soc="http://mysoccer.it"
     xmlns="http://www.w3.org/1999/xhtml">

   <xsl:template match="soc:soccer">
     <html>
       <head>
    <title>Famous players ranked</title>
       </head>
       <body>
         <h1>Overall ranking</h1>
         <xsl:apply-templates match="soc:players"/>
       </body>
     </html>
   </xsl:template>

   <xsl:template match="soc:players">
     <xsl:apply-templates select="soc:player">
       <xsl:sort select="soc:goals * 3 + 2 * soc:assists" order="descending"/>
     </xsl:apply-templates>
   </xsl:template>

   <xsl:template match="soc:player">
     <xsl:value-of select="soc:name"/> 
     with score
     <xsl:value-of select="soc:goals * 3 + 2 * soc:assists"/>
     <br/>
   </xsl:template>

 </xsl:stylesheet>

在浏览器中(同时使用Opera和Firefox),它会生成

 Overall ranking

 Laudrup with score 83
 Pelé with score 81
 Gullit with score 81
 Hagi with score 67
 Beckenbauer with score 57
 Spiderman with score 157

蜘蛛侠应该出现在列表的顶部。有人可以指出我的错误吗?

我在样式表中使用version="2.0",因为version="1.0"使用了Error loading stylesheet: Parsing an XSLT stylesheet failed.

1 个答案:

答案 0 :(得分:3)

看起来它正在做一个字母数字排序,所以1&lt; 5等

尝试将排序行更改为:

<xsl:sort select="soc:goals * 3 + 2 * soc:assists" data-type="number" order="descending"/>