Change element text and keep CDATA if present
Hi, when changing the text of an element, the CDATA section gets stripped away. I'm aware of the CDATA class, but I only want to apply it if the element had a CDATA section before the text change. I'm also already parsing the XML data with 'strip_cdata=False'. Something like: newText = someTransformation(element.text) element.text = CDATA(newText) if hasCDATA(element) else newText But I can't figure out how to do the detection of 'hasCDATA'. Thanks for any hints! Greetings, Daniel
Something like: newText = someTransformation(element.text) element.text = CDATA(newText) if hasCDATA(element) else newText
Ok, I'm now just using as a workaround for hasCDATA: hasCDATA = etree.tostring(element).find(b"<![CDATA[") >= 0 Not very nice and efficient, but for the one time operation in my case sufficient. Greetings, Daniel
participants (1)
-
Daniel Trstenjak