Okay ... ? (Yeah. Sorry ... didn't read the whole message.) Which DOM are you using?
I parsed your xml snippet with xml.dom.minidom (PyXML 0.84). Every child of <nd> had proper a nextSibling and a proper previousSibling. The first child (<DOM Text node "\n ">) didn't have a previousSibling (as expected). But it did have a nextSibling (<DOM Element: test2 at ...>).
(This also works fine without PyXML.)
As for previousSibling and nextSibling giving back the text nodes instead of the nodes you want:
While this is not good code, it works for your purpose (if you can solve the problem of text nodes giving you None for previous and next siblings).
if node.previousSibling is None:
if node.previousSibling.nodeType == node.TEXT_NODE:
if node.nextSibling is None:
if node.nextSibling.nodeType == node.TEXT_NODE: