bryan sammon
bryan sammon

Reputation: 7441

How to select namespace value via XPath

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.

Upvotes: 4

Views: 2040

Answers (1)

kjhughes
kjhughes

Reputation: 111491

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

Upvotes: 6

Related Questions