使用 PowerShell 2.0 将多个 XML 文件合并为一个?
我有一个非常大的 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 方式 是什么?
干杯, 乔金
I have a directory of very large XML files with a structure as this:
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>
Now I am looking for a simple way to merge these files (*.xml) files into one output file:
<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:
<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?
Cheers,
Joakim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然执行此操作的 XSLT 方法非常简短,但 PowerShell 方法也很短:
希望这会有所帮助,
While the XSLT way to do this is pretty short, so is the PowerShell way:
Hope this helps,
就我个人而言,我不会使用 PowerShell 来完成这样的任务。
通常,您使用 PowerShell 来访问这样的配置文件,
然后您可以像处理对象一样处理 xml。很酷。
如果您需要处理大型 xml 结构,那么使用
[xml]
(相当于XmlDocument
)会占用相当大的内存。然而,这几乎就是 PowerShell 支持 xml 的所有方式(
get-command *xml* -CommandType cmdlet
将为您提供所有类似 xml 的命令)。当然可以使用 .NET 类进行 xml 操作,但该代码不会像真正的 PowerShell 方法那样漂亮。因此,对于您的任务,您需要使用一些读者/作家来完成此任务,恕我直言,这是不值得做的。
这就是为什么我认为 xslt 是更好的方法;)
如果您需要灵活,您可以在脚本执行期间生成 xlst 模板,或者只是替换文件名,这没问题。
Personally I would not use PowerShell for such a task.
Typically you use PowerShell to accessing config files like this
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 toXmlDocument
) is quite memory expensive.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.
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.