I'm trying to merge multiple xml files in specific folder using xslt and saxon-HE 9.9.1 .NET. I need to create a generic merger so that I will not add a static tag inside the template to use it with different nodes' names, I tried to make a loop to add the root or the top level tag once in the beginning but it also close the tag before the xml end but there is an issue with the top level tag Example: XML File1:
<Arr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Src>
<name>C</name>
<pr>pr</pr>
<par>
<Q>
<isC>false</isC>
<name>hrh1</name>
<type>xx1</typ>
</Q>
</par>
<st />
</Src>
<Src>
<name>C</name>
<pr></pr>
<par>
<Q>
<isC>false</isC>
<name>hrh2</name>
<type>xx2</typ>
</Q>
</par>
<st>
<Src>
<name>st1</name>
<pr>prst1</pr>
<par>
<Q>
<isC>false</isC>
<name>q1</name>
<type>t1</type>
</Q>
<Q>
<isC>false</isC>
<name>q2</name>
<type>t2</type>
</Q>
</par>
<st />
</Src>
</st>
</Src>
</Arr>
XML File2:
<Arr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Src>
<name>CFile2</name>
<pr>C2</pr>
<par>
<Q>
<isC>false</isC>
<name>hrh1</name>
<type>xx1</typ>
</Q>
</par>
<st />
</Src>
</Arr>
expected output:
<Arr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Src>
<name>C</name>
<pr>pr</pr>
<par>
<Q>
<isC>false</isC>
<name>hrh1</name>
<type>xx1</typ>
</Q>
</par>
<st />
</Src>
<Src>
<name>C</name>
<pr></pr>
<par>
<Q>
<isC>false</isC>
<name>hrh2</name>
<type>xx2</typ>
</Q>
</par>
<st>
<Src>
<name>st1</name>
<pr>prst1</pr>
<par>
<Q>
<isC>false</isC>
<name>q1</name>
<type>t1</type>
</Q>
<Q>
<isC>false</isC>
<name>q2</name>
<type>t2</type>
</Q>
</par>
<st />
</Src>
</st>
</Src>
<Src>
<name>CFile2</name>
<pr>C2</pr>
<par>
<Q>
<isC>false</isC>
<name>hrh1</name>
<type>xx1</typ>
</Q>
</par>
<st />
</Src>
</Arr>
my current template which is produces wrong output because of the top level tag closure
<?xml version="1.0" encoding="windows-1256"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:user="http://tempuri.org/msxsl" >
<xsl:output method="xml" indent="yes" encoding="windows-1256" />
<xsl:template name="main">
<xsl:for-each select="collection('.?select=*.xml')/*">
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:copy>
<xsl:copy-of select="/*/node()"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="/*/node()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I run from the cmd with the following command:
transform -it:main -xsl:merge_xml.xslt -o:output.xml
my current output which is wrong
<Arr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Src>
<name>C</name>
<pr>pr</pr>
<par>
<Q>
<isC>false</isC>
<name>hrh1</name>
<type>xx1</typ>
</Q>
</par>
<st />
</Src>
<Src>
<name>C</name>
<pr></pr>
<par>
<Q>
<isC>false</isC>
<name>hrh2</name>
<type>xx2</typ>
</Q>
</par>
<st>
<Src>
<name>st1</name>
<pr>prst1</pr>
<par>
<Q>
<isC>false</isC>
<name>q1</name>
<type>t1</type>
</Q>
<Q>
<isC>false</isC>
<name>q2</name>
<type>t2</type>
</Q>
</par>
<st />
</Src>
</st>
</Src>
</Arr>
<Src>
<name>CFile2</name>
<pr>C2</pr>
<par>
<Q>
<isC>false</isC>
<name>hrh1</name>
<type>xx1</typ>
</Q>
</par>
<st />
</Src>