-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
Comments
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"`
} |
@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 <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 (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. |
@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 |
Blocked on #13400. |
During the discussion in #11724 @rogpeppe wrote:
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 matchname="foo"
but notns:name="bar"
.The text was updated successfully, but these errors were encountered: