Firstly, you can name the struct fields whatever you like, except it needs 
to start with a capital letter.  It doesn't have to match the XML element 
name at all.

Secondly, what you're looking at here is an XML *namespace* declaration.

The special attribute
xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx";
binds the local namespace prefix "edmx" to the full namespace URI "
http://docs.oasis-open.org/odata/ns/edmx";
and makes the local namespace prefix "edmx" available to this element and 
its children.

With the scope of this declaration, the element <edmx:Edmx> means "element 
name Edmx within namespace http://docs.oasis-open.org/odata/ns/edmx";

The namespace prefix is arbitrary and can be chosen by whoever generates 
the XML document.  The following XML is equivalent and needs to be parsed 
identically:

<womble:Edmx Version="4.0" xmlns:womble="
http://docs.oasis-open.org/odata/ns/edmx";>
</womble:Edmx>

You *could* pick up the xmlns attribute explicitly if you want, by matching 
the pseudo-namespace "xmlns" (see here 
<https://pkg.go.dev/golang.org/x/net/webdav/internal/xml#Name>):

type Metadata struct {
Version   string `xml:"Version,attr"`
Namespace string `xml:"xmlns edmx,attr"`
}

However, normally you don't want to do this, because whoever generates the 
XML is completely free to use whatever namespace prefix they like, e.g. 
"womble" instead of "edmx" as shown above.

What you should do instead is to get encoding/xml to match the actual 
namespace.  Here is an example:
https://play.golang.org/p/1fT_tDBhZJ_y

On Friday, 8 October 2021 at 16:09:50 UTC+1 RS wrote:

> I want to unmarshal an xml data to a data structure. 
> Problem is the following 
> This xml comes from a Odata Server and has fields "edmx:Edmx".
> How should the fields of struct be named? 
> I observe that the fields name of struct representing xml should be the 
> same name as xml field name. 
> Now how one can define edmx:Edmx from xml to a field that xml packet of 
> golang can parse it?
>
> <edmx:Edmx Version="4.0" xmlns:edmx="
> http://docs.oasis-open.org/odata/ns/edmx";>
> </edmx:Edmx>
>
> type Metadata struct {
> <???WHATFIELD NAME> xml.Name `xml:"edmx:Edmx"`
> <???WHAT FIELD NAME>    string `xml:"xmlns:edmx,attr"`
> Version string `xml:"Version,attr"` //this works 
> ....
>
> }
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/58df4016-ed97-47e5-a696-4a245af439bcn%40googlegroups.com.

Reply via email to