XML & XSL - xsl:if


This tag is a simple conditional construct. Multi conditional testing is also possible by using xsl:choose, xsl:when, and xsl:otherwise.

Syntax

<xsl:if expr="script-expression" language="language-name" test="pattern">

Attributes

Parent Elements

Child Elements

Purpose

The example below will put a horizontal line between each continent.

A Countries Example Using xls:if

XML Data Tree Structure

Click here to see the XML structure of this example.

XML & XSL in Action

Click here to see the XSL formatted display of the example shown below.

The XSL Style Sheet

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
	<xsl:template>
		<xsl:apply-templates select="@*"/>
			<xsl:if test="@name[.='Africa']"><HR/></xsl:if>
			<xsl:if test="@name[.='Asia']"><HR/></xsl:if>
			<xsl:if test="@name[.='Europe']"><HR/></xsl:if>
			<xsl:if test="@name[.='Latin America and the Caribbean']"><HR/></xsl:if>
			<xsl:if test="@name[.='North America']"><HR/></xsl:if>
			<xsl:if test="@name[.='Oceania']"><HR/></xsl:if>
			<xsl:value-of select="@name"/>
		<xsl:apply-templates/>
	</xsl:template>
</xsl:stylesheet>