Through trial and error, it looks like xpath() returns an array, even if only
one element is found in the tree.
This works:
HERE = template_tree.xpath('//here')
if len(HERE):
print("HERE:",HERE)
parent = HERE[0].getparent()
html_tree = lxml.html.fragment_fromstring("<div>blah</div>",
parser=lxml.html.HTMLParser())
parent.insert(parent.index(HERE[0]),html_tree)
parent.remove(HERE[0])
print(et.dump(template_root))
else:
print("Not HERE")
_______________________________________________
lxml - The Python XML Toolkit mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/lxml.python.org/
Member address: [email protected]