New yaz_xml_get_prop utility YAZ-839
[yaz-moved-to-github.git] / src / record_conv.c
index e6e1cdd..b117340 100644 (file)
@@ -20,6 +20,7 @@
 #include <yaz/nmem.h>
 #include <yaz/tpath.h>
 #include <yaz/z-opac.h>
+#include <yaz/xml_get.h>
 
 #if YAZ_HAVE_XML2
 #include <libxml/parser.h>
@@ -331,26 +332,23 @@ static void *construct_select(const xmlNode *ptr,
         return 0;
     else
     {
-        struct _xmlAttr *attr;
         NMEM nmem = nmem_create();
         struct select_info *info = nmem_malloc(nmem, sizeof(*info));
+        const char *attr_str;
+        const char *xpath = 0;
 
         info->nmem = nmem;
         info->xpath_expr = 0;
-        for (attr = ptr->properties; attr; attr = attr->next)
+        attr_str = yaz_xml_get_prop(ptr, "path%s", &xpath);
+        if (attr_str)
         {
-            if (!xmlStrcmp(attr->name, BAD_CAST "path") &&
-                attr->children && attr->children->type == XML_TEXT_NODE)
-                info->xpath_expr =
-                    nmem_strdup(nmem, (const char *) attr->children->content);
-            else
-            {
-                wrbuf_printf(wr_error, "Bad attribute '%s'"
-                             "Expected xpath.", attr->name);
-                nmem_destroy(nmem);
+            wrbuf_printf(wr_error, "Bad attribute '%s'"
+                         "Expected xpath.", attr_str);
+            nmem_destroy(nmem);
                 return 0;
-            }
         }
+        if (xpath)
+            info->xpath_expr = nmem_strdup(nmem, xpath);
         return info;
     }
 }
@@ -378,17 +376,18 @@ static int convert_select(void *vinfo, WRBUF record, WRBUF wr_error)
             if (xpathObj)
             {
                 xmlNodeSetPtr nodes = xpathObj->nodesetval;
-                wrbuf_rewind(record);
                 if (nodes)
                 {
                     int i;
+                    if (nodes->nodeNr > 0)
+                        wrbuf_rewind(record);
                     for (i = 0; i < nodes->nodeNr; i++)
                     {
                         xmlNode *ptr = nodes->nodeTab[i];
                         if (ptr->type == XML_ELEMENT_NODE)
                             ptr = ptr->children;
-                        if (ptr->type == XML_TEXT_NODE)
-                            for (; ptr; ptr = ptr->next)
+                        for (; ptr; ptr = ptr->next)
+                            if (ptr->type == XML_TEXT_NODE)
                                 wrbuf_puts(record, (const char *) ptr->content);
                     }
                 }