RE: [xsl] iterate over a folder and update XML using XSLT

Subject: RE: [xsl] iterate over a folder and update XML using XSLT
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 29 Mar 2010 22:20:05 +0100
You can either control this from the command line by setting -s to a
directory, in which case the transformation is run once per input file, or
you can control it from within the stylesheet using the collection()
function. You're doing both, so you are getting nested iterations, which you
don't want.

You have two options:

(a) enter the transformation at a named template (-it:main), with no source
document, and supply the name of the directory as a parameter:

java -jar saxon9.jar -it:main -xsl:my.xsl dir="folder"

<xsl:param name="folder"/>
<xsl:template name="main">
  <xsl:for-each select="collection($folder)">
    ...
  </xsl:for-each>
</xsl:template>

or (b) write the transformation to process a single document, and then apply
it to all the files in a folder by giving directory names to the -s and -o
options on the command line.

(Another option (c) is to use Kernow, which is very handy for this kind of
thing).

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 

> -----Original Message-----
> From: a kusa [mailto:akusa8@xxxxxxxxx] 
> Sent: 29 March 2010 22:06
> To: xsl-list
> Subject: [xsl] iterate over a folder and update XML using XSLT
> 
> Hi
> 
> Here is my requirement:
> 
> 1) Iterate over 100 XML files in a folder.
> 2) Append a <para> with some content in it to each file.
> 
> I have a transformation program and I use saxon's command 
> line to run the transformation over the group of files.
> 
> So I run
> 
> java -jar saxon9.jar -s: <here i specify the path to the folder> -o:
> <here i specify the outout dir> -xsl:<name of my xsl>
> 
> Now since each transformation is individualistic, the problem 
> I am having is that ever transformed xml output has the 
> content repeated as many times as the number of input xml files.
> 
> So, if I have 10 files in the folder, when I sued 
> <xsl:for-each select="collection('<path>')">, which by the 
> way takes a long time to transform, it appends 10 <para> 
> elements to each xml file.
> 
> So what is the approach to iterate over folder in XSLT? Or 
> can it be done?
> 
> Can the experts please help me understand this?
> 
> Thanks in advance.

Current Thread