XML namespace confusion

192 views
Skip to first unread message

Anschel Schaffer-Cohen

unread,
May 27, 2013, 11:41:40 PM5/27/13
to golan...@googlegroups.com
When using the encoding/xml package, I've come across a problem: the package will fill a struct member like "link" with a field like "aoeu:link", even though the two are in different namespaces. It's easy enough to specify a required namespace, but how do I specify that I want no namespace at all?

If that's not clear, here's some code that works (http://play.golang.org/p/e6GUdnbfRm) and here's some that doesn't because the <aoeu:link> field overwrites the <link> field (http://play.golang.org/p/m3Pg9ZZQHu).

Is there some simple way to get just the "link" field? I don't have the option to change the XML source.

adnaan badr

unread,
May 28, 2013, 2:42:51 AM5/28/13
to golan...@googlegroups.com
Hey,
         Change the Link field to []string type. http://play.golang.org/p/eomTG106q2. It will give you both values.

Anschel Schaffer-Cohen

unread,
May 28, 2013, 8:04:48 AM5/28/13
to golan...@googlegroups.com
That doesn't actually solve my problem, because there's no way for my program to know which value came from the <link> tag and which came from <xyz:link> tags.

adnaan badr

unread,
May 28, 2013, 9:23:47 AM5/28/13
to golan...@googlegroups.com

Anschel Schaffer-Cohen

unread,
May 28, 2013, 3:05:58 PM5/28/13
to adnaan badr, golan...@googlegroups.com
Unless I'm misunderstanding the example, that still doesn't work because I explicitly want those tags not in *any* namespace.


--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/xYONX8C_ngw/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Anschel

Bob Hutchison

unread,
May 28, 2013, 4:50:23 PM5/28/13
to Anschel Schaffer-Cohen, adnaan badr, golan...@googlegroups.com

Hi,

Assuming I understand your question...

Try this: http://play.golang.org/p/onODqrp0ig

You've got a couple of problems. First that's not well formed XML since the aoeu namespace prefix is undefined, you should define it -- Go is allowing the ill formed XML but it shouldn't. Second, you've not defined a default namespace, you don't have to but it's making things harder. You said you can't change the XML so I've shown one way to get around that by defining a default namespace outside of the XML file. In the code snippet there, I arbitrarily chose 'no-namespace' as the default namespace (this is *not* a namespace prefix, it's the namespace). To set it you have to get at the decoder, the call to Unmarshal is replaced with it's contents with the DefaultSpace set.

Hope that helps.

Cheers,
Bob

You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Anschel Schaffer-Cohen

unread,
May 30, 2013, 6:58:42 PM5/30/13
to golan...@googlegroups.com, Anschel Schaffer-Cohen, adnaan badr
As it turns out, I'm using Google App Engine, which doesn't have DefaultSpace implemented. Maybe it's using an old version of Go?

Nigel Tao

unread,
May 30, 2013, 8:21:57 PM5/30/13
to Anschel Schaffer-Cohen, golang-nuts, adnaan badr
On Fri, May 31, 2013 at 8:58 AM, Anschel Schaffer-Cohen
<ansc...@gmail.com> wrote:
> As it turns out, I'm using Google App Engine, which doesn't have
> DefaultSpace implemented. Maybe it's using an old version of Go?

DefaultSpace is new in Go 1.1. App Engine currently runs Go 1.0, but
App Engine Go 1.1 is in beta (you can try it now, see
https://groups.google.com/forum/?fromgroups#!topic/google-appengine-go/V1HMwZHbjZQ
for details) and should be launching soon. Watch
https://groups.google.com/forum/?fromgroups#!forum/google-appengine-go
for news.
Reply all
Reply to author
Forward
0 new messages