Languages

Menu
Sites
Language
xpath issue with libxml
Hi, I am in write a xml parse demo app, I want to use xpath to query a specific element in a xml file. It's ok for a normal xml format file. But if the xml element has a namespace prefix, I can't get the result via xpath. example: xml snippet, 2015-06-02T09:19:29.535Z ... as above, the root element "Search" with a namespace prefix "ns2", my xpath is "/ns:Search/Response/View/ViewId", it's not work, I also try xpath as "/Search/Response/View/ViewId", not work. it's ok with a relative xpath "Response/View/ViewId". I guess it's a namespace related issue, I want to fix it, currently, I don't find a full guide about xpath syntax. anyone can help me? thanks a lot.

Responses

3 Replies
daniel kim

Hi,

This site has a Xpath tutorial and references. hope you can find useful information on it.

    http://www.zvon.org/xxl/XPathTutorial/General/examples.html

Regards,

Vikram

Hi,

Possible "xmlXPathRegisterNs()" can help you, please include the header file "#include <libxml/xpathInternals.h>" to check the details usage case. 

colin Rao

Hi Vkiram,

Thank you! It solved my issue. 

xmlChar *xpath = (xmlChar*)"/ns:Search/Response/View/ViewId";

xpathctx = xmlXPathNewContext(doc);
xmlXPathRegisterNs(xpathctx, "ns", "http://www.navteq.com/lbsp/Search-Search/4");

Also, I tested, the namespace prefix "ns" in xpath namespace register could be different with the original xml namespace prefix, just keep them same between xpath namespace register and xpath express,