如何在XPath中按属性名称空间查找元素 [英] How to find elements by attribute namespace in XPath

查看:77
本文介绍了如何在XPath中按属性名称空间查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用XPath查找在给定名称空间中具有元素的所有元素.

I'm trying to use XPath to find all elements that have an element in a given namespace.

例如,在以下文档中,我想找到foo:bardoodah元素:

For example, in the following document I want to find the foo:bar and doodah elements:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:foo="http://foo.example.com">
  <foo:bar quux="value">Content</foo:bar>
  <widget>Content</widget>
  <doodah foo:quux="value">Content</doodah>
</root>

我知道我可以使用以下XPath表达式从给定的名称空间中加载所有属性:

I know I can use the following XPath expression to load all attributes from a given namespace:

"//@*[namespace-uri()='http://foo.example.com']"

但是:

  • 这不会给我元素,而只是给我属性.
  • 如果元素包含来自该命名空间的多个属性,则此选择器将按属性而不是按元素返回结果

是否有可能获得我想要的东西,还是必须收集属性并计算它们所对应的元素的唯一集合?

Is it possible to get what I want, or do I have to gather the attributes and calculate the unique set of elements they correspond to?

推荐答案

使用:

//*[namespace-uri()='yourNamespaceURI-here'
   or
    @*[namespace-uri()='yourNamespaceURI-here']
   ]

谓词两个条件由XPath or运算符排序.

the predicate two conditions are or-ed with the XPath or operator.

XPath表达式因此选择了以下任一元素:

The XPath expression thus selects any element that either:

  • 属于指定的名称空间.
  • 具有属于指定名称空间的属性.

这篇关于如何在XPath中按属性名称空间查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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