title: Applying xpath on XML where namespaces are with children with XOM
body: <p>I'm using XOM library to parse xmls. I have the following XML</p>
  
  <pre><code>&lt;ns4:parent xmlns:ns4="http://sample.xml.com/ns4"&gt;
      &lt;ns4:child1 xmlns:ns1="http://sample.xml.com/ns1" xmlns:xsi="http://sample.xml.com/xsi"&gt;
          &lt;ns1:child2&gt;divaStatus&lt;/ns1:child2&gt;
          &lt;xsi:child&gt;something&lt;/xsi:child&gt;
      &lt;/ns4:child1&gt;
  &lt;/ns4:parent&gt;
  </code></pre>
  
  <p>And I want to apply a xpath like <code>ns4:parent/ns4:child1/ns1:child2</code>. So my code is like below</p>
  
  <pre><code>Document doc = new Builder().build(inStream);  //inStream is containing the xml
  XPathContext xc = XPathContext.makeNamespaceContext(doc.getRootElement());
  doc.query("ns4:parent/ns4:child1/ns1:child2", xc);
  </code></pre>
  
  <p>And I'm getting <code>XPathException</code> here.</p>
  
  <pre><code> Exception in thread "main" nu.xom.XPathException: XPath error: XPath expression uses unbound namespace prefix ns1.
  </code></pre>
  
  <p>I can understand that since im making the namespace context by Root Element only, its not getting the namespaces of its children. So one work around might be traversing through all the children and collecting their namespaces and add it into the <code>XpathContext</code> object. But my xml can be of 10 to 20k lines. So Im afraid that how efficient the traversal approach will be. </p>
  
  <p>Looking forward for any better suggestion</p>