[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: most efficient flat file listing to hierarchical

Subject: Re: most efficient flat file listing to hierarchical
From: George Cristian Bina <george@xxxxxxxxxxxxx>
Date: Thu, 11 Jan 2007 23:31:48 +0200
Re:  most efficient flat file listing to hierarchical
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.