private775 icon

[PS] Find xml node xpath

private775 | PRO | 05/16/18 11:24:54 AM UTC | 0 ⭐ | 847 👁️ | Never ⏰ | []
PowerShell |

650 B

|

None

|

0 👍

/

0 👎

function FindXPath([System.Xml.XmlNode]$node) {
    [string]$cur = ''
    if($node -ne $null){
        if($node.NodeType -eq [System.Xml.XmlNodeType]::Attribute) {
            $cur = "/@[$($node.Name)]"
        } elseif($node.NodeType -eq [System.Xml.XmlNodeType]::Element) {
            $cur = "/$($node.Name)"
        } elseif($node.NodeType -eq [System.Xml.XmlNodeType]::Document) {
            $cur = ""
        } else {
            $cur = "Not Supported"
        }
        $cur = "$(FindXPath($node.ParentNode))$($cur)"
    }
    return $cur
}
 
[xml]$xml = Get-Content $xmlfile
 
$nodes = $xml.SelectNodes("//Guid")
$nodes | % { FindXPath $_ }

Comments