libxml2 - How to apply successively two xpath expressions with libxml? -


To sum up, I'm beginning to fully libxml and I have to use an existing source code. The main idea is to apply the first xpath expression to extract a set of nodes from an xml file. Then, for each node, the second express expression will be applied to extract some values.

The current source code:

  int xt_parseXmlResult (xmlDocPtr doc, const char * xpath, Assoc_arrayc_t expr, arrayc_t * result) {xmlXPathContextPtr xpathCtx = xmlXPathNewContext (DOC); // Register namespace ... / * * Main Akspeth expression / evaluate xmlXPathObjectPtr xpathNodes = xmlXPathEvalExpression ((xmlChar *) xpath, xpathCtx); / * Now we apply xpath expressions on each node returned by the first xpath request * / // first loop of XML document because we each // time to create a new context, we document Int nbDocs = xpathNodes- & gt ; Nodesetval-> NodeNr; {XmlXPathContextPtr subCtx = xmlXPathNewContext (doc) for (line = 0; row & lt; nbDocs; line ++); // register namespace ... // this line subCtx-> node = xpathNodes-> nodesetval-> Update reference to use nodeset related to nodeTab [row]; For (col = 0; col & lt; expr.nbItems; col ++) {// evaluate expression xpathRows = xmlxpathEvalExpression ((xmlChar *) expr.itemList [Cola] .val, subCtx); Results-> Data [(line + 1) * result-> NbCols + col] = strdup ((* four *) xmlxpathCastToString (xpathRows)); XmlXPathFreeObject (xpathRows); } XmlXPathFreeContext (subCtx); Sub CTX = faucet; } XmlFreeDoc (doctor); XmlXPathFreeContext (xpathCtx); XmlXPathFreeObject (xpathNodes); Return 0; }  

The problem I think is to use comes from the line

  // Nodset associated with this line subCtx-> node = xpathNodes Update reference - & gt; Nodesetval-> NodeTab [line];  

Because the second xpath expression is applied to the root of the XML file, not the root of each node.

How to do such a thing

Some sample codes according to your needs and language Modify it is C #, but it should be fairly similar. Note that the second xpath is not starting with "/" and is using an example of the one returned from the first one. Any end of the xpath "/" Not in

  XmlDocument doc = new XmlDocument (); Doc.Load (docfile); XmlNodeList item = doc.SelectNodes ("/ part1 / part2"); Foreign objects (items in items) {XMLNode x = items. Selection node ("Part 3"); // Dostuff}  

Comments