Skip to content

Proposal: Make it easier to define Top-Level Namespaces with prefix #9

@thoro

Description

@thoro

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions