[XML-SIG] Multiple top nodes

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Tue, 31 Oct 2000 22:17:10 +0100

> Which I would have expected, if it was trying to parse into a Document.

Well, you are trying to parse a document. At least this is what
FromXml* assumes.

It *will* assume that you want to create a DocumentFragment if you
pass it the ownerDocument parameter, e.g.

from xml.dom import implementation
from xml.dom.ext.reader import Sax2



doc = implementation.createDocument(None,None,None)
print Sax.FromXml(frag,ownerDocument=doc)

However, it still will use a SAX parser to parse the fragment, and SAX
does not support parsing fragments. Specifically, the expat parser
will complain about the ill-formedness of the document.

If you absolutely need to make this work, then you can use the sgmlop
driver, which performs less error checking. Unfortunately, FromXml*
does not support an application-provided driver, so your only solution
at the moment is to set the environment variable PY_SAX_PARSER to
xml.sax.drivers.drv_sgmlop. With that setting, my program above
creates a DocumentFragment.

Please note that this is abusing defects in sgmlop, which should
detect errors in the document more reliably. I'm surprised this worked
with PyXML 0.5.