0

I have written a block of XSLT and I want to sort my xml file, but that doesn't work.

my XSLT:

  .....  
  <xsl:template match="/">
    <html>
      <body>
        <h1>The second one that i can sort the result</h1>
        <table>
          <tr bgcolor="blue">
            <th>Name</th>
            <th>ID</th>
            <th>preis</th>
            <th>Lieferant</th>
          </tr>
          <xsl:for-each select="//lieferungen/artikel">
            <tr>
              <xsl:apply-templates select="name"/>
              <td><xsl:value-of select="@id"/></td>
              <td><xsl:apply-templates><xsl:sort select="preis" order="ascending"/</xsl:apply-templates></td>
              <xsl:apply-templates select="lieferant"/>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="name">
    <td><xsl:value-of select="node()"/></td>  
  </xsl:template>
  <xsl:template match="lieferant">
    <td><xsl:value-of select="node()"/></td>
  </xsl:template>
</xsl:stylesheet>

and the xml is:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="C:\Users\Babak\Desktop\XSLT\sort.xslt"?>
<!-- Edited by XMLSpy® -->
<lieferungen  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:myspace:lieferungen ....">
  <artikel id="3526">
    <name>apfel</name>
    <preis stueckpreis="true">15.97</preis>
    <lieferant>Fa. Krause</lieferant>
  </artikel>
 ...
4

1 回答 1

1

Assuming the "artikel" should be ordered by preis put the xsl:sort as first statement into your xsl:for:

Something like:

<xsl:for-each select="//lieferungen/artikel">
     <xsl:sort select="preis" order="ascending"/>
      <tr>

       </tr>
 </xsl:for-each>

What you tried:

 <td><xsl:apply-templates><xsl:sort select="preis" order="ascending"/></xsl:apply-templates></td>

does not work because the current node is already "artikel". The xsl:apply-templates is done for all children (and attributes) of "artikel".

于 2013-07-17T10:12:42.707 回答