[xml] patch: registering namespaces to XPath context in xmllint shell



Daniel Veillard wrote:

On Tue, Jun 08, 2004 at 09:25:57AM -0500, Eric Haszlakiewicz wrote:

The xmllint shell does need someway to specify prefix<->namespace mappings. It not very useful without that.


I take patches Based on the existing code xmlShell* in debugXML.c and the example on how to register a namespace to a context in http://xmlsoft.org/examples/index.html#xpath1.c that should not be hard,

Here is a try at following that hint :-)
It adds a new "setns" command that requires one parameter in the prefix=[nsuri] format.
ciao
ste

cvs -z3 diff -ub -r HEAD
Index: debugXML.c
===================================================================
RCS file: /cvs/gnome/libxml2/debugXML.c,v
retrieving revision 1.88
diff -u -b -r1.88 debugXML.c
--- a/debugXML.c        25 Mar 2004 09:35:48 -0000      1.88
+++ b/debugXML.c        15 Jun 2004 23:43:33 -0000
@@ -1561,6 +1561,66 @@
}
#endif

+#ifdef LIBXML_XPATH_ENABLED
+/**
+ * xmlShellRegisterNamespace:
+ * @ctxt:  the shell context
+ * @arg:  a string in prefix=nsuri format
+ * @node:  unused
+ * @node2:  unused
+ *
+ * Implements the XML shell function "setns"
+ * register/unregister a prefix=namespace pair
+ * on the XPath context
+ *
+ * Returns 0 on success and a negative value otherwise.
+ */
+static int
+xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
+      xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
+{
+    xmlChar* nsListDup;
+    xmlChar* prefix;
+    xmlChar* href;
+    xmlChar* next;
+
+    nsListDup = xmlStrdup((xmlChar *) arg);
+    next = nsListDup;
+    while(next != NULL) {
+       /* skip spaces */
+       /*while((*next) == ' ') next++;*/
+       if((*next) == '\0') break;
+
+       /* find prefix */
+       prefix = next;
+       next = (xmlChar*)xmlStrchr(next, '=');
+       if(next == NULL) {
+           fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
+           xmlFree(nsListDup);
+           return(-1);
+       }
+       *(next++) = '\0';
+
+       /* find href */
+       href = next;
+       next = (xmlChar*)xmlStrchr(next, ' ');
+       if(next != NULL) {
+           *(next++) = '\0';
+       }
+
+       /* do register namespace */
+       if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
+           fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, 
href);
+           xmlFree(nsListDup);
+           return(-1);
+       }
+    }
+
+    xmlFree(nsListDup);
+    return(0);
+}
+#endif
+
/**
 * xmlShellGrep:
 * @ctxt:  the shell context
@@ -2246,6 +2306,8 @@
                  fprintf(ctxt->output, "\tls [path]    list contents of path or the current directory\n");
#ifdef LIBXML_XPATH_ENABLED
                  fprintf(ctxt->output, "\txpath expr   evaluate the XPath expression in that context and print 
the result\n");
+                 fprintf(ctxt->output, "\tsetns nsreg  register a namespace to a prefix in the XPath evaluation 
context\n");
+                 fprintf(ctxt->output, "\t             format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a 
prefix)\n");
#endif /* LIBXML_XPATH_ENABLED */
                  fprintf(ctxt->output, "\tpwd          display current working directory\n");
                  fprintf(ctxt->output, "\tquit         leave shell\n");
@@ -2297,6 +2359,13 @@
        } else if (!strcmp(command, "base")) {
            xmlShellBase(ctxt, NULL, ctxt->node, NULL);
#ifdef LIBXML_XPATH_ENABLED
+        } else if (!strcmp(command, "setns")) {
+            if (arg[0] == 0) {
+               xmlGenericError(xmlGenericErrorContext,
+                               "setns: prefix=[nsuri] required\n");
+            } else {
+                xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
+            }
        } else if (!strcmp(command, "xpath")) {
            if (arg[0] == 0) {
                xmlGenericError(xmlGenericErrorContext,







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