比较lxml中祖先元素的子元素

时间:2016-03-21 05:44:32

标签: python xml xpath lxml

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()) 

1 个答案:

答案 0 :(得分:1)

  

“如果refpttitle包含title,我想删除designum的{​​{1}}个孩子。 p>

以下XPath应根据上述标准返回要删除的refpt元素:

refpt

如果您需要确保只有//leg:heading[desig/designum/refpt]/title/refpt 下的refpttitle匹配id下的designum,请尝试此XPath:

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