JimBob117
JimBob117

Reputation: 99

Remove Duplicate XML Nodes in PowerShell

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>

Here is my current code:

#Load XML file into $catalogXML
[xml]$catalogXML = (Get-Content (C:\test.xml))

$softwareVersionsArray = $catalogXML.catalog.software

Which outputs this:

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

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!

Upvotes: 1

Views: 3229

Answers (2)

Denis
Denis

Reputation: 3

you can also do the following:

<xsl:for-each select="//name[not(preceding::name/. = .)]>
  <xsl:value-of select=".">
</xsl:for-each>

Upvotes: 0

user4003407
user4003407

Reputation: 22122

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]}

Upvotes: 5

Related Questions