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 11:55:02 -0400
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



Current Thread