Problem with Elementtree and XMLSchem instance type

Stefan Behnel stefan_ml at
Sat Aug 7 23:58:20 CEST 2010

Roland Hedberg, 30.07.2010 15:21:
> I have the following XML snippet:
> <RoleDescriptor
>      xmlns:xsi=""
>      xmlns:fed=""
>      xsi:type="fed:SecurityTokenServiceType">
>      ....
> </RoleDescriptor>
> This part after parsing with Elementtree gives me an Element instance
> with the following properties:
>> tree.tag
> {urn:oasis:names:tc:SAML:2.0:metadata}RoleDescriptor
>> tree.keys()
> ['{}type']
>> tree['{}type']
> fed:SecurityTokenServiceType
> And there is the problem, I've lost the coupling between the prefix
> 'fed' and the namespace
> "".
> Is there any way I can get at the prefix<->  namespace mapping from an
> Element instance ?
> I've read the documentation I can find and there is nothing that tells
> me how to get at the mapping.
> If I print the Element instance the prefix 'fed' is replace by 'ns0' or
> something like that.
> Definitely something that has no connection to the original 'fed'.

Yes, ElementTree does that. There are two ways to work around this: recent 
ET versions support explicitly defining namespace-prefix mappings at a 
module global level (IIRC, starting with ET 1.3/Py2.7/Py3.2), so you can 
provide a fixed prefix if you know what's coming in.

Failing that, you can switch to lxml.etree, which keeps namespace prefixes 
as seen in the parsed document.


More information about the Python-list mailing list