Modify XML Parent Attributes iterating through Child attributes using powershell

后端 未结 2 1541
北海茫月
北海茫月 2020-12-20 08:15

So I have the following xml (items.xml) and I want to find the attributes of the child node item iterate through the attributes and if I find similar attributes at parent no

2条回答
  •  無奈伤痛
    2020-12-20 09:17

    You can get parent element from an attribute via OwnerElement property. So this is one possible way to get the desired output :

    $model.SelectNodes("//item/@*") |
        ForEach {
            # set attributes value on `model` element
            $_.OwnerElement.ParentNode.ParentNode.SetAttribute($_.LocalName, $_.Value)
            # remove attributes except `name` from `item` element
            If ($_.LocalName -ne "name") { $_.OwnerElement.RemoveAttribute($_.LocalName) }
        }
    

提交回复
热议问题