[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] most efficient flat file listing to hierarchical


Subject: Re: [xsl] most efficient flat file listing to hierarchical
From: George Cristian Bina <george@xxxxxxxxxxxxx>
Date: Thu, 11 Jan 2007 23:31:48 +0200

Abel,

Your solution does not work correctly, on the sample input it gives:

<?xml version="1.0" encoding="UTF-8"?>
<dir name="en">
   <file name="test.html"/>
   <file name="test1.html"/>
   <dir name="resource">
      <dir name="style">
         <file name="test.css"/>
      </dir>
   </dir>
</dir>
<file name="favicon.ico"/>
<dir name="cn"/>
<dir name="cn"/>

Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


Abel Braaksma wrote:
Hi All,

Here's another one. I couldn't resist to add to the queue, and I didn't yet see a template-only approach. This time, no call-template, no xsl:when and xsl:if. No grouping. I believe (but I'm not so good at terminology) that this approach is called "tree walking".

It is simple and straightforward: define the definition of a 'directory' and a 'file' and create a matching template. Next, to build the tree from the strings, walk each sibling one by one. The directory-grouping is done by a simple starts-with.

In terms of efficiency: it saves disk space, but I don't believe it is efficient in terms of speed.

Here it is:

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="2.0">

   <xsl:output indent="yes" />
     <xsl:template match="/listing">
       <xsl:apply-templates select="item[not(contains(., '/'))]" />
   </xsl:template>
     <xsl:template match="item[text()[not(matches(., '\.'))]]">
       <dir name="{replace(., '.*/', '')}">
           <xsl:apply-templates select="
           following-sibling::item[starts-with(., current())][1]" />
       </dir>
   </xsl:template>
     <xsl:template match="item" />

   <xsl:template match="item[text()[matches(., '\.')]]">
       <file name="{replace(., '.*/', '')}" />
       <xsl:apply-templates select="
           following-sibling::item[starts-with(., replace
           (current(), '[^/]+$', ''))][1]" />
   </xsl:template>
</xsl:stylesheet>



Btw, I really enjoyed reading up on all the approaches. "Tasks" like this show the maturity and versatility of the language. Larry Wall (perl) should be pleased that even in XSLT, "there is more than one way to do it".

Cheers,

-- Abel Braaksma
  http://www.nuntia.nl


Current Thread