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

Namespaces keys order

Open eduardocp opened this issue 4 years ago • 7 comments

Why namespaces key and value has the key as the uri of namespace instead of the real key on xml?

Ex.:

var namespaces = <String, String>{
    'soap' : 'http://www.w3.org/2003/05/soap-envelope/',
};
var builder = new xml.XmlBuilder();
builder.processing('xml', 'version="1.0"');
builder.element('soap:Envelop', namespaces: namespaces, nest: () { });

var result = builder.build();

produces:

<?xml version="1.0"?><soap:Envelop xmlns:http://schemas.xmlsoap.org/soap/envelope/="soap"/>

eduardocp avatar Oct 22 '19 15:10 eduardocp

You need to define the namespaces like so:

  final namespaces = {
    'http://www.w3.org/2003/05/soap-envelope/': 'soap' ,
  };

See the documentation.

renggli avatar Oct 23 '19 18:10 renggli

Yes, I saw the documentation. Maybe I expressed myself badly. I wanted to understand why this is so, since it is more intuitive (in my opinion) that the key is alias not uri.

eduardocp avatar Oct 23 '19 20:10 eduardocp

I see, this is not very obvious. I am trying to remember why the key-values are ordered like this? Other implementations also seem to do it this way, i.e. http://effbot.org/zone/element-namespaces.htm. Probably this has to do with the underlying implementation, but maybe there could be better ways of exposing this to users?

renggli avatar Oct 24 '19 08:10 renggli

I think the main reason is that a namespace is defined by a uri, therefore it seems a good idea to make it a key.

A namespace is not required to have an alias (prefix). Still points to a key with a null value.

But a namespace can have zero or more aliases (not zero or one alias).

Making the alias the key would solve the or more problem, but not the zero case.

Since it is very unlikely that a single element would use multiple aliases for a certain namespace, having the namespace as a key makes more sense.

I think instead of using a `Map<String,String?>', a separate type would have been a better option.

rspilker avatar Jan 06 '22 15:01 rspilker

Hmm, the signature is not Map<String,String?) but Map<String,String>. However, you could always call void namespace(String uri, [String? prefix]) instead.

rspilker avatar Jan 06 '22 15:01 rspilker

Yeah, the signature should be Map<String, String?> for consistency.

renggli avatar Jan 06 '22 16:01 renggli

Fixed this with f460492c7501ac525ebfdf96456eebdf82f28a12.

renggli avatar Jan 06 '22 17:01 renggli