Subject: Re: [xsl] Converting attributes to elements and preserving the hierarchy From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx> Date: Wed, 19 Dec 2001 17:06:46 +0000 |
Wendell asked: > *Anything* you can do with a for-each, you can do with a template > (with a mode if necessary). > > I wonder whether the reverse is the case. (Can anyone suggest > something that can't be done with a for-each, but can be done with > templates? I can think of things much harder to do with for-each, > but impossible?) It's impossible to process arbitrary levels of nesting with just xsl:for-each (well, unless you are prepared to generate a stylesheet with a stylesheet to do it!). Say you had a document with section elements that can nest inside each other. You can do something like the following to process the top-level sections: <xsl:for-each select="section"> ... </xsl:for-each> To process the sections at the next level, you need to add a nested xsl:for-each: <xsl:for-each select="section"> ... <xsl:for-each select="section"> ... </xsl:for-each> </xsl:for-each> And of course to process the sections nested inside that, you need another one: <xsl:for-each select="section"> ... <xsl:for-each select="section"> ... <xsl:for-each select="section"> ... </xsl:for-each> </xsl:for-each> </xsl:for-each> And so on. But if you don't know how many levels the sections might nest to, then you can't predict how many xsl:for-eachs you're going to need. With templates, on the other hand, it's straight-forward: <xsl:template match="section"> ... <xsl:apply-templates select="section" /> </xsl:template> In fact, sections are an easy example (in that you don't need to do much different with each section). Imagine trying to process an HTML document, converting all the phrase elements (b, i, em, strong, a, ...) into something else. Then not only would you have to have the xsl:for-eachs nesting an arbitrary number of times, you'd also have to have each one containing a massive xsl:choose that tested exactly which of the types of elements you were processing. Nested and unpredictable structures like these appear in document-oriented XML all the time. They also occur on occasion in more data-oriented XML - for example XML representing hierarchical structures, menus nested within menus. Cheers, Jeni --- Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Converting attributes to , David Carlisle | Thread | Re: [xsl] Converting attributes to , Wendell Piez |
[xsl] Re: document() merge DISTINCT, Dimitre Novatchev | Date | Re: [xsl] document() merge DISTINCT, Alex Schuetz |
Month |