Marshal XML with xmlns:xsi

761 views
Skip to first unread message

Matope Ono

unread,
Mar 30, 2014, 12:34:32 PM3/30/14
to golan...@googlegroups.com
Hi, I have a trouble with XML Marshaling/Unmarshaling.


I want to convert between following XML and my struct.

Figure.1
    <People>
      <Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Student">
        <ID>2222</ID>
        <Name>Alice</Name>
      </Person>
      <Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Student">
        <ID>3333</ID>
        <Name>Bob</Name>
      </Person>
    </People>

 Figure.2
type Person struct {
Xmlns_xsi string `xml:"xmlns xsi,attr,omitempty"`
Xsi_type    string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr,omitempty"`
ID              string `xml:",omitempty"`
Name         string `xml:",omitempty"`
}
type People struct {
Person []Person `xml:"Person,omitempty"`
}


I could unmarshal perfectly. All values I wanted are in my Person struct.
But marshal didn't output format that I wanted to. I wanted xml like Figure.1
In Figure.3, many unintended attributes on Person element. ("_xmlns" ?)

Figure.3
----------- unmarshal -----------
{Person:[{Xmlns_xsi:http://www.w3.org/2001/XMLSchema-instance Xsi_type:Student ID:2222 Name:Alice} {Xmlns_xsi:http://www.w3.org/2001/XMLSchema-instance Xsi_type:Student ID:3333 Name:Bob}]}
------------ marshal ------------
<People>
  <Person xmlns:_xmlns="xmlns" _xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:XMLSchema-instance="http://www.w3.org/2001/XMLSchema-instance" XMLSchema-instance:type="Student">
    <ID>2222</ID>
    <Name>Alice</Name>
  </Person>
  <Person xmlns:_xmlns="xmlns" _xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:XMLSchema-instance="http://www.w3.org/2001/XMLSchema-instance" XMLSchema-instance:type="Student">
    <ID>3333</ID>
    <Name>Bob</Name>
  </Person>
</People>

What do I need to do to marsha/unmarshal such a XML?

Thanks.

Russ Amos

unread,
Mar 31, 2014, 9:17:03 AM3/31/14
to golan...@googlegroups.com
I hit this same problem a few weeks ago, and issue #7535 was filed. 

Matope Ono

unread,
Mar 31, 2014, 10:09:49 PM3/31/14
to golan...@googlegroups.com
Thank you for your reply!
I wish that issue would be fixed soon.

2014年3月31日月曜日 22時17分03秒 UTC+9 Russ Amos:
Reply all
Reply to author
Forward
0 new messages