title: Add a XML node to a document namespace-aware in scala
body: <p>Let's suppose we have the following XML document, parsed in Scala:</p>
  
  <pre><code>&lt;ns1:doc-root xmlns:ns1="http://namespace1url.com/schema" xmlns:ns2="http://namespace2url.com/schema"&gt;
    &lt;ns1:a&gt;
      &lt;ns2:b&gt;Random content&lt;/ns2:b&gt;
    &lt;/ns1:a&gt;
  &lt;/ns1:doc-root&gt;
  </code></pre>
  
  <p>This document comes from a variety of systems and vendors who can decide on the prefix of the namespaces at will. (Even make any of them the default namespace)</p>
  
  <p>So, my task here is to add the following element into the <code>a</code> element:</p>
  
  <pre><code>&lt;ns1:c&gt;
    &lt;ns2:d&gt;Other random content&lt;/ns2:d&gt;
  &lt;/ns1:c&gt;
  </code></pre>
  
  <p>Here, I know that both elements here, <code>c</code> and <code>d</code> have the same namespaces as <code>a</code> and <code>b</code>, respectively. But I don't know what prefix have been chosen for the document. In this example, both the document issuer and I, have chosen <code>ns1</code> and <code>ns2</code> for the prefixes, but they could (Very probably) be different.</p>
  
  <p>So that the resulting component results as:</p>
  
  <pre><code>&lt;ns1:doc-root xmlns:ns1="http://namespace1url.com/schema" xmlns:ns2="http://namespace2url.com/schema"&gt;
    &lt;ns1:a&gt;
      &lt;ns2:b&gt;Random content&lt;/ns2:b&gt;
      &lt;ns1:c&gt;
        &lt;ns2:d&gt;Other random content&lt;/ns2:d&gt;
      &lt;/ns1:c&gt;
    &lt;/ns1:a&gt;
  &lt;/ns1:doc-root&gt;
  </code></pre>
  
  <p>Now the question is, <strong>how I manage namespace variations in Scala</strong>? I know how to use <code>RewriteRule</code> to iterate through the document adding and removing elements. But <strong>how I know which prefix to use for each element?</strong></p>
  
  <p>In python, I'd indicate each element namespace and the framework would automagically manage namespace declarations at the root element, prefixes and so...</p>
  
  <p>How can this be done in Scala?</p>
  
  <p>Thank you for your support</p>