go-xml
go-xml copied to clipboard
Feature/add namespace
New config for allowing to add a namespace tag to struct Issue #12
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"`
}