Latest web development tutorials

XSLT <xsl: if>

Instrukcja XSLT Elementy referencyjny Instrukcja XSLT Elementy referencyjny

Definicja i Wykorzystanie

<Xsl: if> element zawiera szablon, tylko wtedy, gdy zostaną spełnione określone warunki, przed zastosowaniem tego szablonu.

Porada: <xsl: choose> i <xsl: when> i <xsl: otherwise> wyrazić wiele warunków są używane w połączeniu z testem!


gramatyka

<xsl:if
test="expression">

<!-- Content: template -->

</xsl:if>

nieruchomość

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


Przykłady

Kiedy cena jest wyższa niż 10 CD, wybierz wartość tytułu i artysty:

Przykład 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>

Spróbuj »

Wyświetla tytuł każdej płycie. Jeśli nie jest to ostatni lub przedostatni CD, CD-title pomiędzy każdą wkładką "." Jeśli ostatniej płycie, po czym dodaje się po hasłem "!". Jeśli jest to przedostatni CD, a następnie po dodaniu tytułem ", i":

Przykład 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>

Spróbuj »


Instrukcja XSLT Elementy referencyjny Instrukcja XSLT Elementy referencyjny