New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/xml: failure to handle conflicting tags in different namespaces #8535

Open
jbardin opened this Issue Aug 15, 2014 · 8 comments

Comments

Projects
None yet
8 participants
@jbardin
Copy link
Contributor

jbardin commented Aug 15, 2014

What does 'go version' print?

go1.3.1

What steps reproduce the problem?

Try to decode document with the same tag in multiple namespaces
<example>
    <title>Example</title>
    <link>http://example.com/default<;/link>
    <ns:link xmlns:ns="http://www.w3.org/2005/Atom";>http://example.com/ns<;/ns:link>
</example>`

http://play.golang.org/p/FT56UXolGO


What happened?
main.Example field "Link" with tag "link" conflicts with field
"AtomLink" with tag "http://www.w3.org/2005/Atom link"

What should have happened instead?

It should be possible to decode both tags in this document.

Also, if the name-spaced tag isn't provided, the second "link" element shadows
the first though it's in another namespace.
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

ianlancetaylor commented Aug 15, 2014

Comment 1:

Labels changed: added repo-main, release-none.

@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Aug 21, 2014

Comment 3 by glen.newton:

I am also encountering this issue & is impacting my application. Note that issue only
occurs when one of the namespaces is empty. See same example with two explicit name
spaces: http://play.golang.org/p/tpGAWsc_Yo <-- Works OK.
@klauern

This comment has been minimized.

Copy link

klauern commented Feb 17, 2017

I just ran into this issue working with a legacy SOAP API that had a lot of namespaces in the fields.

@Matt3o12

This comment has been minimized.

Copy link

Matt3o12 commented Jun 30, 2017

I am also having this issue (funnily enough with RSS readers as well). Is there a workaround, yet or do we have to wait for Go 10 (ie. #14407)

@Matt3o12

This comment has been minimized.

Copy link

Matt3o12 commented Jul 1, 2017

I found a workaround by creating a custom string-type with a custom unmarshal function. If anyone is interested, here it is:

type NonNamespaceString string

func (s *NonNamespaceString) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	if start.Name.Space != "" {
		// We do not want a namespace, so we need to consume it
		d.Skip()
		return nil
	} else {
		str := ""
		d.DecodeElement(&str, &start)
		*s = NonNamespaceString(str)
	}

	return nil
}

I also create a gist with a unit test, if you are interested.

It seems to work and can almost be used as a drop-in replacement for any string. You just need to cast it back to a string if you want to use it.

@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Apr 12, 2018

Change https://golang.org/cl/106575 mentions this issue: encoding/xml : add check of namespaces to detect field names conflicts

@iWdGo

This comment has been minimized.

Copy link
Contributor

iWdGo commented Apr 12, 2018

Comparing namespaces of fields was missing and detecting false conflicts. A fix is submitted.

@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Apr 27, 2018

Change https://golang.org/cl/109855 mentions this issue: encoding/xml : Fixes to enforce XML namespace standard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment