[xsl] Generating a tree

Subject: [xsl] Generating a tree
From: Marcus Andersson <marcus@xxxxxxxxxx>
Date: Tue, 04 May 2004 22:16:24 +0200
Hi gurus

I want to generate an "unfolded" tree down to a selected element. So I want to render all ancestors to the selected element and all their siblings but not the siblings children. Also, the root shouldn't be included and I don't want the siblings to the top-level (below the root element) ancestor to be included. Another wish is that if the selected element is a section element I also want to render it's children (just one level though).

Example source XML:

<site>
  <section name="level1_1">
    <section name="level2_1>
      <page name="level3_1"/>
    </section>
    <section name="level2_2"/>
    <page name="level2_3"/>
    <page name="level2_4"/>
    <section name="level2_5">
      <section name="level3_1">
        <page name="level4_1"/>
      </section>
      <page name="level3_2"/>
      <section name="level3_3"> <-- Selected element
        <page name="level4_1"/>
        <page name="level4_2"/>
      </section>
      <page name="level3_4"/>
    </section>
  </section>
  <page name="level1_2"/>
  <section name="level1_3">
    ...
  </section>
</site>

Wanted result (with indents and everything :) :
node name="level1_1"
  node name="level2_1"
  node name="level2_2"
  node name="level2_3"
  node name="level2_4"
  node name="level2_5"
    node name="level3_1"
    node name="level3_2"
    node name="level3_3"
      node name="level4_1" //shouldn't be rendered if page
      node name="level4_2" //shouldn't be rendered if page
    node name="level3_4"

It would also be nice if one could draw something extra to indicate whether the node has children or not (like a plus or minus...).

How would you do it? I've made a couple of tries with anscestor-or-self::etc but I just tangle myself into hairy loops.

The template/templates that will do this will be called on the selected element level.

/Marcus

ps. I'm using MSXML so node-set() is available.

Current Thread