XSLT <xsl: when> Element
Definicja i Wykorzystanie
<Xsl: when> Element służy do <xsl: choose> element określa podobnych działań. <Xsl: when> Element oceni wyraz, jeśli to prawda, to zwraca realizację postanowień działania.
Uwaga: <xsl: when> i <xsl: choose> i <xsl: otherwise> element, stosowany w połączeniu wyrazić wiele testów warunkowych.
gramatyka
<xsl:when
test="boolean-expression">
<!-- Content: template -->
</xsl:when>
test="boolean-expression">
<!-- Content: template -->
</xsl:when>
nieruchomość
属性 | 值 | 描述 |
---|---|---|
test | boolean-expression | 必需。规定要测试的布尔表达式。 |
Przykład 1
Następujący kod w cenie cd jest więcej niż 10 kolumny są dodawane do kolorze różowym tle muzyki.
<?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>
Zobacz plik XML , zobaczyć, jak plik XSL , przeglądać wyniki .
Przykład 2
Zadeklarować zmienną o nazwie "kolor" z. Jego wartość jest przyporządkowana do atrybutu koloru bieżącego elementu. Jeśli bieżący element ma atrybut koloru, "kolor" wartości będzie "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>
<xsl:choose>
<xsl:when test="@color">
<xsl:value-of select="@color"/>
</xsl:when>
<xsl:otherwise>green</xsl:otherwise>
</xsl:choose>
</xsl:variable>
Kompletna instrukcja XSLT element odniesienia