diff --git a/src/encoding/xml/marshal.go b/src/encoding/xml/marshal.go
index 0c3cc0dc3618a1..ae39846f5b6c14 100644
--- a/src/encoding/xml/marshal.go
+++ b/src/encoding/xml/marshal.go
@@ -543,8 +543,9 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplat
}
}
- // If a name was found, namespace is overridden with an empty space
+ // If a empty name was found, namespace is overridden with an empty space
if tinfo.xmlname != nil && start.Name.Space == "" &&
+ tinfo.xmlname.xmlns == "" && tinfo.xmlname.name == "" &&
len(p.tags) != 0 && p.tags[len(p.tags)-1].Space != "" {
start.Attr = append(start.Attr, Attr{Name{"", xmlnsPrefix}, ""})
}
diff --git a/src/encoding/xml/xml_test.go b/src/encoding/xml/xml_test.go
index 8205ac31484d84..c6754a85286626 100644
--- a/src/encoding/xml/xml_test.go
+++ b/src/encoding/xml/xml_test.go
@@ -1064,14 +1064,19 @@ func TestIssue7113(t *testing.T) {
XMLName Name `xml:""` // Sets empty namespace
}
+ type D struct {
+ XMLName Name `xml:"d"`
+ }
+
type A struct {
XMLName Name `xml:""`
C C `xml:""`
+ D D
}
var a A
structSpace := "b"
- xmlTest := ``
+ xmlTest := ``
t.Log(xmlTest)
err := Unmarshal([]byte(xmlTest), &a)
if err != nil {