[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: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 28 Aug 2007 07:37:59 -0400
Re:  Apply Templates
At 2007-08-28 12:05 +0100, John Smith wrote:
The concept of templates is confusing me a bit as I am used to think
of functions like imperative programming languages.

Many of my students are in this mindset when they are first introduced to XSLT.


So...

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?

No ... <xsl:apply-templates/> does a "push" of the addressed nodes to your stylesheet and your stylesheet "catches" the node in a template rule ... the template rule then governs the creation of the result tree.


1. Does
<template match="xyz"> ... </template>
match xyz regardless of whether I wanted it do do so or not? 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???

The starting point is when the processor pushes the root node (addressed as "/") at your stylesheet ... you catch it in an appropriate template rule and then all the control is in your hands. If you fail to catch a node pushed at your stylesheet, there are built-in template rules that do the work for you.


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" />

Those are different: the first pushes all child nodes of the current node, the second pushes all child elements named "xyz".


3. As there is also the "call-template" form of using a template, why
should one not always use that instead???, as it appears more easy to
understand in terms of the flow of the program.

Because it is imperative it would seem that way given your background you described. It is much like a function call. You call it when you want under control of the stylesheet. Template rules that are matched are driven by your data (though directed by your stylesheet in which data drives the action).


I know xsl is not
executed like other "normal(imperative)" programming languages, but
when using apply-template I feel like I lose control of what is
happening when and in which order.

No, you are not losing control, you are delegating control to your data instead of to your logic. Template matching "reacts" to the information found in your XML source.


4. What's the best practice for using applying templates in terms of
when to use which form??
<apply-templates />

Push all children nodes (elements, text, pis, comments) to your stylesheet's template rules that are in the unnamed mode.


<apply-templates select="xyz" />

Push all children elements named "xyz" at the template rules in your stylesheet in the unnamed mode.


<apply-templates select="xyz" mode="foo" />

Push all children elements named "xyz" at the template rules in your stylesheet in the mode named "foo".


<call-template name="bar" />

Immediately process the template in the template rule named "bar" regardless of what the current node is at the time.


5. Is it possible to use apply-templates without the "select"
attribute but with a "mode" attribute???

Absolutely ... then you are pushing all child nodes at the template rules in your stylesheet that are labeled with the given mode.


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

Absolutely.


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?

The template can be triggered either by you calling it or by you pushing a source tree node at it that matches what it is looking for. At all times you are in control of which is happening. When you call the template, it acts. When you push nodes at your stylesheet and any of them match the template, the template acts when the node arrives at the stylesheet.


7 thanks,

You're welcome ... these are pretty basic questions ... on our web site you will find a free download of an excerpt of our commercial electronic publication where answers to these basic questions are included.


There are also commercial training classes (I'm delivering a publicly-subscribed class in less than two weeks in San Francisco ... registration is still open; also check out Mulberry and Isogen as top-notch training companies in this area).

I hope this helps.

. . . . . . . . . Ken

--
Upcoming public training: XSLT/XSL-FO Sep 10, UBL/code lists Oct 1
World-wide corporate, govt. & user group XML, XSL and UBL training
RSS feeds:     publicly-available developer resources and training
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Jul'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

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.
Email
First Name
Last Name
Company
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.