go-xml icon indicating copy to clipboard operation
go-xml copied to clipboard

Feature/add namespace

Open carolinasolfernandez opened this issue 5 years ago • 1 comments

New config for allowing to add a namespace tag to struct Issue #12

carolinasolfernandez avatar Jun 05 '20 15:06 carolinasolfernandez

So if I understand, this creates an xmlns:<QNAME> attribute, then hard-codes QNAME into the name of each xml element in the struct. It's a clever way to work around the lack of qname support in the encoding/xml marshaller.

The provided example works great, and the result can be marshalled back into the expected XML. I have a few concerns, though:

  • If XmlNS is not populated, and you Marshal the type, it will not have a namespace defined.

  • The example is a little simple and I don't know if this feature would work with a more complex schema. Imagine a bad schema that uses different prefixes for the same target NS in different parts of the schema:

Or what if the targetNamespace does not have a prefix defined in any xmlns attributes in the source schema? It's not strictly required. I tried removing it from your example and this is the result:

type Book struct {
	XMLNs  string `xml:"xmlns:,attr,omitempty"`
	Title  string `xml:":title"`
	Author string `xml:":author"`
}

type Library struct {
	XMLNs string `xml:"xmlns:,attr,omitempty"`
	Book  string `xml:":book"`
}

droyo avatar Jun 09 '20 23:06 droyo