1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-10-28 23:14:57 +03:00

Add xmlXPathSetContextNode and xmlXPathNodeEval

This patch adds xmlXPathSetContextNode and xmlXPathNodeEval,
which make it easier to evaluation XPath expressions with a
context node other than the document root without poking about
inside the internals of the context.

This patch is compile-tested only, and is my first libxml2
contribution, so please go easy.

Signed-off-by: Alex Bligh <alex@alex.org.uk>
This commit is contained in:
Alex Bligh
2013-03-23 17:23:27 +00:00
committed by Daniel Veillard
parent 87f3287d9b
commit 28876afb4e
2 changed files with 50 additions and 0 deletions

View File

@@ -508,6 +508,13 @@ XMLPUBFUN int XMLCALL
*/
XMLPUBFUN long XMLCALL
xmlXPathOrderDocElems (xmlDocPtr doc);
XMLPUBFUN int XMLCALL
xmlXPathSetContextNode (xmlNodePtr node,
xmlXPathContextPtr ctx);
XMLPUBFUN xmlXPathObjectPtr XMLCALL
xmlXPathNodeEval (xmlNodePtr node,
const xmlChar *str,
xmlXPathContextPtr ctx);
XMLPUBFUN xmlXPathObjectPtr XMLCALL
xmlXPathEval (const xmlChar *str,
xmlXPathContextPtr ctx);

43
xpath.c
View File

@@ -15078,6 +15078,49 @@ xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) {
return(res);
}
/**
* xmlXPathSetContextNode:
* @node: the node to to use as the context node
* @ctx: the XPath context
*
* Sets 'node' as the context node. The node must be in the same
* document as that associated with the context.
*
* Returns -1 in case of error or 0 if successful
*/
int
xmlXPathSetContextNode(xmlNodePtr node, xmlXPathContextPtr ctx) {
if ((node == NULL) || (ctx == NULL))
return(-1);
if (node->doc == ctx->doc) {
ctx->node = node;
return(0);
}
return(-1);
}
/**
* xmlXPathNodeEval:
* @node: the node to to use as the context node
* @str: the XPath expression
* @ctx: the XPath context
*
* Evaluate the XPath Location Path in the given context. The node 'node'
* is set as the context node. The context node is not restored.
*
* Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
* the caller has to free the object.
*/
xmlXPathObjectPtr
xmlXPathNodeEval(xmlNodePtr node, const xmlChar *str, xmlXPathContextPtr ctx) {
if (str == NULL)
return(NULL);
if (xmlXPathSetContextNode(node, ctx) < 0)
return(NULL);
return(xmlXPathEval(str, ctx));
}
/**
* xmlXPathEvalExpression:
* @str: the XPath expression