I have some XSLT code like following
<xsl:choose>
<xsl:when test="v:Values = 'true'">
<xsl:text>A</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>B</xsl:text>
</xsl:otherwise>
...
</xsl:choose>
I want to use this chunk of code many times in the same file. Can I put it in a template and call it when needed?
Yes – It’s called xsl:call-template .
Any template can be given a name. The name can be qualified by a namespace. For example…
If the template has a name, it you can even omit the match condition like so…
Named templates have as many parameters as you like. The above fragment is an example of declaring one parameter named phone-number. Within the template’s sequence constructor, you will refer to this parameter, in the same manner as a variable, like so…
To invoke a named-template, use xsl:call-template from within a sequence constructor. For example …
Notice that
xsl:with-paramis used to pass actual parameter value.Note that in XSLT 2.0 you can also define functions callable from within XPATH expressions. In some circumstances, functions may be a better alternative to named templates.
Refer: