[xsl] RE: Display children of node with given Id!!

Subject: [xsl] RE: Display children of node with given Id!!
From: "Adam J Knight" <adam@xxxxxxxxxxxxxxxxx>
Date: Wed, 16 Feb 2005 20:07:50 +1000
Hi all,

Following on from my previous post.
Thanks to those who replied, it is much appreciated.
This stylesheet matches all nodes that are descended from node with a given
id.

Two Things:
1) How do I modify this stylesheet to include the rendering of all top level
tree nodes? Can I do this in the <xsl:apply-templates select=""/> element.
I have tried <xsl:apply-templates select="parent::tree or //tree_node[@id =
$id]"/>???

2) How do I modify this stylesheet to only display immediate children(not
grandchildren ect) descended from the node with a given id. In the case node
id = 7 , nodes 8, 9 will only be displayed. Not 10.


COMPLETE XML STRUCTURE:

<tree>
  <tree_node id="7" value="Test">
    <tree_node id="8" value="Test Sub"/>
    <tree_node id="9" value="Test Sub One">
      <tree_node id="10" value="Test Sub Two"/>
    </tree_node>
  </tree_node>
  <tree_node id="11" value="Test 2"/>
</tree>

COMPLETE XSL FILE:
<?xml version="1.0"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:param name="css"/>
  <xsl:param name="url"/>
  <xsl:param name="id"/>
  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <table cellspacing="0" cellpadding="2" class="{$css}">
      <tr>
	  <td class="ui_tree_bar">
          <a href="{$url}&amp;action=add">add</a>
        </td>
	</tr>
	<xsl:apply-templates select="//tree_node[@id=$id]"/>
    </table>
  </xsl:template>

  <xsl:template match="tree_node">

	<tr>
        <td>
          <table width="100%">
	      <tr>
	        <td align="right" width="{count(ancestor-or-self::*) * 10}">
                <a href="{$url}&amp;node={@id}">+</a>
              </td>
	        <td>
	          <xsl:value-of select="@value"/>
	        </td>
		  <td width="25%">
		    <a href="{$url}&amp;action=edit&amp;node={@id}">edit</a>
		    <a
href="{$url}&amp;action=delete&amp;node={@id}">delete</a>
		  </td>
            </tr>
	    </table>
	  </td>
      </tr>
	<xsl:apply-templates/>

  </xsl:template>

</xsl:stylesheet>

Cheers,
Adam
 
NB: "Pray as if everything depended upon God and work as if everything
depended upon man."

Current Thread