You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ydnar
changed the title
encoding/xml: allow caller to specify preferred namespace prefix
proposal: encoding/xml: allow caller to specify preferred namespace prefix
Oct 6, 2021
I think it is really important that the standard library gets proper handling of XML namespaces and namespace prefixes. The current implementation is definitely broken as has been raised in multiple issues. I can elaborate on the problems and short comings if needed,
In my view, this PR provides a good solution, or at least a good basis for a solution. I found myself forced to clone the standard xml package together with this patch in my new repo https://github.com/Eyevinn/dash-mpd that handles DASH MPD XMLs and need proper namespaces and prefixes.
In the discussion about previous ticket, Dave Cheney argued that one cannot change the behavior of such an established stdlib package as XML due to the risk of breaking someones code. I think there is some truth in that argument, so I'd like to suggest that we start working on a variant of the xml package that supports namespaces and namespace prefixes. It could be called encoding/xml2, and this PR could make a good start. It would be nice if it could be done in the 1.21 time frame.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Use
xml.Marshal
to marshal namespaced XML, andxml.Unmarshal
to decode that XML.Example: https://play.golang.org/p/-6Ee8tcLl2L
What did you expect to see?
Namespace prefixes preserved round-trip.
What did you see instead?
Namespace prefixes written, but not recognized as the relevant tag when decoding.
Proposal
Change the
encoding/xml
package to allow callers to specify a preferred namespace prefix, and allow decoding of prefixed tags.The preferred prefix can be specified by prefixing the XML tag name in a struct tag or
(xml.Name).Local
.This should be backwards-compatible with existing Go 1
encoding/xml
clients.Example
This change would allow a structure defined above to correctly round-trip encode and decode with namespace prefixes:
Would encode to (and decode from):
Implementation
A working implementation can be found at #48641, built on earlier work by @rogpeppe and others.
Related Issues
This would fix #43168 and update #11431 and #8068.
The text was updated successfully, but these errors were encountered: