Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate a lot of XML files with XSLT?

I have a folder with many XML files and I want a simple XSLT transformation that can merge all these files into one and put all the data under only one node named <files>...</files>. I don't care about the order.

P.S. I have no index file with the names of all the files. I searched "XSLT concatenate XML files" on google but the results always spoke about using the document() function and getting the names of the files from an index file. I would like to see the solution to this problem using the collection() function, if possible, please.

like image 337
Ariel Avatar asked Jan 30 '26 11:01

Ariel


1 Answers

XSLT 2.0 processors usually allow that (pulling in files from a directory/folder) using the collection function but the argument to that function is processor dependent. Using Saxon 9 you could use

<xsl:param name="folder-url" select="'file:///C:/dir/folder'"/>

<xsl:template name="main">
  <files>
   <xsl:copy-of select="collection(concat($folder-url, '?select=*.xml'))"/>
  </files>
</xsl:template>

See http://www.saxonica.com/documentation/index.html#!sourcedocs/collections for details respectively check the documentation of your XSLT 2.0 processor.

like image 172
Martin Honnen Avatar answered Feb 01 '26 06:02

Martin Honnen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!