0

Can't seem to figure out how to remove the element 'framelineName' and all the sub-elements attached to it. Bottom area in the else statement will only delete the element framelineName. I want to also delete 'line', 'left', and 'right'.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from lxml import etree as ET


def cash_rules_everything_around_me():
    shaolin = ET.SubElement(root, "Shaolin")
    wtClan = ET.SubElement(root, "wtClan")
    wtClan.set('StatenIsland', 'NYC')
    RZA = ET.SubElement(shaolin, "RZA")
    RZA.set('StatenIsland', 'NYC')
    gf = ET.SubElement(RZA, "GhostfaceKillah")
    rk = ET.SubElement(RZA, "Raekwon")
    wutang = "36 chambers"

    for wu in wutang:
        if wu != "36 chambers":
            wtClan.text = "A Tribe Called Quest"

        else:
            for w in root.xpath("//wtClan [@StatenIsland=\'NYC']"):
            w.getparent().remove(w)
            tree = ET.ElementTree(root)
            tree.write("wutang.xml", pretty_print=True, xml_declaration=True, encoding='UTF-8')


if __name__ == '__main__':
    root = ET.Element("HipHop")
    cash_rules_everything_around_me()
Axel Foley
  • 29
  • 7
  • What is *root*? Are you building an XML and removing its elements? Please include a fuller code block for context and [MCVE]. – Parfait Dec 30 '18 at 00:37
  • Updated... with some fresh names and variables. – Axel Foley Dec 30 '18 at 03:12
  • Nice example! However, why remove elements from the very XML you are building? Why not conditionally create element and children per logic? Usually one removes from XML previously built and being parsed. – Parfait Dec 30 '18 at 03:29
  • Example is on point, haha. I have to remove things (or maybe there is another way!) cause this is an app I'm making. With this issue, you select something from a comboBox and it needs to replace or delete all the these settings in the XML. SubElement and all children need to be swapped out/deleted. I have this project spread across multiple .py files. Everything works so far, except this issue. – Axel Foley Dec 30 '18 at 03:50
  • Looks like Beautiful Soup is a lot easier to build/edit/add/parse/etc .xml trees – Axel Foley Dec 31 '18 at 04:33
  • [Lxml](https://stackoverflow.com/questions/47229309/what-are-the-differences-between-lxml-and-elementtree/50416216#50416216) is a robust, conformant XML DOM library. – Parfait Dec 31 '18 at 12:43

1 Answers1

0

To remove an element you need the actual element object not a list which is the return of lxml's xpath. Consider findall for iterating through element and move xpath logic to an if statement:

...
# ITERATE THROUGH A LIST (NOT STRING)
for wu in [wutang]:
    if wu != "36 chambers":
        wtClan.text = "A Tribe Called Quest"

    else:
        for w in root.findall("//wtClan"):
            if w.attributed['StatenIsland']
               root.remove(w)

tree = ET.ElementTree(root)
tree.write("wutang.xml", pretty_print=True, 
           xml_declaration=True, encoding='UTF-8')

Rextester demo (using built-in etree but compatible with lxml)

Parfait
  • 104,375
  • 17
  • 94
  • 125