summaryrefslogtreecommitdiff
path: root/xpath.c
diff options
context:
space:
mode:
authorAlex Bligh <alex@alex.org.uk>2013-03-23 17:23:27 +0000
committerDaniel Veillard <veillard@redhat.com>2013-04-01 16:42:12 +0800
commit28876afb4e67f9f4292fc2a7134f6b97f0618b7d (patch)
treebf457c449d736ae5031dcabf026c311109380688 /xpath.c
parent87f3287d9b00bdc39be032a8a3f2d46860eaf787 (diff)
downloadlibxml2-28876afb4e67f9f4292fc2a7134f6b97f0618b7d.tar.gz
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>
Diffstat (limited to 'xpath.c')
-rw-r--r--xpath.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/xpath.c b/xpath.c
index c5b6aeb7..97410e7b 100644
--- a/xpath.c
+++ b/xpath.c
@@ -15079,6 +15079,49 @@ xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) {
}
/**
+ * 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
* @ctxt: the XPath context