Given xml with a series of <Set>
elements that should all have an id
attribute, I need to validate that, and then process only those elements that do have an id.
I had thought that
$elementIDs = $elements.GetAttribute('id')
would only return actual values, as in an element that either did not have the attribute or the attribute was blank would not be included in the array. Not so, I can get blank values back. Later, when I iterate through that list I can test for a blank id
foreach ($id in $elementIDs) {
if ($id) {
But I would rather just have a clean list. I thought perhaps I could method chain, like so
$elementIDs = $elements.HasAttribute('id').GetAttribute('id')
but method chaining isn't an option here.
So I thought .Where()
might work. Either
$elementIDs = $elements.GetAttribute('id').Where({$_ -ne $Null})
or
$elementIDs = $elements.GetAttribute('id').Where({$args -ne $Null})
or even
$elementIDs = $elements.GetAttribute('id').Where({param ($id) $id -ne $Null})
But none seems to work. So, is there a simple, elegant way to do this, or am I stuck with $Nulls in my array and a conditional?