[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: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 11 Jan 2007 11:26:54 GMT

I started to write an xslt2 soln, but didn't use any xslt2 features, so this
seems to work in 1 as well, it doesn't return the items in quite the
order you asked, but perhaps an xsl:sort would fix that.

Also you didn't say what was the criterion for things being a directory.
this uses the existence of a file in the directory (so empty directories
would be classified as <file/>) an alternative would be to flag
extensionless files as directories (which would make Makefile be classed
as an empty directory, even though it's a file)

David


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

  <xsl:output indent="yes"/>

  <xsl:template match="listing">
    <xsl:apply-templates select="item"/>
  </xsl:template>
 

  <xsl:template match="item">
    <xsl:param name="path" select="''"/>
    <xsl:variable name="rel" select="substring-after(.,$path)"/>
    <xsl:choose>
      <xsl:when test="not(starts-with(.,$path))"/>
      <xsl:when test="contains($rel,'/')"/>
      <xsl:when test="../item[starts-with(.,concat(current(),'/'))]">
	<dir name="{$rel}">
	  <xsl:apply-templates select="../item">
	    <xsl:with-param name="path" select="concat(current(),'/')"/>
	  </xsl:apply-templates>
	</dir>	
      </xsl:when>
      <xsl:otherwise>
	<file name="{$rel}"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>



$ saxon listing.xml listing.xsl
<?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">
   <file name="test.xml"/>
</dir>


Current Thread