[XSL-LIST Mailing List Archive Home]
Re: [xsl] Apply Templates, when to use which? how do templates work?
Subject: Re: [xsl] Apply Templates, when to use which? how do templates work?|
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 28 Aug 2007 13:50:37 +0100
> How does one get rid of default templates?
just define templates that do something else.
for example if you do (and I often do)
Unexpected element: <xsl:value-of select="name()"/>
Then the default behaviour if templates are applied to an element for
which a more specific template has been defined is to make a warning
message and not process the eelement or its descendents, instead of the
original default which is to silently ignore the element but process its
> What's the behaviour of xslt 2.0 in this regards?
xslt2 allows you to start at a named template (that is starting with an
implicit call to xsl:call-template, rather than an implict call to
> but it appears that using call-template can
> achieve what the others achieve plus the use of parameters.
No, on both counts, xsl:call-template can not replicate apply-template
functionality (unless you actually try to implement xslt using xslt as
the implementation language. And both apply-templates and call-template
have the same functionality with respect to parameter passing (with the
same syntax as well).
> Also, are there any efficiency implications with each of these ?
> <apply-templates />
> > <apply-templates select="xyz" />
> > <apply-templates select="xyz" mode="foo" />
as I said earlier <apply-templates />is <apply-templates
select="node()"/> so the difference between the first two is not a mater
of efficiency as requirements. It's like asking if calculating 1+1 is
more efficient than calculating cos(x)^2 it may be but if it's not the
answer you need that's not very usieful information. Clearly depending on
the complaecity of you real XPath expression ("xyz" here) this may take
an arbitrary amount of time to calculate teh set of nodes taht are going
to be evaluated, but if that is the set you need, then quickly
evaluating teh set of all child nodes with the first form isn't going to
help. the third form is just as efficient or inefficient as the second,
the process the xslt engine goes through is the same but using teh
templates marked with mode="foo" rather than those without a mode
attribute as happens in the first two cases.
> > <call-template name="bar" />
This is completely different to apply-templates so you can't really
compare it for efficiency.
There are several good sxslt tutorials on the web, and it's well worth
just experimenting with some small stylesheets to see the difference.
As a start consider a strylesheet that's just doing name changing, that
is copy th einput to the output but change <img> to <object>
using apply-templates you take an identity transform and add one extra
template to do teh chaneg specified
and that's all you need. It's virtually impossible to do that kind of
transform using call-template unless you code an explicit recursive walk
over the input tree (as you would have to do in XQuery for example,
which is essentially XSLT without xsl:apply-templates).
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.
This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs.