Skip to content

Commit 9f9d66d

Browse files
committed
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 <[email protected]> Reviewed-by: Dave Cheney <[email protected]>
1 parent 44e9031 commit 9f9d66d

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/encoding/xml/marshal_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,8 @@ var marshalTests = []struct {
640640
`<x xmlns="space1">` +
641641
`<c>c1</c>` +
642642
`<d>d1</d>` +
643+
`</x>` +
644+
`<x>` +
643645
`<e>e1</e>` +
644646
`</x>` +
645647
`</top>`,

src/encoding/xml/typeinfo.go

+8
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ func structFieldInfo(typ reflect.Type, f *reflect.StructField) (*fieldInfo, erro
194194
return finfo, nil
195195
}
196196

197+
if finfo.xmlns == "" && finfo.flags&fAttr == 0 {
198+
// If it's an element no namespace specified, get the default
199+
// from the XMLName of enclosing struct if possible.
200+
if xmlname := lookupXMLName(typ); xmlname != nil {
201+
finfo.xmlns = xmlname.xmlns
202+
}
203+
}
204+
197205
// Prepare field name and parents.
198206
parents := strings.Split(tag, ">")
199207
if parents[0] == "" {

0 commit comments

Comments
 (0)