xml.etree and namespaces -- why?

Robert Latest boblatest at yahoo.com
Wed Oct 19 13:36:06 EDT 2022


Jon Ribbens wrote:
> That's because you *always* need to know the URI of the namespace,
> because that's its only meaningful identifier. If you assume that a
> particular namespace always uses the same prefix then your code will be
> completely broken. The following two pieces of XML should be understood
> identically:
>
>     <svg xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">
>       <g inkscape:label="Ebene 1" inkscape:groupmode="layer" id="layer1">
>
> and:
>
>     <svg xmlns:epacskni="http://www.inkscape.org/namespaces/inkscape">
>       <g epacskni:label="Ebene 1" epacskni:groupmode="layer" id="layer1">
>
> So you can see why e.get('inkscape:label') cannot possibly work, and why
> e.get('{http://www.inkscape.org/namespaces/inkscape}label') makes sense.

I get it. It does.

> The xml.etree author obviously knew that this was cumbersome, and
> hence you can do something like:
>
>     namespaces = {'inkspace': 'http://www.inkscape.org/namespaces/inkscape'}
>     element = root.find('inkspace:foo', namespaces)
>
> which will work for both of the above pieces of XML.

Makes sense. It forces me to make up my own prefixes which I can then safely
use in my code rather than relying on the xml's generator to not change their
prefixes.

BTW, I only now thought to look at what actually is at Inkscape's namespace
URI, and it turns out to be quite a nice explanation of what a namespace is and
why it looks like a URL.


More information about the Python-list mailing list