If, Choose, When, Otherwise

Etudions les structures de contrôle alternative : si-alors et choix-parmi.
 feuille12.xsl  appliquée sur atomes.xml --> résultat
  <xsl:template 
       match="classification_atomique">
    <html><body>
      <table>
        <tr>
          <th>symbole</th>
          <th>numero</th>
        </tr>
        <xsl:apply-templates/>
    </table></body></html>
  </xsl:template>
  <xsl:template match="atome">
    <xsl:if test="type='gaz rare'">
      <tr>
        <td>
          <xsl:value-of select="nom"/>
        </td>
        <td>
          <xsl:value-of select="numero"/>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>
<html>
   <body>
      <table>
         <tr>
            <th>symbole</th>
            <th>numero</th>
         </tr>
         <tr>
            <td>h&eacute;lium</td>
            <td>2</td>
         </tr>
         <tr>
            <td>n&eacute;on</td>
            <td>10</td>
         </tr>   
         <tr>
            <td>argon</td>
            <td>18</td>
         </tr>         
      </table>
   </body>
</html>

La règle xsl:if teste une condition et si elle vaut vrai, exécute ses transformations. Il n'y a pas de clause "else". Içi, seuls les gaz rares sont "retenus".

 feuille13.xsl  appliquée sur atomes.xml --> résultat
<xsl:template match="classification_atomique">
  <html><body>
    <table>
      <tr>
        <th>type</th>
        <th>symbole</th>
        <th>numero</th>
      </tr>
      <xsl:apply-templates/>
  </table></body></html>
</xsl:template>
<xsl:template match="atome">
    <tr>
      <td>
        <xsl:choose>
          <xsl:when test="contains(type,'métal')">
            métal </xsl:when>
          <xsl:when test="contains(type,'gaz')">
            gaz </xsl:when>
        </xsl:choose>
      </td>
      <td>
        <xsl:value-of select="nom"/>
      </td>
      <td>
        <xsl:value-of select="numero"/>
      </td>
    </tr>
</xsl:template>
<html>
   <body>
      <table>
         <tr>
            <th>type</th>
            <th>symbole</th>
            <th>numero</th>
         </tr>    
         <tr>
            <td></td>
            <td>hydrog&egrave;ne</td>
            <td>1</td>
         </tr>        
         <tr>
            <td>
               gaz               
            </td>
            <td>h&eacute;lium</td>
            <td>2</td>
         </tr>       
         <tr>
            <td>
               m&eacute;tal 
               
            </td>
            <td>lithium</td>
            <td>3</td>
         </tr>
         
         <tr>
....

Plusieures instructions "when" et éventuellement une instruction "otherwise" sont parenthèsées dans un balisage "choose".

<xsl:if test="pattern"> transformations </xsl:if>
les transformations ne sont appliquées au noeud courant que si l'évaluation du pattern (prédicat
XPath) donne la valeur vrai.

<xsl:choose> plusieures transformations xsl:when [transformation xsl:otherwise] <xsl:choose>
Les transformations xsl:when ou xsl:otherwise s'applique au même noeud courant. Dès qu'une transformation xsl:when s'applique, sinon et éventuellement la transformation xsl:otherwise, la transformation xsl:choose se termine.

<xsl:when test="pattern"> transformations </xsl:when>
les transformations ne sont appliquées au noeud courant que si l'évaluation du pattern (prédicat
XPath) donne la valeur vrai.

<xsl:otherwise> transformations </xsl:otherwise>
les transformations ne sont appliquées au noeud courant que si aucune transformation xsl:when ne s'applique.

Exercice :

A partir de famille_atome.xml, construire un nouveau fichier xml ne comportant que les atomes de masse <= 12, comme suit :

$ java Transform feuille12exo.xsl famille_atome.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<classification_atomique>
<famille>gaz rare
<atome>hélium</atome>
</famille>
<famille>métal alcalin
<atome>lithium</atome>
</famille><famille>métal alcalino-terreux
...

 index  suivant