<xsl:otherwise> | |
Définit la casse else ou default d'un élément <xsl:choose>. Cet élément apparaît toujours dans un élément <xsl:choose>, et il doit toujours apparaître en dernier. | |
Catégorie | |
Subinstruction (<xsl:otherwise> apparaît toujours comme une partie d'un élément <xsl:choose>). |
|
Attributs obligatoires | |
Aucun. |
|
Attributs facultatifs | |
Aucun. |
|
Contenu | |
Un modèle. |
|
Apparaît dans | |
L'élément <xsl:choose>. |
|
Défini dans | |
XSLT section 9.2, Traitement conditionnel avec xsl:choose. |
|
Exemple | |
L'exemple suivant utilise <xsl:choose> qui itère sur un ensemble de valeurs pour choisir la couleur d'arrière-plan d'une cellule d'une table HTML. Le document XML suivant a servi d'entrée : <?xml version="1.0"?> <list xml:lang="en"> <title>Albums I've bought recently:</title> <listitem>The Sacred Art of Dub</listitem> <listitem>Only the Poor Man Feel It</listitem> <listitem>Excitable Boy</listitem> <listitem xml:lang="sw">Aki Special</listitem> <listitem xml:lang="en-gb">Combat Rock</listitem> <listitem xml:lang="zu">Talking Timbuktu</listitem> <listitem xml:lang="jz">The Birth of the Cool</listitem> </list> La feuile de style suivante utilise un élément <xsl:choose> dans un élément <xsl:attribute> pour déterminer la valeur correcte de l'attribut bgcolor. L'élément <xsl:otherwise> génère la valeur whitesmoke pour les quatre éléments <listitem> du document source : <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="/"> <html> <head> <title> <xsl:value-of select="list/title"/> </title> </head> <body> <h1><xsl:value-of select="list/title"/></h1> <table border="1"> <xsl:for-each select="list/listitem"> <tr> <td> <xsl:attribute name="bgcolor"> <xsl:choose> <xsl:when test="@bgcolor"> <xsl:value-of select="@bgcolor"/> </xsl:when> <xsl:when test="position() mod 4 = 0"> <xsl:text>papayawhip</xsl:text> </xsl:when> <xsl:when test="position() mod 4 = 1"> <xsl:text>mintcream</xsl:text> </xsl:when> <xsl:when test="position() mod 4 = 2"> <xsl:text>lavender</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>whitesmoke</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:attribute> <xsl:value-of select="."/> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Le document XML généré est le suivant : Les quatre lignes ont pour couleur d'arrière-plan whitesmoke ; cette valeur a été générée par l'élément <xsl:otherwise> : <html> <head> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Albums I've bought recently:</title> </head> <body> <h1>Albums I've bought recently:</h1> <table border="1"> <tr> <td bgcolor="mintcream">The Sacred Art of Dub</td> </tr> <tr> <td bgcolor="lavender">Only the Poor Man Feel It</td> </tr> <tr> <td bgcolor="whitesmoke">Excitable Boy</td> </tr> <tr> <td bgcolor="papayawhip">Aki Special</td> </tr> <tr> <td bgcolor="mintcream">Combat Rock</td> </tr> <tr> <td bgcolor="lavender">Talking Timbuktu</td> </tr> <tr> <td bgcolor="whitesmoke">The Birth of the Cool</td> </tr> </table> </body> </html> Une fois affiché, le document HTML ressemble à laFigure A-7. Document itérant sur différentes couleurs d'arrière-plan |