xsl-list
[Top] [All Lists]

RE: Tree from directory listing

2004-12-10 10:39:24
It's essentially a recursive grouping problem. (Yes, I have seen these
before!) In XSLT 2.0 you can solve it like this:

<xsl:template name="g">
  <xsl:param name="files" as="element(file)*"/>
  <xsl:param name="level" as="xs:integer"/>
  <xsl:for-each-group select="$files"
     group-adjacent="tokenize(@path, '/')[$level]">
   <folder name="current-grouping-key()">
     <xsl:call-template name="g">
       <xsl:with-param name="files" select="current-group() except ."/>
       <xsl:with-param name="level" select="$level + 1"/>
     </xsl:call-template>
   </folder>
  </xsl:for-each-group>
</xsl:template>

I haven't tried to distinguish folders from files here, I leave that as an
exercise for the reader (perhaps best done in a second pass).

A 1.0 solution is beyond my concentration powers at this time on a Friday
evening.

Michael Kay
http://www.saxonica.com/ 

-----Original Message-----
From: Thomas Zöchling [mailto:thomas(_dot_)zoechling(_at_)gmx(_dot_)at] 
Sent: 10 December 2004 17:04
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Tree from directory listing

Hi list!

Does someone know a method to create a nested xml Tree 
structure from file
listing with paths?

eg.
...
<file name ="f1.xyz" path="/test/"/>
<file name ="f2.xyz" path="/test/folderInFolder/"/>
<file name ="f3.xyz" path="/test/folderInFolder/"/>
<file name ="f4.xyz" path="/test/folderInFolder2/"/>
<file name ="f5.xyz" path="/test2/folderInFolder3/"/>
...

to

<folder name="test">
    <folder name="folderInFolder">
        <file name="f2.xyz"/>
        <file name="f3.xyz"/>
    </folder
    <folder name="folderInFolder2">
        <file name="f4.xyz"/>
    </folder
<folder name ="test">
    <folder name="folderInFolder3">
        <file name="f5.xyz">
    </folder>
</folder>

Maybe anyone solved a similar problem?


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>