Reputation: 859
I use xmlXPathEval to select some nodes from a ovf file(xml format), but failed.
The xml to parse is as below:
<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by VMware ovftool 3.0.2 (build-931074), UTC time: 2013-07-15T06:41:49.67628Z-->
<Envelope vmw:buildId="build-931074" xmlns="http://schemas.dmtf.org/ovf/envelope/1">
//children elements
</Envelope>
The selecting code is as below:
xmlXPathContextPtr context;
xmlXPathObjectPtr obj;
xmlNodeSetPtr nodeset=NULL;
int32 count=0;
//valid doc input
assert(doc != NULL);
//valid xpath
assert(xpath != NULL);
assert(nodeset_po != NULL);
context = xmlXPathNewContext(doc);·
assert (NULL != context);
obj = xmlXPathEval((xmlChar *)xpath, context);
assert (NULL != obj);
if (xmlXPathNodeSetIsEmpty(obj->nodesetval)) {
//The select nodes is always empty, even when I use "/Envelope" to select the root element
;;
} else {
nodeset = obj->nodesetval;
count = nodeset->nodeNr;
;;
}
The comments in the code details the failuer.
And to make it stranger, when I use another xml as the target file to parse, I can select all the nodes as expected.
So I am wondering if there is any requirements for xmlXPathEval to work properly?
Or am I missing something to get such confusing result?
Upvotes: 0
Views: 1004
Reputation: 101728
Looks like this is the old default namespace problem. The XPath /Envelope
will only select elements in the null namespace, but your element is in the namespace http://schemas.dmtf.org/ovf/envelope/1
.
I am not familiar with this particular library, but it looks like you need to declare the namespace and assign it a prefix in an xmlXPathContext
, then use that prefix in your XPath:
/p:Envelope
Upvotes: 1