views:

591

answers:

2

Hi,

I have an XSLT script that I use for code generation where I process multiple input XML files, and output the files to Javascript code. For each xml input file, I output one Javascript file.

So, I would have the following files/transformations occuring:

view1.xml -> view1.js
view2.xml -> view2.js
...

Now I have a requirement to also create a one additional file during the XSL processing where I will need to append information regarding each input file processed to a common Javascript file.

So, now I would have:

view1.xml -> view1.js / append to common.js 
view2.xml -> view2.js / append to common.js
...

My question is, is it possible to append to a file during the processing of multiple input files? I am currently using Saxon 9.1.0.7, and I looked at the xsl:result-document element, but this appears to overwrite any previously written content.

Thanks.

A: 

There is a function in XSLT "document()". You can use this to process multiple xml input files from a single XSLT and hence a single output, I am not sure if it helps in your case.

some thing like.

 <xsl:apply-templates select="document('doc1.xml')"/>
 <xsl:apply-templates select="document('doc2.xml')"/>
 <xsl:apply-templates select="document('doc3.xml')"/>

 <xsl:template match='nodeX">do something</xsl:template>
 <xsl:template match='nodeY">do something else</xsl:template>
Ratnesh Maurya
I would prefer not to have to specify the input file names. Right now I am processing all files in an input directory, and supplying an output directory for the transformation files. There will be many more input files created, and I would rather not have to add a document() element for each new input file.
Steve
for that you can write another xml file on the fly (using some other script). containing name of all the files and process it and add document() tags in a loop in XSLT. This is the best i could think of as of now. :-)
Ratnesh Maurya
I would hope there would be a more straight forward way, but this does sound like it could work if there are no other options. I'm using Ant for the transformation, so I could just add a script call in the build, and call another transformation. Thanks
Steve
A: 

No, I don't think you can append to an existing (non-XML, even) file from within XSLT.

On the other hand, appending a result to a file is easily done in the environment that runs your transformation. Be it a programming language or a shell, text file concatenation should be a trivial excercise. I suggest you do it there, instead of trying to find some way of bending XSLT to do it.

Tomalak
Good point. I do need an attribute from each input file processed, but I could easily extract this using a scripting language as well and append this information. Thanks.
Steve
hmm.. I missed, dont try to solve a problem which doesn't exists :-)
Ratnesh Maurya