简体   繁体   中英

Change namespace of node libxml2

I want to change namespace of a node in xml.

doc = novi_xml_getdoc(doc_name);
if(doc==NULL){
    return -1;
}

sprintf(buff, "//%s:capable-switch",ofprefix[ofconfig_version]);
node = xmlXPathEvalExpression(xpath, context)
if(node == NULL){
    return -1;
}
xmlNsPtr ns = xmlNewNs(node,"new-namespace", "prefix");
xmlSetNs(node, ns);

xmlSaveFormatFile (doc_name, doc, 1);
xmlFreeDoc(doc);

But this does not change the namespace of the node. The namespace remains same. I saw couple of examples, but all are related to changing namespace of childnode.

Moreover, I guess if we could modify the node by other way such as deleting and creating it again it will work. But not dont know how to link this node with its child nodes.

The node result of xmlXPathEvalExpression isn't directly the xmlNodePtr you're expecting, it's an xmlXPathObjectPtr .

http://www.xmlsoft.org/html/libxml-xpath.html#xmlXPathObject

You'll need to drill in to node->nodesetval->nodeTab[0] to get the first hit of the expression. It's also good to test node->nodesetval for NULL, check node->nodesetval->nodeNr for how many hits there are, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM