'Golang XML Marshal with namespaces

I am using golang xml to Marshal/Unmarshal it. I want to input and output after Marshal same.

Here is my example :

https://play.golang.org/p/KH6mFXLVdH

xml input has tag name with namespace, ex: x14:dataValidation

after Marshal data that Unmarshal from input xml, i want tag name same it.

Have any solutions?

Thanks



Solution 1:[1]

The last project I worked on which used XML heavily ran into the same problem. As @algrebe mentioned, it seems to be an outstanding issue in the Go standard library's XML implementation. There are other XML libraries out there which you can use that do support namespaces. I've had pretty good success with https://github.com/beevik/etree.

Solution 2:[2]

Referencing your example, you need to include the XMLName element and pass the schema namespace and element name in the xml output params:

type Ext struct {
    XMLName         xml.Name `xml:"http://schemas.microsoft.com/office/spreadsheetml/2009/9/main ext"`
    XmlnsMx         string   `xml:"mx,attr,omitempty"`
    XmlnsX14        string   `xml:"x14,attr,omitempty"`
    URI             string   `xml:"uri,attr"`
    MxPLV           *MxPLV
    DataValidations *X14DataValidations
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kamil Kisiel
Solution 2 David W.