xml 如何在 XPath 中按属性命名空间查找元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/529556/
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 12:21:20  来源:igfitidea点击:

How to find elements by attribute namespace in XPath

xmlxpathnamespaces

提问by Gareth

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

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

For example, in the following document I want to find the foo:barand doodahelements:

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

<?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>

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

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

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

However:

然而:

  • This doesn't give me the elements, just the attributes.
  • Where elements contain multiple attributes from that namespace, this selector will return a result per-attribute rather than per-element
  • 这没有给我元素,只是属性。
  • 如果元素包含来自该命名空间的多个属性,则此选择器将返回每个属性而不是每个元素的结果

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?

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

回答by Dimitre Novatchev

Use:

用:

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

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

谓词两个条件与 XPathor运算符进行或运算。

The XPath expression thus selects any element that either:

XPath 表达式因此选择任何元素:

  • belongs to the specified namespace.
  • has attributes that belong to the specified namespace.
  • 属于指定的命名空间。
  • 具有属于指定命名空间的属性。

回答by jor

I'm not sure if this is what you mean, but by only deleting one char in your XPath you get all elements in a certain namespace:

我不确定这是否是您的意思,但是通过仅删除 XPath 中的一个字符,您可以获得某个命名空间中的所有元素:

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

回答by Johannes Weiss

You could try

你可以试试

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

It will give you element foo:bar and element doodah (if you change tal:quuxto foo:quuxin your XML-data):

它将为您提供元素 foo:bar 和元素 doodoh(如果您在 XML 数据中更改tal:quuxfoo:quux):

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

Is that what you want?

那是你要的吗?

回答by potyl

Your XPath expression is almost perfect. Instead of asking for attributes "@" ask for elements "" and it should work:

您的 XPath 表达式几乎是完美的。而不是要求属性“@ ”要求元素“”,它应该工作:

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