Re: [xsl] Breadth First Traversal

Subject: Re: [xsl] Breadth First Traversal
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 8 May 2001 15:11:29 +0100
Hi Dan,

> 1) Can Breadth First Traversal be coded without <xsl:call-template>?
>    In other words, just use <xsl:apply-templates select="?">.

If you're still interested, what about:

<xsl:variable name="as" select="//a" />

<xsl:template match="a">
   <!-- give the output for the a element -->
   <xsl:value-of select="@id" />
   <xsl:text>&#xA;</xsl:text>
   <!-- work out the depth of this a element -->
   <xsl:variable name="depth" select="count(ancestor::a)" />
   <!-- find the next node in the document at the same depth -->
   <xsl:variable name="next"
                 select="following::a[count(ancestor::a) = $depth][1]" />
   <xsl:choose>
      <!-- if there is one, apply templates to it -->
      <xsl:when test="$next">
         <xsl:apply-templates select="$next" />
      </xsl:when>
      <!-- if there isn't, apply templates to the first node in the
           document that has a depth one greater than this one -->
      <xsl:otherwise>
         <xsl:apply-templates
               select="$as[count(ancestor::a) = $depth + 1][1]" />
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

(Actually, you can *always* avoid using xsl:call-template by using a
moded template instead, but I think you were just after a different
technique here.)

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



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


Current Thread