[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: "John Smith" <debrief@xxxxxxxxx>
Date: Tue, 28 Aug 2007 12:44:48 +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 the 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)
How does one get rid of default 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="/">
> hello world
> then processing any input document always produces "hello world" it
> doesn't matter what other match templates are in the stylesheet as they
> will never be executed as this template just outputs the text and stops
> it does not apply templates to any more nodes.
What's the behaviour of xslt 2.0 in this regards?
> > 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.
What do each do differently? I don't have experience with
xslt(obviously apparent) but it appears that using call-template can
achieve what the others achieve plus the use of parameters.
Also, are there any efficiency implications with each of these ?
Many thanks for the reply, it has cleared up a lot of things I was
struggling with :S