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>You can create a dict mapping for your namespace(s), find the <em>node</em> then call <em>root.remove</em>  passing the node, you don't call <em>.remove</em> on the node:</p>

<pre><code>x = """&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;
&lt;/project&gt;"""
import lxml.etree as et
from StringIO import StringIO

tree = et.parse(StringIO(x))
root =tree.getroot()

nsmap = {"mav":"http://maven.apache.org/POM/4.0.0"}

root.remove(root.find("mav:dependencies", namespaces=nsmap))

print(et.tostring(tree))
</code></pre>

<p>Which would give you:</p>

<pre><code>&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;/project&gt;
</code></pre>

python xml lxml