encoding/xml: dealing with ,attr prefix

543 views
Skip to first unread message

Kiki Sugiaman

unread,
Oct 16, 2015, 2:54:02 PM10/16/15
to golang-nuts
Hi all,

Specifying attribute prefix this way: `xml:"prefix:attribute,attr"`
seems to make Unmarshal fail to capture such attribute. Not specifying
the prefix makes Unmarshal capture the attributes just fine, but will
cause Marshal to lose the prefix.
How do I capture prefixed attributes without causing Marshal to lose the
prefix?

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


Thanks

Matt Harden

unread,
Oct 16, 2015, 4:17:18 PM10/16/15
to Kiki Sugiaman, golang-nuts
Go implements XML namespaces, and always calls them by their URLs, rather than the short prefixes they are given.

You have chosen two prefixes that have special meaning in XML. The xmlns: prefix creates a new XML namespace with the given value as its URL. The xml: prefix is automatically defined with a URL of "http://www.w3.org/XML/1998/namespace".

So by saying xmlns:attribute="val" you're creating a new namespace called "attribute" whose URL is "val". Avoid using xmlns as a namespace prefix and that attribute will work.

In the case of xml:lang="en" you can capture this by specifying the namespace as http://www.w3.org/XML/1998/namespace.




--
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.

Kiki Sugiaman

unread,
Oct 16, 2015, 6:36:28 PM10/16/15
to Matt Harden, golang-nuts


On 17/10/15 07:16, Matt Harden wrote:
> Go implements XML namespaces, and always calls them by their URLs,
> rather than the short prefixes they are given.
>
> You have chosen two prefixes that have special meaning in XML. The
> xmlns: prefix creates a new XML namespace with the given value as its
> URL. The xml: prefix is automatically defined with a URL of
> "http://www.w3.org/XML/1998/namespace".
>
> So by saying xmlns:attribute="val" you're creating a new namespace
> called "attribute" whose URL is "val". Avoid using xmlns as a
> namespace prefix and that attribute will work.

I provided a bad example with "val". What if I really want to
capture/re-marshal the url of a namespace declaration (for use in inner
tags)?

example:
<tag xmlns="http://...." xmlns:myns="http://...." xml:lang="en">
<myns:innertag ....>
....
</myns:innertag>
</tag>


Thanks.

>
> In the case of xml:lang="en" you can capture this by specifying the
> namespace as http://www.w3.org/XML/1998/namespace.
>
> http://play.golang.org/p/Qe4NoynaL3
>
>
>
> On Fri, Oct 16, 2015 at 1:53 PM Kiki Sugiaman <ksug...@gmail.com
> <mailto:ksug...@gmail.com>> wrote:
>
> Hi all,
>
> Specifying attribute prefix this way: `xml:"prefix:attribute,attr"`
> seems to make Unmarshal fail to capture such attribute. Not specifying
> the prefix makes Unmarshal capture the attributes just fine, but will
> cause Marshal to lose the prefix.
> How do I capture prefixed attributes without causing Marshal to
> lose the
> prefix?
>
> http://play.golang.org/p/JhqcxN0EQf
>
>
> Thanks
>
> --
> 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
> <mailto:golang-nuts%2Bunsu...@googlegroups.com>.

Matt Harden

unread,
Oct 16, 2015, 7:50:15 PM10/16/15
to Kiki Sugiaman, golang-nuts
With encoding/xml, there is no way to capture the data in xmlns: declarations. I'm actually surprised the attribute is captured when you don't specify the namespace; that might be considered a bug. If the namespace is actually used by an attribute or inner element, then you can capture it with XMLName variables, or by implementing xml.Unmarshaler or xml.UnmarshalerAttr.

Kiki Sugiaman

unread,
Oct 17, 2015, 3:11:50 AM10/17/15
to Matt Harden, golang-nuts
Seems to do the trick, thanks!
Reply all
Reply to author
Forward
0 new messages