xsl-list
[Top] [All Lists]

[xsl] Re: Anyone have XSLT that generates XML showing a Windows folder structure?

2020-05-13 10:37:33
Thank you John, Michael, Steven, and Dave.

I decided to go the route that John suggested, and use the EXPath File module. 
John was right, it was fairly easy to implement the desired functionality using 
the EXPath File module. Below is what I created. It seems to work fine. If you 
see any potential problems in it, or ways to improve it, please let me know.  
/Roger
------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
    xmlns:file="http://expath.org/ns/file";
    xmlns:f="function"
    xmlns:xs="http://www.w3.org/2001/XMLSchema";
    extension-element-prefixes="file"
    exclude-result-prefixes="#all"
    version="2.0">
    
    <xsl:template match="/">
        <folder-structure>
            <xsl:variable name="uri-to-this-xslt-program" select="base-uri()"/>
            <xsl:variable name="current-directory" 
select="f:remove-last-slash-filename-from-URI($uri-to-this-xslt-program)"/>
            <xsl:sequence select="f:get-folder-structure($current-directory)" />
        </folder-structure>
    </xsl:template>
    
    <xsl:function name="f:get-folder-structure" as="element()*">
        <xsl:param name="uri" as="xs:string"/>
        
        <xsl:for-each select="file:children($uri)">
            <xsl:variable name="item" select="."/>
            <xsl:choose>
                <xsl:when test="file:is-file($item)">
                    <file><xsl:value-of select="file:name(.)"/></file>
                </xsl:when>
                <xsl:when test="file:is-dir($item)">
                    <xsl:element name="{file:name(.)}">
                        <xsl:sequence 
select="f:get-folder-structure(concat($uri, '/', file:name(.)))" />
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="error()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
        
    </xsl:function>
    
    <xsl:function name="f:remove-last-slash-filename-from-URI" as="xs:string">
        <xsl:param name="uri" as="xs:string"/>
        
        <xsl:value-of 
select="f:reverse-string(substring-after(f:reverse-string($uri), '/'))"/>
        
    </xsl:function>
    
    <xsl:function name="f:reverse-string" as="xs:string">
        <xsl:param name="s" as="xs:string"/>
        
        <xsl:value-of 
select="codepoints-to-string(reverse(string-to-codepoints($s)))"/>
        
    </xsl:function>
    
</xsl:stylesheet>
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>