xml - remove tag and contents based on child tag value - python lxml -


i trying locate particular tag, based on child's contents , remove parent tag , contents, cant find answer. here xml:

<video>     <crew>       <member billing="top">         <name>some guy</name>         <roles>           <role>painter</role>           <role>decorator</role>         </roles>       </crew>       <crew billing="top">         <name>another guy</name>         <roles>           <role>primary</role>         </roles>       </crew>     </crew> </video> 

what want search see if <role>primary</role> exists in <crew> block, if want delete whole <crew> block <role>primary</role> exists in, it's parent. result be:

<video>     <crew>       <member billing="top">         <name>some guy</name>         <roles>           <role>painter</role>           <role>decorator</role>         </roles>       </crew> </video> 

it's not @ end , maybe buried within many other <crew> tags, know if block contains <role>primary</role> want remove whole <crew> block resides in. have tried:

for find1 in root.iter(tag='role'):     find1 = find1.text     if find1 == "primary":         path = tree.xpath('//video/crew')         etree.strip_elements(path, 'member') 

but removes every <crew> tag , it's contents. kind regards.

using xpath:

for crew in root.xpath('.//crew[descendant::role[contains(text(), "primary")]]'):     crew.getparent().remove(crew) 

Comments

Post a Comment