Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set lxml etree text to string with tags

I have a problem regarding the lxml.etree library. I have a string like

string = "this<a/>is<b/>nice"

and I want to set this string as the text of an element node.

node.text = string

But everytime if I print out the text of the node, its escaped like this:

"this&lt;a\&gt;is&lt;b\&gt;nice"

So how do I set the text so that its not escaped anymore? I cant do it e.g. with node.tail or anything, because I have more than one node inside the text.

like image 389
aAnnAa Avatar asked Nov 16 '25 11:11

aAnnAa


1 Answers

What you could do is add a root element to the string to make it well-formed and then parse it using tostring(). Then you can add the Element as a child of the target element.

Once it's where it's supposed to be, you can use strip_tags() to remove the temporary root element.

Example...

Python

from lxml import etree

doc = etree.fromstring("<doc/>")

print(f"doc before: \"{etree.tostring(doc).decode()}\"")

string = "this<a/>is<b/>nice"

fragment = etree.fromstring(f"<temp>{string}</temp>")
doc.append(fragment)
etree.strip_tags(doc, "temp")

print(f"doc after: \"{etree.tostring(doc).decode()}\"")

Console Output

doc before: "<doc/>"
doc after: "<doc>this<a/>is<b/>nice</doc>"
like image 195
Daniel Haley Avatar answered Nov 19 '25 01:11

Daniel Haley