with-param_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"/>
</products>
with-param_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:attribute
name="color"><xsl:call-template
name="color"><xsl:with-param
name="id" select="@id"/></xsl:call-template></xsl:attribute>
</PRODUCT>
</xsl:template>
<xsl:template
name="color">
<xsl:param
name="id"/>
<xsl:choose>
<xsl:when
test="$id = 'p1'">green</xsl:when>
<xsl:when
test="$id = 'p2'">blue</xsl:when>
<xsl:otherwise>white</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
with-param_1-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<PRODUCTS>
<PRODUCT
id="p1" price="3250" stock="4" color="green"/>
<PRODUCT
id="p2" price="1000" stock="5" color="blue"/>
<PRODUCT
id="p3" price="1200" stock="19" color="white"/>
</PRODUCTS>
The called template must declare the same parameter in order to pick up the value. Parameters can also be used at global level acting like an external variable getting its value from outside the stylesheet, see: xsl:param.
Parameters can also be sent from template to template with xsl:apply-templates.
Updated 2009-03-19