New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ElementTree adds spurious ns0: namespace prefix
#113425
Comments
This is because of the spurious `ns0:` XML namespace prefix, see python/cpython#113425.
This is because of the spurious `ns0:` XML namespace prefix, see python/cpython#113425.
|
First of all, I think this should be a feature request, instead of a bug, because it is not WRONG and it does not contradict the documentation. From a feature request point of view, I like the propsed result, but there are a couple of things to concern.
I'm not saying it's impossible (well very few things are really impossible in software world), but we need further discussion (presumably with some core devs involved) in this. I won't be too optimistic about this request because of the reasons I mentioned above. You might be recommended to use some 3rd party libraries to do this kind of work. Let's see if this can raise anyone's interests to the topic :) |
Based on the very limited information you provided this sounds like you should report this as a bug on their end - they are the ones who failed to correctly implement the Namespaces in XML specification. |
|
@hexagonrecursion I agree that it's a bug on their end, but consider the Robustness principle - "be conservative in what you send, be liberal in what you accept". Randomly adding namespace prefixes where none are required is not being "conservative in what you send". Fixing ElementTree to produce XML that is less likely to cause problems, and fixing bugs in consumers, are not mutually exclusive propositions. |
Bug description:
Minimal reproducer
Expected output
Actual output
Discussion
It would appear that, if a namespace isn't registered, ElementTree will use a
ns0:namespace prefix by default during serialization, instead of simply using the default namespace.Now to be fair, technically this is a valid thing to do, as the resulting XML is semantically the same. However I still find this behavior problematic because:
ElementTree should just use the default namespace, and only resort to prefixes if the default namespace is already in use.
Workaround
In the above example, pass
default_namespace="somens"toET.tostring(). Note this requires the user to know the namespace URL in advance. If they don't know it in advance they can get it from the root element but ElementTree doesn't make that easy: it requires ugly string parsing on the element name.CPython versions tested on:
3.11
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: