In Powershell, suppose I have the following xml:
<Users>
<User Name="Foo">
<Friends>
<Friend Name="Bar"/>
</Friends>
</User>
<User Name="Foo2" />
<User Name="Foo3">
<Friends>
<Friend Name="Bar"/>
</Friends>
</User>
</Users>
How can I get all the users that have a "Bar" as a friend? (In this example it would be Foo,Foo3).
Should I use xpath?
Thanks!
I have a preference for using XPath these days. I've run into issues using PowerShell's xml adapter that are annoying like the name collision on item
:
$xml = [xml]@'
<Users>
<User Name="Foo">
<Friends>
<Friend Name="Bar"/>
</Friends>
</User>
<User Name="Foo2" />
<User Name="Foo3">
<Friends>
<Friend Name="Bar"/>
</Friends>
</User>
</Users>
'@
Select-Xml '//User[contains(Friends/Friend/@Name, "Bar")]' $xml |%{$_.Node.Name}
In this case I would use XPath as well. @TomWij provided the correct xpath.
In case that the xpath expression would be too complicated, I would for sure used the 'classic' approach.
$x.Users.User | ? { $_.Friends.Friend.Name -eq 'Bar' }
(in this case if you don't have script mode on, it doesn't matter that there is no Friends
element. $_.Friends
will return $null
and $null.Friend
returns $null
as well and so on. So finally $null -eq 'Bar'
returns false and the element is removed by Where-Object)
Yes, XPath would be sufficient for that!
Check Powershell to test your XPath to see how it can be done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With