RE: [xsl] getting all nodes from a certain level in the xml hierarchy

Subject: RE: [xsl] getting all nodes from a certain level in the xml hierarchy
From: "Américo Albuquerque" <aalbuquerque@xxxxxxxxxxxxxxxx>
Date: Fri, 27 Sep 2002 12:26:05 +0100
Hi Peter.

Try this:

 <xsl:template match="/">
  <table border="1">
   <tr>
    <td>level</td>
    <!-- this template shows the level number for each level it
encounters -->
    <xsl:call-template name="header">
     <xsl:with-param name="nodes" select="Folder"/>
    </xsl:call-template>
   </tr>
   <tr>
    <td>&#160;</td>
    <!-- and this shows the names of the Folders of the current level
-->
    <xsl:call-template name="folders">
     <xsl:with-param name="nodes" select="Folder"/>
    </xsl:call-template>
   </tr>
  </table>
 </xsl:template>

 <xsl:template name="header">
  <xsl:param name="nodes" select="."/>
  <xsl:param name="level" select="0"/>

  <td><xsl:value-of select="$level"/></td>
  <xsl:if test="$nodes/Folder">
  <xsl:call-template name="header">
   <xsl:with-param name="nodes" select="$nodes/Folder"/>
   <xsl:with-param name="level" select="$level + 1"/>
  </xsl:call-template>
  </xsl:if>
 </xsl:template>

 <xsl:template name="folders">
  <xsl:param name="nodes" select="."/>

  <td>
  <xsl:for-each select="$nodes">
   <xsl:if test="position() > 1"><br/></xsl:if>
   <xsl:value-of select="@NAME"/>
  </xsl:for-each>
  </td>
  <xsl:if test="$nodes/Folder">
  <xsl:call-template name="folders">
   <xsl:with-param name="nodes" select="$nodes/Folder"/>
  </xsl:call-template>
  </xsl:if>
 </xsl:template>

Hope that this helps you.

-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Peter Menzel
Sent: Friday, September 27, 2002 11:15 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: AW: [xsl] getting all nodes from a certain level in the xml
hierarchy


Yes, I need a HTML table with just one row.
All nodes of the same level are in one <td>, seperated by <br/>.


-----Ursprüngliche Nachricht-----
Von: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]Im Auftrag von Martin
Lormes
Gesendet: Freitag, 27. September 2002 11:38
An: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Betreff: Re: [xsl] getting all nodes from a certain level in the xml
hierarchy


> my problem is the following:
> my XML document maps the structure of a folder tree (just like the
> unix
file
> hierarchy) but without files, e.g.:
>
> <Folder NAME="/">
>   <Folder NAME="a"/>
>   <Folder NAME="b">
>     <Folder NAME="ba"/>
>     <Folder NAME="bb"/>
>   </Folder>
>   ...
>   <Folder NAME="z">
>     <Folder NAME="za">
> ...
>                 <Folder NAME="very deep folder"/>
>       ...
>     </Folder>
>   </Folder
> </Folder>
>
> The real folder NAMEs are like real folder names, and have no specific

> length or content. The depth of the deepest node is unknown.
>
> I need to access all nodes that have the same depth.
> So first i need root, then all direct childs of root, then all nodes
> that are two levels under root, because i want tu put them in a table
> like:
>
> level | 0 |  1  |  2   ...         x
> ------+---+-----+------     ---------------
>       | / |  a  |  ba       very deep folder
>       |   |  b  |  bb
>             ...
>       |   |  z  |  za
>
> first I started to try <xsl:for-each select="//Folder"> and then
> <xsl:for-each select="//Folder/Folder"> but because I do not know the
depth
> of the tree, this won't work..

Another reason, why this wouldn't work: "//Folder" selects all folders
at any tree level. "//Folder/Folder selects alls folders which have a
parent folder.

> Can anybody help me, what is the direction i should go?
> I don't think there is an easy way to use XPath for adressing nodes on

> the same level?

In General "/*/*/*/*" selects all nodes at the fourth level, for
instance.

However I can't yet think of a single transformation that could possibly
yield such a list. Do you really want text-output? Or do you want an
HTML table? If it helps to have an intermediate document containing a
simple list of Folders with their level in an attribute value, you can
use:

<xsl:for-each select="//Folder">
<Folder NAME="{@NAME}" LEVEL="{count(ancestor::*)}"/> </xsl:for-each>

It should be rather easy to build your table from this intermediate
structure.


Martin Lormes



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


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



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


Current Thread