![]() Your problem is that node.remove() only removes direct subelements of node. If you can switch to lxml, the loop is slightly less cumbersome: for weight in tree.findall("//weight"):Īs to your second question, the ElementTree documentation uses "node" more-or-less interchangably with "element." More specifically, it appears to use the word "node" to refer either to a Python object of type "Element" or the XML element to which such an object refers. # Remove the weight element from its parent element # Find the parent element of each "weight" element, using XPATHįor parent in root.findall('.//weight/.'): # tree = ElementTree.parse(filename_path) ![]() # Alternatively, parse the XML that lives in 'filename_path' Specifically, no element knows its own parent, so we have to discover that relationship "by hand." from xml.etree import ElementTree Removing an element from a tree, regardless of its location in the tree, is needlessly complicated by the ElementTree API. However, am I wrong in thinking the terminology is used differently here so that nodes are not a subset of elements? Are the two terms still related in a similar way? I come from a finite element background, where nodes are understood as part of an element, defining portions / corner boundaries of what creates an element. Second question: What is the relation of nodes and elements in this context? I have read this helpful resource, but have reached a point where I am stuck. Print "Weight line removed from ", filename_path Print "for-loop check : loop has been entered"Ĭurrent_xml_tree.getroot().remove(weight_value_elem) Print weight_value_elem # Location of element 'weight' is listedįor weight_node_loc in current_xml_er('weight'): Print weight_nodes # result is an empty list Weight_nodes = current_xml_root.findall('weight') # Get all nodes entitled 'weight' from element Weight_value = weight_value_elem.get('value') Thus far, I have the following code: from xml.etree import ElementTreeĬurrent_xml_tree = ElementTree.parse(filename_path) # Path to example.xmlĬurrent_xml_root = current_xml_tree.getroot()Ĭurrent_xml_level_2_node = current_xml_root.findall('XML_level_2')įor weight_value_elem in current_xml_er('weight'): ![]() Using the xml code (let's just say it is called "example.xml"): Goal: remove the sub-element "weight" from the XML file. ![]() However, there is a push to use Python in my group so I am using this as both a functional and learning exercise.Ĭould you please correct my use of terms and understanding? I do not simply just want the code to work, but to know that my understanding of how it works is correct. I hope to save myself a great deal of manual effort by making these changes automated and typically I would have just done this in a language such as C++ that I am more familiar with. functions.I need help adjusting my XML file with Python and the elementTree library.įor some background, I am not a student and work in industry. In OP's case, it looks like we're outside of a callback in Node-land, so document isn't a thing, only Puppeteer page. The rule of thumb is: if it's a callback or stringified function body, it's in the browser and you can only use browser/DOM concepts like HTMLElement.remove(), window and document, otherwise it runs in Node and you can only call Puppeteer API functions. You can hardcode the selector into the eval string/function as well but I figured it'd be useful to show it coming from a variable in two different ways just in case.Īs with anything in Puppeteer, understanding which code runs in Node/Puppeteer-land and which code runs in browser/console-land is extremely important. If for some reason you need to select and remove in console browser-land: const selector = ".foo" ĭocument.querySelector("$").remove()ĭocument.querySelectorAll(selector).forEach(el => el.remove()), Wait for a selector, then remove it: const el = await page.waitForSelector(selector) Remove all nodes matching selector: await page.$$eval(selector, els => els.forEach(el => el.remove())) Remove the first node matching selector: await page.$eval(selector, el => el.remove()) ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |