ElementTree and xsi to xmlns conversion?

Matthew Thorley ruach at chpc.utah.edu
Wed May 18 17:23:15 EDT 2005


Why does ElementTree.parse convert my xsi to an xmlns?

When I do this
from elementtree import ElementTree

# Sample xml
mgac ="""
<mgac xmlns="http://www.chpc.utah.edu/~baites/mgacML"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.chpc.utah.edu/~baites/mgacML
http://www.chpc.utah.edu/~baites/mgacML/mgac.xsd"><cluster
name="Si4H"></cluster></mgac>
"""

xml = ElementTree.fromstring(mgac)
ElementTree.tostring(xml)

I get this
'<ns0:mgac ns1:schemaLocation="http://www.chpc.utah.edu/~baites/mgacML
http://www.chpc.utah.edu/~baites/mgacML/mgac.xsd"
xmlns:ns0="http://www.chpc.utah.edu/~baites/mgacML"
xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance"><ns0:cluster
name="Si4H" /></ns0:mgac>'


The xsi is gone and has been replaced by a new xmlns, which is also NOT
inherited by the child elements.

ElementTree.tostring(xml.getchildren()[0])

'<ns0:cluster name="Si4H"
xmlns:ns0="http://www.chpc.utah.edu/~baites/mgacML" />'

If some one could please explain where I'm off I'd really appreciate it.
I need to use xsi: to validate the document, and I'm not sure how to
pass it on to the children when I reformat the doc.

Thanks
-Matthew



More information about the Python-list mailing list