Unmarshal XML with namespace

1,351 views
Skip to first unread message

dan...@verisart.com

unread,
May 18, 2015, 12:11:27 AM5/18/15
to golan...@googlegroups.com
Hi, I think this topic has been covered before but want to see if anyone can suggest a workaround. I need to load a METS xml file.

A very simplified version would look like this:
<METS:mets xmlns:METS="http://www.loc.gov/METS/" LABEL="Abigail Adams Hobart's sampler">
  <METS:metsHdr CREATEDATE="2008-02-14T00:00:00.001"></METS:metsHdr>
</METS:mets>

My problem is that golang doesn't seem to apply the METS namespace to the attributes, so if I mark my xml fields as `xml:"http://www.loc.gov/METS/ LABEL"` as I think would be correct, golang doesn't unmarshal the value into it. I could remove the namespace qualification, but that doesn't seem to be the correct way since the attribute is part of the METS namespace and I wouldn't know if a parent xml element has declared that namespace. How should I deal with this?

Here's an example:

Thanks,
Dan

Jonathan Carroll

unread,
May 19, 2015, 6:37:44 AM5/19/15
to golan...@googlegroups.com
Can you have a look a this http://play.golang.org/p/tGD8FXpLC3 and see if it's working right - specify the namespace in the xml.Name .

Also specify the name space in metsHdr..


Jonathan Carroll

unread,
May 19, 2015, 6:42:01 AM5/19/15
to golan...@googlegroups.com
..more

I think the error you made was to specify namespace in the "attr" fields - because these are embedded in the xmlName they have to inherit the namespace so it's not necessary. Ditto with your metshdr struct - I think the rule is only specify namespace in the element name not associated tags.

I found this package fiddly too.

dan...@verisart.com

unread,
May 19, 2015, 1:25:46 PM5/19/15
to golan...@googlegroups.com
Hi Jonathan, yes that does seem to be a better way to handle it thanks. 

roger peppe

unread,
May 21, 2015, 3:51:54 AM5/21/15
to dan...@verisart.com, golang-nuts
This an XML issue, not a Go one. The default name space for attributes
is empty, not the default name space of the enclosing element.
Even if it that wasn't the case, your example would still be wrong
because there is no default namespace (xmlns="....") there.

I'd agree that this behaviour is surprising (it has caught me
out in the past), but that's just XML.
Nothing wrong with Go here (note that go 1.4 *does* get it
wrong when encoding XML, but that is fixed in tip).

If your attribute explicitly mentioned the METS name space,
it works: http://play.golang.org/p/ebUBE0uvJg
Likewise if you don't mention the name space in the Go
field tag: http://play.golang.org/p/gQkjp5S_gV

cheers,
rog.

>
> Here's an example:
> http://play.golang.org/p/K-hFbkI1Er
>
> Thanks,
> Dan
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.

dan...@verisart.com

unread,
May 21, 2015, 10:25:58 AM5/21/15
to golan...@googlegroups.com, dan...@verisart.com
Hi Roger,

I see what you mean about the default namespace of attributes. In other xml files I see this often:

<wrap xmlns:mets="http://www.example.com">
  <something mets:createdate="2012"></something>
</wrap>

In this way, the attribute namespace can be declared in a succinct manner. But I can't find any way to do this with the xml encoder in golang. Agreed, that currently the way it is done is not wrong. Its just something that would be nice to have since, when formatting, people expect it to look more like this.

Dan

roger peppe

unread,
May 21, 2015, 12:03:27 PM5/21/15
to dan...@verisart.com, golang-nuts
On 21 May 2015 at 15:25, <dan...@verisart.com> wrote:
> Hi Roger,
>
> I see what you mean about the default namespace of attributes. In other xml
> files I see this often:
>
> <wrap xmlns:mets="http://www.example.com">
> <something mets:createdate="2012"></something>
> </wrap>
>
> In this way, the attribute namespace can be declared in a succinct manner.

That's fine, and should work OK with the go1.4 XML unmarshaler.
Your XML looked like this though:

<wrap xmlns:mets="http://www.example.com">
<something createdate="2012"></something>
</wrap>

which puts the createdate attribute in the empty namespace, not
in http://www.example.com.

> But I can't find any way to do this with the xml encoder in golang. Agreed,
> that currently the way it is done is not wrong. Its just something that
> would be nice to have since, when formatting, people expect it to look more
> like this.

If you're talking about *encoding*, the go1.4 XML package is
broken with respect to encoding XML with namespaces.
If you want to do this, I would suggest forking encoding/xml from tip
for the time
being. We've got one at github.com/juju/xml.

cheers,
rog.
Reply all
Reply to author
Forward
0 new messages