[xml] XPath problem



Hello,
I have got problem with XPath.
When following program is run "./program hello.xml, ( hello.xml is under
source code of program)
it doesn't show node <marek>
when I delete node <epp> with all its content, program works properly
I suppose that it is sth wrong with content of <epp>
Maybe someone knows why.
Thanks for help.


#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <libxml/xpath.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("Cos jest nie tak");
     getchar();
     return NULL;
     }

 result = xmlXPathEvalExpression(xpath,context);
 double w = xmlXPathCastNodeSetToNumber(result->nodesetval);
 printf("Ilosc znalezionych: %d\n",w);getchar();
 if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
                printf("No result\n");
  return NULL;
 }

 xmlXPathFreeContext(context);

 return result;
}
int
main(int argc, char **argv) {

 char *docname;
 xmlDocPtr doc;
 xmlChar *xpath = ("//marek");
 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 version="1.0" encoding="UTF-8" standalone="no" ?>
    <epp xmlns="urn:iana:xml:ns:epp-1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="urn:iana:xml:ns:epp-1.0 epp-1.0.xsd">
  <hello />
<marek>something</marek>
</epp>



 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]