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