XSLT <xsl: when> elemento
Definizione e utilizzo
<Xsl: when> elemento viene utilizzato per il <xsl: choose> elemento specifica le azioni correlate. <Xsl: when> elemento valuterà l'espressione, se restituisce vero, allora l'attuazione delle disposizioni dell'azione.
Nota: <xsl: when> e <xsl: choose> e <xsl: altrimenti> elemento utilizzato in abbinamento ad esprimere più test condizionali.
grammatica
<xsl:when
test="boolean-expression">
<!-- Content: template -->
</xsl:when>
test="boolean-expression">
<!-- Content: template -->
</xsl:when>
proprietà
属性 | 值 | 描述 |
---|---|---|
test | boolean-expression | 必需。规定要测试的布尔表达式。 |
esempio 1
Il seguente codice nel prezzo cd è più di 10 colonne vengono aggiunte alla sfondo rosa dell'artista colore.
<?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>'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>
<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>
Visualizzare il file XML , vedere il file XSL , visualizzare i risultati .
esempio 2
Dichiarare una variabile chiamata "colore" di. Il suo valore viene assegnato all'attributo colore dell'elemento corrente. Se l'elemento corrente non ha attributo di colore, "colore" del valore sarà "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>
<xsl:choose>
<xsl:when test="@color">
<xsl:value-of select="@color"/>
</xsl:when>
<xsl:otherwise>green</xsl:otherwise>
</xsl:choose>
</xsl:variable>
Manuale Completo XSLT elemento di riferimento