|
Subject: Re: [xsl] Tree from directory listing From: Geert Josten <Geert.Josten@xxxxxxxxxxx> Date: Thu, 16 Dec 2004 10:23:28 +0100 |
Hi Geert, Thanks a lot for pointing the bug.. I was infact testing my stylesheet again, and was getting StackOverFlow error. And your mail pointed the bug.. Thanks for saving my time!
I suggest, please post your XSL. It'll help the original poster.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common"
exclude-result-prefixes="exslt"> <xsl:template match="root">
<xsl:variable name="folders">
<!-- create a temporary tree with a folder structure -->
<xsl:copy>
<xsl:copy-of select="@*" /> <xsl:for-each select="file">
<!-- sort the files for nicer output -->
<xsl:sort select="@path" />
<xsl:sort select="@name" /> <!-- build a folder structure for each file (separately) -->
<xsl:apply-templates select="." mode="build-folders">
<xsl:with-param name="path" select="concat(@path, @name)" />
</xsl:apply-templates>
</xsl:for-each>
</xsl:copy>
</xsl:variable> <!-- we have files in separate folder elements, now merge them -->
<xsl:apply-templates select="exslt:node-set($folders)/node()" mode="merge" />
</xsl:template> <xsl:template match="file" mode="build-folders">
<xsl:param name="path-base" />
<xsl:param name="path" /> <!-- assumption: path always starts with '/' --> <xsl:choose>
<!-- should not occur, actually -->
<xsl:when test="string-length($path) = 0" /> <!-- path contains slashes, output a folder -->
<xsl:when test="$folder-count > 0">
<!-- Note: folder-count = 1 means at least two slashes -->
<xsl:variable name="folder-name" select="substring-before(substring-after($path, '/'), '/')" />
<xsl:variable name="folder-path" select="concat($path-base, '/', $folder-name)" />
<xsl:variable name="remainder" select="substring-after(substring-after($path, '/'), '/')" /> <folder name="{$folder-name}" path="{$folder-path}">
<!-- and recurse to add sub folders -->
<xsl:apply-templates select="." mode="build-folders">
<xsl:with-param name="path" select="concat('/', $remainder)" />
<xsl:with-param name="path-base" select="$folder-path" />
</xsl:apply-templates>
</folder>
</xsl:when> <!-- no more slashes, output the file -->
<xsl:otherwise>
<!-- note: could do a xsl:copy-of select="." as well -->
<file name="{@name}" />
</xsl:otherwise>
</xsl:choose>
</xsl:template> <xsl:template match="root" mode="merge">
<xsl:copy>
<xsl:copy-of select="@*" /> <!-- insert top-level files -->
<xsl:copy-of select="file" /> <xsl:template match="folder" mode="merge">
<xsl:copy>
<!-- note: one might want to suppress the path attribute -->
<xsl:copy-of select="@*" />| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| Re: [xsl] Tree from directory listi, Mukul Gandhi | Thread | Re: [xsl] Tree from directory listi, Mukul Gandhi |
| RE: [xsl] [XSLT2.0] xsl:analyze-str, Michael Kay | Date | Re: [xsl] Tree from directory listi, Mukul Gandhi |
| Month |