leethomason / tinyxml2

TinyXML2 is a simple, small, efficient, C++ XML parser that can be easily integrated into other programs.
zlib License
5.04k stars 1.83k forks source link

XML namespaces #755

Open MalcolmMcLean opened 5 years ago

MalcolmMcLean commented 5 years ago

I'm adding namespace support to TinyXML2.

The way I'm planning to do it is this: you have three new user-exposed functions, XMLDocument::RegisterNamespace() and XMLElemement::Prefix() and XMLElement::LocalName().

By registering a namespace, you associate your own prefix with that namespace. A call to XMLElement::Prefix() then gives you your own prefix back, instead of the prefix used by the document author. LocalName() is just a trivial function which strips the prefix from the element's id.

So essentially parser code will look like this

XMLElement *el = /* intialise me */
if(!strcmp(el->Prefix(), "jim") && !strcmp(el->localName(), "element"))
{
    /* Ok, we've hit an element in the namespace we registered with prefix "jim" */
}

Any comments anyone? Is this a good way to do it?

yuriy709 commented 4 years ago

You should consider that

<node xmlns:primer = "http://test/test"> 
  <primer:value>val</primer:value>
</node> 

and

<node xmlns:test = "http://test/test"> 
  <test:value>val</test:value>
</node> 

are equals ( namespace "http://test/test" + node name "value"). Prefixes are aliases to make documents shorter, you should check full namespace name.

I'd recommend to make perser code look like this:

XMLElement el = / intialise me */ if (el->equals("https://namespace/for/jim", "element")) { }