TOGEEK
TOGEEK

Reputation: 741

Filtering XML attributes

I have this script below which filters out a list of xmls based on the attribute value:

gci $path | Select-Xml -XPATH "//Order[contains(@Value, '0')]"

I am trying to filter out those XMLs with a "value of zero". The problem is that when I run the script against my source, it returns everything that has a "0" in the value. How can I change the filter to only filter on "0", ie. no value greater than zero? I guess there is a filter to replace "contains" to something like "match", but I've tried this. Can't find any alternatives on the Internet.

Upvotes: 0

Views: 1729

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

Use a Where-Object filter with a -not condition to exclude all files where your XPath expression produces a match:

Get-ChildItem $path | Where-Object {
  -not (Select-Xml -Path $_.FullName -XPath "//Order[contains(@value, '0')]")
}

Note that this might produce false negatives, though (e.g. if the attribute value contains a value like 10, 201, …). If you only want to exclude files where an attribute value is exactly 0 you can do so with a numeric comparison:

Get-ChildItem $path | Where-Object {
  -not (Select-Xml -Path $_.FullName -XPath "//Order[@value = 0]"
}

Upvotes: 1

Related Questions