I wrote a C++ XPath parser with the libxml++ library, which was built on the C libxml2 library. It works great when the xmlns is not present in xml but it breaks when that namespace is added.
Sample xml:
<A xmlns="http://some.url/something">
<B>
<C>hello world</C>
<B>
</a>
Sample XPath:
string xpath = "/A/B/C" // returns nothing when xmlns is present in the XML
I found this answer and tried adjusting my XPath to the following, which does work but it makes the XPath kind of obnoxious to read and write.
string xpath = "/*[name()='A']/*[name()='B']/*[name()='C']"
Ideally I want to register the namespace so I can use normal XPaths. I've also searched through the libxml++ documentation and found a Node.set_namespace but it just causes an exception when I try to use it.
root_node->set_namespace("http://some.url/something");
// exception: The namespace (http://some.url/something) has not been declared.
However, the root_node is definitely aware of the namespace when it parses the XML document:
cout << "namespace uri: " << root_node->get_namespace_uri();
// namespace uri: http://some.url/something
At this point I am out of ideas so help is greatly appreciated.
EDIT Also tried:
Element *root_node = parser->get_document()->get_root_node();
root_node->set_namespace_declaration("http://some.url/something","x");
cout << "namespace uri: " << root_node->get_namespace_uri() << endl;
cout << "namespace prefix: " << root_node->get_namespace_prefix() << endl;
// namespace uri: http://some.url/something
// namespace prefix:
Does not complain but doesn't appear to register the namespace.