Navin Dalal
Navin Dalal

Reputation: 179

Comparing children of ancestor element in 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>

I want to remove the refpt child of title if both title and designum contains refpt.

My Code :

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

Upvotes: 1

Views: 63

Answers (1)

har07
har07

Reputation: 89325

"I want to remove the refpt child of title if both title and designum contains refpt"

The following XPath should return refpt elements to be removed according to the criteria mentioned above :

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

If you need to make sure that you only remove refpt under title when it has matching id with the one under designum, try this XPath :

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

Upvotes: 1

Related Questions