XSLT <xsl:choose>Elemento


XSLT 元素参考手册Manual de 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 true, se procesa el contenido de <xsl:otherwise>.

Si ningún <xsl:when> es true y no existe un elemento <xsl:otherwise>, no se crea ningún contenido.

Consejo:Para pruebas condicionales simples, use el elemento <xsl:if> en su lugar.


Sintaxis

<xsl:choose>

<!-- Content:(xsl:when+,xsl:otherwise?) -->

</xsl:choose>

Atributos

Ninguno


Ejemplo

El siguiente código agregará un color de fondo rosa a la columna artist cuando el precio del CD sea superior a 10.

Ejemplo

<?xml version="1.0" encoding="ISO-8859-1"?>
<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 &gt; 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>

Pruébalo »

Declare una variable llamada "color". Asigne el valor de esta variable al atributo color del elemento actual. Si el elemento actual no tiene un atributo color, el valor de "color" es "green":

<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>


XSLT 元素参考手册Manual de referencia de elementos XSLT Otras extensiones