10-26-2019, 09:27 PM
The solution is quite simple actually, you have to tell ElementTree that the default name space is the SVG name space:
A bit of background: in complicated cases of XML (and people who like XML like complicated cases), you can have XML tags from several origins that unfortunately use the same name for different things. For instance, a geotracker application could have it own concept of "path" which is displayed using an SVG path. So to know which path is which, XML defines "namespaces", and here there would be one for the app, and one for SVG.
A namespace is defined by a unique string, which is usually a URLs. This is why you see the xmlns="http://www.w3.org/2000/svg" in the header. This says that the default name space is "http://www.w3.org/2000/svg". If there were several namespaces, the other ones would be defined by something like xmlns:geoapp="http://www.geoapp.com/geoappns".
Then in the XML file, to use the tags:
When you read the XML without a default namespace, Python add the namespace to all the tag names (if you look at the Python variables, the tags are named {http://www.w3.org/2000/svg}path}. So when you print, this information is not lost, because Python creates a namespace name on the fly and adds it to the tag, this is where your ns0 comes from. And everything will work if you amend the XML to declare ns0 as being the SVG namespace with an xmlns:ns0="http://www.w3.org/2000/svg". But you can also declare the namespace names with register_namespace() to let Python use the right names.
Code:
ET.register_namespace('',"http://www.w3.org/2000/svg")
tree=ET.ElementTree(ET.fromstring(xml))
A bit of background: in complicated cases of XML (and people who like XML like complicated cases), you can have XML tags from several origins that unfortunately use the same name for different things. For instance, a geotracker application could have it own concept of "path" which is displayed using an SVG path. So to know which path is which, XML defines "namespaces", and here there would be one for the app, and one for SVG.
A namespace is defined by a unique string, which is usually a URLs. This is why you see the xmlns="http://www.w3.org/2000/svg" in the header. This says that the default name space is "http://www.w3.org/2000/svg". If there were several namespaces, the other ones would be defined by something like xmlns:geoapp="http://www.geoapp.com/geoappns".
Then in the XML file, to use the tags:
Code:
<!-- this is a tag in the default namespace -->
<path />
<!-- a tag with expliciit name space -->
<geoapp:path />
When you read the XML without a default namespace, Python add the namespace to all the tag names (if you look at the Python variables, the tags are named {http://www.w3.org/2000/svg}path}. So when you print, this information is not lost, because Python creates a namespace name on the fly and adds it to the tag, this is where your ns0 comes from. And everything will work if you amend the XML to declare ns0 as being the SVG namespace with an xmlns:ns0="http://www.w3.org/2000/svg". But you can also declare the namespace names with register_namespace() to let Python use the right names.