简体   繁体   中英

Merging XML Files together into merged.xml file

I will try to make my question as short as possible and with every detail needed..

I have a folder with MANY XML files and I need to merge all them together to have only one XML with all the details inside each one of them (merged.xml)

I would like to make that merge using XSL transform and a batch file

Examples of XML files:

File1.xml

<?xml version="1.0" encoding="UTF-16"?>
<Errors>
<Error>
<Number>1</Number>
...Some other elements...
</Error>
</Errors>

File2.xml

<?xml version="1.0" encoding="UTF-16"?>
<Errors>
</Error>
<Number>2</Number>
...Some other elements...
</Error>
</Errors>

Batch file code used to run XSL transformation on files:

for %%f in (File*.xml) do (
msxsl "%%~nf.xml" merge.xsl >> "merged.xml"
)
pause

I tried this XSL transformation:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/Errors">
<xsl:copy>
<xsl:apply-templates select="//*/Error"/>
</xsl:copy>
</xsl:template>


<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

However the output (merged.xml) file is:

<?xml version="1.0" encoding="UTF-16"?>
<Errors>
<Error>
<Number>1</Number>
...Some other elements...
</Error>
</Errors>
<?xml version="1.0" encoding="UTF-16"?>
<Errors>
<Error>
<Number>2</Number>
...Some other elements...
</Error>
</Errors>

The needed (merged.xml) file is:

<?xml version="1.0" encoding="UTF-16"?>
<Errors>
<Error>
<Number>1</Number>
...Some other elements...
</Error>
<Error>
<Number>2</Number>
...Some other elements...
</Error>
</Errors>

Sorry for making it long!!

Thanks in advance :) Looking forward you answer

XSLT is not the tool to use. Based on your desired results, you just want to concatenate all the files together, with the caveat that the resulting file is a well-formed XML document. Basic algorithm:

  • For each file, remove <?xml?> processing instructions, <Errors> , and </Errors> tags.
  • Concatentate all files together
  • Prepend <?xml version="1.0" encoding="UTF-16"?><Errors> to concatentated output.
  • Append </Errors> to concatendated output.

These steps can likely be streamlined by using the proper tool: Perl, Python, et.al.

Just cuz the data is XML does not mean you have to use an XML tool to get the job done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM