Original: A Egil refsnes translation: Atzie
Seven. XSL Control statements
1. Conditional Statement If...then
XSL also has conditional statements (hehe ~ ~ Good, like the program language). The specific syntax is to add a xsl:if element, similar to this
<xsl:if match= ". [artist= ' Bob Dylan '] >
... some output ...
</xsl:if>
The example above is rewritten as:
<?xml version= ' 1.0 '?>
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >
<xsl:template match= "/" >
<body>
<table border= "2" bgcolor= "Yellow" >
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select= "CATALOG/CD" >
<xsl:if match= ". [artist= ' Bob Dylan '] >
<tr>
<td><xsl:value-of select= "TITLE"/></td>
<td><xsl:value-of select= "ARTIST"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</xsl:template>
</xsl:stylesheet>
2. Choose of XSL
The purpose of choose is to have multiple conditions, giving different display results. The specific syntax is to add a set of xsl:choose,xsl:when,xsl:otherwise elements:
<xsl:choose>
<xsl:when match= ". [artist= ' Bob Dylan '] >
... some code ...
</xsl:when>
<xsl:otherwise>
... some code ....
</xsl:otherwise>
</xsl:choose>
The example above is rewritten as:
<?xml version= ' 1.0 '
<xsl:stylesheet xmlns:xsl= http://www.w3.org/TR/ Wd-xsl "
<xsl:template match="/"
<body>
<table Border= "2" bgcolor= "yellow"
<tr>
<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 match= ". [ artist= ' Bob Dylan ']
<td bgcolor= "#ff0000" ><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>
</xsl:template>
</ Xsl:stylesheet>