RE: [xsl] template match for subelement throughout tree

Subject: RE: [xsl] template match for subelement throughout tree
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 15 Apr 2003 15:57:15 -0400
Kathy,

At 01:48 PM 4/15/2003, you wrote:
May I ask:

The first templates creates a "whee" attribute for each <parent>?

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


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================


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



Current Thread