1

While using xpath, do we need to have a document name space aware to be able to use local-name

"//*[local-name()='Example']"

Form the tests I performed looks like it does not play a role, but this raises another question.

Does xpath care if the document is namespace aware or not ? Any pointer to rights docs or resources is appreciated. Thanks

GionJh
  • 2,742
  • 2
  • 29
  • 68

1 Answers1

1

No, you do not have to know a document name space when using local-name.
The only difference between local-name and name is that local-name will give you local name only, without the namespace (if exists) while name will give you the name including the the namespace (if exists). See this explanation for more details.

In general, however, be aware that using local-host() to defeat namespaces is not recommended. The purpose of XML namespaces is to allow names such as Example to exist in an XML document from multiple XML vocabularies without conflict. Namespaces enable this capability and shouldn't be defeated without good reason. For examples of how to declare and use namespace prefixes for XPath in many different hosting languages and libraries, see How does XPath deal with XML namespaces?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Thanks for your fast response, small additional question: I see that using the org.w3c.dom.Document API, there's no easy way to achieve that if you do not know whether the tag you are looking for has a name space or not, would you have any hint on how xpath achieves that ? – GionJh Oct 27 '22 at 16:55
  • Well, I have no such practical experience, but I guess: you can try using 2 xpathes: one with `name` and the second with `local-name`. In case they will give you the same result you will know there is no name space there, otherwise you will be able to get the name space by reducing the local name received with `local-name` from total name received with `name`. Or maybe I did not understand your question? – Prophet Oct 27 '22 at 17:02
  • It's my pleasure :) – Prophet Oct 27 '22 at 17:08
  • 1
    Thanks a lot @kjhughes. if you wish you are welcome to edit my answer – Prophet Oct 27 '22 at 17:22