Both xsl:for-each
and xsl:template
are used to retrieve nodes from xml in an xsl stylesheet. But what is the fundamental difference between them? P
It doesn't really matter, but you may want to think about it for the following thumb of rules that I found:
<xsl:for-each>
.Reference and read more at: http://www.jenitennison.com/blog/node/9
Both the 'for-each' and 'template' are used to retrieve the nodes from xml in the xsl. But what is the difference between them in basically
Here are some of the most important differences:
xsl:apply-templates
is much richer and deeper than xsl:for-each
, even
simply because we don't know what code will be applied on the nodes of
the selection -- in the general case this code will be different for
different nodes of the node-list.
The code that will be applied
can be written way after the xsl:apply template
s was written and by
people that do not know the original author.
The FXSL library's implementation of higher-order functions (HOF) in XSLT wouldn't be possible if XSLT didn't have the <xsl:apply-templates>
instruction.
Summary: Templates and the <xsl:apply-templates>
instruction is how XSLT implements and deals with polymorphism.
Reference: See this whole thread: http://www.stylusstudio.com/xsllist/200411/post60540.html
for-each
can only be used inside one place in your template. Templates can be re-used with different apply-templates
calls. I mostly use templates instead of for-each because of the added flexibility.
One use of for-each
I haven't seen mentioned: you can use it to switch the context node to another document. I've used it to transform data XML to a HTML input form. The template that matched a data field contained a for-each that selected a single node: the xs:element
in the XSD that described the data field to transform.
Using the description in the XSD one data field could be transformed to a radio button group, a drop down box, or a plain and simple text input. Without for-each
I couldn't walk through the two documents at the same time.
In general, I prefer matching templates. I find that it corresponds to the notion of a single transform applied at once better than for-each-ing this node, then the next, then the one after that, etc.. But that's a personal preference, of course.
These are to complete different XSLT instructions.
More than push vs. pull style, this is more like iteration vs. recursion.
xsl:for-each
is an iterator instruction with all the benefits and constrains of iteration in a stateless declarative paradigm: a good processor should not polute the call stack.
xsl:apply-templates
is a general recursion instruction. General in the sense that it's more powerful than xsl:call-template
: you "throw" the selected nodes to the pattern matching mechanism, a truly "dynamic function invocation".
I think this has some what to do with understanding push vs. pull style processing than just comparing xsl:for-each
or xsl:template match="..."
. You often see programmers from another discipline using a lot of xsl:if
, xsl:choose
and for-loops when the problem could have been solved in a more elegant XSLTish way.
But to the question: In my opinion, if you consider using xsl:for-each
instead of processing the data with xsl:apply-templates
you need to rethink. There are cases where a for-loop is suitable in XSLT, but whenever a matching template would do the same, templates are the way to go. In my experience, you can usually do most xsl:for-each
with an xsl:apply-templates
instead.
Some benefits as I see it of using matching templates over a for-loop are:
xsl:next-match
in XSLT 2.0 you can chain templates together in powerful ways.xsl:apply-templates
and let the processor work for you.