如何通过 XPath 选择命名空间值 [英] How to select namespace value via XPath
问题描述
我正在尝试使用 XPath 从这个 XML 中获取 xmlns:attr
属性值.我似乎无法理解.
I'm trying to get the xmlns:attr
attribute value from this XML using XPath. I can't seem to get it.
<a:b xmlns:attr="value">
</a:b>
这是来自根节点.
我尝试了几乎所有组合,但似乎找不到任何有效的组合.
I've tried just about every combination, but I can't seem to find anything that works.
推荐答案
抛开未声明的 a:
命名空间的干扰,让我们改用这个例子:
Setting aside the distraction of the undeclared a:
namespace, let's instead use this example:
<b xmlns:attr="value"/>
注意:您对 attr
的名称选择掩盖了在上述 XML 中 attr
不是 的事实属性而是一个命名空间前缀.
Note: Your name choice of attr
belies the fact that in the above XML, attr
is not an attribute but rather is a namespace prefix.
使用 namespace
轴:
Use the namespace
axis:
/b/namespace::attr
将评估为
value
XPath 2.0
在 XPath 2.0 版中,名称空间轴已弃用,不需要受宿主语言支持.
In XPath Version 2.0, the namespace axis is deprecated and need not be supported by a host language.
改为使用 namespace-uri-for-prefix()
:
/b/namespace-uri-for-prefix('attr',.)
将评估为
value
这篇关于如何通过 XPath 选择命名空间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!