问题
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