简体   繁体   English

将XML文件合并到merged.xml文件中

[英]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) 我有一个包含许多XML文件的文件夹,我需要将所有这些文件合并在一起,以仅包含一个XML,并且每个文件中的所有详细信息(merged.xml)

I would like to make that merge using XSL transform and a batch file 我想使用XSL转换和批处理文件进行合并

Examples of XML files: XML文件示例:

File1.xml File1.xml

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

File2.xml 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: 用于对文件运行XSL转换的批处理文件代码:

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

I tried this XSL transformation: 我尝试了以下XSL转换:

<?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: 但是,输出(merged.xml)文件为:

<?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: 所需的(merged.xml)文件是:

<?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. XSLT不是要使用的工具。 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. 根据所需的结果,您只想将所有文件连接在一起,但需要注意的是,生成的文件是格式良好的XML文档。 Basic algorithm: 基本算法:

  • For each file, remove <?xml?> processing instructions, <Errors> , and </Errors> tags. 对于每个文件,删除<?xml?>处理说明, <Errors></Errors>标签。
  • Concatentate all files together 并置所有文件
  • Prepend <?xml version="1.0" encoding="UTF-16"?><Errors> to concatentated output. <?xml version="1.0" encoding="UTF-16"?><Errors>附加到合并的输出中。
  • Append </Errors> to concatendated output. </Errors>附加到合并的输出中。

These steps can likely be streamlined by using the proper tool: Perl, Python, et.al. 使用适当的工具(Perl,Python等)可以简化这些步骤。

Just cuz the data is XML does not mean you have to use an XML tool to get the job done. 只是因为数据是XML并不意味着您必须使用XML工具来完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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