Hello, As a follow-up to the code example here: https://lxml.de/api.html#cdata >>> parser = etree.XMLParser(strip_cdata=False) >>> root = etree.XML('<root><![CDATA[test]]></root>', parser) >>> root.text 'test' So here, the .text is a string and if I check the type of it >>> type(root.text) <class 'str'> then it’s an actual str instance. Yet it serializes with the knowledge that .text originated from a CDATA: >>> etree.tostring(root) b'<root><![CDATA[test]]></root>' Is there a way to find out whether .text came from a CDATA, that root’s text() node should be wrapped? It seems to be stored away somewhere, but I can’t find that bit… Thanks, Jens -- Jens Tröger http://savage.light-speed.de/
Jens Tröger schrieb am 23.04.20 um 05:35:
As a follow-up to the code example here: https://lxml.de/api.html#cdata
>>> parser = etree.XMLParser(strip_cdata=False) >>> root = etree.XML('<root><![CDATA[test]]></root>', parser) >>> root.text 'test'
So here, the .text is a string and if I check the type of it
>>> type(root.text) <class 'str'>
then it’s an actual str instance. Yet it serializes with the knowledge that .text originated from a CDATA:
>>> etree.tostring(root) b'<root><![CDATA[test]]></root>'
Is there a way to find out whether .text came from a CDATA, that root’s text() node should be wrapped? It seems to be stored away somewhere, but I can’t find that bit…
It's stored in the tree internally, but it is hidden from the API level. What's your intention here? It looks like you are replacing text and would like it to appear in a CDATA section *if* the original text was. But it shouldn't normally make a difference whether you CDATA-wrap the text or not, so unless you have a reason to escape the specific text that you are writing, you can just ignore the original text wrapping. Stefan
participants (2)
-
Jens Tröger
-
Stefan Behnel