aboutsummaryrefslogtreecommitdiff
path: root/src/encoding
diff options
context:
space:
mode:
authorRoger Peppe <rogpeppe@gmail.com>2015-03-06 11:32:42 +0000
committerroger peppe <rogpeppe@gmail.com>2015-03-09 09:10:30 +0000
commit9f9d66d3b66d1d23fd5578f30b2fa084e4f83902 (patch)
tree18813d43328fe659d8c6fa67e3fdb939a9b54990 /src/encoding
parent44e903158faca58bf495dc2f263d6973f62b9d17 (diff)
downloadgo-9f9d66d3b66d1d23fd5578f30b2fa084e4f83902.tar.xz
encoding/xml: fix default namespace of tags
The struct XMLName sets the default namespace, but that's not good enough for nested tags, because an earlier tag can set the implicit parents of a subsequent tag. This change makes sure that we always explicitly set the namespace on a tag when possible. See https://go-review.googlesource.com/#/c/5910/4/src/encoding/xml/marshal_test.go@628 for discussion. Change-Id: If1afc536471c0be83e5dd80381b598476ea3f44d Reviewed-on: https://go-review.googlesource.com/6927 Reviewed-by: Nigel Tao <nigeltao@golang.org> Reviewed-by: Dave Cheney <dave@cheney.net>
Diffstat (limited to 'src/encoding')
-rw-r--r--src/encoding/xml/marshal_test.go2
-rw-r--r--src/encoding/xml/typeinfo.go8
2 files changed, 10 insertions, 0 deletions
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 {
`<x xmlns="space1">` +
`<c>c1</c>` +
`<d>d1</d>` +
+ `</x>` +
+ `<x>` +
`<e>e1</e>` +
`</x>` +
`</top>`,
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] == "" {