Python 如果childnode';s的子节点包含特定值

Python 如果childnode';s的子节点包含特定值,python,xml,dom,Python,Xml,Dom,我需要过滤XML文件中的某些值,如果节点包含此值,则应删除该节点 <?xml version="1.0" encoding="utf-8" ?> <ogr:FeatureCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ogr.maptools.org/ TZwards.xsd" xmlns:ogr="http://ogr.maptools

我需要过滤XML文件中的某些值,如果节点包含此值,则应删除该节点

<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://ogr.maptools.org/ TZwards.xsd"
 xmlns:ogr="http://ogr.maptools.org/"
 xmlns:gml="http://www.opengis.net/gml">
    <gml:boundedBy></gml:boundedBy>                   
    <gml:featureMember>
        <ogr:TZwards fid="F0">
            <ogr:Region_Nam>TARGET</ogr:Region_Nam>
            <ogr:District_N>Kondoa</ogr:District_N>
            <ogr:Ward_Name>Bumbuta</ogr:Ward_Name>
        </ogr:TZwards>
    </gml:featureMember>
    <gml:featureMember>
        <ogr:TZwards fid="F1">
            <ogr:Region_Nam>REMOVE</ogr:Region_Nam>
            <ogr:District_N>Kondoa</ogr:District_N>
            <ogr:Ward_Name>Pahi</ogr:Ward_Name>
         </ogr:TZwards>
    </gml:featureMember>
</ogr:FeatureCollection>
期望输出:

<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection>
    <gml:boundedBy></gml:boundedBy>                   
    <gml:featureMember>
        <ogr:TZwards fid="F0">
            <ogr:Region_Nam>TARGET</ogr:Region_Nam>
            <ogr:District_N>Kondoa</ogr:District_N>
            <ogr:Ward_Name>Bumbuta</ogr:Ward_Name>
        </ogr:TZwards>
    </gml:featureMember>
</ogr:FeatureCollection>

目标
康多阿
布姆布塔

我的输出仍然包含所有节点。

您需要在python代码中声明名称空间:

from xml.dom import minidom
import xml.etree.ElementTree as ET

tree = ET.parse('/tmp/input.xml').getroot()
namespaces = {'gml': 'http://www.opengis.net/gml', 'ogr':'http://ogr.maptools.org/'}
for child in tree.findall('gml:featureMember', namespaces=namespaces):
    if len(child.find('ogr:TZwards', namespaces=namespaces)):
        name = child.find('ogr:TZwards', namespaces=namespaces).find('ogr:Region_Nam', namespaces=namespaces).text
        if name != 'TARGET':
            tree.remove(child)

out = ET.ElementTree(tree)
out.write("/tmp/out.xml")

你能确认它有效吗?因为如果我运行你的解决方案,我仍然有相同数量的featureMembers。这可能是名称空间的问题吗?您的问题之一是前缀没有在xml中声明。所以我把它们取下来做测试。如果你从xml和python代码中删除前缀,我会删除它们,因为我认为它们已经过时了。。。更新原始帖子^
from xml.dom import minidom
import xml.etree.ElementTree as ET

tree = ET.parse('/tmp/input.xml').getroot()
namespaces = {'gml': 'http://www.opengis.net/gml', 'ogr':'http://ogr.maptools.org/'}
for child in tree.findall('gml:featureMember', namespaces=namespaces):
    if len(child.find('ogr:TZwards', namespaces=namespaces)):
        name = child.find('ogr:TZwards', namespaces=namespaces).find('ogr:Region_Nam', namespaces=namespaces).text
        if name != 'TARGET':
            tree.remove(child)

out = ET.ElementTree(tree)
out.write("/tmp/out.xml")