L'élément <xsl:choose> combiné avec <xsl:when> et <xsl:otherwise>, permet de construire des tests conditionnels à l'instar des commandes switch de Java ou Javascript.
<xsl:choose>
<xsl:when test="condition">
instructions...
</xsl:when>
...
<xsl:otherwise>
instructions...
</xsl:otherwise>
</xsl:choose>
Cet élément peut être contenu dans les instructions suivantes :
L'élément <xsl:choose> ne peut donc contenir que les élément <xsl:when> et <xsl:otherwise>.
Exemple : [voir]<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"
media-type="text/html; charset=ISO-8859-1" version="4.0"/>
<xsl:template match="/">
<html>
<head>
<title>La logithèque :
<xsl:value-of select="logitheque/categorie[position()=7]/@nom"/>
</title>
</head>
<body>
<table border="0" width="60%" class="produit">
<tr>
<th>Logiciel</th>
<th>Lien</th>
</tr>
<xsl:apply-templates
select="logitheque/categorie[position()=7]/logiciel"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="logitheque/categorie[position()=7]/logiciel">
<xsl:choose>
<xsl:when test="editeur/@lien != ''">
<xsl:variable name="url" select="editeur/@lien"/>
<tr>
<td class="c1">
<a href="{editeur/@lien}" target="_blank"
style="font-size:10pt; font-weight:bold">
<xsl:apply-templates select="nom"/>
</a>
</td>
<td>
<xsl:value-of select="$url"/>
</td>
</tr>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="url">failed.html</xsl:variable>
<tr>
<td class="c1">
<a href="failed.html" target="_blank"
style="font-size:10pt; font-weight:bold">
<xsl:apply-templates select="nom"/>
</a>
</td>
<td>
<xsl:value-of select="$url"/>
</td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet> |