Latest web development tutorials

XSLT <xsl: choose>

Instrukcja XSLT Elementy referencyjny Instrukcja XSLT Elementy referencyjny

Definicja i Wykorzystanie

<Xsl: choose> i <xsl: when> i <xsl: otherwise> używany w połączeniu, aby wyrazić wiele testów warunkowych.

Jeśli nie ma <xsl: when> jest prawdą, proces <xsl: otherwise> treść.

Jeśli nie ma <xsl: when> jest prawdą, a nie <xsl: otherwise> element nie tworzą niczego.

Wskazówka: W przypadku prostych testów warunkowym, użyj <xsl: if> zamiast.


gramatyka

<xsl:choose>

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

</xsl:choose>

nieruchomość

nie


Przykłady

Poniższy kod będzie wyższa niż 10 w cenie dysku CD, należy dodać kolumnę w kolorze różowym tle muzyki.

Przykłady

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

Spróbuj »

Zadeklarować zmienną o nazwie "kolor" z. Wartość zmiennej przypisuje atrybutowi barwowej bieżącego elementu. Jeśli bieżący element ma atrybut koloru, "kolor" ma wartość "zielony":

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


Instrukcja XSLT Elementy referencyjny Instrukcja XSLT Elementy referencyjny