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

Subject: Re: [xsl] [xslt 2.0] Difference betwen functions and templates
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sat, 21 Jul 2007 12:04:28 +0530
Hi Justin,
 I agree with you, that "xsl:template match= ..." is the essence of
XSLT. Without it, XSLT cannot work.

I feel, it's interesting to discuss the differences between
"xsl:template name= ..." and xsl:function. Both are callable
components, and are sort of subroutines (they, actually are). Are both
of these needed in XSLT 2.0, or we can just have xsl:function or
xsl:template name= ...

As we have already discussed, xsl:function (a user-defined function)
can be called only from within a XPath expression (something like,
/abc/pqr[xx = ns:func()]/uvw

(this is a new thing in XSLT/XPath 2.0, and is a very useful innovation).

But, xsl:template name= ... *cannot* be called from a XPath
expression. Now the questions is, is xsl:template name= ... able to do
something, which xsl:function cannot do? Yes, xsl:template name= has
some features, which xsl:function doesn't have.

Please consider the below example:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xx="http://dummy-ns";
xmlns:xsl=""; version="2.0">

<xsl:output method="xml" indent="yes" />

<xsl:template match="/">
    <xsl:call-template name="dummy-template" />

<xsl:function name="xx:dummy-func" as="node()+">

<xsl:template name="dummy-template">


I am calling a named template, using xsl:call-template. The output of
this stylesheet is:

<?xml version="1.0" encoding="UTF-8"?>
<result xmlns:xx="http://dummy-ns";>

Can we do a similar thing using xsl:function? We cannot..

For example, will this work?

<xsl:template match="/">

or, will this work?

<xsl:template match="/">
   <xsl:value-of select="xx:dummy-func()" />

(xsl:value-of constructs a text node, so this won't work)

I would have considered xsl:function and xsl:template name= ... to be
equivalent, if [1] produced the same output as, xsl:call-template.

My conclusion therefore is, xsl:function and xsl:template name= are
very different, and both are required in XSLT 2.0.

On 7/19/07, Justin Johansson <procode@xxxxxxxxxx> wrote:

Thanks for your contribution to this lively discussion.

>I feel, having xsl:template in XSLT 2.0 is very important, for XSLT
>2.0 to be backward compatible with XSLT 1.0.

It is not a question of backward compatibilty.

xsl:template is the very essence of XSLT as opposed to XQuery.

Take away xsl:template's from XSLT and you essentiallly have
XQuery availalble to you in a lesser XML-based syntax.

The whole model of XSLT is to present to the programmer
a series of nodes which can be matched to a path-match
expression and then have automatic invocation of a rule to
prepare (return) a result (nodal) series (aka sequence) upon
recursive descent of the input XML tree.

This is important in anyone's undertanding of XSLT :-

What the XSLT processor does for you, and saves you the
trouble of, is to do a recursive descent of the source XML
tree and match rules, which you declare via xsl:template,
to be fired (much like callbacks in C/C++) whenever
the recursive descent of the source tree encounters
an XML node which matches the pattern you declared via
xsl:template match=

XQuery has no facility to do this.  XQuery is dumbed down XSLT,
though has some facilities which are important when dealing
with larger databases.

XQuery, exists soley for the reason of an attempt to by-pass an
XML-based programming language syntax for the processing XML.

I am sure that Michael Kay would reply in a more concise manner than myself.

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.

>They probably look similar, but IMHO both are useful members of XSLT
>2.0. As you also said, xsl:function can be called from within an XPath
>expression (and, this is a very important difference) while
>xsl:template cannot be.

>I feel, having xsl:template in XSLT 2.0 is very important, for XSLT
>2.0 to be backward compatible with XSLT 1.0.

>And of course, xsl:function is really beneficial, as also stated by
>others in this thread.
>Mukul Gandhi

Justin Johansson
Schema-aware XSLT evangelist

Mukul Gandhi

Current Thread