2011-10-26 27 views
0

我想对一些XML数据进行排序,我遵循W3s教程,但是我的代码不工作,什么错误?用XSLT排序小数点

<xsl:for-each select="garage/car[colour='red']"> 
    <xsl:apply-templates> 

<xsl:sort select="number(price)" order="descending" data-type="number" /> 
</xsl:apply-templates> 

    <tr>  
    <td><xsl:value-of select="make"/></td> 
    <td><xsl:value-of select="model"/></td> 
    <td><xsl:value-of select="price"/></td> 
    </tr> 


    </xsl:for-each> 

XML例子〜:

<garage> 
    <car> 
     <make>vw</make> 
     <model>golf</model> 
     <color>red</color> 
     <price>5.99</price> 
    </car> 
    <car> 
     <make>ford</make> 
     <model>focus</model> 
     <color>black</color> 
     <price>3.66</price> 
    </car> 
    <car> 
     <make>honda</make> 
     <model>civic</model> 
     <color>red</color> 
     <price>15.99</price> 
    </car> 
</garage> 
+0

你确定有人知道你的xml是什么吗? –

+0

现在添加了xml .... – Lunar

+0

W3C提供了教程? – Saxoier

回答

0

所以,你要表现出下降的价格秩序所有的红色轿车?

如果您的颜色/颜色拼写一致,它会有所帮助!我是英国人,感觉你的痛苦!

而且你混合起来的for-each和应用模板(包括工作)

应用模板方法:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" version="1.0" indent="yes" /> 

    <xsl:template match="/"> 
     <xsl:apply-templates select="garage/car[color='red']"> 
     <xsl:sort select="number(price)" order="descending" data-type="number" /> 
     </xsl:apply-templates> 
    </xsl:template> 

    <xsl:template match="car[color='red']"> 
     <tr> 
     <td> 
      <xsl:value-of select="make" /> 
     </td> 
     <td> 
      <xsl:value-of select="model" /> 
     </td> 
     <td> 
      <xsl:value-of select="price" /> 
     </td> 
     </tr> 
    </xsl:template>  
</xsl:stylesheet> 

的for-each方法:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" version="1.0" indent="yes" /> 

    <xsl:template match="/"> 
     <xsl:for-each select="garage/car[color='red']"> 
     <xsl:sort select="number(price)" order="descending" data-type="number" /> 
     <tr> 
      <td> 
       <xsl:value-of select="make" /> 
      </td>  
      <td> 
       <xsl:value-of select="model" /> 
      </td> 
      <td> 
       <xsl:value-of select="price" /> 
      </td> 
     </tr> 
     </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet>