Nicholas Orr wrote, On 2008-09-29 04:51:
On 29/09/2008, at 11:38 AM, G. Ken Holman wrote:
I've got some XML that contains a list of items and groups, and each
group could contain either more items or more groups like so :
<catalog>
<item />
<group>
< group >
< item />
< item />
< group >
< group >
< item />
etc
So groups can contain either items or more groups or nothing. Items
only exist at the root (catalog) of this part of the tree or inside
groups. I'm outputting a single list of all of the items and group
currently using this :
<xsl:for-each select="item|descendant::group|descendant::group/item">
I expect you would have a more easily maintained stylesheet if you had:
This entire xslt file is pretty simple, it's basically just that one
for each and produces a single list of elements. Is there any other
advantages (other than easier maintenance) to use templates over the
for-each?
One advantage is that using the default processing engine in XSLT where
rules are expressed in terms of templates are more robust, if the
templates are properly defined, than for-each in terms of handling
faulty but well-formed XML formatted data. In particular, omission of
expected nodes (elements, attributes, etc.) can be handled. Note that by
validating the XML formatted data, you can avoid such situations.
[...]
/Jonas
--
Carpe Diem!
===
Jonas Mellin, Assistant Professor in Computer Science
School of Humanities and Informatics, Building E-2
University of Skövde, P.O. Box 408, SE-541 28 Skövde, Sweden
Phone: +46 500 448321, Fax: +46 500 448399
Email: jonas(_dot_)mellin(_at_)his(_dot_)se, URL: http://www.his.se/melj
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--