tags:

views:

6952

answers:

3

in XSLT processing, is there a performance difference between apply-template and call-template? In my stylesheets there are many instances where I can use either, which is the best choice?

+3  A: 

apply-template and call-template do not perform the same task, performance comparison is not really relevant here. call-template takes a template name as a parameter whereas apply-template takes an xpath expression. Apply-template is therefore much more powerful since you do not really know which template will be executed. You will get performance issues if you use complex xpath expressions. Avoid "//" in your xpath expressions since every node of your input document will be evaluated.

Alexandre Victoor
A: 

It may depend on the xml parser you are using. I can't speak for anything but .NET 2003 parser where I did some informal performance tests on push vs pull XSLT code. This is similar to what you are asking: apply-template = push and call-template = pull. I was convinced push would be faster, but that was not the case. It was about even.

Sorry I don't have the exact tests now. I recommend trying it out with your parser of choice and see if there is any major difference. My bet is there won't be.

Andrew Cowenhoven
+15  A: 

As with all performance questions, the answer will depend on your particular configuration (in particular the XSLT processor you're using) and the kind of processing that you're doing.

<xsl:apply-templates> takes a sequence of nodes and goes through them one by one. For each, it locates the template with the highest priority that matches the node, and invokes it. So <xsl:apply-templates> is like a <xsl:for-each> with an <xsl:choose> inside, but more modular.

In contrast, <xsl:call-template> invokes a template by name. There's no change to the context node (no <xsl:for-each>) and no choice about which template to use.

So with exactly the same circumstances, you might imagine that <xsl:call-template> will be faster because it's doing less work. But if you're in a situation where either <xsl:apply-templates> or <xsl:call-template> could be used, you're probably going to be doing the <xsl:for-each> and <xsl:choose> yourself, in XSLT, rather than the processor doing it for you, behind the scenes. So in the end my guess it that it will probably balance out. But as I say it depends a lot on the kind of optimisation your processor has put into place and exactly what processing you're doing. Measure it and see.

My rules of thumb about when to use matching templates and when to use named templates are:

  • use <xsl:apply-templates> and matching templates if you're processing individual nodes to create a result; use modes if a particular node needs to be processed in several different ways (such as in the table of contents vs the body of a document)
  • use <xsl:call-template> and a named template if you're processing something other than an individual node, such as strings or numbers or sets of nodes
  • (in XSLT 2.0) use <xsl:function> if you're returning an atomic value or an existing node
JeniT
thank you for explaining that <xsl:call-template> operates on the current context node
David Alpert