from xml.dom import Node, ext from xml.dom.ext.reader import PyExpat test_doc = """ LADIES

LADIES

Agathas

Four and forty lovers had Agathas in the old days,...

Young Lady

I have fed your lar with poppies,...

Lesbia Illa

Memnon, Memnon, that lady... """ def link_title_invert(): #build a DOM tree from the file reader = PyExpat.Reader() doc = reader.fromString(test_doc) h2_elements = doc.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'h2') for e in h2_elements: parent = e.parentNode a_list = filter(lambda x: (x.nodeType == Node.ELEMENT_NODE) and (x.localName == 'a'), e.childNodes) a = a_list[0] e.removeChild(a) for node in a.childNodes: #Automatically also removes the child from a e.appendChild(node) parent.replaceChild(a, e) a.appendChild(e) ext.Print(doc) #reclaim the object; not necessary with Python 2.0 reader.releaseNode(doc) if __name__ == '__main__': import sys link_title_invert()