XSLT: How to get file names from a certain directory? XSLT: How to get file names from a certain directory? xml xml

XSLT: How to get file names from a certain directory?


XSLT has nothing built-in for this task. XSLT is a transformation language - for dynamic output you generally need a transformation source that contains everything already (just in a different form) – you cannot create XML from nothing.

The three ways you can tackle the problem are:

  1. Build the XML in a programming language, leaving out XSLT altogether. This is the simplest way to get the result you want.
  2. Build an XSL stylesheet that accepts a parameter, put a (pre-built) delimited list of files into that parameter, let the XSLT handle the string and make XML from it. This involves external handling as well, basically this is option 1., plus you'd have to write an XSL stylesheet that does string handling (something that XSL has not been geared to)
  3. Use extension functions and do the directory processing from within the XSL. An example how to get started can be found in my answer to this question. This is not very portable as extension functions are platform specific.

It boils down to this:

  • You will need help from an external programming language
  • You do not absolutely need XSLT do accomplish the task, since XML output is all you need and no transformation is required.

Ergo: Don't use XSL for this.


You can't do that in native XSLT, but various implementations allow you to add extensions to the functionality.

For example, in C# you can add a user defined URN:

 <xsl:stylesheet {snipped the usual xmlns stuff}    xmlns:user="urn:user" >

then use the functions within "user"

  <xsl:value-of select="user:getdirectory( @mydir )" />

within the C# you associate "user" to a C# class:

 XSLThelper xslthelper      = new XSLThelper( );  // your class here xslArgs.AddExtensionObject( "urn:user", xslthelper );

and your class defines the "getdirectory" function:

public class XSLThelper{public string getdirectory(System.Xml.XPath.XPathNavigator xml, string strXPath, string strNULL){       //blah    }}

Hugh amount of homework left here! MSDN Resource


The Q is six years old and answered. Just throwing my 2 cents in for those who land here again.

I've needed an XML file representing the filenames in a directory. I've done it three ways:

  1. XSLT 2.0 document() as others have pointed out in this thread. Drawback is performance as it reads the file into the dom parser, when all you really wanted is the name. Also as LarsH pointed out in the OP comments, this works only with valid XML files. If you have a non-xml or malformed xml file in the recurse, it crashes the transform.

  2. The tool xmlstarlet with the command xmlstarlet ls > filenames.xml

  3. A crude bash script I made up (it could be optimized):

DIRECTORY=$1RESULTFILE=$2# put the directory listing into a temp file# modify the ls command for your needsls -A1 $DIRECTORY > /tmp/zFiles.txt# remove detritus and wrap the lines in <file> elementssed -i -e's/^[^$]*$/   <file filename="&"\/>/' /tmp/zFiles.txt# build the fileecho '<?xml version="1.0" encoding="UTF-8"?>' > $RESULTFILEecho "<files>" >> $RESULTFILEcat /tmp/zFiles.txt >> $RESULTFILEecho "</files>" >> $RESULTFILE

I had used the bash script for a long time but now I use the xmlstarlet method exclusively. The result starlet file contains specific file attributes such as permissions and dates, I have found that helpful.