Re: [xsl] Is xsl:for-each "syntactic sugar"?

Subject: Re: [xsl] Is xsl:for-each "syntactic sugar"?
From: "C. M. Sperberg-McQueen" <cmsmcq@xxxxxxxxxxxxxxxxx>
Date: Fri, 7 May 2010 10:43:45 -0600
On 7 May 2010, at 00:27 , Markus Karg wrote:


4. Favor recursive functions over xsl:for-each. (True or False)

Totally wrong, as it is much more complex to read and understand as a simple loop.

Very interesting -- my experience with XSLT is completely different. I think the use of apply-templates with a mode is much easier to read and understand than for-each.

For several years, in fact, I consistently found that if I had a bug
in a stylesheet that used for-each, recasting the for-each in terms
of apply-templates with a special mode invariably removed the bug.
In some cases, of course, I knew the bug was in the for-each, but
in a few cases I had no idea where the bug was; since the bug went
away when I eliminated the for-each, I inferred that it had been
there.  Perhaps this is just because I can never remember the
difference between context node and current node, and so make
mistakes.

So I think my advice to XSLT authors would be:  write the form which
seems clearer to you.  If unlike me you find for-each easy to
understand and to write without bugs, then by all means use it!
But if like me you find apply-templates natural and easy, then do not
be frightened away from it by fears that XSLT processors may optimize
it less well.  It may be that (as Dimitre Novatchev suggests)
for-each may run faster.  And so if you run into performance problems,
consider trying it.  (But remember Jon Bentley's first rule of program
optimization:  don't do it until you know you need to do it.)

YMMV, of course.

--
****************************************************************
* C. M. Sperberg-McQueen, Black Mesa Technologies LLC
* http://www.blackmesatech.com
* http://cmsmcq.com/mib
* http://balisage.net
****************************************************************

Current Thread