我有一个非常大的XML文件目录,其结构如下:

file1.xml:

<root>
 <EmployeeInfo attr="one" />
 <EmployeeInfo attr="two" />
 <EmployeeInfo attr="three" />
</root>

file2.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的新的最佳实践,我想知道将XML文档的结构合并为一个的最简单,最纯的PowerShell方法是什么?

干杯,
约阿基姆

最佳答案

尽管XSLT的实现方法非常短,但是PowerShell的方法也是如此:

$finalXml = "<root>"
foreach ($file in $files) {
    [xml]$xml = Get-Content $file
    $finalXml += $xml.InnerXml
}
$finalXml += "</root>"
([xml]$finalXml).Save("$pwd\final.xml")

希望这可以帮助,

09-30 18:06
查看更多