Re: [xsl] Iterate through tree structure

Subject: Re: [xsl] Iterate through tree structure
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 31 Mar 2009 12:16:57 -0400
Jim,

If I understand this correctly, you don't actually have to recursively call a template. You only have to iterate through (a) ancestor dir elements of your parent::dir, and then (2) the parent dir element (and maybe its descendants).

The reason I say "maybe" is that the source as offered gives two page6 pages:

At 11:28 AM 3/31/2009, you wrote:
<dir name="dir1" id="x1">
 <page pname = "page1"></page>
 <dir name="dir2" id="x2">
   <page pname = "page2"></page>
  <dir name="dir3" id="x3">
    <page pname = "page3"></page>
  </dir>
  <dir name="dir4" id="x4">
    <page pname = "page4"></page>
   <dir name="dir5" id="x5">
     <page pname = "page5"></page>
     <page pname = "page6"></page>
    <dir name="dir6" id="x6">
      <page pname = "page6"></page>
    </dir>
   </dir>
  </dir>
 </dir>
</dir>

So we don't know on what basis the dir6 is included in the result you say you want.


The above is a snapshot as it can go down "n" levels.  If I'm at page6
then my navigation needs to be:-

<ul>
  <li>dir1</li>
  <li>dir2</li>
  <li>dir4</li>
</ul>
<ul>
  <li>dir5</li>
  <li>dir6</li>
</ul>

In any case, something like this (assuming the second page6 in your stated source is erroneous):


<xsl:template match="page6">
<ul>
<xsl:apply-templates mode="list-dir" select="parent::dir/ancestor::dir"/>
<!-- apply-templates generates output ordered as they are in the source,
so this will get dir1, dir2, dir4 -->
</ul>
<ul>
<xsl:apply-templates mode="list-dir" select="parent::dir/descendant-or-self::dir"/>
<!-- this gets dir5 (the parent of page6) and dir6 (the dir inside dir5) -->
</ul>
</xsl:template>


I hope that helps --
Wendell



======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread