Re: Recursive Templates

Subject: Re: Recursive Templates
From: Tony Graham <tgraham@xxxxxxxxxxxxxxxx>
Date: Sun, 1 Aug 1999 17:04:24 -0400 (EST)
At 2 Aug 1999 03:12 +1000, Michael Stillwell wrote:
 > I've just started using XSL and am wondering how to achieve the
 > transformation such as the following.  I want to transform
 > 
 >   <section>Introduction</section>
 >   
 > into (say)
 > 
 >   <caps><strong>Introduction</strong></caps>

<xsl:template match="section">
  <caps>
    <strong>
      <xsl:apply-templates/>
    </strong>
  </caps>
</xsl:template>

 > My first attempt used templates like
 > 
 >   <xsl:template match="emph" name="emph">
 >   <strong>
 >   <xsl:apply-templates/>
 >   </strong>
 >   </xsl:template>
 > 
 >   <xsl:template match="section">
 >   <caps>
 >   <emph>
 >   <xsl:apply-templates/>
 >   </emph>
 >   </caps>
 >   </xsl:template>
 > 
 > Unfortunately, the <emph> ... </emph> tags produced but the "section"
 > template are not transformed into <strong> ... </strong> tags.  I
 > more or less understand why this is, but the best set of templates I
 > have found that do what I want are

You appear to be confusing the source tree and the result tree.  When
it's time to process a particular node in the source tree, the XSL
processor finds the xsl:template with the best matching "match"
attribute and uses the body of that xsl:template to add (or not add)
nodes to the result tree.  The <emph> in your xsl:template is added to
the result tree, and that's all that happens to it.  It doesn't get
any templates applied to it.

 >   <xsl:template match="emph" name="emph">
 >   <strong>
 >   <xsl:copy-of select="$content"/>
 >   </strong>
 >   </xsl:template>
 > 
 >   <xsl:template match="section">
 >   <caps>
 >   <xsl:call-template name="emph">
 >     <xsl:with-param name="content">
 >       <xsl:apply-templates/>
 >     </xsl:with-param>
 >   </xsl:call-template>  
 >   </caps>
 >   </xsl:template>
 > 
 > I cannot help but think there is a better way.

There are many better ways.  I gave a version earlier with a single
xsl:template.  If you really want to do it with multiple xsl:template
elements, try:

<xsl:template match="emph" name="emph">
  <strong>
    <xsl:apply-templates/>
  </strong>
</xsl:template>

<xsl:template match="section">
  <caps>
    <xsl:call-template name="emph"/>
  </caps>
</xsl:template>


Regards,


Tony Graham
======================================================================
Tony Graham                            mailto:tgraham@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9632
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