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

Open
olivere opened this issue Jul 1, 2015 · 6 comments

Comments

@olivere
Copy link

@olivere olivere commented Jul 1, 2015

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.

@antonyho

This comment has been minimized.

Copy link

@antonyho antonyho 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
@iWdGo

This comment has been minimized.

Copy link
Contributor

@iWdGo iWdGo 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
@lelvisl

This comment has been minimized.

Copy link

@lelvisl lelvisl commented May 7, 2019

some updates?

@kjm789

This comment has been minimized.

Copy link

@kjm789 kjm789 commented Jun 20, 2019

?

@andybons andybons removed this from the Go1.13 milestone Jul 8, 2019
@mantyr

This comment has been minimized.

Copy link

@mantyr mantyr commented Aug 5, 2019

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.

@mantyr

This comment has been minimized.

Copy link

@mantyr mantyr commented Aug 6, 2019

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

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