[xsl] Stupid beginner question: recursing a list of (kind of) nested el ements

Subject: [xsl] Stupid beginner question: recursing a list of (kind of) nested el ements
From: Graham Hannington <Ghannington@xxxxxxx>
Date: Wed, 8 Jan 2003 19:25:03 -0000
I have a document containing elements like this:


<topic file="1.htm">One</topic>

<topic file="2.htm">Two</topic>

<subtopics>

  <topic file="2a.htm">Two Ay</topic>

  <topic file="2b.htm">Two Bee</topic>

  <subtopics>

    <topic file="2bi.htm">Two Bee Eye</topic>

    <topic file="2bi.htm">Two Bee Eye Eye</topic>

  </subtopics>

</subtopics>

<topic file="3.htm">Three</topic>


that I want to into properly nested elements, similar to this:


<fox:outline>

  <fox:label>One</fox:label>

</fox:outline>

<fox:outline>

  <fox:label>Two</fox:label>

  <fox:outline>

    <fox:label>Two Ay</fox:label>

    <fox:label>Two Bee</fox:label>

    <fox:outline>

      <fox:label>Two Bee Eye</fox:label>

      <fox:label>Two Bee Eye Eye</fox:label>

    </fox:outline>

  </fox:outline>

</fox:outline>

<fox:outline>

  <fox:label>Three</fox:label>

</fox:outline>


I've achieved this, but only by resorting to a truly stupid set of nested
<xsl:for-each> tags (which will only work up to three <subtopics> deep).

I'd like to use a more elegant recursive template, but my attempts so far
have resulted in properly nested elements (as desired), interspersed with
unwanted duplicates.  My attempts so far are all variations on this theme:

	<xsl:template match="topic" mode="outline">

		<fox:outline>
			<fox:label>
				<xsl:value-of select="." />
			</fox:label>

			<!-- If the next tag is a subtopics, then apply
templates to its children -->
			<xsl:apply-templates
select="following-sibling::*[1][self::subtopics]/child::*" mode="outline" />

		</fox:outline>

	</xsl:outline>

The problem is, I don't know how to exclude <topic> tags from processing by
the template when they've already been processed (at least, I think that's
the problem).

Graham Hannington

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


Current Thread