Issue
I need all namespaces of xml document.
<out1:case<br />
xmlns:out="http://ns1"<br />
xmlns:out1="http://ns2" schemaVersion="1.0"><br />
</out1:case>
XPathFactory.newInstance().newXPath().evaluate("namespace-uri(/*)", document.getChildNodes());
It return only "http://ns2"
How I could get list of "http://ns1"
and "http://ns2"
?
Solution
To get all namespaces that are in-scope on the outermost element, use /*/namespace::*
. To get all namespaces that are in-scope anywhere in the document, use //*/namespace::*
. However (a) the latter will include a large number of duplicates, and (b) if your input is a DOM, I'm not sure how the namespace nodes will be represented in the result of the XPath expression.
In XPath 2.0 you have more flexibility, for example you could return the distinct namespace URIs declared anywhere in the document as a sequence of strings using distinct-values(//*/namespace::*)
.
Answered By - Michael Kay
Answer Checked By - Marie Seifert (JavaFixing Admin)