Re: [xsl] Expandeable Tree!

Subject: Re: [xsl] Expandeable Tree!
From: Robert Koberg <rob@xxxxxxxxxx>
Date: Thu, 10 Feb 2005 15:46:14 -0800
Adam J Knight wrote:
Hi all,

First time posting, so I hope I am doing ok.

I have a xml document that gets generated from a database (MySql).
<?xml version="1.0"?>
<tree>
  <tree_node id="7" depth="0" parent="0" value="Test">
    <tree_node id="8" depth="1" parent="7" value="Test Sub"/>
    <tree_node id="9" depth="1" parent="7" value="Test Sub One">
      <tree_node id="10" depth="2" parent="9" value="Test Sub Two"/>
    </tree_node>
  </tree_node>
</tree>


First, the depth and parent attributes are not necessary because you have that information in the XML hierarchy.


However all xsl processing is done server side (Using PHP 4 XSLT Processor).
Default view is all level (depth) 0 nodes displayed. A user clicks a html
link that sends the id of the node clicked to the xsl file as a parameter
and all children nodes are displayed. If depth is greater than one, all
ancestor nodes will need to be displayed also.

You could do something like:


<xsl:template match="/">
  <ul class="nav">
    <xsl:apply-templates/>
  </ul>
</xsl:template>

<xsl:template match="*">
  <xsl:choose>
    <xsl:when test="child::*">
      <li class="folder">
        <a href="my.php?id={@id}">
          <xsl:value-of select="@value"/>
        </a>
        <ul>
          <xsl:apply-templates/>
        </ul>
      </li>
    </xsl:when>
    <xsl:otherwise>
      <li class="page">
        <a href="my.php?id={@id}">
          <xsl:value-of select="@value"/>
        </a>
      </li>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

best,
-Rob

Current Thread