apply-templates_1-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>
apply-templates_1-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:apply-templates/>
</PRODUCTS>
</xsl:template>
<xsl:template
match="product">
<PRODUCT
id="{@id}" price="{@price}" stock="{@stock}"/>
</xsl:template>
<xsl:template
match="product[@id = 'p2']"/>
<xsl:template
match="product[@stock <= 3]">
<PRODUCT
id="{@id}" price="{@price}" stock="{@stock}" reorder="yes"/>
</xsl:template>
</xsl:stylesheet>
apply-templates_1-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>
In the stylesheet above there are no benefits compared to a stylesheet with just one template and an xsl:choose switch inside an xsl:for-each. But many templates give us the option of reusing templates. They can be placed in other stylesheets we can include using xsl:include or import using xsl:import into the main stylesheet.
Note that an XSLT processor has some build in default templates. If an element is found that is not matched by a template in the stylesheet, the XSLT processor should say "xsl:apply-templates" in order to continue. See: http://www.w3.org/TR/xslt20/#built-in-rule.
Updated 2009-03-19