You can pull in and count all XML documents in a directory with Saxon 9 doing e.g. count(collection('file:///C:/dir/subdir?select=*.xml'))
, see http://www.saxonica.com/documentation/index.html#!sourcedocs/collections. But the files need to be well-formed XML documents for that to work and will be loaded and parsed.
Is it possible to count files in directory using XSLT?
Question
I need a way to count the number of files in a directory and store that back to a variable .
<xsl:variable name="no_of_files" />
I am not getting any hint to proceed
La solution 2
Autres conseils
Another solution, which would not involve the expense of reading and parsing all the files in the directory, and which would include non-XML files, is to use the EXPath file extension. This is implemented in Saxon 9.5 PE.
See http://expath.org/spec/file
count(file:list($fir, $recursive))
I am not aware of a way to achieve this in pure XSLT. But even if it is possible - XSLT is clearly not a natural fit for the task.
There are other languages that are more apt to handle a file system. Either
- count the files in a directory beforehand (with a shell script or with the language that is executing Saxon) and pass this information to the XSLT processor as a parameter. See for example the Saxon documentation.
or
- execute external code from within XSLT to retrieve the file count. @Michael-O suggests one way to approach this here.
On a different note, it might be interesting to know why you need a file count in the first place. Usually, if you describe your problem rather than your attempted solution, answers become more helpful.