diff --git a/src/encoding/xml/marshal_test.go b/src/encoding/xml/marshal_test.go
index 601bb30d03..8362421db7 100644
--- a/src/encoding/xml/marshal_test.go
+++ b/src/encoding/xml/marshal_test.go
@@ -640,6 +640,8 @@ var marshalTests = []struct {
`` +
`c1` +
`d1` +
+ `` +
+ `` +
`e1` +
`` +
``,
diff --git a/src/encoding/xml/typeinfo.go b/src/encoding/xml/typeinfo.go
index 22248d20a6..c9a6421f28 100644
--- a/src/encoding/xml/typeinfo.go
+++ b/src/encoding/xml/typeinfo.go
@@ -194,6 +194,14 @@ func structFieldInfo(typ reflect.Type, f *reflect.StructField) (*fieldInfo, erro
return finfo, nil
}
+ if finfo.xmlns == "" && finfo.flags&fAttr == 0 {
+ // If it's an element no namespace specified, get the default
+ // from the XMLName of enclosing struct if possible.
+ if xmlname := lookupXMLName(typ); xmlname != nil {
+ finfo.xmlns = xmlname.xmlns
+ }
+ }
+
// Prepare field name and parents.
parents := strings.Split(tag, ">")
if parents[0] == "" {