[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XSLT Generators / xsl:for-each vs. xsl:template

Subject: Re: XSLT Generators / xsl:for-each vs. xsl:template
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 10 May 2001 09:31:38 +0100
xslt recursion example
Hi Kyle,

> My question is if there are any strong feelings out there in regard
> to the loop vs. recursion model of writing XSLT.

This difference is sometimes referred to as pull vs. push (although
you're not using *true* push in your recursion example as you're still
selecting the nodes that you're applying templates to).

Strong feelings have previously been articulated on this list that
newcomers to XSLT should not use xsl:for-each because of their
tendency to think it's equivalent to the for loops they're used to in
procedural programming languages.

Generally templates have the advantage of breaking up your code nicely
- it's a bit like the difference between using sub-functions or
embedding the code within the main function itself. For
maintainability and to prevent repeating code all over the place, it
can be better to use templates.  On the other hand, using an
xsl:for-each makes the code easier to keep track of as you can work
through exactly what's happening right there.

The one time that I personally would recommend using xsl:for-each
rather than a template is if the set of instructions for the nodes
you're interested in use the position of the current node. For
example, if you were numbering the articles, I would use:

  <xsl:for-each select="rss/channel/item">
     <Article num="{position()}">
        <xsl:apply-templates select="title" />
     </Article>
  </xsl:for-each>

The reason for this is that the position() of the current node is
based on its position in the current node list - the nodes that are
being processed at the moment. If you used a template, the point at
which you select this node set and the point at which you use
information about it are separated. This means you're more likely to
change the nodes that are being selected without realising the effect
that this will have on the numbering that you're using. For example,
you might select all the children of the 'channel' element instead,
which means that the numbering would change. Keeping the code within
the xsl:for-each means that you can see exactly what nodes are being
selected and how changing that set of nodes will affect the numbering
of the nodes you're interested in.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.