Difference between <xsl: apply-template> and <xsl: call-template>?
At the most basic level, you use <xsl:apply-templates> when you want the processor to process nodes automatically, and you use <xsl:call-template/> when you want finer control over processing. Therefore, if you have:
<foo> <boo>World</boo> <bar>Hello</bar> </foo> And you have the following XSLT:
<xsl:template match="foo"> <xsl:apply-templates/> </xsl:template> <xsl:template match="bar"> <xsl:value-of select="."/> </xsl:template> <xsl:template match="boo"> <xsl:value-of select="."/> </xsl:template> You will get WorldHello result. Essentially, you said “bar handle and boo this way”, and then you let the XSLT processor process these nodes as they appear. In most cases, you should do something in XSLT.
Sometimes, however, you want to do something more fun. In this case, you can create a special template that does not match a specific node. For instance:
<xsl:template name="print-hello-world"> <xsl:value-of select="concat( bar, ' ' , boo )" /> </xsl:template> And you can call this template during <foo> processing, rather than automatically processing the child nodes of foo :
<xsl:template match="foo"> <xsl:call-template name="print-hello-world"/> </xsl:template> In this particular artificial example, you now get "Hello World" because you redefined the default processing to do the trick.
Hope this helps.
Could you explain to me the difference between
<xsl:apply-template>and<xsl:call-template>, and when should I use<xsl:call-template>?
You can use <xsl:call-template> , but almost never follows .
It is in XSLT that you allow the XSLT processor to determine exactly which template best matches the node, and decide to use this template to process the node. This gives us clean, easy, and powerful extensibility and polymorphism.
In general, comparing xsl:apply-templates - xsl:call-template similar to comparing a call to a virtual method from a base class with a call to a non-virtual method directly.
Here are some important differences :
xsl:apply-templatesmuch richer and deeper thanxsl:call-templatesand even fromxsl:for-each, simply because we don’t know which code will be used on the nodes of the choice - in general, this code will be different for different nodes of the list node.The code that will be applied can be written after
xsl:apply-templatesand people who do not know the original author.
FXSL library implementation of higher order functions (HOF) in XSLT is not possible if XSLT did not have an <xsl:apply-templates> instruction.
Summary The templates and the <xsl:apply-templates> statement are how XSLT implements and processes polymorphism. The use of xsl:call-template can and should be avoided, which does not allow polymorphism and reuse and flexibility limitations.
Link See this chain: http://www.stylusstudio.com/xsllist/200411/post60540.html