title: normalizing XML namespaces in a DOM document in Java?
body: <p>I wonder if there is a straightforward way to 'normalize' XML namespace definitions in an XML document represented as a DOM document in Java?</p>
  
  <p>the reason I'd need this is to be able to compare two documents, which both use XML namespaces.</p>
  
  <p>as XML namespaces can be specified anywhere in the document (in the root element, in any of the elements), etc., two documents that are in effect the same may be significantly different when looked at from a DOM tree perspective. for example, one can have all namespace attributes defined in the root element, while the other can define each namespace at the 'highest' element in the DOM tree hierarchy where the namespace is applicable. In essence these can be the same documents, but when comparing them, say with XmlUnit, one will get comparison issues. </p>
  
  <p>to provide two examples:</p>
  
  <pre><code>&lt;root xmlns:foo="http://foo/"&gt;
      &lt;e1&gt;
          &lt;foo:e2&gt;bar&lt;/foo:e2&gt;
      &lt;/e1&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>vs:</p>
  
  <pre><code>&lt;root&gt;
      &lt;e1 xmlns:foo="http://foo/"&gt;
          &lt;foo:e2&gt;bar&lt;/foo:e2&gt;
      &lt;/e1&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>these documents are in effect the same, but an XML comparison will find them different.</p>
  
  <p>I wonder if there is a straightforward / easy way to normalize namespace definitions, say, put them all in the root element?</p>
  
  <p>of course one can write such code himself, but if this was available already, that would be way better :)</p>