XML:

<leg:heading nline="true">
  <desig inline="true" searchtype="SECT-NUM">
    <designum>
      <refpt type="ext" id="USM.A000001y1966e.S1"/>1.
    </designum>
  </desig>
  <title inline="true" searchtype="SECT-TITLE">
    <refpt type="ext" id="USM.A000001y1966e.S1"/>Short title, commencement and application
  </title>
</leg:heading>


如果title和designum都包含refpt,我想删除title的refpt子代。

我的代码:

for elem in doc.xpath('//leg:heading',namespaces={'leg':'http://www.lexis-nexis.com/glp/leg'}):
    for element in elem.getiterator():
        if(element.tag=='refpt'):
            print(element.tag.getparent())

最佳答案

“如果refpttitle都包含title,我想删除designumrefpt子级”


下面的XPath应该返回refpt元素,该元素应根据上述标准删除:

//leg:heading[desig/designum/refpt]/title/refpt


如果需要确保仅在refpttitle下的匹配的id匹配时删除designum下的,请尝试以下XPath:

//leg:heading/title/refpt[@id = ../../desig/designum/refpt/@id]

10-08 07:41