2012-02-09 33 views
-1

我是XSLT新手,我想将我的XML数据转换为HTML表格,然后在preis字段上对表格进行排序:这是我的代码,但它不起作用。请你告诉我是什么问题在我的代码:在XSLT中排序?

<xsl:stylesheet> 
<xsl:template match="/"> 
    <html> 
     <body> 
     <h1> 
      Hallo 
     </h1> 
     <table border="1"> 

      <xsl:apply-templates> 
      <xsl:sort select="preis" data-type="number" order="ascending"/> 
      </xsl:apply-templates> 

     </table> 
     </body> 
    </html> 
    </xsl:template> 
    <xsl:template match="artikel"> 
    <tr> 

     <xsl:apply-templates select="name" /> 
     <xsl:apply-templates select="lieferant" /> 
     <xsl:apply-templates select="preis"/> 
    </tr> 
    </xsl:template> 
    <xsl:template match="name|lieferant|preis"> 
    <td> 
     <xsl:value-of select="."/> 
    </td> 
    </xsl:template> 

</xsl:stylesheet> 

和XML文件:

<lieferungen> 
    <artikel id="3526"> 
    <name>apfel</name> 
    <lieferant>Fa. Krause</lieferant> 
    <preis stueckpreis="true">8.97</preis> 
    </artikel> 
    <artikel id="7866"> 
    <name>Kirschen</name> 
    <preis stueckpreis="false">10.45</preis> 
    <lieferant>Fa. Helbig</lieferant> 
    </artikel> 
    <artikel id="3526"> 
    <preis stueckpreis="true">12.67</preis> 
    <lieferant>Fa. Liebig</lieferant> 
    <name>apfel</name> 
    </artikel> 
    <artikel id="7866"> 
    <preis stueckpreis="false">17.67</preis> 
    <name>Kirschen</name> 
    <lieferant>Fa. Krause</lieferant> 
    </artikel> 
    <artikel id="3627"> 
    <name>apfel</name> 
    <lieferant>Fa. Mertes</lieferant> 
    <preis stueckpreis="true">9.54</preis> 
    </artikel> 
    <artikel id="7866"> 
    <name>Kirschen</name> 
    <lieferant>Fa. Hoeller</lieferant> 
    <preis stueckpreis="false">16.45</preis> 
    </artikel> 
    <artikel id="7868"> 
    <preis>3.20</preis> 
    <name>Kohl</name> 
    <lieferant>Fa. Hoeller</lieferant> 
    </artikel> 
    <artikel id="7866"> 
    <name>Kirschen</name> 
    <lieferant>Fa. Richard</lieferant> 
    <preis stueckpreis="false">12.45</preis> 
    </artikel> 
    <artikel id="3245"> 
    <preis stueckpreis="false">15.67</preis> 
    <name>Bananen</name> 
    <lieferant>Fa. Hoeller</lieferant> 
    </artikel> 
    <artikel id="6745"> 
    <name>Kohl</name> 
    <lieferant>Fa. Reinhardt</lieferant> 
    <preis stueckpreis="false">3.10</preis> 
    </artikel> 
    <artikel id="7789"> 
    <name>Ananas</name> 
    <preis stueckpreis="true">8.60</preis> 
    <lieferant>Fa. Richard</lieferant> 
    </artikel> 
</lieferungen> 
+0

没有输入XML,所需的输出和实际输出的一个样本,我们如何应该知道“不起作用”是什么意思? – Oded 2012-02-09 10:07:49

+0

我的意思是价格(数量)应该是排序accesnding或descendind – Baper 2012-02-09 10:15:01

+0

现在你正在使事情更混乱。你需要上升还是下降?再说一遍,如果不明白“不行”意味着什么,我们就无法提供帮助。 – Oded 2012-02-09 10:16:12

回答

0

这应该工作,如果我理解你的问题

BTW!商品标签中的子标签的顺序是不一样的所有文章

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0"> 
<xsl:template match="/"> 
<xsl:for-each select="lieferungen/artikel"> 
<xsl:sort select="preis" data-type="number" order="ascending"/> 
    <td> 
     <xsl:copy-of select="."/> 
    </td> 
</xsl:for-each> 
</xsl:template> 

</xsl:transform> 

输出:

<td xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <artikel id="6745"> 
    <name>Kohl</name> 
    <lieferant>Fa. Reinhardt</lieferant> 
    <preis stueckpreis="false">3.10</preis> 
</artikel> 
</td> 
<td xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <artikel id="7868"> 
    <preis>3.20</preis> 
    <name>Kohl</name> 
    <lieferant>Fa. Hoeller</lieferant> 
    </artikel> 
</td> 
<td xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <artikel id="7789"> 
    <name>Ananas</name> 
    <preis stueckpreis="true">8.60</preis> 
    <lieferant>Fa. Richard</lieferant> 
    </artikel> 
</td> 
... 
... 
<td xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <artikel id="7866"> 
    <preis stueckpreis="false">17.67</preis> 
    <name>Kirschen</name> 
    <lieferant>Fa. Krause</lieferant> 
    </artikel> 
</td>