Latest web development tutorials

XSLT <xsl: if> element

XSLT Elements Reference Manual XSLT Elements Reference Manual

Definition and Usage

<Xsl: if> element contains a template, only if specified conditions are met, before you apply this template.

Tip: The <xsl: choose> and <xsl: when> and <xsl: otherwise> to express multiple conditions are used in conjunction with the test!


grammar

<xsl:if
test="expression">

<!-- Content: template -->

</xsl:if>

Attributes

属性 描述
test expression 必需。规定要测试的条件。


Examples

When the price is higher than 10 CD, select the value of the title and artist of:

Example 1

<?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 &gt; 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"

Display the title of each CD. If it is not the last or penultimate CD, the CD-title between each insert "." If the last CD, then added after the heading "!." If this is the penultimate CD, then after adding the title ", and":

Example 2

<?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>
<p>Titles:
<xsl:for-each select="catalog/cd">
<xsl:value-of select="title"/>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:if test="position()=last()-1">
<xsl:text> and </xsl:text>
</xsl:if>
<xsl:if test="position()=last()">
<xsl:text>!</xsl:text>
</xsl:if>
</xsl:for-each>
</p>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

try it"


XSLT Elements Reference Manual XSLT Elements Reference Manual