XSLT <xsl:quando>
❮ Completa il riferimento agli elementi XSLT
Definizione e utilizzo
L'elemento <xsl:when> viene utilizzato per specificare un'azione per l'elemento <xsl:choose>. L'elemento <xsl:when> valuta un'espressione e, se restituisce true, viene eseguita un'azione.
Nota: l'elemento <xsl:when> viene utilizzato insieme a <xsl:choose> e <xsl:otherwise> per esprimere più test condizionali.
Sintassi
<xsl:when
test="boolean-expression">
<!-- Content: template -->
</xsl:when>
Attributi
Attribute | Value | Description |
---|---|---|
test | boolean-expression | Required. Specifies a Boolean expression to be tested |
Esempio 1
Il codice sottostante aggiungerà un colore di sfondo rosa alla colonna artista QUANDO il prezzo del cd è superiore a 10.
<?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>
Visualizza il file XML , Visualizza il file XSL e Visualizza il risultato .
Esempio 2
Dichiara una variabile denominata "color". Imposta il suo valore sull'attributo color dell'elemento corrente. Se l'elemento corrente non ha un attributo color, il valore di "color" sarà "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>
❮ Completa il riferimento agli elementi XSLT