Today I was confronted with the problem of obtaining a list of all namespace attributes for a given XML document.
I found that it’s possible to achieve this quickly using the XPath expression //namespace::*
, which leverages the namespace axis defined in the XPath 1.0 specification.
I’m using Xalan bundled with Sun JDK 1.6, and so my code for getting a NodeList
object (and then performing some post-processing on it) is as follows:
1 // ... 2 NodeList nodeList = (NodeList) xPath.evaluate("//namespace::*", document, XPathConstants.NODESET); 3 // ...