encoding/xml: loss of xmlns= in encoding since Go 1.4
rsc opened this issue · 8 comments
It's not obvious at first glance how, but https://golang.org/cl/2660 changed the behavior of the XML marshaler for people who were generating xmlns attributes "by hand". For example:
package main
import (
"encoding/xml"
"fmt"
"log"
)
type T struct {
Ns string `xml:"xmlns,attr"`
Body string
}
func main() {
t := &T{Ns: "http://example.com/ns", Body: "hello world"}
x, err := xml.Marshal(t)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", x)
}
In Go 1.4 this program printed:
<T xmlns="http://example.com/ns"><Body>hello world</Body></T>
After this CL it prints:
<T><Body>hello world</Body></T>
In the absence of compelling justification for a breaking change, I think we should try to continue to support this way of setting the default xmlns.
I've seen tests break due to this change, and presumably real programs would break too, if the xmlns= were important.
What can we do to interpret these old programs correctly in the new more-namespace-aware world for Go 1.5?
Thanks for the report. Proposed https://go-review.googlesource.com/#/c/11635/ as a fix.
CL https://golang.org/cl/11635 mentions this issue.
This is issue is solved by the fix submitted for #20614
I submitted additional tests which provide various solutions to the described problem.
Change https://golang.org/cl/106835 mentions this issue: encoding/xml : adding tests
Change https://golang.org/cl/109855 mentions this issue: encoding/xml : Fixes to enforce XML namespace standard
Change https://golang.org/cl/355353 mentions this issue: encoding/xml: support xmlns prefixes
The behavior described in the issue seems restored for a while.
The example above ( https://go.dev/play/p/dLD8f9w5_2o ) prints the expected xmlns
attribute
<T xmlns="http://example.com/ns"><Body>hello world</Body></T>