AW: [xsl] Tree from directory listing

Subject: AW: [xsl] Tree from directory listing
From: Thomas Zöchling <thomas.zoechling@xxxxxx>
Date: Tue, 14 Dec 2004 12:46:33 +0100
Thanks for the FAQ.

I figured it out myself (with a lot of help from this post, found in the
archives:
 http://www.biglist.com/lists/xsl-list/archives/200408/msg00855.html)

I use the xsl for transforming Bookmark Lists to a Tree, but it could also
be useful to generate Directory Trees from Listings.
It is almost the same as the solution in the post mentioned above, except
that the path information is stored in an attribute.

BUT:
If the path attributes are unsorted, the script ends in an infinite
recursion.
I suppose this is the part, where the recursion doesnt stop:

<xsl:with-param name="xbelflat" select="$xbelflat[starts-with(@path,
$newPath) and @path != $newPath]"/>

Does someone have an idea how I could recursive group, without sorting by
@path?



The stylesheet takes the following input and produces XBEL formatted XML
(http://pyxml.sourceforge.net/topics/xbel/)


XML:
...
<bookmark href="http://slashdot.org/"; name="Slashdot: News for nerds, stuff
that matters" path="Bookmarks/Bookmarks Toolbar Folder/"></bookmark>
<bookmark href="http://www.heise.de/"; name="heise online"
path="Bookmarks/Bookmarks Toolbar Folder/"></bookmark>
<bookmark href="http://futurezone.orf.at/"; name="ORF ON Futurezone"
path="Bookmarks/News/"></bookmark>
....

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:template name="transformDocument">
		<xsl:param name="path"/>
		<xsl:param name="xbelflat" select="/.."/>
		<xsl:if test="$xbelflat">
			<xsl:variable name="step">
				<xsl:variable name="rest"
select="substring-after($xbelflat[1]/@path, concat($path, '/'))" />
				<xsl:choose>
					<xsl:when test="contains($rest,
'/')">
						<xsl:value-of
select="substring-before($rest, '/')" />
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$rest"
/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>
			<xsl:variable name="newPath" select="concat($path,
'/', $step)" />
			<xsl:variable name="titleLen"
select="string-length(string($step)) &gt; 0" />
			<xsl:if test="$titleLen">
				<xsl:text
disable-output-escaping="yes">&lt;folder&gt;</xsl:text>
				<title>
					<xsl:value-of select="$step"/>
				</title>
			</xsl:if>
			<xsl:apply-templates select="$xbelflat[@path =
$newPath]"/>
			<xsl:call-template name="transformDocument">
				<xsl:with-param name="path"
select="$newPath" />
				<xsl:with-param name="xbelflat"
select="$xbelflat[starts-with(@path, $newPath) and @path != $newPath]"/>
			</xsl:call-template>
			<xsl:if test="$titleLen">
				<xsl:text
disable-output-escaping="yes">&lt;/folder&gt;</xsl:text>
			</xsl:if>
			<xsl:call-template name="transformDocument">
				<xsl:with-param name="path" select="$path"/>
				<xsl:with-param name="xbelflat"
select="$xbelflat[not(starts-with(@path, $newPath))]"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	<xsl:template match="xbelflat">
		<xbel version="1.0">
			<xsl:variable name="root"
select="substring-before(bookmark[1]/@path, '/')" />
			<folder>
				<title>
					<xsl:value-of select="$root"/>
				</title>
				<xsl:call-template name="transformDocument">
					<xsl:with-param name="path"
select="$root" />
					<xsl:with-param name="xbelflat"
select="bookmark" />
				</xsl:call-template>
			</folder>
		</xbel>
	</xsl:template>
	<xsl:template match="bookmark">
		<bookmark href="{@href}"><title><xsl:value-of
select="@name"/></title></bookmark>
	</xsl:template>
</xsl:stylesheet>



-----Urspr|ngliche Nachricht-----
Von: Pawson, David [mailto:David.Pawson@xxxxxxxxxxx]
Gesendet: Montag, 13. Dezember 2004 11:32
An: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Betreff: RE: [xsl] Tree from directory listing

http://www.dpawson.co.uk/xsl/sect4/N9291.html

shows a couple of approaches.

HTH DaveP

    -----Original Message-----
    From: Thomas Zvchling [mailto:thomas.zoechling@xxxxxx]
    Sent: 10 December 2004 17:04
    To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
    Subject: [xsl] Tree from directory listing

    Hi list!

    Does someone know a method to create a nested xml Tree
    structure from file listing with paths?


--
DISCLAIMER:

NOTICE: The information contained in this email and any attachments is
confidential and may be privileged.  If you are not the intended
recipient you should not use, disclose, distribute or copy any of the
content of it or of any attachment; you are requested to notify the
sender immediately of your receipt of the email and then to delete it
and any attachments from your system.

RNIB endeavours to ensure that emails and any attachments generated by
its staff are free from viruses or other contaminants.  However, it
cannot accept any responsibility for any  such which are transmitted.
We therefore recommend you scan all attachments.

Please note that the statements and views expressed in this email and
any attachments are those of the author and do not necessarily represent
those of RNIB.

RNIB Registered Charity Number: 226227

Website: http://www.rnib.org.uk

Current Thread