XSLT <xsl:if>
❮ Reference prvku XSLT
Definice a použití
Element <xsl:if> obsahuje šablonu, která bude použita pouze v případě, že je zadaná podmínka pravdivá.
Tip: Použijte <xsl:choose> ve spojení s <xsl:když> a <xsl:jinak> k vyjádření více podmíněných testů!
Syntax
<xsl:if
test="expression">
<!-- Content: template -->
</xsl:if>
Atributy
Attribute | Value | Description |
---|---|---|
test | expression | Required. Specifies the condition to be tested |
Příklady
Vyberte hodnoty názvu a interpreta, POKUD je cena CD vyšší než 10:
Příklad 1
<?xml version="1.0" encoding="UTF-8"?>
<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>
Zobrazení názvu každého CD. Pokud to není poslední nebo předposlední CD, vložte mezi každý titul CD ",". Pokud je to poslední CD, přidejte "!" za titulem. Pokud se jedná o předposlední CD, přidejte za název ", a ":
Příklad 2
<?xml version="1.0" encoding="UTF-8"?>
<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>
❮ Reference prvku XSLT