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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:17:46  来源:igfitidea点击:

XPath: How to check if an attribute exists?

xmlxpath

提问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 的节点/子节点的计数