使用PowerShell 2.0将多个XML文件合并为一个?

时间:2022-07-30 21:51:05

I have a directory of very large XML files with a structure as this:

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

file1.xml:

file1.xml:

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

file2.xml:

file2.xml:

<root>
 <EmployeeInfo attr="four" />
 <EmployeeInfo attr="five" />
 <EmployeeInfo attr="six" />
</root>

Now I am looking for a simple way to merge these files (*.xml) files into one output file:

现在我正在寻找一种将这些文件(* .xml)文件合并到一个输出文件中的简单方法:

<root>
 <EmployeeInfo attr="one" />
 <EmployeeInfo attr="two" />
 <EmployeeInfo attr="three" />
 <EmployeeInfo attr="four" />
 <EmployeeInfo attr="five" />
 <EmployeeInfo attr="six" />
</root>

I was thinking about using pure XSLT such as this one:

我正在考虑使用像这样的纯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>

This works but isn't as flexible as I want. Being a novice with PowerShell (version 2) eager to learn new best pracctices of working with XML in PowerShell I am wondering what is the simplest, purest PowerShell way of merging the structre of XML documents into one?

这可行,但不如我想要的灵活。作为PowerShell(第2版)的新手,渴望学习在PowerShell中使用XML的新的最佳实践,我想知道将XML文档的结构合并为一个最简单,最纯粹的PowerShell方法是什么?

Cheers, Joakim

干杯,Joakim

2 个解决方案

#1


11  

While the XSLT way to do this is pretty short, so is the PowerShell way:

虽然XSLT的方法很短,但PowerShell的方式也是如此:

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

Hope this helps,

希望这可以帮助,

#2


2  

Personally I would not use PowerShell for such a task.

就个人而言,我不会将PowerShell用于此类任务。

Typically you use PowerShell to accessing config files like this

通常,您使用PowerShell访问这样的配置文件

$config = [xml](gc web.config)

then you can work with the xml like with objects. Pretty cool. If you need to process large xml structures, then using [xml] (which is equivalent to XmlDocument) is quite memory expensive.

那么你可以像对象一样使用xml。很酷。如果你需要处理大型xml结构,那么使用[xml](相当于XmlDocument)非常耗费内存。

However, that's almost everything how PowerShell supports xml (get-command *xml* -CommandType cmdlet will give you all xml like commands).
It is of course possible to use .NET classes for xml operations, but that code won't be as pretty as true PowerShell approach. So, for your task you would need to use some readers/writers for that, which is imho not worthy doing.

但是,这几乎是PowerShell支持xml的所有方法(get-command * xml * -CommandType cmdlet将为您提供所有类似xml的命令)。当然可以将.NET类用于xml操作,但该代码不如真正的PowerShell方法那么漂亮。所以,对于你的任务,你需要使用一些读者/作者,这是不值得做的。

That's why I think xslt is better approach ;) If you need to be flexible, you can generate the xlst template during script execution or just replace the file names, that's no problem.

这就是为什么我认为xslt是更好的方法;)如果你需要灵活,你可以在脚本执行期间生成xlst模板或只是替换文件名,这没有问题。

#1


11  

While the XSLT way to do this is pretty short, so is the PowerShell way:

虽然XSLT的方法很短,但PowerShell的方式也是如此:

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

Hope this helps,

希望这可以帮助,

#2


2  

Personally I would not use PowerShell for such a task.

就个人而言,我不会将PowerShell用于此类任务。

Typically you use PowerShell to accessing config files like this

通常,您使用PowerShell访问这样的配置文件

$config = [xml](gc web.config)

then you can work with the xml like with objects. Pretty cool. If you need to process large xml structures, then using [xml] (which is equivalent to XmlDocument) is quite memory expensive.

那么你可以像对象一样使用xml。很酷。如果你需要处理大型xml结构,那么使用[xml](相当于XmlDocument)非常耗费内存。

However, that's almost everything how PowerShell supports xml (get-command *xml* -CommandType cmdlet will give you all xml like commands).
It is of course possible to use .NET classes for xml operations, but that code won't be as pretty as true PowerShell approach. So, for your task you would need to use some readers/writers for that, which is imho not worthy doing.

但是,这几乎是PowerShell支持xml的所有方法(get-command * xml * -CommandType cmdlet将为您提供所有类似xml的命令)。当然可以将.NET类用于xml操作,但该代码不如真正的PowerShell方法那么漂亮。所以,对于你的任务,你需要使用一些读者/作者,这是不值得做的。

That's why I think xslt is better approach ;) If you need to be flexible, you can generate the xlst template during script execution or just replace the file names, that's no problem.

这就是为什么我认为xslt是更好的方法;)如果你需要灵活,你可以在脚本执行期间生成xlst模板或只是替换文件名,这没有问题。