for-each_3-input.xml
<?xml version="1.0"?>
<products>
<product
id="p1" price="3250" stock="4"/>
<product
id="p2" price="1000" stock="5"/>
<product
id="p3" price="1200" stock="19"/>
<product
id="p4" price="1500" stock="5"/>
<product
id="p5" price="1225" stock="3"/>
</products>
for-each_3-stylesheet.xsl
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output
indent="yes"/>
<xsl:template
match="/">
<PRODUCTS>
<xsl:for-each
select="products/product">
<xsl:choose>
<xsl:when
test="@id = 'p2'"/>
<xsl:when
test="@stock <= 3">
<PRODUCT
id="{@id}" price="{@price}" stock="{@stock}" reorder="yes"/>
</xsl:when>
<xsl:otherwise>
<PRODUCT
id="{@id}" price="{@price}" stock="{@stock}"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</PRODUCTS>
</xsl:template>
</xsl:stylesheet>
for-each_3-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<PRODUCTS>
<PRODUCT
id="p1" price="3250" stock="4"/>
<PRODUCT
id="p3" price="1200" stock="19"/>
<PRODUCT
id="p4" price="1500" stock="5"/>
<PRODUCT
id="p5" price="1225" stock="3" reorder="yes"/>
</PRODUCTS>
The above works and it's ok but when we have a template with an xsl:choose switch inside an xsl:for-each it is high time finding a better structure for the stylesheet using many templates and xsl:apply-templates.
Updated 2009-03-19