xsl-list
[Top] [All Lists]

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

2020-05-13 09:08:24
FAQ, section 4

1.

Accessing a directory full of xml files.

Francis Norton

This is a pure DOS / XML / XSLT way of creating an XML file
containing directory listing. It's based on my earlier
solution which didn't tolerate embedded spaces in filenames.
Warning. If a file name contains the ampersand character it
will fail to parse! If you need that, use a java filter to
remove.

For *nix users, According to the XML spec the line-parsing
technique should be OS independent. So you'd change the
batch file (and SED those pesky ampersands while you're at
it), but no change to the XML file, and all you need to do
to the XSL file is swap the '\' for a '/'...:)
(a perl script is added to the end of this answer)

In other words the processing-a-line-separated-file
technique should be portable without change, and the
specific utility should be fairly easily transportable.



The solution now takes a line-separated text file and
processes it into an XML file. Doing this requires two uses
of XML entities, firstly a system entity to read the text
file into the content of an XML element; and secondly a
character entity to access the acii 10 linefeed character to
parse that content.

For anyone unfamiliar with system entities, run the
xmlDir.bat, then see the difference between looking at
xmlDir.xml in a text processor and in an xml processor like
IE5. Ta-da...

I was never very fond of XML entities so this was a useful
exercise for me, I hope it helps others too.

1. The batch file

@echo off
cd > xmlDir.lst
dir *.xml /b >> xmlDir.lst
saxon xmlDir.xml xmlDir.xsl > xmlFiles.xml

Note that the last line needs changing to call your
own saxon processor (not the java version)

2. The xml file

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE xmlDir [
<!ENTITY xmlDirList
           SYSTEM "xmlDir.lst">
  ]>

<xmlDir>&xmlDirList;</xmlDir>

Note that this won't work until you have created
the entity by running the batch file, and saving
it in a location where the xml file can access it.

3. The xsl file

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<!-- root function -->
<xsl:template match="/xmlDir">

  <!-- create our root output element -->
  <xmlDir>
<!-- the path is on the first line, filenames on the others -->
    <xsl:call-template name="file">
      <!-- all the CR-LF pairs have been normalised
  to ascii 10, as specified in
http://www.w3.org/TR/1998/REC-xml-19980210#sec-line-ends
      -->
      <xsl:with-param name="path"
 select="substring-before(string(), '
')" />
      <xsl:with-param name="flist"
 select="substring-after(string(), '
')" />
    </xsl:call-template>

  </xmlDir>

</xsl:template>


<!-- process the individual files
  in the space-separated file list -->
<xsl:template name="file">
  <xsl:param name="path" />
  <xsl:param name="flist" />

  <xsl:if test="$flist != ''">

<!-- output the path and the first filename as one element -->
    <xmlFile><xsl:value-of
             select = "concat($path, '\',
    substring-before($flist, '
'))"
              /></xmlFile>

<!-- now recurse with same path and rest of the filenames -->
    <xsl:call-template name="file">
      <xsl:with-param name="path" select="$path" />
      <xsl:with-param name="flist"
 select="substring-after($flist, '
')" />
    </xsl:call-template>

  </xsl:if>

</xsl:template>

</xsl:stylesheet>

On Wed, 13 May 2020 at 14:59, Michael Kay mike(_at_)saxonica(_dot_)com
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Hans-Jürgen Rennau did some work in this area:

https://www.parsqube.de/en/publications/foxpath-for-selecting-files-and-folders/

Michael Kay
Saxonica

On 13 May 2020, at 13:56, Costello, Roger L. costello(_at_)mitre(_dot_)org 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Oops! Correction:

For this folder structure:

root
   persons
       local
           JohnDoe.xml
           MarySmith.xml
       remote
           BillAnderson.xml

the XSLT code outputs this XML:

<root>
    <persons>
         <local>
             JoeDoe.xml
             MarySmith.xml
       </local>
       <remote>
            BillAnderson.xml
      </remote>
   </persons>
</root>

/Roger

-----Original Message-----
From: Costello, Roger L. <costello(_at_)mitre(_dot_)org>
Sent: Wednesday, May 13, 2020 8:51 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Anyone have XSLT that generates XML showing a Windows folder 
structure?

Hi Folks,

Do you have XSLT code that does this: Given a root folder, show all the 
subfolders and files within it, repeat for each subfolder.

Example, for this folder structure:

root
   persons
       local
           JohnDoe.xml
           MarySmith.xml
       remote
           BillAnderson.xml

the XSLT code outputs this XML:

<root>
    <persons>
         <local>
             <john>JoeDoe.xml</john>
             <mary>MarySmith.xml</mary>
       </local>
       <remote>
            <bill>BillAnderson.xml</bill>
      </remote>
   </persons>
</root>

If someone has already done this, would you mind sharing it, please?

/Roger


XSL-List info and archive
EasyUnsubscribe (by email)



-- 
Dave Pawson
XSLT XSL-FO FAQ.
Docbook FAQ.
--~----------------------------------------------------------------
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>