The technically correct answer is that this is ...
Impossible. You need to distinguish between an abstract document that represents the source text and the actual text of the source text. XPath works with abstraction, not source, and the location of the pseudo xmlns attribute only applies to the latter.
However...
You can fake it with the following XPath 2.0 expression:
//*[not(namespace-uri()=ancestor::*/namespace-uri())]
This selects any element that does not have an ancestor in the same namespace, which theoretically means that it selects all elements in which the namespace is declared. However, it will not intercept namespaces that will be re-declared. For example, consider this document:
<html xmlns="http://www.w3.org/1999/xhtml"> <head/> <body> <p xmlns="http://something"> <p xmlns="http://something"/> </p> </body> </html>
The above expression selects the html element and the first p . The second p has an ancestor in the same namespace, so it is not selected, although it indicates xmlns .
source share