[xsl] [xslt 2.0] Difference betwen functions and templates

Subject: [xsl] [xslt 2.0] Difference betwen functions and templates
From: Justin Johansson <procode@xxxxxxxxxx>
Date: Thu, 19 Jul 2007 10:19:46 +0900
Please correct me if I am wrong on this one :-

There is very little difference between the end result of executing an
XSLT 2.0 function or template.  That is, both generate a result sequence.
The difference is the context in which they may be invoked.

xsl:templates can, and must, be invoked within a sequence constructor either
via xsl:apply-templates (it the case of a match template) or xsl:call-template
(in the case of a named template)

xsl:functions can, and must, be invoked from an XPath expression.

Consequently the use of call-template is extemely analogous to
invoking a function apart from the context in which the respective
constructs are called.  There are subtle differences in the declaration
of params for a template as opposed to a function.  There is also
a difference in the focus being initially defined for a (named) template
but not so for a function.

The bottom line is that named templates and functions are essentially
the same thing and it would have been possible for XSLT 2.0 to do
away with either xsl:call-template or xsl:function.

The rationale for asking this question is that is generally a good idea
to eliminate choices (from the programmer) for the way something is
accomplished unless there are compelling reasons to provide the
alternatives.

This is not to suggest that XSLT 2.0 should be changed;  rather it is
to assist in writing an article of XSLT style guidelines.

Thanks to all.

Justin Johansson
Freelance XML / XSLT / XQuery Developer
Australia

procode(at)tpg(dot)com(dot)au

Current Thread