How to retrieve namespaces in XML files using Xpath?

There are a few techniques that you might try; which you use will depend on exactly what information you need to get out of the document, how rigorous you want to be, and how conformant the XPath implementation you're using is One way to get the namespace URI associated with a particular prefix is using the namespace:: axis. This will give you a namespace node whose name is the prefix and whose value is the namespace URI. For example, you could get the default namespace URI on the document element using the path: namespace::*name()='' You might be able to use that to set up the namespace associations for your XPathNavigator.Be warned, though, that the namespace:: axis is one of those corners of XPath 1.0 that isn't always implemented A second way of getting that namespace URI is to use the namespace-uri() function on the document element (which you've said will always be in that namespace).

The expression: namespace-uri(/*) will give you that namespace An alternative would be to forget about associating a prefix with that namespace, and just make your path namespace-free. You can do this by using the local-name() function whenever you need to refer to an element whose namespace you don't know. For example: local-name() = 'Element' You could go one step further and test the namespace URI of the element against the one of the document element, if you really wanted: local-name() = 'Element' and namespace-uri() = namespace-uri(/*) A final option, given that the namespace seems to mean nothing to you, would be to run your XML through a filter that strips out the namespaces.

Then you won't have to worry about them in your XPath at all. The easiest way to do that would be simply to remove the xmlns attribute with a regular expression, but you could do something more complex if you needed to do other tidying at the same time.

There are a few techniques that you might try; which you use will depend on exactly what information you need to get out of the document, how rigorous you want to be, and how conformant the XPath implementation you're using is. One way to get the namespace URI associated with a particular prefix is using the namespace:: axis. This will give you a namespace node whose name is the prefix and whose value is the namespace URI.

For example, you could get the default namespace URI on the document element using the path: /*/namespace::*name()='' You might be able to use that to set up the namespace associations for your XPathNavigator. Be warned, though, that the namespace:: axis is one of those corners of XPath 1.0 that isn't always implemented. A second way of getting that namespace URI is to use the namespace-uri() function on the document element (which you've said will always be in that namespace).

The expression: namespace-uri(/*) will give you that namespace. An alternative would be to forget about associating a prefix with that namespace, and just make your path namespace-free. You can do this by using the local-name() function whenever you need to refer to an element whose namespace you don't know.

For example: //*local-name() = 'Element' You could go one step further and test the namespace URI of the element against the one of the document element, if you really wanted: //*local-name() = 'Element' and namespace-uri() = namespace-uri(/*) A final option, given that the namespace seems to mean nothing to you, would be to run your XML through a filter that strips out the namespaces. Then you won't have to worry about them in your XPath at all. The easiest way to do that would be simply to remove the xmlns attribute with a regular expression, but you could do something more complex if you needed to do other tidying at the same time.

Thanks you very much for your detailed answer It seems that I have no reputation yet to vote you up – Luis Filipe Sep 24 '08 at 9:03 an exemplary answer, thank you, JeniT – kostja Dec 15 '10 at 9:20.

Unfortunately, XPath doesn't have any concept of "default namespace". You need to register namespaces with prefixes with the XPath context, and then use those prefixes in your XPath expressions. It means for very verbose xpath, but it's a basic shortcoming of XPath 1.

Apparently XPath 2 will address this, but that's no use to you right now. I suggest that you programmatically examine your XML document for the namespace, associate that namespace with a prefix in the XPath context, then use the prefix in the xpath expressions.

It seems it will have to boil down to that..! Thanks – Luis Filipe Sep 23 '08 at 18:30 I suspect this is the actualy answer since it seems to be the desire was to avoid the added complexities of querying a namespace in XPath. Don't forget to accept the appropriate answer. – AnthonyWJones Sep 23 '08 at 21:25.

This 40-line xslt transformation provides all the useful information about the namespaces in a given XML document: ' When applied on the following XML document: the wanted result is produced: Hope this helped. Cheers, Dimitre Novatchev.

Depending on what you are working with for parsing, this might help you out!

I am using C#, so I could use Regex. I think I will use Regex to retrieve the namespace if XPath classes do not have a built-in way to do that. Thank you.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions