Skip to content
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: empty namespace conventions are badly documented #11735

Open
ainar-g opened this issue Jul 16, 2015 · 4 comments
Open

encoding/xml: empty namespace conventions are badly documented #11735

ainar-g opened this issue Jul 16, 2015 · 4 comments
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@ainar-g
Copy link
Contributor

ainar-g commented Jul 16, 2015

During the discussion in #11724 @rogpeppe wrote:

The existing Go convention is that if no namespace is specified in the struct tag, it will match any namespace including the empty namespace.

But the package docs don't seem to mention that. This behaviour should be documented better, because a lot of people will assume that xml:"name,attr" will match name="foo" but not ns:name="bar".

@levpaul
Copy link

levpaul commented Aug 29, 2015

Can you explain how we can unmarshal xml which might look like:

<body rid="1234" xmpp:version="1.0" xml:lang="en" />
type Payload struct {
  XMLName   xml.Name `xml:"body"`
  Rid       int      `xml:"rid,attr"`
  XmlLang   string   `xml:"xml:lang,attr"`
  XmppVer   string   `xml:"xmpp:version,attr"`
}

@ainar-g
Copy link
Contributor Author

ainar-g commented Aug 29, 2015

@levilovelock Such questions are better asked at StackOverflow or other forums IMO. Although your question does uncover some package docs' flaws. Let's go point by point.

Firstly, your data doesn't seem to define the xmpp namespace anywhere. It should be something like

<body rid="1234" xmpp:version="1.0" xml:lang="en" xmlns:xmpp="urn:xmpp:xbosh" />

Secondly, namespace URLs and names in tags are separated by spaces, not colons. This point clearly doesn't get enough attention in the package docs.

Thirdly, the xml prefix is defined as http://www.w3.org/XML/1998/namespace. The docs do mention www.w3.org/TR/REC-xml-names/ which in turn mentions this namespace, but I think this should be made clearer.

(Fourthly your capitalization does not conform to the Go style recomendations but that's just me nitpicking.)

With all that in mind, here is your type:

type Payload struct {
    XMLName xml.Name `xml:"body"`
    RID     int      `xml:"rid,attr"`
    XMLLang string   `xml:"http://www.w3.org/XML/1998/namespace lang,attr"`
    XMPPVer string   `xml:"urn:xmpp:xbosh version,attr"`
}

Playground: http://play.golang.org/p/YLOJozEqFl.

@levpaul
Copy link

levpaul commented Aug 30, 2015

@ainar-g - ugh, I ran into this problem on a plane and couldn't do my instinctive google - instead I bogged myself down in the source and figured it was an issue with the lib. Alas my understanding of xml namespacing is the major issue, and as you noted stackoverflow does indeed seem like the correct place for it: http://stackoverflow.com/questions/18934327/golang-how-to-unmarshal-xml-attributes-with-colons

I want to thank you though for taking the time to help me, I do appreciate it. Even for nitpicking is legit and I'll adjust my vars for the future :) Cheers

@rsc
Copy link
Contributor

rsc commented Nov 25, 2015

Blocked on #13400.

@rsc rsc modified the milestones: Go1.7, Go1.6 Nov 25, 2015
@rsc rsc modified the milestones: Go1.8, Go1.7 May 18, 2016
@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 7, 2016
@rsc rsc modified the milestones: Go1.9Early, Go1.8 Oct 26, 2016
@bradfitz bradfitz modified the milestones: Go1.9Maybe, Go1.9Early May 3, 2017
@ianlancetaylor ianlancetaylor modified the milestones: Go1.10, Go1.9Maybe Aug 3, 2017
@rsc rsc modified the milestones: Go1.10, Go1.11 Nov 22, 2017
@gopherbot gopherbot modified the milestones: Go1.11, Unplanned May 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants