cat*_*eof 13 c xml xpath namespaces libxml2
我在这里粘贴一些代码,使用gcc file.c -lxml2编译没有警告,假设你的系统中安装了libxml2.
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <assert.h>
#include <libxml/tree.h>
#include <libxml/xpathInternals.h>
xmlDocPtr
getdoc (char *docname) {
    xmlDocPtr doc;
    doc = xmlParseFile(docname);
    if (doc == NULL ) {
        fprintf(stderr,"Document not parsed successfully. \n");
        return NULL;
    }
    return doc;
}
xmlXPathObjectPtr
getnodeset (xmlDocPtr doc, xmlChar *xpath){
    xmlXPathContextPtr context;
    xmlXPathObjectPtr result;
    context = xmlXPathNewContext(doc);
    if (context == NULL) {
        printf("Error in xmlXPathNewContext\n");
        return NULL;
    }
    if(xmlXPathRegisterNs(context,  BAD_CAST "new", BAD_CAST "http://www.example.com/new") != 0) {
        fprintf(stderr,"Error: unable to register NS with prefix");
        return NULL;
    }
    result = xmlXPathEvalExpression(xpath, context);
    xmlXPathFreeContext(context);
    if (result == NULL) {
        printf("Error in xmlXPathEvalExpression\n");
        return NULL;
    }
    if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
        xmlXPathFreeObject(result);
                printf("No result\n");
        return NULL;
    }
    return result;
}
int
main(int argc, char **argv) {
    char *docname;
    xmlDocPtr doc;
    xmlChar *xpath = (xmlChar*) "/new:book/section1";
    xmlNodeSetPtr nodeset;
    xmlXPathObjectPtr result;
    int i;
    xmlChar *keyword;
    if (argc <= 1) {
        printf("Usage: %s docname\n", argv[0]);
        return(0);
    }
    docname = argv[1];
    doc = getdoc(docname);
    result = getnodeset (doc, xpath);
    if (result) {
        nodeset = result->nodesetval;
        for (i=0; i < nodeset->nodeNr; i++) {
            keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);
        printf("keyword: %s\n", keyword);
        xmlFree(keyword);
        }
        xmlXPathFreeObject (result);
    }
    xmlFreeDoc(doc);
    xmlCleanupParser();
    return (1);
}
我的问题是我想解析以下的xml
<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://www.example.com/new">
    <section1>Sec_1</section1>
    <section2>Sec_2</section2>
</book>
book元素定义该元素内的命名空间.我想打印xpath/book/section1中的值,它返回NULL.当我试图在命名空间下返回元素时,我也会得到错误,即/ new:book/section1
我假设我的代码失败,因为我没有正确使用名称空间前缀.我没时间了.能否请你帮忙?
dma*_*o12 30
事实证明,正如我从这里发现的那样,它并不是libXml的失败,这是一个问题,因为libXml 正确遵循XML/XPATH规范.
但是,如果您可以控制要解析的xml文档,那么R Bourdeau提出的解决方案是正确的.
XPATH查询的上下文独立于xml文档中的命名空间限定符.默认命名空间强制所有子标记进入命名空间; 它们不需要在文档中进行限定,但必须在xpath查询中进行限定.幸运的是,您new使用libXml 注册了命名空间,因此cateof的解决方案应该可行.
xmlXPathRegisterNs(context,  BAD_CAST "new", BAD_CAST "http://www.example.com/new"
xmlChar *xpath = (xmlChar*) "/new:book/new:section1";
我在这里列出了xml的可见性:
<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://www.example.com/new">
    <section1>Sec_1</section1>
    <section2>Sec_2</section2>
</book>