Proposal: Make it easier to define Top-Level Namespaces with prefix
Opened this issue · 0 comments
First of, works amazingly, and also generates exactly what the encoding/xml should have been doing from the beginning.
Currently to include top-level namespaces with a prefix you'd have to do the following:
type DidlLite struct {
XMLName xml.Name `xml:"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/ DIDL-Lite"`
UPNP string `xml:"http://www.w3.org/2000/xmlns/ xmlns:upnp,attr"`
DC string `xml:"http://www.w3.org/2000/xmlns/ xmlns:dc,attr"`
Items []Item `xml:"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/ item"`
}
DidlLite{
UPNP: "urn:schemas-upnp-org:metadata-1-0/upnp/",
DC: "http://purl.org/dc/elements/1.1/",
}
Which means that the struct is not useable without an initializer (NewDidlLite) function.
A small change could make defining top-level namespaces more user friendly, but would entail a bit of magic (i.e. implicit knowledge)
type DidlLite struct {
XMLName xml.Name `xml:"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/ DIDL-Lite"`
UPNP string `xml:"urn:schemas-upnp-org:metadata-1-0/upnp/ xmlns:upnp,attr"`
DC string `xml:"http://purl.org/dc/elements/1.1/ xmlns:dc,attr"`
Items []Item `xml:"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/ item"`
}
This would pre-encode the namespace for the prefix inside the xml tags.
The change in marshal.go, L845 could look like that:
if prefix == xmlnsPrefix && attr.Name.Space != xmlnsURL {
if prefix, prefixCreated := p.createPrefix(attr.Name.Space, local); prefixCreated {
p.writePrefixAttr(prefix, attr.Name.Space)
}
continue
}
So that it will only write out the prefix and not any additional attribute.
I understand that this functionality might be undesirable, but I landed on it intuitivly before figuring out how else it could be done.