在 PowerShell 中删除重复的 XML 节点 [英] Remove Duplicate XML Nodes in PowerShell
问题描述
我有一个如下所示的 XML 文件:
I have an XML file that looks like the following:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<software>
<name>MozillaFirefox</name>
<version>31.3.0</version>
<installer_location>/Mozilla/Firefox/31.3.0.exe</installer_location
</software>
<software>
<name>GoogleChrome</name>
<version>35.7</version>
<installer_location>/Google/Chrome/35.7.msi</installer_location
</software>
<software>
<name>MozillaFirefox</name>
<version>33.4.0</version>
<installer_location>/Mozilla/Firefox/33.4.0.exe</installer_location>
</software>
</catalog>
这是我当前的代码:
#Load XML file into $catalogXML
[xml]$catalogXML = (Get-Content (C:\test.xml))
$softwareVersionsArray = $catalogXML.catalog.software
输出:
name version installer_location
---- ------- ------------------
MozillaFirefox 31.3.0 /Mozilla/Firefox/31.3.0.exe
GoogleChrome 35.7 /Google/Chrome/35.7.msi
MozillaFirefox 33.4.0 /Mozilla/Firefox/33.4.0.exe
我需要帮助进行编码,以便删除任何重复项,并保留第一个条目(即显示 Firefox 31.3.0 并删除 Firefox 33.3.4).我尝试了各种 XPath 语句和 Select -Unique 过滤器都无济于事.在此先感谢您的帮助!
I need assistance coding this so that any duplicates are removed and that the first entry is the one that is kept (i.e. Firefox 31.3.0 is displayed and Firefox 33.3.4 is removed). I've tried various XPath statements and Select -Unique filters to no avail. Thanks in advance for any help!
推荐答案
为了得到想要的结果,你可以通过 name
对元素进行分组:Group-Object name
;然后从每个组中选择第一个元素:ForEach-Object {$_.Group[0]}
.
To get desired results, you can group elements by name
: Group-Object name
; and than select first element from each group: ForEach-Object {$_.Group[0]}
.
$catalogXML.catalog.software|
Group-Object name|
ForEach-Object {$_.Group[0]}
这篇关于在 PowerShell 中删除重复的 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!