XSLT <xsl: if> element
<Xsl: if> element is used to place a conditional test against the XML file content.
<Xsl: if> element
To place the conditions for testing the contents of the XML file, add the XSL document <xsl: if> element.
grammar
<xsl:if test="expression">
...如果条件成立则输出...
</xsl:if>
...如果条件成立则输出...
</xsl:if>
Place where <xsl: if> element
To add conditional tests in the XSL file <xsl: for-each> element is added inside <xsl: if> element:
Examples
<?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">
<xsl:if test="price > 10">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:if>
</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">
<xsl:if test="price > 10">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
try it"
Note: The value of the testattribute contains the essential need to be evaluated expression.
The above code will only output prices higher than the CD title and artist elements 10.