mardi 24 janvier 2017

Changing the order of XML when rendering with XSL

I'm having an issue where I want to render a list of cars in the browser in a particular order but the XML is in a different order (which cannot be changed).

How can I write my XSL to render cars of the same make together, ordered by their top speed?

XML:

<?xml version="1.0" encoding="UTF-8"?>
<Vehicle>
    <Car>
        <Make>Audi</Make>
        <Model>R8</Model>
        <TopSpeed>200</TopSpeed>
    </Car>
    <Car>
        <Make>Lamborghini</Make>
        <Model>Aventador</Model>
        <TopSpeed>250</TopSpeed>
    </Car>
    <Car>
        <Make>Ferrari</Make>
        <Model>TDF</Model>
        <TopSpeed>220</TopSpeed>
    </Car>
    <Car>
        <Make>Lamborghini</Make>
        <Model>Gallardo</Model>
        <TopSpeed>200</TopSpeed>
    </Car>
</Vehicle>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR">
<xsl:template match="/">
<html> 
<body>
  <h2>My Car Collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th style="text-align:left">Make</th>
      <th style="text-align:left">Model</th>
      <th style="text-align:left">Top Speed</th>
    </tr>
    <xsl:for-each select="Vehicle/Car">
    <tr>
      <td><xsl:value-of select="Make"/></td>
      <td><xsl:value-of select="Model"/></td>
      <td><xsl:value-of select="TopSpeed"/></td>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

This outputs a table in the following order:

  • Audi R8
  • Lamborghini Aventador
  • Ferrari TDF
  • Lamborghini Gallardo

But I'm trying to get the two Lamborghinis to appear one after the order, highest top speed first.

Any help will be appreciated.




Aucun commentaire:

Enregistrer un commentaire