Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除PowerShell中重复的XML节点_Xml_Powershell - Fatal编程技术网

删除PowerShell中重复的XML节点

删除PowerShell中重复的XML节点,xml,powershell,Xml,Powershell,我有一个如下所示的XML文件: <?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</instal

我有一个如下所示的XML文件:

<?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>
哪个输出:

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过滤器,但都没有用。提前感谢您的帮助

要获得所需的结果,可以按
名称
组对象名称
;然后从每个组中选择第一个元素:
ForEach对象{$\.group[0]}

$catalogXML.catalog.software|
Group-Object name|
ForEach-Object {$_.Group[0]}

要获得所需的结果,可以按
名称
组对象名称
;然后从每个组中选择第一个元素:
ForEach对象{$\.group[0]}

$catalogXML.catalog.software|
Group-Object name|
ForEach-Object {$_.Group[0]}

您还可以执行以下操作:

<?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>

您还可以执行以下操作:

<?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>