[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: template match for subelement throughout tree
Kathy,
At 01:48 PM 4/15/2003, you wrote: May I ask: Same code, commented -- <xsl:template match="parent"> <xsl:apply-templates mode="whee"/> <!-- selects all child nodes for processing (the default); processes them by finding templates to match them and instantiating each template - BUT - only matches templates in mode="whee" --> <xsl:apply-templates/> <!-- does exactly the same, EXCEPT matches templates in no mode --> </xsl:template> <xsl:template match="child"> <!-- any elements named 'child' can be matched by this template --> <xsl:text>Oo!</xsl:text> <!-- when instantiated, outputs the text 'Oo!' --> </xsl:template> <xsl:template match="child" mode="whee"> <!-- the same as the last, also matches 'child' elements, except this time in a mode="whee" --> <xsl:text>Whee!</xsl:text> <!-- when instantiated, outputs the text 'Whee!' --> </xsl:template> If your input looks like this <parent> <child/> <child/> <child/> </parent> Your output will look like: Whee!Whee!Whee!Oo!Oo!Oo! That is: first one 'Whee!' for each child, then one 'Oo!'. Both templates were applied *for each* (note language ;-) <child> element since the template matching <parent> said "apply templates (to my children), but use mode 'whee'", and then after that it said "apply templates (to my children)". Note (IMPORTANT) that the order of the templates doesn't matter (change it and see), but the order of the apply-templates instructions certainly does (again, try it). Why is the <xsl:apply-templates/> needed after <xsl:apply-templates mode="whee"/>? It's only here to show what happens if we process the children of the current node (the <parent> element matching the template), in no mode. (We only use modes when we need them.) Does each <child> end up with Whee! and Oo! ? Yup -- because first we processed the children (my example has three of them) with a template that gave us a Whee!, then after that they were processed again with a template that gave us an Oo! Sorry to be dense...brain overload. Not at all: the stuff isn't really self-explanatory. I brought it up because it seemed to me that underlying your trouble with xsl:for-each problem was really that you're still feeling your way with the XSLT processing model. One reason for the school of thought that says "newbies shouldn't use for-each" is that for-each lets you avoid learning about this stuff and still get useful results -- until you hit a brick wall. You have to learn how templates work if you want to learn to use the language effectively to do anything more than trivial. :-> This topic is covered in the literature and in the archives of this list. Research "XSL processing model". I suggest copying the toy data and stylesheet and playing around with it. For fun, try adding the templates <xsl:template match="pet"> <!-- any elements named 'pet' can be matched by this template --> <xsl:text>Woof!</xsl:text> <!-- when instantiated, outputs the text 'Woof!' --> </xsl:template> <xsl:template match="pet" mode="whee"> <xsl:text>Grrr!</xsl:text> </xsl:template> and running the whole collection of templates on source like <parent> <child/> <pet/> <child/> <child/> <pet/> </parent> This exercise will be very rewarding for anyone not yet quite sure of how templates work. Cheers, Wendell
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|