For XML query structure, use 'operator' instead of 'binary'.
authorAdam Dickmeiss <adam@indexdata.dk>
Thu, 23 Feb 2006 11:17:25 +0000 (11:17 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Thu, 23 Feb 2006 11:17:25 +0000 (11:17 +0000)
For top-level of query, use <query><rpn>..</rpn></query> rather than
<query type="rpn">..</query> due to XML schemas and other unpleasant
things.

src/xmlquery.c
test/tstxmlquery.c

index 446dec6..3dd3007 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2005, Index Data ApS
  * All rights reserved.
  *
- * $Id: xmlquery.c,v 1.4 2006-02-19 18:44:23 adam Exp $
+ * $Id: xmlquery.c,v 1.5 2006-02-23 11:17:25 adam Exp $
  */
 
 /**
@@ -207,7 +207,7 @@ xmlNodePtr yaz_query2xml_rpnstructure(const Z_RPNStructure *zs,
     {
         Z_Complex *zc = zs->u.complex;
 
-        xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "binary", 0);
+        xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "operator", 0);
         if (zc->roperator)
             yaz_query2xml_operator(zc->roperator, node);
         yaz_query2xml_rpnstructure(zc->s1, node);
@@ -261,8 +261,7 @@ void yaz_rpnquery2xml(const Z_RPNQuery *rpn, void *docp_void)
 void yaz_query2xml(const Z_Query *q, void *docp_void)
 {
     xmlDocPtr *docp = (xmlDocPtr *) docp_void;
-    xmlNodePtr top_node, child_node = 0;
-    const char *type = 0;
+    xmlNodePtr top_node, q_node = 0, child_node = 0;
 
     assert(q);
     assert(docp);
@@ -273,31 +272,28 @@ void yaz_query2xml(const Z_Query *q, void *docp_void)
     {
     case Z_Query_type_1: 
     case Z_Query_type_101:
-       type = "rpn";
-       child_node = yaz_query2xml_rpn(q->u.type_1, top_node);
+        q_node = xmlNewChild(top_node, 0, BAD_CAST "rpn", 0);
+       child_node = yaz_query2xml_rpn(q->u.type_1, q_node);
         break;
     case Z_Query_type_2:
-       type = "ccl";
-       child_node = yaz_query2xml_ccl(q->u.type_2, top_node);
+        q_node = xmlNewChild(top_node, 0, BAD_CAST "ccl", 0);
+       child_node = yaz_query2xml_ccl(q->u.type_2, q_node);
         break;
     case Z_Query_type_100:
-       type = "z39.58";
-       child_node = yaz_query2xml_z3958(q->u.type_100, top_node);
+        q_node = xmlNewChild(top_node, 0, BAD_CAST "z39.58", 0);
+       child_node = yaz_query2xml_z3958(q->u.type_100, q_node);
         break;
     case Z_Query_type_104:
         if (q->u.type_104->which == Z_External_CQL)
        {
-           type = "cql";
-           child_node = yaz_query2xml_cql(q->u.type_104->u.cql, top_node);
+            q_node = xmlNewChild(top_node, 0, BAD_CAST "cql", 0);
+           child_node = yaz_query2xml_cql(q->u.type_104->u.cql, q_node);
        }
     }
-    if (child_node && type)
+    if (child_node && q_node)
     {
        *docp = xmlNewDoc(BAD_CAST "1.0");
        xmlDocSetRootElement(*docp, top_node); /* make it top node in doc */
-
-       /* set type attribute now */
-       xmlNewProp(top_node, BAD_CAST "type", BAD_CAST type);
     }
     else
     {
@@ -672,11 +668,11 @@ void yaz_xml2query_rpnstructure(const xmlNode *ptr, Z_RPNStructure **zs,
     if (!ptr || ptr->type != XML_ELEMENT_NODE)
     {
         *error_code = 1;
-        *addinfo = "missing rpn structure node";
+        *addinfo = "missing rpn operator, rset, apt node";
         return;
     }
     *zs = (Z_RPNStructure *) odr_malloc(odr, sizeof(Z_RPNStructure));
-    if (!xmlStrcmp(ptr->name, BAD_CAST "binary"))
+    if (!xmlStrcmp(ptr->name, BAD_CAST "operator"))
     {
         Z_Complex *zc = odr_malloc(odr, sizeof(Z_Complex));
         
@@ -741,8 +737,18 @@ static void yaz_xml2query_(const xmlNode *ptr, Z_Query **query, ODR odr,
     if (ptr && ptr->type == XML_ELEMENT_NODE && 
         !xmlStrcmp(ptr->name, BAD_CAST "query"))
     {
-        const char *type = (const char *)
-            xmlGetProp((xmlNodePtr) ptr, BAD_CAST "type");
+        const char *type;
+        ptr = ptr->children;
+        while (ptr && ptr->type != XML_ELEMENT_NODE)
+            ptr = ptr->next;
+        if (!ptr || ptr->type != XML_ELEMENT_NODE)
+        {
+            *error_code = 1;
+            *addinfo = "missing query content";
+            return;
+        }
+        type = (const char *) ptr->name;
+
         *query = (Z_Query*) odr_malloc(odr, sizeof(Z_Query));
         if (!type || !strcmp(type, "rpn"))
         {
index 7ed5477..aea6177 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2005, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: tstxmlquery.c,v 1.9 2006-02-19 18:44:23 adam Exp $
+ * $Id: tstxmlquery.c,v 1.10 2006-02-23 11:17:25 adam Exp $
  */
 
 #include <stdlib.h>
@@ -111,64 +111,64 @@ static void tst()
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@attr 1=4 computer", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
+                     "<query><rpn set=\"Bib-1\">"
                      "<apt><attr type=\"1\" value=\"4\"/>"
                      "<term type=\"general\">computer</term></apt>"
-                     "</query>\n",
+                     "</rpn></query>\n",
                      "RPN: @attrset Bib-1 @attr 1=4 computer"
                      ), XML_MATCH);
     
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@attr 2=1 @attr 1=title computer",
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
+                     "<query><rpn set=\"Bib-1\">"
                      "<apt><attr type=\"1\" value=\"title\"/>"
                      "<attr type=\"2\" value=\"1\"/>"
                      "<term type=\"general\">computer</term></apt>"
-                     "</query>\n",
+                     "</rpn></query>\n",
                      "RPN: @attrset Bib-1 @attr \"1=title\" @attr 2=1 computer"
                      ), XML_MATCH);
 
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@attr 2=1 @attr exp1 1=1 computer",
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
+                     "<query><rpn set=\"Bib-1\">"
                      "<apt><attr set=\"Exp-1\" type=\"1\" value=\"1\"/>"
                      "<attr type=\"2\" value=\"1\"/>"
                      "<term type=\"general\">computer</term></apt>"
-                     "</query>\n",
+                     "</rpn></query>\n",
                      "RPN: @attrset Bib-1 @attr Exp-1 1=1 @attr 2=1 computer"
                      ), XML_MATCH);
     
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@and a b", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
-                     "<binary type=\"and\">"
+                     "<query><rpn set=\"Bib-1\">"
+                     "<operator type=\"and\">"
                      "<apt><term type=\"general\">a</term></apt>"
                      "<apt><term type=\"general\">b</term></apt>"
-                     "</binary></query>\n",
+                     "</operator></rpn></query>\n",
                      "RPN: @attrset Bib-1 @and a b"
                      ), XML_MATCH);
     
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@or @and a b c", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
-                     "<binary type=\"or\">"
-                     "<binary type=\"and\">"
+                     "<query><rpn set=\"Bib-1\">"
+                     "<operator type=\"or\">"
+                     "<operator type=\"and\">"
                      "<apt><term type=\"general\">a</term></apt>"
-                     "<apt><term type=\"general\">b</term></apt></binary>"
+                     "<apt><term type=\"general\">b</term></apt></operator>"
                      "<apt><term type=\"general\">c</term></apt>"
-                     "</binary></query>\n",
+                     "</operator></rpn></query>\n",
                      "RPN: @attrset Bib-1 @or @and a b c"
                      ), XML_MATCH);
 
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@set abe", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
-                     "<rset>abe</rset></query>\n",
+                     "<query><rpn set=\"Bib-1\">"
+                     "<rset>abe</rset></rpn></query>\n",
                      "RPN: @attrset Bib-1 @set abe"
                      ), XML_MATCH);
 
@@ -177,56 +177,56 @@ static void tst()
                         knownunit, proxunit */
                      "@prox 0 3 1 2 k 2           a b", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
-                     "<binary type=\"prox\" exclusion=\"false\" "
+                     "<query><rpn set=\"Bib-1\">"
+                     "<operator type=\"prox\" exclusion=\"false\" "
                      "distance=\"3\" "
                      "ordered=\"true\" "
                      "relationType=\"2\" "
                      "knownProximityUnit=\"2\">"
                      "<apt><term type=\"general\">a</term></apt>"
                      "<apt><term type=\"general\">b</term></apt>"
-                     "</binary></query>\n",
+                     "</operator></rpn></query>\n",
                      "RPN: @attrset Bib-1 @prox 0 3 1 2 k 2 a b"
                      ), XML_MATCH);
 
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@term numeric 32", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
+                     "<query><rpn set=\"Bib-1\">"
                      "<apt>"
                      "<term type=\"numeric\">32</term></apt>"
-                     "</query>\n",
+                     "</rpn></query>\n",
                      "RPN: @attrset Bib-1 @term numeric 32"
                      ), XML_MATCH);
     
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@term string computer", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
+                     "<query><rpn set=\"Bib-1\">"
                      "<apt>"
                      "<term type=\"string\">computer</term></apt>"
-                     "</query>\n",
+                     "</rpn></query>\n",
                      "RPN: @attrset Bib-1 @term string computer"
                      ), XML_MATCH);
     
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@term null void", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
+                     "<query><rpn set=\"Bib-1\">"
                      "<apt>"
                      "<term type=\"null\"/></apt>"
-                     "</query>\n",
+                     "</rpn></query>\n",
                      "RPN: @attrset Bib-1 @term null x"
                      ), XML_MATCH);
 
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@attrset gils @attr 4=2 x", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"GILS\" type=\"rpn\">"
+                     "<query><rpn set=\"GILS\">"
                      "<apt>"
                      "<attr type=\"4\" value=\"2\"/>"
                      "<term type=\"general\">x</term></apt>"
-                     "</query>\n",
+                     "</rpn></query>\n",
                      "RPN: @attrset GILS @attr 4=2 x"
                      ), XML_MATCH);
 #endif