Open
Description
It's not uncommon for XML to contain QNames as element and attribute values, e.g.
<my-document xmlns:foo="http//..." >
<my-element>foo:bar</my-element>
</my-document>
In order to correctly unmarshal the value, you need to know the namespace bindings in effect for my-element, but Decoder doesn't appear to expose this information. A simple addition to encoding/xml of:
func (d *Decoder) NamespaceBindings() map[string]string {
return d.ns
}
allows unmarshallers to access the necessary information, for example, I can now write:
type QName struct {
Namespace string
Local string
}
func (qname *QName) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var s string
d.DecodeElement(&s, &start)
i := strings.Index(s, ":")
prefix := ""
if i >= 0 {
prefix = s[:i]
qname.Namespace = s[i+1:]
} else {
qname.Namespace = s
}
var ok bool
qname.Namespace, ok = d.NamespaceBindings()[prefix]
if !ok {
return errors.New("Unbound namespace prefix: " + prefix)
}
return nil
}
Arguably, something like the above, and a corresponding attribute unmarshaller could be provided on the standard xml.Name.
More discussion of this issue here:
https://groups.google.com/forum/#!searchin/golang-nuts/QName/golang-nuts/DexmVLQOJxk/whBaKK9ntHsJ
go version go1.5 darwin/amd64