Home > Back-end >  Merging XML Files Using SAXON and XSLT
Merging XML Files Using SAXON and XSLT

Time:10-05

I am new to XSLT and SAXON and I wish to accomplish the following:

I have a long list of XML files representing some benchmark, all of the following structure:

<benchmark>
    <xpath>
        <query>xxxxx</query>
        <ast depth="6" size="11">
            ...
        </ast>
        <schemas>
            <validation schema="xquery-3.0.xsd" valid="yes"/>
            ...
        </schemas>
    </xpath>
    <xpath>
        <query>yyyyy</query>
        <ast depth="6" size="11">
            ...
        </ast>
        <schemas>
            <validation schema="xquery-3.0.xsd" valid="yes"/>
            ...
        </schemas>
    </xpath>
</benchamark>

I just want to merge them all in one XML file containing all the content (all <xpath> elements and their childs) from all files under one root benchmark node.

CodePudding user response:

The easiest approach would be to start the transformation with a named template and no input XML but to pull in all data from the directory with the collection function:

<xsl:template name="main">
  <benchmark>
    <xsl:copy-of select="collection('../../benchmark/?select=*xml')/*/node()"/>
  </benchmark>
</xsl:template>

and then call Saxon with the options -xsl:mergefiles.xsl -it:main -o:merged-file.xml.

  • Related