Re: How *build* new elements and *replace* elements with xml.dom.minidom ?
- From: Stefan Behnel <stefan_ml@xxxxxxxxx>
- Date: Fri, 12 Jun 2009 07:08:19 +0200
Johannes Bauer wrote:
Stefan Behnel schrieb:
So I need to build hyperlinks (a elements) with href attribute andTry lxml.html instead. It makes it really easy to do these things. For
replace the text elements (numbers) somehow.
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':'http://exslt.org/regular-expressions'})
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.
Yep, I keep advertising it all over the place, but there are still so many
references to minidom on the web that it's hard to become the first hit in
Google when you search for "Python XML". ;)
Actually, the first hit (for me) is currently PyXML, which is officially
unmaintained. Wasn't there 'some' Python developer working for Google? What
about fixing their database?
Does it compile with Python3?
Sure. :)
Stefan
.
- References:
- How *build* new elements and *replace* elements with xml.dom.minidom ?
- From: Chris Seberino
- Re: How *build* new elements and *replace* elements with xml.dom.minidom ?
- From: Stefan Behnel
- Re: How *build* new elements and *replace* elements with xml.dom.minidom ?
- From: Johannes Bauer
- How *build* new elements and *replace* elements with xml.dom.minidom ?
- Prev by Date: Re: Specify the sorting direction for the various columns/
- Next by Date: Re: Need help in Python regular expression
- Previous by thread: Re: How *build* new elements and *replace* elements with xml.dom.minidom ?
- Next by thread: Re: Can not dump class object created on runtime
- Index(es):
Relevant Pages
|