Hi!

I'm struggling with serializing XML (and deserializing again).

But first things first:

go version devel +434e0bc Mon Jun 29 16:07:14 2015 +0000 darwin/amd64  # (tried 1.4 and master)
Darwin aero 14.4.0 Darwin Kernel Version 14.4.0: Thu May 28 11:35:04 PDT 2015; root:xnu-2782.30.5~1/RELEASE_X86_64 x86_64

I'm trying to serialize a struct to generate XML like this (see http://play.golang.org/p/fMvL86lzB0):

<person xmlns="ns1" xmlns:ns2="ns2">
  <name>Oliver</name>
  <ns2:phone>110</ns2:phone>
</person>

When defining Person like this ...

type Person struct {
    XMLName xml.Name `xml:"ns1 person"`
    Name    string   `xml:"name"`
    Phone   string   `xml:"ns2 phone,omitempty"`
}

... it serialized into the following (which is semantically correct, I guess, but not the same as above):

<person xmlns="ns1">
  <name>Oliver</name>
  <phone xmlns="ns2">110</phone>
</person>

I can fake it like this:

type Person struct {
    XMLName xml.Name `xml:"ns1 person"`
    NS2 string `xml:"xmlns:ns2,attr"`

    Name    string   `xml:"name"`
    Phone   string   `xml:"ns2:phone,omitempty"`
}

... by initializing NS2 before serializing (see http://play.golang.org/p/2dEljm97c8). Unfortunately then I'm not able to deserialize correctly as the Phone field will not be blank (see http://play.golang.org/p/RxG2ImcWbm).

Maybe it's just a documentation issue and I'm missing an example. There are some other issues regarding XML and namespaces/namespace prefixes (e.g. #6800, #9519, #7113) some of which are closed and some of which are open, so I'm a bit confused about the status.

Comment From: antonyho

This issue has been unplanned for 2 years. Is there someone who are looking into it?

Comment From: iwdgo

The XMLName field must be unique for each struct as it is providing the name of the tag of the element. It follows that binding another namespace can only be done as you suggest using an attribute like '''NS2 string xml:"ns2,attr"''' without the xmlns in this case.

When unmarshaling/deserializing, the namespaces bindings are done at the top tag (Person) as requested. To access the alternate namespace ns2, as documentation states, a space is needed and not a : and your line becomes '''Phone string xml:"ns2 phone,omitempty"''' In that case, you obtain the desired result main.Person{XMLName:xml.Name{Space:"ns1", Local:"person"}, NS2:"ns2", Name:"Oliver", Phone:"110"}

Comment From: lelvisl

some updates?

Comment From: kjm789

?

Comment From: mantyr

I propose to do so:

type Person struct {
    XMLName xml.Name `xml:"ns1 person"`
    Name    string   `xml:"name"`
    Phone   string   `xml:"ns2 phone,omitempty,prefix"`
}

,prefix indicates the need for use namespace as a prefix and ignore when Unmarshal.

Comment From: mantyr

Or xml:"ns2 phone,omitempty" prefix="ns2" for marshal.

Comment From: duffiye

some updates?

Comment From: mantyr

some updates?

https://github.com/mantyr/xmlutils

I went a long way. It works for me.

P.S. If you decide to fork encoding/xml then be patient ... :)

Comment From: lorddang

This issue has been unplanned for 6 years. Is there someone who are looking into it?

Comment From: davedbase

This issue has been unplanned for 8 years. Is there someone looking into it?

🤣

Comment From: m29h

I made a fork of encoding/xml that serializes namespaced tags much more like @olivere anticipated. (even though with not so much control) See this Playground where the original playground from @olivere has encoding/xml replaced with my forked module while everything else is kept the same.

It produces

<ns1:person xmlns:ns1="ns1">
  <ns1:name>Oliver</ns1:name>
  <ns2:phone xmlns:ns2="ns2">110</ns2:phone>
</ns1:person>

while staying 100% interface and feature compatible with encoding/xml

Comment From: iwdgo

The submitted example can be written as a test of a roundtrip. It succeeds when removing the "xmlns:" and the colon ":" after 'ns2' which do not seem in line with the current documentation. Using the submitted fix of #9519, the test passes. This case seems to somehow duplicate.

Comment From: kolkov

Hi! Are there any plans for an update?

Comment From: deborggraever

I been strugle with the same issue for few days, so tried add support for namespace prefix on #70944

Also added a standalone package of it. https://github.com/deb-ict/go-xml

Comment From: 3052

some updates?

https://github.com/mantyr/xmlutils

I went a long way. It works for me.

P.S. If you decide to fork encoding/xml then be patient ... :)

@mantyr parsing element namespace produces invalid results https://github.com/mantyr/xmlutils/issues/4