1

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

1
  • Do you want to do that from within the stylesheet? You could try this. Commented Mar 4, 2014 at 8:39

3 Answers 3

2

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))
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.