XPath:如何检查属性是否存在? [英] XPath: How to check if an attribute exists?

查看:40
本文介绍了XPath:如何检查属性是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下 XML,我如何编写 XPath 查询来拉取属性 foo 存在的节点?:

Given the following XML, how do I write an XPath query to pull nodes where the attribute foo exists?:

<node1>
  <node2>
    <node3 foo='bar'></node3>
    <node3></node3>
    <node3 bar='foo'></node3>
    <node3 foo='foobar'></node3>
  </node2>
</node1>

推荐答案

简短而甜蜜:

//*[@foo]

当然,您应该使用更具体的表达方式.但是使用 [@attributeName] 可以获得具有该属性的所有节点.

Of course you should use a more specific expression. But with [@attributeName] you get all nodes which have that attribute.

这篇关于XPath:如何检查属性是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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