1

I have list of files in the folder/subfolders. How to run a single XSLT to run all the files in the folder/subfolder. Is it possible in saxon command line?

I have tried the below command but its not working:

 java -jar saxon9.jar -o:foldername -xsl:xslfilename.xsl

Your help much appreciated.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
VSe
  • 919
  • 2
  • 13
  • 29
  • See @MichaelKay's answer to a similar, albeit not quite duplicate, question [here](http://stackoverflow.com/a/15020606/290085). – kjhughes Feb 25 '15 at 16:46
  • What do you mean by "list of files"? What version of Saxon? If the list is a file (XML preferably but plain text would work) with the paths and you're able to use the saxon extension functions, I can add an example that uses `saxon:discard-document` so you can process a lot of files without running out of memory. – Daniel Haley Feb 25 '15 at 18:04

2 Answers2

5

Since you are using Saxon, you can use xslt 2.0 and the collection function.

for example:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
    <xsl:for-each select="collection(concat('file:///c:/filesarehere', '?select=*.xml;recurse=yes'))">
        <!--process nodes-->
    </xsl:for-each>
</xsl:template>

PhillyNJ
  • 3,859
  • 4
  • 38
  • 64
  • 5
    When I'm doing this I normally use `` as the entry point, and `-it:main` on the command line, to avoid the need for a dummy input document to match the `match="/"`. Also, I don't know if "c:/..." works as the collection URI, I would use "file:///c:/..." – Michael Kay Feb 25 '15 at 17:56
  • 1
    @MichaelKay Thanks - forgot about adding `file:///` - I edited my answer to reflect this. – PhillyNJ Feb 25 '15 at 18:30
3

If you want to create one output file for each input file, with corresponding names, then you can set -s:inputDir and -o:outputDir on the command line, and it will process all the files in the directory. But this is a bit inflexible, for example if there are some non-XML files in the directory that you want to ignore. Controlling the process from within the stylesheet, using the collection() function, as suggested by @PhilVallone is more flexible.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164