Is there a way in C++, using Tinyxml , TinyXpath , such that, a string containing :
<ns:abcd>
<ns:defg>
<ns:hijk>
</ns:hijk>
</ns:defg>
</ns:abcd>
transforms to
<abcd>
<defg>
<hijk>
</hijk>
</defg>
</abcd>
EDIT:
I was using Tinyxml and Tinyxpath.
My workflow was :
a) Create a dom-tree using TinyXML
b) Pass dom-tree to Tinyxpath for xpath evaluations
To add namespace removal, I used following function :
void RemoveAllNamespaces(TiXmlNode* node)
{
TiXmlElement* element = node->ToElement();
if(!element){
return;
}
std::string elementName = element->Value();
std::string::size_type idx = elementName.rfind(':');
if(idx != std::string::npos)
{
element->SetValue(elementName.substr( idx + 1).c_str());
}
TiXmlNode* child = element->IterateChildren(NULL);
while(child)
{
RemoveAllNamespaces(child);
child = element->IterateChildren(child);
}
}
So workflow changed to :
a) Create a dom-tree using TinyXML
b) Remove namespace from the domtree using RemoveAllNamespaces(domtree.Root() )
c) Pass modified-dom-tree to Tinyxpath for xpath evaluations
Ok, in response to the edited question, a few notes:
that doesn't actually treat the namespaces (consider
xmlns="http://blabla.com/uri"
style default namespaces), but that's actually a TinyXml limitation (eek):it doesn't treat attributes (which can also be qualified)
Here's what I'd do a quick & dirty (assumes TIXML_USE_STL as you were supposedly already using):
On my MSVC test it prints