title: How to remove elements from XML using Python
body: <p>I got stuck with XML and Python. The task is simple but I couldn't resolve it so far and spent on that long time. I came here for an advice how to solve it with couple of lines.</p>
  
  <p>Thanks for any help with traversing the tree. I always ended up with too many or too few elements. Elements can be nested without limit. Given example is just an example. I will accept any solution, not picky about dom, minidom, sax, whatever..</p>
  
  <p>I have an XML file similar to this one:</p>
  
  <pre><code>&lt;root&gt;
      &lt;elm&gt;
          &lt;elm&gt;Common content&lt;/elm&gt;
  
          &lt;elm xmlns="http://example.org/ns"&gt;
              &lt;elm lang="en"&gt;Content EN&lt;/elm&gt;
              &lt;elm lang="cs"&gt;žluťoučký koníček&lt;/elm&gt;
          &lt;/elm&gt;
  
          &lt;elm xml:id="abc123"&gt;Common content&lt;/elm&gt;
  
          &lt;elm lang="en"&gt;Content EN&lt;/elm&gt;
          &lt;elm lang="cs"&gt;Content CS&lt;/elm&gt;
  
          &lt;elm lang="en"&gt;
              &lt;elm&gt;Content EN&lt;/elm&gt;
              &lt;elm&gt;Content EN&lt;/elm&gt;
          &lt;/elm&gt;
  
          &lt;elm lang="cs"&gt;
              &lt;elm&gt;Content CS&lt;/elm&gt;
              &lt;elm&gt;Content CS&lt;/elm&gt;
          &lt;/elm&gt;
      &lt;/elm&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>What I need - parse the XML and write a new file. The new file should contain all the elements for given language and elements without <code>lang</code> attribute.</p>
  
  <p>For "cs" language the output file should containt this:</p>
  
  <pre><code>&lt;root&gt;
      &lt;elm&gt;
          &lt;elm&gt;Common content&lt;/elm&gt;
  
          &lt;elm xmlns="http://example.org/ns"&gt;
              &lt;elm lang="cs"&gt;žluťoučký koníček&lt;/elm&gt;
          &lt;/elm&gt;
  
          &lt;elm xml:id="abc123"&gt;Common content&lt;/elm&gt;
  
          &lt;elm lang="cs"&gt;Content CS&lt;/elm&gt;
  
          &lt;elm lang="cs"&gt;
              &lt;elm&gt;Content CS&lt;/elm&gt;
              &lt;elm&gt;Content CS&lt;/elm&gt;
          &lt;/elm&gt;
      &lt;/elm&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>If you can make it to omit the <code>lang</code> attribute in the new file, even better. But it's not that important.</p>
  
  <p><strong>UPDATE1:</strong> Added unicode characters and namespace attribute.</p>
  
  <p><strong>UPDATE2:</strong> Using Python 2.5, standard libraries preferred.</p>