繁体   English   中英

如果childnode的childnode包含特定值,则删除XML节点

[英]Remove XML node if childnode's childnode contains specific value

我需要为某些值过滤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>

如果<ogr:Region_Nam>包含TARGET ,则Python脚本应保留<gml:featureMember>节点,并删除所有其他节点。

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

tree = ET.parse('input.xml').getroot()

removeList = list()
for child in tree.iter('gml:featureMember'):
    if child.tag == 'ogr:TZwards':
        name = child.find('ogr:Region_Nam').text
        if (name == 'TARGET'):
            removeList.append(child)

for tag in removeList:
    parent = tree.find('ogr:TZwards')
    parent.remove(tag)

out = ET.ElementTree(tree)
out.write(outputfilepath)

所需的输出:

<?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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM