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



Johannes Bauer wrote:
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':'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
.



Relevant Pages

  • Re: Howd They Do This?
    ... >> How Dubya's bio got to be the first hit is a whole different matter... ... Google organises its results based on how many ... "Scrotum are a fantastic local recording studio", ...
    (rec.audio.pro)
  • Re: Man pages
    ... I usually do a Google search for examples. ... If I was interested in, say, Postfix, I would Google for "postfix" and ... the very first hit is "Postfix Standard Configuration ... to set up greylisting on Sendmail as a milter. ...
    (Fedora)
  • Re: Derren Brown unique technology thats never been used before
    ... I just put "derren brown horse race" into google and the first hit ... another 10 seconds effort would have found me an accessible link to the ...
    (uk.media.tv.misc)
  • Re: Why does this site do so poorly on Google?
    ... I am the first hit on yahoo, altavista and msn but don't feature in the first 3 pages on google or ask. ... That is of course a sub domain of sourceforge.net, which I doubt is much different from http://your-site.your-isp.com or any other such "free" web site. ... Dave K MCSE. ...
    (uk.net.web.authoring)
  • Re: Why does this site do so poorly on Google?
    ... I am the first hit on yahoo, altavista and msn but don't feature in the first 3 pages on google or ask. ... Dave K MCSE. ...
    (uk.net.web.authoring)