RE: [xsl] template match for subelement throughout tree

Subject: RE: [xsl] template match for subelement throughout tree
From: "Kathy Burke" <Kathy_Burke@xxxxxxxxx>
Date: Tue, 15 Apr 2003 13:48:03 -0400
Wendell,

Thanks for replying...I'm still foggy on the mode attribute...am I
interpreting your sample code correctly? May I ask:

The first templates creates a "whee" attribute for each <parent>?
Why is the <xsl:apply-templates/> needed after <xsl:apply-templates
mode="whee"/>?
Does each <child> end up with Whee! and Oo! ?
Sorry to be dense...brain overload.

Kathy

<xsl:template match="parent">
   <xsl:apply-templates mode="whee"/>
   <xsl:apply-templates/>
</xsl:template>

<xsl:template match="child">
    <xsl:text>Oo!</xsl:text>
</xsl:template>

<xsl:template match="child" mode="whee">
    <xsl:text>Whee!</xsl:text>
</xsl:template>

-----Original Message-----
From: Wendell Piez [mailto:wapiez@xxxxxxxxxxxxxxxx]
Sent: Tuesday, April 15, 2003 11:55 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] template match for subelement throughout tree


Hi Kathy,

At 07:46 AM 4/15/2003, you wrote:
>I'm still doing something wrong...sorry. For example, when I applied the
>changes to the template below, I get error messages about the "< not
allowed
>as attribute" etc.

That sounds like a simple syntax error -- my guess is a missing close quote 
after an attribute value, something like

<xsl:for-each select="node>
   <xsl:apply-templates/>
</xsl:for-each>

The XML parser sees nothing wrong with the attribute value until it gets to 
the < in the next line.

But it's a red herring. (Although it does indicate how useful it is to have 
a well-formed parser for checking your source and/or stylesheet, to snare 
these errors before running; they're easy to make and easy to fix, but have 
nothing to do with XSLT as such.)

>  Also, I thought for-each was needed in order to create a
>new table row for each listitem?

for-each is only a convenience for when you want your template inside 
another template. (The content of the for-each is actually a template and 
follows the rules of how templates are made.)

>P.S. When SHOULD you use for-each?

Some have advised newbies *never* to use for-each. While this point of view 
may be a bit extreme, it's certainly the case that understanding how 
for-each is really no different from calling templates on the selected 
nodes is a big step forward in understanding the XSLT processing model.

These two templates:

<xsl:template match="parent">
   <xsl:for-each select="child">
    <xsl:text>Whee!</xsl:text>
   </xsl:for-each>
   <xsl:apply-templates/>
</xsl:template>

<xsl:template match="child">
    <xsl:text>Oo!</xsl:text>
</xsl:template>

Are the same as:

<xsl:template match="parent">
   <xsl:apply-templates mode="whee"/>
   <xsl:apply-templates/>
</xsl:template>

<xsl:template match="child">
    <xsl:text>Oo!</xsl:text>
</xsl:template>

<xsl:template match="child" mode="whee">
    <xsl:text>Whee!</xsl:text>
</xsl:template>

The trade-of is that the for-each is a bit more concise, at the price of 
not being able to use the 'whee' template anywhere else in your processing 
logic.

If you always prefer templates (sometimes you'll need a mode), you develop 
an appreciation of the strengths of the processing model more rapidly. 
Occasionally having a separate template is a bit of a pain (maybe a new 
mode seems like overkill for a simple thing) -- then you can use for-each.

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



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


Current Thread