获取父节点?

Get parent node?

我编写了一个脚本,通过 ID 前缀从庞大的数据集中删除不需要的对象。

这就是这些对象的结构:

  <wfsext:Replace vendorId="AdV" safeToIgnore="false">
    <AX_Anschrift gml:id="DENWAEDA0000001G20161222T083308Z">
      <gml:identifier codeSpace="http://www.adv-online.de/">urn:adv:oid:DENWAEDA0000001G</gml:identifier>
      ...
    </AX_Anschrift>
    <ogc:Filter>
      <ogc:FeatureId fid="DENWAEDA0000001G20161222T083308Z" />
    </ogc:Filter>
  </wfsext:Replace>

我想删除 <wfsext:Replace>...</wfsext:Replace>

中的这些完整片段

我的脚本中有一个代码片段:

file = etree.parse(portion_file)
root = file.getroot()
nsmap = root.nsmap.copy()
nsmap['adv'] = nsmap.pop(None)
node = root.xpath(".//adv:geaenderteObjekte/wfs:Transaction", namespaces=nsmap)[0]
for t in node:
    for obj in t:
        objecttype = str(etree.QName(obj.tag).localname)
        if objecttype == 'Filter':
            pass
        else:
            objid = (obj.xpath('@gml:id', namespaces=nsmap))[0][:16]
            if debug:
                print('{} - {}'.format(objid[:16], objecttype))
            if objid[:6] != prefix:
                #parent = obj.getparent()
                t.remove(obj)

t.remove(obj) 删除 <AX_Anschrift>..</AX_Anschrift> 但不删除对象的其余部分。我试图通过使用 obj.getparent() 获取父节点,但这给了我一个错误。如何抓住它?

obj.getparent() is t,所以你实际上不需要调用getparent(),只需删除整个对象:

node.remove(t)

或者,如果您想删除整个 wfs:Transaction

node.getparent().remove(node)