XML DOM: XML/XHTML inside a text node

Walter Dörwald walter at livinglogic.de
Fri Nov 4 08:51:10 EST 2005


noahlt at gmail.com wrote:
> In my program, I get input from the user and insert it into an XHTML
> document.  Sometimes, this input will contain XHTML, but since I'm
> inserting it as a text node, xml.dom.minidom escapes the angle brackets
> ('<' becomes '<', '>' becomes '>').  I want to be able to
> override this behavior cleanly.  I know I could pipe the input through
> a SAX parser and create nodes to insert into the tree, but that seems
> kind of messy.  Is there a better way?

You could try version 2.13 of XIST (http://www.livinglogic.de/Python/xist)

Code looks like this:

from ll.xist.ns import html, specials

text = "Number 1 ... the <b>larch</b>"

e = html.div(
    html.h1("And now for something completely different"),
    html.p(specials.literal(text))
)
print e.asBytes()


This prints:
<div><h1>And now for something completely different</h1><p>Number 1 ... 
the <b>larch</b></p></div>

I hope this is what you need.

Bye,
    Walter Dörwald



More information about the Python-list mailing list