Delete entire node using lxml
<p>I have a an xml document like the following:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
    &lt;groupId&gt;company&lt;/groupId&gt;
    &lt;artifactId&gt;art-id&lt;/artifactId&gt;
    &lt;version&gt;RELEASE&lt;/version&gt;
&lt;/parent&gt;

&lt;properties&gt;
    &lt;tomcat.username&gt;admin&lt;/tomcat.username&gt;
    &lt;tomcat.password&gt;admin&lt;/tomcat.password&gt;
&lt;/properties&gt;

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;asdf&lt;/groupId&gt;
        &lt;artifactId&gt;asdf&lt;/artifactId&gt;
        &lt;version&gt;[3.8,)&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;asdf&lt;/groupId&gt;
        &lt;artifactId&gt;asdf&lt;/artifactId&gt;
        &lt;version&gt;[4.1,)&lt;/version&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;
</code></pre>

<p>how can I delete the entire node "dependencies"?</p>

<p>I have looked at other questions and answers on stackoverflow and what is different about is the namespace aspect of this xml, and the other questions ask to delete a subelement like "dependency" while I want to delete the whole node "dependencies."  Is there an easy way using lxml to delete the entire node?</p>

<p>The following gives a 'NoneType' object has no attribute 'remove' error:</p>

<pre><code>from lxml import etree as ET

tree = ET.parse('pom.xml')
namespace = '{http://maven.apache.org/POM/4.0.0}'
root = ET.Element(namespace+'project')
root.find(namespace+'dependencies').remove()
</code></pre>

<p>First, grab the root node. Since it is <code>&lt;project ... &gt;</code> (vs <code>&lt;project .../&gt;</code>) the "parent" element of <code>dependencies</code> is <code>project</code>. Example from the documentation:</p>

<blockquote>
  <p>import xml.etree.ElementTree as ET<br>
    tree = ET.parse('country_data.xml')<br>
    root = tree.getroot()  </p>
</blockquote>

<p>Once you have the root, check <code>root.tag()</code>, it should be "project".</p>

<p>Then do <code>root.remove(root.find('dependencies'))</code>, where <code>root</code> is the <code>project</code> node.</p>

<p>If it were <code>&lt;project .../&gt;</code> then it would be invalid XML since there must be a root element. I can see exactly where you are coming from, though.</p>

python xml lxml