[xml] [PATCH] Add xmlXPathSetContextNode and xmlXPathSetContextNode



This patch adds xmlXPathSetContextNode and xmlXPathSetContextNode,
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>
---
 doc/libxml2-api.xml    |    2 ++
 include/libxml/xpath.h |    7 +++++++
 xpath.c                |   32 ++++++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 0 deletions(-)

diff --git a/doc/libxml2-api.xml b/doc/libxml2-api.xml
index 0e9b075..d5213ef 100644
--- a/doc/libxml2-api.xml
+++ b/doc/libxml2-api.xml
@@ -3613,6 +3613,8 @@
      <exports symbol='xmlXPathObjectCopy' type='function'/>
      <exports symbol='xmlXPathFreeNodeSetList' type='function'/>
      <exports symbol='xmlXPathEval' type='function'/>
+     <exports symbol='xmlXPathNodeEval' type='function'/>
+     <exports symbol='xmlXPathSetContextNode' type='function'/>
      <exports symbol='xmlXPathCastNodeSetToString' type='function'/>
      <exports symbol='xmlXPathCompiledEval' type='function'/>
      <exports symbol='xmlXPathEvalExpression' type='function'/>
diff --git a/include/libxml/xpath.h b/include/libxml/xpath.h
index 8819a29..c67fbf9 100644
--- a/include/libxml/xpath.h
+++ b/include/libxml/xpath.h
@@ -508,6 +508,13 @@ XMLPUBFUN int XMLCALL
  */
 XMLPUBFUN long XMLCALL
                    xmlXPathOrderDocElems       (xmlDocPtr doc);
+XMLPUBFUN void 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);
diff --git a/xpath.c b/xpath.c
index c5b6aeb..d76f7c4 100644
--- a/xpath.c
+++ b/xpath.c
@@ -15079,6 +15079,38 @@ 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.
+ */
+void
+xmlXPathSetContextNode(xmlNodePtr node, xmlXPathContextPtr ctx) {
+    if (node->doc == ctx->doc)
+        ctx->node = node;
+}
+
+/**
+ * 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) {
+    xmlXPathSetContextNode(node, ctx);
+    return(xmlXPathEval(str, ctx));
+}
+
+/**
  * xmlXPathEvalExpression:
  * @str:  the XPath expression
  * @ctxt:  the XPath context
-- 
1.7.4.1



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]