diff --git a/src/encoding/xml/marshal_test.go b/src/encoding/xml/marshal_test.go index d2e5137afd7c05..c54db080a8092f 100644 --- a/src/encoding/xml/marshal_test.go +++ b/src/encoding/xml/marshal_test.go @@ -2490,3 +2490,34 @@ func TestInvalidXMLName(t *testing.T) { t.Errorf("error %q does not contain %q", err, want) } } + +func TestMismatchedTagNames(t *testing.T) { + type InnerStruct struct { + XMLName Name `xml:"innerStruct"` + Name string `xml:"name"` + } + + type Outer struct { + XMLName Name `xml:"outer"` + Inner *InnerStruct `xml:"inner"` + } + + dec := NewDecoder(strings.NewReader(` + + + foo + +"`)) + + holder := &Outer{} + if err := dec.Decode(holder); err != nil { + t.Errorf("Unexpected error: %q", err) + } + if holder == nil { + t.Error("Holder should be non-nil") + } else if holder.Inner == nil { + t.Error("Inner should be non-nil") + } else if holder.Inner.Name != "foo" { + t.Errorf("Expected name to be %q, but was %q", "foo", holder.Inner.Name) + } +} diff --git a/src/encoding/xml/read.go b/src/encoding/xml/read.go index ef5df3f7f6aecc..20172b812e74a2 100644 --- a/src/encoding/xml/read.go +++ b/src/encoding/xml/read.go @@ -423,9 +423,6 @@ func (d *Decoder) unmarshal(val reflect.Value, start *StartElement) error { // Validate and assign element name. if tinfo.xmlname != nil { finfo := tinfo.xmlname - if finfo.name != "" && finfo.name != start.Name.Local { - return UnmarshalError("expected element type <" + finfo.name + "> but have <" + start.Name.Local + ">") - } if finfo.xmlns != "" && finfo.xmlns != start.Name.Space { e := "expected element <" + finfo.name + "> in name space " + finfo.xmlns + " but have " if start.Name.Space == "" { diff --git a/src/encoding/xml/typeinfo.go b/src/encoding/xml/typeinfo.go index 162724ef1a58b0..00539470cc331b 100644 --- a/src/encoding/xml/typeinfo.go +++ b/src/encoding/xml/typeinfo.go @@ -211,17 +211,6 @@ func structFieldInfo(typ reflect.Type, f *reflect.StructField) (*fieldInfo, erro finfo.parents = parents[:len(parents)-1] } - // If the field type has an XMLName field, the names must match - // so that the behavior of both marshaling and unmarshaling - // is straightforward and unambiguous. - if finfo.flags&fElement != 0 { - ftyp := f.Type - xmlname := lookupXMLName(ftyp) - if xmlname != nil && xmlname.name != finfo.name { - return nil, fmt.Errorf("xml: name %q in tag of %s.%s conflicts with name %q in %s.XMLName", - finfo.name, typ, f.Name, xmlname.name, ftyp) - } - } return finfo, nil }