xsl-list
[Top] [All Lists]

RE: Re: getting all nodes from a certain level in the xml hierarchy

2002-09-27 04:21:18

Here Demitre uses the function count(ancestor::*|.) three times within
the same template - can anyone tell me if this gets re-evaluated each
time, therefore using a variable would be better, or if it gets stored
(as some kind of optimisation maybe), so using a variable is unecessary?


cheers
andrew


-----Original Message-----
From: Dimitre Novatchev [mailto:dnovatchev(_at_)yahoo(_dot_)com]
Sent: 27 September 2002 11:46
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Re: getting all nodes from a certain level in the xml
hierarchy



--- "Peter Menzel" <mai00bfy at studserv dot uni-leipzig dot 
de> wrote:

Hallo

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..

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?

Nice greetings, Peter


Here's a simple solution using the Muenchian method for grouping:

source xml (provided by you):
----------------------------
<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>

stylesheet:
----------
<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 
 <xsl:output method="text"/>
 
  <xsl:key name="kDepth" match="Folder" 
           use="count(ancestor::*)" />
  
  <xsl:template match="/">
    <xsl:for-each select="//Folder
                             [
                              generate-id()
                             =
                              generate-id(key('kDepth',
                                              count(ancestor::*)
                                              )[1]
                                          ) 
                              ]">
    
      <xsl:value-of select="concat('Level ', 
                                   count(ancestor::*),
                                   ': ' 
                                   )"/>
      <xsl:for-each select="key('kDepth',count(ancestor::*))">
        <xsl:value-of select="concat(@NAME, '; ')"/>
      </xsl:for-each>
      
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  

  </xsl:template>
</xsl:stylesheet>


Result:
------

Level 0: /; 
Level 1: a; b; z; 
Level 2: ba; bb; za; 
Level 3: very deep folder;


Hope this helped.




=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com

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



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.391 / Virus Database: 222 - Release Date: 19/09/2002
 


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.391 / Virus Database: 222 - Release Date: 19/09/2002
 

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