libxml2가 시스템에 설치되었다고 가정하고 gcc file.c -lxml2를 사용하여 경고없이 컴파일하는 일부 코드를 여기 붙여 넣습니다.libxml2에서 네임 스페이스 및 xpath 오류가 발생했습니다.
#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>
책 요소가 요소 내에 네임 스페이스를 정의를 구문 분석 할 것입니다. 나는 xpath/book/section1에 값을 출력하려고하고 NULL을 반환한다. 요소를 네임 스페이스 아래로 반환하려고하면 오류가 발생합니다. 즉,/new : book/section1
네임 스페이스 접두사를 올바르게 사용하지 않아서 코드가 실패했다고 가정합니다. 나는 시간이 없어. 도와 주실 수 있겠습니까?
이 가능 말할 수 있나요'일부 네임 스페이스를 통해 그것을 반복하지 않도록하기 위해/문서의 모든 요소에 대한 암시 적 기본값임을 libxml' 및 XPath 쿼리에서 끝났습니까? – SasQ