'Prevent python xml.etree from using namespaces [duplicate]
I have an issue that's driving me a bit mad right now because it really shouln't be one, but here we go:
I'm want to modify some svg images using the xml.etree-library in python in order to display them in a webapp afterwards. But when trying to just parse the images and then ouputting them as a String again using ElementTree.tostring I noticed that a default ns0 namespace is being put in front of every xml-tag in the image, which results in my browser not being able to parse the svg when I put the whole thing into an html-doc.
Here's an example of what my input svgs look like:
<?xml version="1.0" standalone="yes"?>
<svg viewbox="0 0 400.0 400.0" xmlns="http://www.w3.org/2000/svg">
<g>
<ellipse cx="200.0" cy="200.0" rx="22.5" ry="15.0" fill="yellow" stroke="black" stroke-width="1"/>
<text x="200.0" y="205.0" text-anchor="middle" font-size="12">aBool</text>
</g>
</svg>
When now doing something like
svg = ElementTree.parse(<file>)
ElementTree.tostring(svg.getroot(), "unicode")
It spits out the following:
<ns0:svg xmlns:ns0="http://www.w3.org/2000/svg" viewbox="0 0 400.0 400.0">
<ns0:g>
<ns0:ellipse cx="200.0" cy="200.0" rx="22.5" ry="15.0" fill="yellow" stroke="black" stroke-width="1" />
<ns0:text x="200.0" y="205.0" text-anchor="middle" font-size="12">aBool</ns0:text>
</ns0:g>
</ns0:svg>
The only thing I want is for the ElementTree.tostring function to not put any namespaces into my tags. While I found a lot of Information on how to do several more complicated things with namespaces in xml.etree, there seems to be no information on how to just not use them. Is it just not possible with this libraray? If so, what library can I use instead?
Solution 1:[1]
As pointed out by mzjn, stackoverflow.com/a/68470618/407651 cointains an answer to my question and it seems to be working.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sushman |
