Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare xml structure using Powershell

Tags:

powershell

I have two xml config files, I need compare ONLY the structure of the two files and display the difference. Please note: when comparing, the values within the xml nodes should be ignored.

ex:

XML 1 
----
<recipe>
  <ingredients>
      <ingredient1></ingredient1><ingredient2></ingredient2>
  </ingredients>
  <description></description>
</recipe>

XML 2
-----
<recipe>
  <ingredients>
    <ingredient1></ingredient1>
  </ingredients>
  <description></description>
  <images></images>
</recipe>

The result should be a difference of the two xml files.

xml1 <ingredient2>
xml2 <images>

Help much appreciated.

like image 487
jack Avatar asked Mar 07 '26 15:03

jack


1 Answers

Quickest solution I can come up with is:

[xml]$xml1 = @"
<recipe>
  <ingredients>
      <ingredient1>
      </ingredient1>
      <ingredient2>
      </ingredient2>
  </ingredients>
  <description></description>
</recipe>
"@

[xml]$xml2 = @"
<recipe>
  <ingredients>
    <ingredient1>dada</ingredient1>
  </ingredients>
  <description>dadad</description>
  <images></images>
</recipe>
"@

$docDiffs=Compare-Object ($xml1.SelectNodes("//*") | Select-Object -Expand Name) ($xml2.SelectNodes("//*") | Select-Object -Expand Name) 

$docDiffs

You will need to do a little bit of work to get the exact formating you need, but the main work had been done. If you want to improve on it you can look at using Select-XML alternatively.

like image 103
Webplanet TFS Consulting Avatar answered Mar 09 '26 03:03

Webplanet TFS Consulting



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!