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 12:20:29 +0100
> 0. Does <apply-templates select="xyz" /> perform a match on the xyz
> node??? xyz node and all its immediate children only??? or xyz node
> and all descendants of xyz node?


the xpath xyz selects a set of nodes, in this case the set of all
children of teh current node with that name. Templates are then applied
to those nodes (only) in document order. this call to apply templates
will not apply templates to descendants of any of these
nodes. Descendants will only be processed if one of the templates that
is applied recursively applies templates.  (The default template for
elements applies templates to child nodes, so there maty not be any
explict calls to apply-templates)


> 1. Does
> <template match="xyz"> ... </template>
> match xyz regardless of whether I wanted it do do so or not?


If you didn't want it to match xyz you would presumably use a different
match pattern. The template _matches_ xyz but it may or may not be
executed depending on whether templates are ever applied to that node.

>  What I'm
> trying to say there is no starting point of execution which I can use
> to then decide which template should be applied at any given moment???

In XSLT 1 the starting point is always / so if for example you have

<xsl:template match="/">
helloe world
</xsl:template>

then processing any input document always produces "hellow world" it
doesn't nmatter what other match templates are in teh stylesheet as they
will never be executed as this template just outputs teh text and stops
it does not apply templates to any more nodes.


> 2. If all templates are looked at for their match criteria regardless,
> then what is the point of having the following two different forms???
> <apply-templates /> and <apply-templates select="xyz" />

they are not two different forms, just that the selct attribute has a
default of node() which selects all child nodes. 
 <apply-templates /> is the same as  <apply-templates select="node()"/> 
it's just a bit of syntactic sugar for this common form.

> 4. What's the best practice for using applying templates in terms of
> when to use which form??
> <apply-templates />
> <apply-templates select="xyz" />
> <apply-templates select="xyz" mode="foo" />
> <call-template name="bar" />


It's not a case of best practice, they all do different things.

> 6. Is it possible to call-template where the template has a "match"
> attribute as well??

yes, if you use apply-templates then templates with match attributes
will be considered, and name attributes are ignored. If you use
call-template then name attributes are considered and match attributes
are ignored. So a template that has both a name and a match attribute
may be called by either mechanism.

> If so how does it work? I mean, does it call the
> template and the template checks for the match on the context node? or
> do the call-template and the "match" apply at different times, as in
> not both at the same time?

No if you call a template by name, any match attribute that it has is
irrelevant to that call.

David

________________________________________________________________________
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