When I use a struct like
type MyStruct struct {
XMLName xml.Name
Attrs []xml.Attrs `xml:",attr,any"`
}
The Namespace taken from the xmlns
-attribute is both an attribute in Attrs
and the value of XMLName.Space
. That is unfortunate, because I'd expect, that Attrs
only contains only attributes, that are not handled elsewhere
Even worse, when I marshal and unmarshal an XML I'd expect to get the same XML. Instead I get something, that looks broken. xmlns
is doubled
package main
import (
"encoding/xml"
"fmt"
)
func main() {
var Foo struct {
XMLName xml.Name `xml:""`
Attr []xml.Attr `xml:",attr,any"`
} = struct {
XMLName xml.Name `xml:""`
Attr []xml.Attr `xml:",attr,any"`
}{}
str := `
<foo xmlns="bar"/>
`
fmt.Println(str)
xml.Unmarshal([]byte(str), &Foo)
b, _ := xml.Marshal(&Foo)
fmt.Println(string(b))
}
https://play.golang.org/p/ulWy-Paha2
Results in
<foo xmlns="bar"/>
<foo xmlns="bar" xmlns="bar"></foo>
How can I avoid this? Is there's a way to tell the decoder to simply ignore certain attributes completely?