Re: [xsl] recursive call of template - how to find parents

Subject: Re: [xsl] recursive call of template - how to find parents
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 5 Nov 2003 10:00:10 +0000
Hi Michael,

> But now I need to output another attribute with every element, and I
> need to output this attribute also from all its parents. How can I
> find out if there´s still a parent left? I give you an example of my
> xml-File my xslt-code and the desired output.

You don't have to do this recursively unless you particularly want to.
The easiest way to create the href attribute that you're after is to
use an <xsl:for-each> loop over the ancestors of the current <topic>
element (and the current <topic> element itself), as follows:

<xsl:template match="topic">
  <li>
    <a>
      <xsl:attribute name="href">
        <xsl:for-each select="ancestor-or-self::topic">
          <xsl:value-of select="@filename" />
          <xsl:if test="position() != last()">/</xsl:if>
        </xsl:for-each>
      </xsl:attribute>
      <xsl:value-of select="."/>
    </a>
    <xsl:if test="topic">
      <ul>
        <xsl:apply-templates select="topic"/>
      </ul>
    </xsl:if>
  </li>
</xsl:template>

Note that you're currently generating the <li> elements for the
top-most <topic> elements in a separate place; it would be easiest if
you could combine them, such that the sitemap template looked like:

<xsl:template name="sitemap">
  <ul>
    <xsl:apply-templates select="//homepage/topic" />
  </ul>
</xsl:template>

(It would also be best, for efficiency, if you could come up with a
more targetted path to the top-level <topic> elements than
"//homepage/topic".)

Cheers,

Jeni

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


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


Current Thread