How can i select the namespace definition tag with an xpath

Viewed 35

I have the below example:

<?xml version="1.0" encoding="UTF-8"?>
<my:root xmlns:my="http://www.example.com/mynamespace">
<my:child name="Sid">
<my:grandchild name="Sue">
<my:greatgrandchild name="Fred" />
</my:grandchild>
</my:child>
</my:root>

and i need to select the attribute xmlns:my using an xpath but it is not working. Can someone please advise?

Thanks!

1 Answers

With XPath 1.0 (and possibly some vendors that still support it in later versions), you can use the namespace:: axis:

/*/namespace::my

or

/*/namespace::*[name()='my']

With XPath 2.0 and greater, you can use the fn:namespace-uri-for-prefix() and fn:in-scope-prefixes() functions:

fn:namespace-uri-for-prefix("my", /*)
Related