Question

I'm attempting to implement the XPath transform from the XMLDSIG spec, and due to a few issues I won't go into, I can't use any additional libraries (e.g. xmlsec).

The spec states that given an XPath expression:

The XPath expression...is evaluated once for each node in the input node-set. The result is converted to a boolean. If the boolean is true, then the node is included in the output node-set. If the boolean is false, then the node is omitted from the output node-set.

I know how to parse in a document, create an XPath context object, evaluate an expression against the context from the document root, and unlink all elements not selected by the expression, but that isn't quite what the standard requires. How do I use the XPath context object to 'walk' the xmlDoc, evaluating an XPath expression at each node and selectively deleting the node?

Was it helpful?

Solution

I eventually found a solution. The context needs to be manually fiddled with to make it work. Here's some skeleton code, minus error checking:

result = xmlXPathEvalExpression(BAD_CAST "(//. | //@* | //namespace::*)", context);
...
compiled = xmlXPathCompile(BAD_CAST expression);
...
for (i = result->nodesetval->nodeNr - 1; i >= 0; i--)
{
  ...
  result->node = result->nodesetval->nodeTab[i];
  rc = xmlXPathCompiledEvalToBoolean(compiled, context);
  ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top