if_1-input.xml
<?xml version="1.0"?>
<products>
<product
id="p1" stock="4"/>
</products>
if_1-stylesheet.xsl
<?xml version="1.0"?>
<xsl:stylesheet
version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable
name="x" select="products/product"/>
<xsl:output
indent="yes"/>
<xsl:template
match="/">
<products>
<product>
<xsl:attribute
name="id" select="$x/@id"/>
<xsl:attribute
name="stock" select="$x/@stock"/>
<xsl:if
test="$x/@stock < 5">
<xsl:attribute
name="reorder" select="'yes'"/>
</xsl:if>
</product>
</products>
</xsl:template>
</xsl:stylesheet>
if_1-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product
id="p1" stock="4" reorder="yes"/>
</products>
The xsl:if element can be nested inside another xsl:if element. If two xsl:if elements follow one another it is probably time to use a real "switch" instead that is xsl:choose. In XPath 2.0 expressions can use if-then-else like this: <xsl:value-of select="if (…) then 'a' else 'b'"/>
Updated 2009-03-19