call-template_1-input.xml
<?xml version="1.0"?>
<products>
<product
id="p1" price="3250" stock="4"/>
<product
id="p2" price="1000" stock="5"/>
</products>
call-template_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/>
<Updated>
<xsl:call-template
name="today"/>
</Updated>
</Products>
</xsl:template>
<xsl:template
match="product">
<Product
id="{@id}" price="{@price}" stock="{@stock}"/>
</xsl:template>
<xsl:template
name="today">
<xsl:value-of
select="format-date(current-date(), '[Y]-[M01]-[D01]')"/>
</xsl:template>
</xsl:stylesheet>
call-template_1-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<Products>
<Product
id="p1" price="3250" stock="4"/>
<Product
id="p2" price="1000" stock="5"/>
<Updated>2009-03-08</Updated>
</Products>
The third template has a name attribute instead of a match attribute. A named template works much like a user-defined function returning something. When using xsl:call-template it is very common also to send parameters with the call, see: xsl:with-param.
Now when we have a named template called "today", we can transfer it to an XSLT template library that can be included in many stylesheets.
The main problem with named template is that we can't call it from an attribute. That is we have no access to it from an XPath expression. In XSLT 2.0 we can use xsl:function to make user-defined functions but xsl:call-template is still popular especially for returning markup.
Updated 2009-03-19