[lxml-dev] Replacing node.text with a node structure
Hi there, I have an XML structure like this <p> hello bar world <other> .... </other> </p> and need it to transform to this <p> hello <foo> bar </foo> world <other> .... </other> </p> p_node.text == 'hello bar world' should be replaced with a text node + Element node + another text node. How can I do this using lxml? Andreas
On 22.12.2009, at 15:45, Andreas Jung wrote:
Hi there,
I have an XML structure like this
<p> hello bar world <other> .... </other> </p>
and need it to transform to this
<p> hello <foo> bar </foo> world <other> .... </other> </p>
p_node.text == 'hello bar world' should be replaced with a text node + Element node + another text node.
How can I do this using lxml?
from lxml.etree import Element, XML, dump p = XML('<p>Hello World<bar>!</bar></p>') dump(p) <p>Hello World<bar>!</bar></p> p.text = 'Hello' foo = Element('foo') foo.text = 'wonderful' foo.tail = 'World' p[:] = [foo] + p[:] dump(p) <p>Hello<foo>wonderful</foo>World<bar>!</bar></p>
Questions like that, my references was: http://infohost.nmt.edu/tcc/help/pubs/pylxml/index.html On Tue, 2009-12-22 at 15:45 +0100, Andreas Jung wrote:
Hi there,
I have an XML structure like this
<p> hello bar world <other> .... </other> </p>
and need it to transform to this
<p> hello <foo> bar </foo> world <other> .... </other> </p>
p_node.text == 'hello bar world' should be replaced with a text node + Element node + another text node.
How can I do this using lxml?
Andreas _______________________________________________ lxml-dev mailing list lxml-dev@codespeak.net http://codespeak.net/mailman/listinfo/lxml-dev
-- Sérgio M. B.
participants (3)
-
Andreas Jung -
Jens Quade -
Sergio Monteiro Basto