Control namespace prefixes output by xml.Marshal?

3,231 views
Skip to first unread message

Jim Robinson

unread,
Jul 10, 2013, 4:50:27 PM7/10/13
to golan...@googlegroups.com
Given the example


I was wondering there was tag syntax or other controls I could use to set the namespace prefixes to be used by the marshal operation?

Jim

James A. Robinson

unread,
Jul 10, 2013, 5:39:56 PM7/10/13
to Will Norris, golan...@googlegroups.com
On Wed, Jul 10, 2013 at 2:01 PM, Will Norris <wi...@willnorris.com> wrote:
> Of course, it's not always possible to change the URLs like this, but that
> seems to be your best option if you can. Otherwise, the xml package reverts
> to using "_", appending a number when necessary.

Yeah, those examples were completely made up, for my actual needs I've
got completely different sets of namespace URLs.

Thank you for the reply,

Jim

Will Norris

unread,
Jul 10, 2013, 5:01:06 PM7/10/13
to Jim Robinson, golan...@googlegroups.com
looking at http://golang.org/src/pkg/encoding/xml/marshal.go#L135 it seems that the xml package will attempt to use the last component of the URL. So, for example, if you were to slightly change your namespace URLs to "http://example.org/ns1" and "http://example.org/ns2", you would get the desired effect: http://play.golang.org/p/i9Q-xaTH4z

Of course, it's not always possible to change the URLs like this, but that seems to be your best option if you can.  Otherwise, the xml package reverts to using "_", appending a number when necessary.

Jim

--
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/groups/opt_out.
 
 

Jim Robinson

unread,
Jul 11, 2013, 8:02:07 PM7/11/13
to golan...@googlegroups.com
As a not-so-random aside, it irritates me to no end to see code written by supposed programmers that depend on specific namespace prefix values.  I've come across two examples of this in less than 24 hours. :(

Matt Joiner

unread,
Dec 10, 2013, 9:51:33 AM12/10/13
to golan...@googlegroups.com
I notice in the examples the root element also has a prefix. But the marshalling code isn't able to reproduce this part of it. Was this ever solved? 

Matt Joiner

unread,
Dec 10, 2013, 11:11:04 AM12/10/13
to wno...@gmail.com, golang-nuts
I'm actually trying to remove the implicit namespace on child elements of an element that has a namespace. That is to say in this example:

<a xmlns="b"><c></c></a>

The namespace of the 'c' element is also 'b'. There's no way to remove this default namespace using xml.Marshal.

Have I got this right?


On Wed, Dec 11, 2013 at 3:02 AM, w.no...@gmail.com <wno...@gmail.com> wrote:
no, as best as I can tell, the namespace of the root element is always set as the default namespace.  As Jim mentioned though, if you (or someone else) are relying on specific namespace prefix values, that code is broken.
--

Matt Joiner

unread,
Dec 10, 2013, 11:32:24 AM12/10/13
to wno...@gmail.com, golang-nuts
I'm not sure your play example accomplished anything:

I get <a xmlns="b"><c></c></a>

In order for 'c' to no longer be in the same namespace as 'a', I believe either of the following 2 must be output:

<d:a xmlns:d="b"><c></c></d:a>

<a xmlns="b"><c xmlns=""></c></a>


On Wed, Dec 11, 2013 at 3:25 AM, w.no...@gmail.com <wno...@gmail.com> wrote:
only way I can find to do it is to actually remove the namespace declaration on your C type (http://play.golang.org/p/PRQcMtviEQ).  Then <c> will be declared as being in the default namespace, which works in this single case, but would create problems if the default namespace changes.  So no, I don't think there is a way to have encoding/xml automatically remove redundant namespace declarations.

James A. Robinson

unread,
Dec 10, 2013, 11:38:33 AM12/10/13
to Matt Joiner, wno...@gmail.com, golang-nuts
On Tue, Dec 10, 2013 at 8:11 AM, Matt Joiner <anac...@gmail.com> wrote:
I'm actually trying to remove the implicit namespace on child elements of an element that has a namespace. That is to say in this example:

<a xmlns="b"><c></c></a>

The namespace of the 'c' element is also 'b'. There's no way to remove this default namespace using xml.Marshal.

Have I got this right?

The last time I looked yes, the marshal routine would always
output all the namespaces.  For my own situation I ended up
writing my own serialization routine (since the format was
simple and isn't going to change any time soon this seemed
reasonable).

Jim

Matt Joiner

unread,
Dec 10, 2013, 11:48:59 AM12/10/13
to wno...@gmail.com, golang-nuts

Perhaps you could construct an example with xml.Marshal that outputs either of the 2 forms I gave? My issue is that I cannot work out how to do either of them.

On 11/12/2013 3:43 AM, "w.no...@gmail.com" <wno...@gmail.com> wrote:
I thought you were trying to get it to output "<a xmlns="b"><c></c></a>".  Perhaps I misunderstood?

In any event, for XML attributes, a global map of prefixes is maintained, and is consulted so that for a given namespace URI, it is only ever mapped to a prefix once, and that prefix is used throughout the document.  For XML elements, if your Go code declares an explicit namespace, then that is always set as the default namespace on the element in the XML output.

James A. Robinson

unread,
Dec 10, 2013, 11:57:53 AM12/10/13
to Matt Joiner, wno...@gmail.com, golang-nuts
On Tue, Dec 10, 2013 at 8:48 AM, Matt Joiner <anac...@gmail.com> wrote:
Perhaps you could construct an example with xml.Marshal that outputs either of the 2 forms I gave? My issue is that I cannot work out how to do either of them.

He's saying that you can do this if you don't
declare the namespace on the c structure.
That's what he's doing in his example, and
I can get the same results in the following:


Basically you take into account how the
Marshal operation works to elide the xml
namespace from the serialized output.

Jim

James A. Robinson

unread,
Dec 10, 2013, 12:08:06 PM12/10/13
to Matt Joiner, wno...@gmail.com, golang-nuts
Oh, so you're saying you want to put c into the empty
default namespace:


<a xmlns="b"><c xmlns=""></c></a>

Sorry, I misunderstood this part of the thread (I
think google groups is only sending me part of
the conversation).



--
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/tGX1xp-nBm4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.

wno...@gmail.com

unread,
Dec 10, 2013, 11:02:19 AM12/10/13
to Matt Joiner, golan...@googlegroups.com
no, as best as I can tell, the namespace of the root element is always set as the default namespace.  As Jim mentioned though, if you (or someone else) are relying on specific namespace prefix values, that code is broken.

On Tue Dec 10 2013 at 6:51:42 AM, Matt Joiner <anac...@gmail.com> wrote:

wno...@gmail.com

unread,
Dec 10, 2013, 11:59:20 AM12/10/13
to Matt Joiner, golang-nuts
I don't think either of those are possible.  It looks like encoding/xml never prefixes element names, but rather always redeclares the default namespace (http://golang.org/src/pkg/encoding/xml/marshal.go#L614), so that eliminates <d:a xmlns:d="b"><c></c></d:a>.

As for <a xmlns="b"><c xmlns=""></c></a>, It doesn't seem possible to declare an empty namespace URI as you have on the <c> element.

wno...@gmail.com

unread,
Dec 10, 2013, 11:25:38 AM12/10/13
to Matt Joiner, golang-nuts
only way I can find to do it is to actually remove the namespace declaration on your C type (http://play.golang.org/p/PRQcMtviEQ).  Then <c> will be declared as being in the default namespace, which works in this single case, but would create problems if the default namespace changes.  So no, I don't think there is a way to have encoding/xml automatically remove redundant namespace declarations.

wno...@gmail.com

unread,
Dec 10, 2013, 11:43:55 AM12/10/13
to Matt Joiner, golang-nuts
I thought you were trying to get it to output "<a xmlns="b"><c></c></a>".  Perhaps I misunderstood?

In any event, for XML attributes, a global map of prefixes is maintained, and is consulted so that for a given namespace URI, it is only ever mapped to a prefix once, and that prefix is used throughout the document.  For XML elements, if your Go code declares an explicit namespace, then that is always set as the default namespace on the element in the XML output.

Reply all
Reply to author
Forward
0 new messages