How to select namespace value via XPath

你。 提交于 2021-01-27 14:20:36

问题


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>

This is from the root node.

I've tried just about every combination, but I can't seem to find anything that works.


回答1:


Setting aside the distraction of the undeclared a: namespace, let's instead use this example:

<b xmlns:attr="value"/>

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.

XPath 1.0

Use the namespace axis:

/b/namespace::attr

will evaluate to

value

XPath 2.0

According to XML Path Language (XPath) 2.0 (Second Edition):

In XPath Version 2.0, the namespace axis is deprecated and need not be supported by a host language.

Instead, use the namespace-uri-for-prefix():

/b/namespace-uri-for-prefix('attr',.)

will evaluate to

value


来源:https://stackoverflow.com/questions/28822216/how-to-select-namespace-value-via-xpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!