查找 xml 节点的完整 xpath [英] Find xml node's full xpath

查看:48
本文介绍了查找 xml 节点的完整 xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有以下 PowerShell 代码行

Suppose the following PowerShell line of code

$node = Select-Xml -Path $filePath -XPath "//*[@$someAttribute]"

如何获取节点的 xpath?我想我可以使用它的 ParentNode 属性向上遍历,但有没有更好的方法来做到这一点?

How can I get the node's xpath? I figured I could just traverse up using its ParentNode property, but is there a better way to do it?

推荐答案

我认为 PowerShell 中没有任何内置功能可以执行您想要的操作.不过,向上递归并不太难.像这样的函数应该可以工作:

I don't think there's anything built into PowerShell to do what you want. Recursing upwards isn't too difficult, though. A function like this should work:

function Get-XPath($n) {
  if ( $n.GetType().Name -ne 'XmlDocument' ) {
    "{0}/{1}" -f (Get-XPath $n.ParentNode), $n.Name
  }
}

这篇关于查找 xml 节点的完整 xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆