【问题标题】:libxml2 error with namespaces and xpath名称空间和 xpath 的 libxml2 错误
【发布时间】:2011-03-09 06:56:40
【问题描述】:

我在这里粘贴一些代码,使用 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

我假设我的代码失败是因为我没有正确使用命名空间前缀。我没时间了。你能帮忙吗?

【问题讨论】:

    标签: c xml xpath namespaces libxml2


    【解决方案1】:

    结果,正如我从here 发现的那样, 这并不是 libXml 的真正失败,而是一个问题,因为 libXml 正确 遵循 XML/XPATH 规范。

    R Bourdeau 提出的解决方案是正确的,但是,如果您可以控制正在解析的 xml 文档。

    XPATH 查询的上下文独立于 xml 文档中的命名空间限定符。默认命名空间强制所有子标签进入一个命名空间;它们不需要在文档中进行限定,但必须在 xpath 查询中进行限定。幸运的是,您使用 libXml 将命名空间注册为 new,因此 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>
    

    【讨论】:

    • 这是与 XPath 和命名空间相关的第一个答案,它实际上解释了发生了什么以及如何解决它。我真诚地感谢你,我的朋友。
    【解决方案2】:

    这是一个令人讨厌的 libXml 库故障。正如 cateof 所指出的,问题在于默认命名空间声明:

    xmlns="http://www.example.com/new"

    两种选择:
    (1)去掉你的书标签中的那个声明 要么 (2) 给它一个名字,并在你的标签中使用这个名字。

    例如

    xmlns:new="http://www.example.com/new"

    那么你的标签都是这样的:

    新:书 新:第 1 节

    等等。

    【讨论】:

    • 是否可以告诉libxml 某个命名空间对于文档中的所有元素都是默认/隐式的,以避免在 XPath 查询中一遍又一遍地重复它?
    【解决方案3】:

    这是默认命名空间的问题。要匹配您需要 /new:tag/new:tag 的路径 等等

    【讨论】:

      猜你喜欢
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多