如何使用 XPath 表达式选择所有叶节点? [英] How to select all leaf nodes using XPath expression?
本文介绍了如何使用 XPath 表达式选择所有叶节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我相信这是可能的,但无法弄清楚语法.像这样:
I believe it's possible but couldn't figure out the syntax. Something like this:
xmlNode.SelectNodes("//*[count(child::*) <= 1]")
但这不正确.
推荐答案
使用:
//node()[not(node())]
如果只需要 element 叶节点(这需要澄清——具有非元素子元素的元素是否被视为叶节点?),则以下 XPath 表达式选择它们:
In case only element leaf nodes are wanted (and this needs clarification -- are elements that have non-element children considered leaf nodes?), then the following XPath expression selects them:
//*[not(*)]
上面的两个表达式可能是最短的选择所需节点(任意节点或元素 -- 叶节点).
Both expressions above are probably the shortest that select the desired nodes (either any-node or element -- leaf nodes).
这篇关于如何使用 XPath 表达式选择所有叶节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文