[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Anyone have XSLT that generates XML showing a Wind

Subject: Re: Anyone have XSLT that generates XML showing a Windows folder structure?
From: "Costello, Roger L. costello@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 13 May 2020 15:37:28 -0000
 Re: Anyone have XSLT that generates XML showing a Wind
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>

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.