XSLT <xsl:se>
❮ Riferimento elemento XSLT
Definizione e utilizzo
L'elemento <xsl:if> contiene un modello che verrà applicato solo se una condizione specificata è vera.
Suggerimento: usa <xsl:scegli> insieme a <xsl:quando> e <xsl:altrimenti> per esprimere più test condizionali!
Sintassi
<xsl:if
test="expression">
<!-- Content: template -->
</xsl:if>
Attributi
Attribute | Value | Description |
---|---|---|
test | expression | Required. Specifies the condition to be tested |
Esempi
Seleziona i valori di titolo e artista SE il prezzo del CD è superiore a 10:
Esempio 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>
Visualizza il titolo di ogni CD. Inserire ", " tra ogni titolo del CD se non è l'ultimo CD o l'ultimo ma uno. Se è l'ultimo CD, aggiungi "!" dietro il titolo. Se è il penultimo CD, aggiungi ", e " dietro il titolo:
Esempio 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>
❮ Riferimento elemento XSLT