?
本文檔使用 PHP中文網(wǎng)手冊(cè) 發(fā)布
<xsl:if> 元素包含了一個(gè)模板,只有指定的條件成立時(shí),才應(yīng)用此模板。
提示:請(qǐng)把 <xsl:choose> 與 <xsl:when> 和 <xsl:otherwise> 協(xié)同使用來(lái)表達(dá)多重條件測(cè)試!
<xsl:if test="expression"> <!-- Content: template --> </xsl:if>
屬性 | 值 | 描述 |
---|---|---|
test | expression | 必需。規(guī)定要測(cè)試的條件。 |
當(dāng) CD 的價(jià)格高于 10 時(shí),選取 title 和 artist 的值:
<?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>
顯示每個(gè) CD 的標(biāo)題。如果不是最后一個(gè)或倒數(shù)第二個(gè) CD,則在每個(gè) CD-title 間插入 ", "。如果是最后一個(gè) CD,則在標(biāo)題后添加 "!"。如果是倒數(shù)第二個(gè) CD,則在標(biāo)題后添加 ", and ":
<?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>