xml XPath:如何检查属性是否存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3737906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XPath: How to check if an attribute exists?
提问by Byron Whitlock
Given the following XML, how do I write an XPath query to pull nodes where the attribute fooexists?:
给定以下 XML,我如何编写 XPath 查询来拉取属性foo存在的节点?:
<node1>
<node2>
<node3 foo='bar'></node3>
<node3></node3>
<node3 bar='foo'></node3>
<node3 foo='foobar'></node3>
</node2>
</node1>
回答by Felix Kling
Short and sweet:
简短而甜蜜:
//*[@foo]
Of course you should use a more specific expression. But with [@attributeName]you get all nodes which have that attribute.
当然,您应该使用更具体的表达方式。但是随着[@attributeName]您获得具有该属性的所有节点。
回答by Ru5
Use the following XPath expression
使用以下 XPath 表达式
//*[boolean(@foo)]
回答by fritz
If you use and xpath, this maybe can help you:
如果您使用和 xpath,这可能可以帮助您:
count(//*[@foo])
it will return count of node/child that have attribute foo
它将返回具有属性 foo 的节点/子节点的计数

