我有一个非常大的 XML 文件的目录,其结构如下:
文件1.xml:
<root>
<EmployeeInfo attr="one" />
<EmployeeInfo attr="two" />
<EmployeeInfo attr="three" />
</root>
文件2.xml:
<root>
<EmployeeInfo attr="four" />
<EmployeeInfo attr="five" />
<EmployeeInfo attr="six" />
</root>
现在我正在寻找一种简单的方法将这些文件 (*.xml) 文件合并到一个输出文件中:
<root>
<EmployeeInfo attr="one" />
<EmployeeInfo attr="two" />
<EmployeeInfo attr="three" />
<EmployeeInfo attr="four" />
<EmployeeInfo attr="five" />
<EmployeeInfo attr="six" />
</root>
我正在考虑使用纯 XSLT,如下所示:
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Container>
<xsl:copy-of select="document('file1.xml')"/>
<xsl:copy-of select="document('file2.xml')"/>
</Container>
</xsl:template>
</xsl:stylesheet>
这可行,但没有我想要的那么灵活。作为 PowerShell(版本 2)的新手,渴望学习在 PowerShell 中使用 XML 的新最佳实践,我想知道什么是最简单、最纯粹的PowerShell方式将 XML 文档的结构合并为一个?
干杯,
乔金
虽然执行此操作的 XSLT 方法非常简短,但 PowerShell 方法也很短:
$finalXml = "<root>"
foreach ($file in $files) {
[xml]$xml = Get-Content $file
$finalXml += $xml.InnerXml
}
$finalXml += "</root>"
([xml]$finalXml).Save("$pwd\final.xml")
希望这可以帮助,
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)