[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Apply Templates, when to use which? how do templat

Subject: Re: Apply Templates, when to use which? how do templates work?
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 28 Aug 2007 13:50:37 +0100
Re:  Apply Templates
> How does one get rid of default templates?

just define templates that do something else.

for example if you do (and I often do)

<xsl:template match="*">
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
xsl:apply-templates select="/")

> 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
<xsl:template match="img">
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. 

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.