How *build* new elements and *replace* elements with xml.dom.minidom ?

Johannes Bauer dfnsonfsduifb at
Thu Jun 11 18:29:22 EDT 2009

Stefan Behnel schrieb:

>> So I need to build hyperlinks (a elements) with href attribute and
>> replace the text elements (numbers) somehow.
> Try lxml.html instead. It makes it really easy to do these things. For
> example, you can use XPath to find all table cells that contain numbers:
> 	td_list = doc.xpath("//td[number() >= 0]")
> or maybe using regular expressions to make sure it's an int:
> 	td_list = doc.xpath("//td[re:match(., '^[0-9]+$')]",
>               namespaces={'re':''})
> and then replace them by a hyperlink:
> 	# assuming links = ['http://...', ...]
> 	from lxml.html.builder import A
> 	for td in td_list:
> 		index = int(td.text)
> 		a = A("some text", href=links[index])
> 		td.getparent().replace(td, a)

Oh no! I was looking for something like this for *ages* but always
fought with minidom - where this is a real pain :-(

Had I only known before that such a wonderful library exists. I'll
definitely use lxml from now on. Does it compile with Python3?

Kind regards,

"Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
         -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
                         <48d8bf1d$0$7510$5402220f at>

More information about the Python-list mailing list