[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: most efficient flat file listing to hierarchical
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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|