Sort

Trions un peu nos atomes :

 feuille14.xsl  appliquée sur atomes.xml --> résultat
  <xsl:template 
        match="classification_atomique">
    <html><body>
      <table>
        <tr>
          <th>symbole</th>
          <th>ligne</th>
          <th>colonne</th>
        </tr>
        <xsl:apply-templates select="atome">
          <xsl:sort select="nom"/>
        </xsl:apply-templates>
    </table></body></html>
  </xsl:template>
  <xsl:template match="atome">
    <tr>
      <td>
        <xsl:value-of select="nom"/>
      </td>
      <td>
        <xsl:value-of select="@ligne"/>
      </td>
      <td>
        <xsl:value-of select="@colonne"/>
      </td>
    </tr>
  </xsl:template>
<html>
   <body>
      <table>
         <tr>
            <th>symbole</th>
            <th>ligne</th>
            <th>colonne</th>
         </tr>
         <tr>
            <td>aluminium</td>
            <td>3</td>
            <td>3</td>
         </tr>
         <tr>
            <td>argon</td>
            <td>3</td>
            <td>8</td>
         </tr>
         <tr>
            <td>azote</td>
            <td>2</td>
            <td>5</td>
         </tr>
....

Dans le template "classification_atomique", "on lance" apply-templates sur les noeuds fils de pattern "atome" selon leur ordre original, puis "on les tri" selon la valeur de leur nom. xsl:sort doit figurer juste aprés apply-templates.

 feuille15.xsl  appliquée sur atomes.xml --> résultat
  <xsl:template 
       match="classification_atomique">
    <html><body>
      <table>
        <tr>
          <th>symbole</th>
          <th>ligne</th>
          <th>colonne</th>
        </tr>
        <xsl:apply-templates select="atome">
          <xsl:sort select="@colonne"/>
            <xsl:sort select="@ligne"/>
        </xsl:apply-templates>
    </table></body></html>
  </xsl:template>
  <xsl:template match="atome">
    <tr>
      <td>
        <xsl:value-of select="nom"/>
      </td>
      <td>
        <xsl:value-of select="@ligne"/>
      </td>
      <td>
        <xsl:value-of select="@colonne"/>
      </td>
    </tr>
  </xsl:template>
<html>
   <body>
      <table>
         <tr>
            <th>symbole</th>
            <th>ligne</th>
            <th>colonne</th>
         </tr>
         <tr>
            <td>hydrog&egrave;ne</td>
            <td>1</td>
            <td>1</td>
         </tr>
         <tr>
            <td>lithium</td>
            <td>2</td>
            <td>1</td>
         </tr>
         <tr>
            <td>sodium</td>
            <td>3</td>
            <td>1</td>
         </tr>
         <tr>
            <td>b&eacute;rillium</td>
            <td>2</td>
            <td>2</td>
         </tr>
....

Içi, le tri s'effectue selon 2 critères : d'abord la valeur de l'attribut colonne, et en cas d'égalité sur la valeur de l'attribut ligne.

<xsl:sort [select="pattern"] [lang="langue"] [data-type="text|number"] [order="ascending|descending"]/>
xsl:sort doit figurer juste aprés xsl:apply-templates ou xsl:for-each ou xsl:sort. Le tri s'effectue sur les valeurs du pattern spécifié. Peuvent être précisés : la langue, ordre lexicographique (par défaut) ou numérique, ordre acendant (apr défaut) ou descendant.

Exercice :

Comme la feuille 15, mais en triant sur le nom de famille et secondairement sur les numeros décroissants d'atomes :

$ java Transform feuille15exo.xsl atomes.xml
<html>
<body>
<table>
<tr>
<th>symbole</th><th>ligne</th><th>colonne</th>
</tr>
<tr>
<td>hydrog&egrave;ne</td><td>1</td><td>1</td>
</tr>
<tr>
<td>aluminium</td><td>3</td><td>3</td>
</tr>
<tr>
<td>argon</td><td>3</td><td>8</td>
</tr>
<tr>
<td>h&eacute;lium</td><td>1</td><td>8</td>
</tr>
<tr>
<td>n&eacute;on</td><td>2</td><td>8</td>
...

 index  suivant