XSLT <xsl:elegir>
❮ Referencia de elementos XSLT
Definición y uso
El elemento <xsl:choose> se usa junto con <xsl:when> y <xsl:otherwise> para expresar múltiples pruebas condicionales.
Si ningún <xsl:when> es verdadero, se procesa el contenido de <xsl:otherwise>.
Si ningún <xsl:when> es verdadero y ningún elemento <xsl:otherwise> está presente, no se crea nada.
Sugerencia: para pruebas condicionales simples, use el elemento <xsl:if> en su lugar.
Sintaxis
<xsl:choose>
<!-- Content:(xsl:when+,xsl:otherwise?) -->
</xsl:choose>
Atributos
Ninguna
Ejemplos
El siguiente código agregará un color de fondo rosa a la columna del artista CUANDO el precio del CD sea superior a 10.
Ejemplo
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<xsl:choose>
<xsl:when test="price > 10">
<td bgcolor="#ff00ff">
<xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Declare una variable llamada "color". Establezca su valor en el atributo de color del elemento actual. Si el elemento actual no tiene atributo de color, el valor de "color" será "verde":
<xsl:variable name="color">
<xsl:choose>
<xsl:when test="@color">
<xsl:value-of select="@color"/>
</xsl:when>
<xsl:otherwise>green</xsl:otherwise>
</xsl:choose>
</xsl:variable>
❮ Referencia de elementos XSLT