How to use <xsl:call-template> and <xsl:apply-template>
How to implement String tokenizer functionality using XSLT
I will demo an example how to use call-template recursively in XSLT, that is to tokenizea given string based on specified delimiter.
<xsl:call-template> - Using this, we can invoke a template by name of the template
This is how we can define a template with the name as 'tokenize' and which accepts two parameters
<xsl:template name="tokenize">
<xsl:param name="separator" select="';'"/>
<xsl:param name="string"/>
<xsl:choose>
<xsl:when test="contains($string,$separator)">
<ns0:Role>
<xsl:value-of select="substring-before($string,$separator)"/>
</ns0:Role>
<xsl:call-template name="tokenize">
<xsl:with-param name="string"
select="substring-after($string,$separator)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<ns0:Role>
<xsl:value-of select="$string"/>
</ns0:Role>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
- This is how we invoke the template by passing the parameters
<xsl:call-template name="tokenize">
<xsl:with-param name="string"
select="oim:StringWithDelimiter"/>
</xsl:call-template>
<xsl:apply-template> - Using this we can invoke template by matching a specific element.
This is how we can define a template to invoke using <apply-template>
<xsl:template match="emp:Employee">
<emp:Name>
<xsl:value-of select="emp:FirstName"/>
</emp:Name>
</xsl:tempate>
How to invoke a template using <apply-template>
<xsl:apply-templates select="/emp:Employee"/>