pyxml minidom: I can remove and append, but not replace

Joseph M . Reagle Jr. reagle at mit.edu
Tue Apr 22 10:48:15 EDT 2003


I have a snippet that replacing a title element with a new element with a modified one (base on an amazon query) -- if that title element already exists. However, I can't do an insertBefore or replace for some odd reason:

            for property in book_dtd:   # step through the DTD
                feature = _get_feature(property,book)
                if property == "title":
                    r_title = doc.createElementNS(book_ns,"title")
                    r_title.appendChild(doc.createTextNode(title))
                    if feature:
                        book.appendChild(r_title)
                        book.removeChild(feature)
#                         book.replaceChild(feature,r_title)
                    else:
                        book.appendChild(r_title)

If I were to comment the "book.{append,remove}Child" and uncomment the book.replaceChild I get the following error:

Traceback (most recent call last):
  File "./pybook.py", line 163, in ?
    bookAugment(doc)
  File "./pybook.py", line 100, in bookAugment
    book.replaceChild(feature,r_title)
  File "/usr/lib/python2.2/site-packages/_xmlplus/dom/minidom.py", line 145, 
in replaceChild
    raise xml.dom.NotFoundErr()
xml.dom.NotFoundErr: Node does not exist in this context

The node is using the same namespace, and I've used the replaceChild in other cases (where I'm actually using a node from a different XML instance!)




More information about the Python-list mailing list