Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to apply successively two xpath expressions with libxml?
    primarykey
    data
    text
    <p>To sum up, i'm a totally beginner in libxml and I have to use an existing source code. The main idea is to apply a first xpath expression to extract a set of nodes from an xml file. Then, for each node, the second xpath expression shall be applied to extract some values.</p> <p>Existing source code is:</p> <pre><code>int xt_parseXmlResult(xmlDocPtr doc, const char *xpath, assoc_arrayc_t expr, arrayc_t *result) { xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); // Register namespaces ... /* * Evaluate main xpath expression */ xmlXPathObjectPtr xpathNodes = xmlXPathEvalExpression((xmlChar *)xpath, xpathCtx); /* * Now we apply the xpath expressions on each node returned by the first xpath request */ // First loop is on the XML document as we have to create a new context each // time we change the document int nbDocs = xpathNodes-&gt;nodesetval-&gt;nodeNr; for (row = 0; row &lt; nbDocs; row++) { xmlXPathContextPtr subCtx = xmlXPathNewContext(doc); // Register namespaces ... // Update context to use the nodeset related to this row subCtx-&gt;node = xpathNodes-&gt;nodesetval-&gt;nodeTab[row]; for (col = 0; col &lt; expr.nbItems; col++) { // Evaluate expression xpathRows = xmlXPathEvalExpression((xmlChar *)expr.itemList[col].val, subCtx); result-&gt;data[(row + 1) * result-&gt;nbCols + col] = strdup((char *)xmlXPathCastToString(xpathRows)); xmlXPathFreeObject(xpathRows); } xmlXPathFreeContext(subCtx); subCtx = NULL; } xmlFreeDoc(doc); xmlXPathFreeContext(xpathCtx); xmlXPathFreeObject(xpathNodes); return 0; } </code></pre> <p>I think that the problem comes from this line</p> <pre><code> // Update context to use the nodeset related to this row subCtx-&gt;node = xpathNodes-&gt;nodesetval-&gt;nodeTab[row]; </code></pre> <p>Because the second xpath expression is applied from the root of the xml file, not the root of each node.</p> <p>Any idea on how to do such thing?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload