Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/xml: Serializing XML with namespace prefix #11496

olivere opened this issue Jul 1, 2015 · 4 comments


None yet
8 participants
Copy link

commented Jul 1, 2015


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

<person xmlns="ns1" xmlns:ns2="ns2">

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">
  <phone xmlns="ns2">110</phone>

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 Unfortunately then I'm not able to deserialize correctly as the Phone field will not be blank (see

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.


This comment has been minimized.

Copy link

commented Jul 11, 2017

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

@bradfitz bradfitz modified the milestones: Go1.10, Unplanned Jul 11, 2017

@rsc rsc modified the milestones: Go1.10, Go1.11 Nov 22, 2017


This comment has been minimized.

Copy link

commented Apr 12, 2018

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"}

@bradfitz bradfitz modified the milestones: Go1.11, Go1.12 Jun 13, 2018

@ianlancetaylor ianlancetaylor modified the milestones: Go1.12, Go1.13 Dec 12, 2018


This comment has been minimized.

Copy link

commented May 7, 2019

some updates?


This comment has been minimized.

Copy link

commented Jun 20, 2019


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.