Deal with resultCountPrecision - SRU 2.0
[yaz-moved-to-github.git] / src / srw.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file srw.c
7  * \brief Implements SRW/SRU package encoding and decoding
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <stdlib.h>
14
15 #include <yaz/srw.h>
16 #include <yaz/wrbuf.h>
17 #if YAZ_HAVE_XML2
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
20 #include <assert.h>
21
22 #include "sru-p.h"
23
24 static void add_XML_n(xmlNodePtr ptr, const char *elem, char *val, int len,
25                       xmlNsPtr ns_ptr)
26 {
27     if (val)
28     {
29         xmlDocPtr doc = xmlParseMemory(val,len);
30         if (doc)
31         {
32             xmlNodePtr c = xmlNewChild(ptr, ns_ptr, BAD_CAST elem, 0);
33             xmlNodePtr t = xmlDocGetRootElement(doc);
34             xmlAddChild(c, xmlCopyNode(t,1));
35             xmlFreeDoc(doc);
36         }
37     }
38 }
39
40 xmlNodePtr add_xsd_string_n(xmlNodePtr ptr, const char *elem, const char *val,
41                             int len)
42 {
43     if (val)
44     {
45         xmlNodePtr c = xmlNewChild(ptr, 0, BAD_CAST elem, 0);
46         xmlNodePtr t = xmlNewTextLen(BAD_CAST val, len);
47         xmlAddChild(c, t);
48         return t;
49     }
50     return 0;
51 }
52
53 xmlNodePtr add_xsd_string_ns(xmlNodePtr ptr, const char *elem, const char *val,
54                              xmlNsPtr ns_ptr)
55 {
56     if (val)
57     {
58         xmlNodePtr c = xmlNewChild(ptr, ns_ptr, BAD_CAST elem, 0);
59         xmlNodePtr t = xmlNewText(BAD_CAST val);
60         xmlAddChild(c, t);
61         return t;
62     }
63     return 0;
64 }
65
66 xmlNodePtr add_xsd_string(xmlNodePtr ptr, const char *elem, const char *val)
67 {
68     return add_xsd_string_ns(ptr, elem, val, 0);
69 }
70
71 static void add_xsd_integer(xmlNodePtr ptr, const char *elem,
72                             const Odr_int *val)
73 {
74     if (val)
75     {
76         char str[40];
77         sprintf(str, ODR_INT_PRINTF, *val);
78         xmlNewTextChild(ptr, 0, BAD_CAST elem, BAD_CAST str);
79     }
80 }
81
82 static int match_element(xmlNodePtr ptr, const char *elem)
83 {
84     if (ptr->type == XML_ELEMENT_NODE && !xmlStrcmp(ptr->name, BAD_CAST elem))
85     {
86         return 1;
87     }
88     return 0;
89 }
90
91 #define CHECK_TYPE 0
92
93 static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
94                               char **val, int *len)
95 {
96 #if CHECK_TYPE
97     struct _xmlAttr *attr;
98 #endif
99     if (!match_element(ptr, elem))
100         return 0;
101 #if CHECK_TYPE
102     for (attr = ptr->properties; attr; attr = attr->next)
103         if (!strcmp(attr->name, "type") &&
104             attr->children && attr->children->type == XML_TEXT_NODE)
105         {
106             const char *t = strchr(attr->children->content, ':');
107             if (t)
108                 t = t + 1;
109             else
110                 t = attr->children->content;
111             if (!strcmp(t, "string"))
112                 break;
113         }
114     if (!attr)
115         return 0;
116 #endif
117     ptr = ptr->children;
118     if (!ptr || ptr->type != XML_TEXT_NODE)
119     {
120         *val = "";
121         return 1;
122     }
123     *val = odr_strdup(o, (const char *) ptr->content);
124     if (len)
125         *len = xmlStrlen(ptr->content);
126     return 1;
127 }
128
129
130 static int match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o,
131                             char **val)
132 {
133     return match_xsd_string_n(ptr, elem, o, val, 0);
134 }
135
136 static int match_xsd_XML_n2(xmlNodePtr ptr, const char *elem, ODR o,
137                             char **val, int *len, int fixup_root)
138 {
139     xmlBufferPtr buf;
140     int no_root_nodes = 0;
141
142     if (!match_element(ptr, elem))
143         return 0;
144
145     buf = xmlBufferCreate();
146
147     /* Copy each element nodes at top.
148        In most cases there is only one root node.. At least one server
149        http://www.theeuropeanlibrary.org/sru/sru.pl
150        has multiple root nodes in recordData.
151     */
152     for (ptr = ptr->children; ptr; ptr = ptr->next)
153     {
154         if (ptr->type == XML_ELEMENT_NODE)
155         {
156             /* copy node to get NS right (bug #740). */
157             xmlNode *tmp = xmlCopyNode(ptr, 1);
158
159             xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
160
161             xmlFreeNode(tmp);
162             no_root_nodes++;
163         }
164     }
165     if (no_root_nodes != 1 && fixup_root)
166     {
167         /* does not appear to be an XML document. Make it so */
168         xmlBufferAddHead(buf, (const xmlChar *) "<yaz_record>", -1);
169         xmlBufferAdd(buf, (const xmlChar *) "</yaz_record>", -1);
170     }
171     *val = (char *) odr_malloc(o, buf->use + 1);
172     memcpy(*val, buf->content, buf->use);
173     (*val)[buf->use] = '\0';
174
175     if (len)
176         *len = buf->use;
177
178     xmlBufferFree(buf);
179
180     return 1;
181 }
182
183 static int match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o,
184                            char **val, int *len)
185 {
186     return match_xsd_XML_n2(ptr, elem, o, val, len, 0);
187 }
188
189 static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o,
190                              Odr_int **val)
191 {
192 #if CHECK_TYPE
193     struct _xmlAttr *attr;
194 #endif
195     if (!match_element(ptr, elem))
196         return 0;
197 #if CHECK_TYPE
198     for (attr = ptr->properties; attr; attr = attr->next)
199         if (!strcmp(attr->name, "type") &&
200             attr->children && attr->children->type == XML_TEXT_NODE)
201         {
202             const char *t = strchr(attr->children->content, ':');
203             if (t)
204                 t = t + 1;
205             else
206                 t = attr->children->content;
207             if (!strcmp(t, "integer"))
208                 break;
209         }
210     if (!attr)
211         return 0;
212 #endif
213     ptr = ptr->children;
214     if (!ptr || ptr->type != XML_TEXT_NODE)
215         return 0;
216     *val = odr_intdup(o, odr_atoi((const char *) ptr->content));
217     return 1;
218 }
219
220 char *yaz_negotiate_sru_version(char *input_ver)
221 {
222     if (!input_ver)
223         return "2.0";
224     if (!strcmp(input_ver, "1.1"))
225         return "1.1";
226     if (!strncmp(input_ver, "1.", 2))
227         return "1.2";
228     if (!strncmp(input_ver, "2.", 2))
229         return "2.0";
230     return 0;
231 }
232
233 static int yaz_srw_record(ODR o, xmlNodePtr pptr, Z_SRW_record *rec,
234                           Z_SRW_extra_record **extra,
235                           void *client_data, int version2)
236 {
237     if (o->direction == ODR_DECODE)
238     {
239         Z_SRW_extra_record ex;
240
241         char *spack = 0;
242         xmlNodePtr ptr;
243 #ifdef Z_SRW_packed
244         rec->packing = 0;
245 #endif
246         rec->recordSchema = 0;
247         rec->recordData_buf = 0;
248         rec->recordData_len = 0;
249         rec->recordPosition = 0;
250         *extra = 0;
251
252         ex.extraRecordData_buf = 0;
253         ex.extraRecordData_len = 0;
254         ex.recordIdentifier = 0;
255
256         for (ptr = pptr->children; ptr; ptr = ptr->next)
257         {
258
259             if (match_xsd_string(ptr, "recordSchema", o,
260                                  &rec->recordSchema))
261                 ;
262             else if (match_xsd_string(ptr, "recordPacking", o, &spack))
263                 ;  /* can't rely on it: in SRU 2.0 it's different semantics */
264             else if (match_xsd_integer(ptr, "recordPosition", o,
265                                        &rec->recordPosition))
266                 ;
267             else if (match_element(ptr, "recordData"))
268             {
269                 /* we assume XML packing, if any element nodes exist below
270                    recordData. Unfortunately, in SRU 2.0 recordPacking
271                    means something different */
272                 xmlNode *p = ptr->children;
273                 for (; p; p = p->next)
274                     if (p->type == XML_ELEMENT_NODE)
275                         break;
276                 if (p)
277                 {
278                     match_xsd_XML_n2(
279                         ptr, "recordData", o,
280                         &rec->recordData_buf, &rec->recordData_len, 1);
281                     rec->recordPacking = Z_SRW_recordPacking_XML;
282                 }
283                 else
284                 {
285                     match_xsd_string_n(
286                         ptr, "recordData", o,
287                         &rec->recordData_buf, &rec->recordData_len);
288                     rec->recordPacking = Z_SRW_recordPacking_string;
289                 }
290             }
291             else if (match_xsd_XML_n(ptr, "extraRecordData", o,
292                                      &ex.extraRecordData_buf,
293                                      &ex.extraRecordData_len) )
294                 ;
295             else
296                 match_xsd_string(ptr, "recordIdentifier", o,
297                                  &ex.recordIdentifier);
298         }
299         if (ex.extraRecordData_buf || ex.recordIdentifier)
300         {
301             *extra = (Z_SRW_extra_record *)
302                 odr_malloc(o, sizeof(Z_SRW_extra_record));
303             memcpy(*extra, &ex, sizeof(Z_SRW_extra_record));
304         }
305     }
306     else if (o->direction == ODR_ENCODE)
307     {
308         xmlNodePtr ptr = pptr;
309         int pack = rec->recordPacking;
310         const char *spack = yaz_srw_pack_to_str(pack);
311
312         add_xsd_string(ptr, "recordSchema", rec->recordSchema);
313         if (spack)
314         {
315             if (version2)
316                 add_xsd_string(ptr, "recordXMLEscaping", spack);
317             else
318                 add_xsd_string(ptr, "recordPacking", spack);
319         }
320         switch (pack)
321         {
322         case Z_SRW_recordPacking_string:
323             add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
324                              rec->recordData_len);
325             break;
326         case Z_SRW_recordPacking_XML:
327             add_XML_n(ptr, "recordData", rec->recordData_buf,
328                       rec->recordData_len, 0);
329             break;
330         case Z_SRW_recordPacking_URL:
331             add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
332                              rec->recordData_len);
333             break;
334         }
335         if (rec->recordPosition)
336             add_xsd_integer(ptr, "recordPosition", rec->recordPosition );
337         if (extra && *extra)
338         {
339             if ((*extra)->recordIdentifier)
340                 add_xsd_string(ptr, "recordIdentifier",
341                                (*extra)->recordIdentifier);
342             if ((*extra)->extraRecordData_buf)
343                 add_XML_n(ptr, "extraRecordData",
344                           (*extra)->extraRecordData_buf,
345                           (*extra)->extraRecordData_len, 0);
346         }
347     }
348     return 0;
349 }
350
351 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
352                            Z_SRW_extra_record ***extra,
353                            int *num, void *client_data, int version2)
354 {
355     if (o->direction == ODR_DECODE)
356     {
357         int i;
358         xmlNodePtr ptr;
359         *num = 0;
360         for (ptr = pptr->children; ptr; ptr = ptr->next)
361         {
362             if (ptr->type == XML_ELEMENT_NODE &&
363                 !xmlStrcmp(ptr->name, BAD_CAST "record"))
364                 (*num)++;
365         }
366         if (!*num)
367             return 1;
368         *recs = (Z_SRW_record *) odr_malloc(o, *num * sizeof(**recs));
369         *extra = (Z_SRW_extra_record **) odr_malloc(o, *num * sizeof(**extra));
370         for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
371         {
372             if (ptr->type == XML_ELEMENT_NODE &&
373                 !xmlStrcmp(ptr->name, BAD_CAST "record"))
374             {
375                 yaz_srw_record(o, ptr, *recs + i, *extra + i, client_data, 0);
376                 i++;
377             }
378         }
379     }
380     else if (o->direction == ODR_ENCODE)
381     {
382         int i;
383         for (i = 0; i < *num; i++)
384         {
385             xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "record",
386                                           0);
387             yaz_srw_record(o, rptr, (*recs)+i, (*extra ? *extra + i : 0),
388                            client_data, version2);
389         }
390     }
391     return 0;
392 }
393
394 static int yaz_srw_version(ODR o, xmlNodePtr pptr, Z_SRW_recordVersion *rec,
395                            void *client_data, const char *ns)
396 {
397     if (o->direction == ODR_DECODE)
398     {
399         xmlNodePtr ptr;
400         rec->versionType = 0;
401         rec->versionValue = 0;
402         for (ptr = pptr->children; ptr; ptr = ptr->next)
403         {
404
405             if (match_xsd_string(ptr, "versionType", o,
406                                  &rec->versionType))
407                 ;
408             else
409                 match_xsd_string(ptr, "versionValue", o, &rec->versionValue);
410         }
411     }
412     else if (o->direction == ODR_ENCODE)
413     {
414         xmlNodePtr ptr = pptr;
415         add_xsd_string(ptr, "versionType", rec->versionType);
416         add_xsd_string(ptr, "versionValue", rec->versionValue);
417     }
418     return 0;
419 }
420
421 static int yaz_srw_versions(ODR o, xmlNodePtr pptr,
422                             Z_SRW_recordVersion **vers,
423                             int *num, void *client_data, const char *ns)
424 {
425     if (o->direction == ODR_DECODE)
426     {
427         int i;
428         xmlNodePtr ptr;
429         *num = 0;
430         for (ptr = pptr->children; ptr; ptr = ptr->next)
431         {
432             if (ptr->type == XML_ELEMENT_NODE &&
433                 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
434                 (*num)++;
435         }
436         if (!*num)
437             return 1;
438         *vers = (Z_SRW_recordVersion *) odr_malloc(o, *num * sizeof(**vers));
439         for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
440         {
441             if (ptr->type == XML_ELEMENT_NODE &&
442                 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
443             {
444                 yaz_srw_version(o, ptr, *vers + i, client_data, ns);
445                 i++;
446             }
447         }
448     }
449     else if (o->direction == ODR_ENCODE)
450     {
451         int i;
452         for (i = 0; i < *num; i++)
453         {
454             xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "version",
455                                           0);
456             yaz_srw_version(o, rptr, (*vers)+i, client_data, ns);
457         }
458     }
459     return 0;
460 }
461
462 Z_FacetTerm *yaz_sru_proxy_get_facet_term_count(ODR odr, xmlNodePtr node)
463 {
464     Odr_int freq;
465     xmlNodePtr child;
466     WRBUF wrbuf = wrbuf_alloc();
467     Z_FacetTerm *facet_term;
468     const char *freq_string = yaz_element_attribute_value_get(
469         node, "facetvalue", "est_representation");
470     if (freq_string)
471         freq = odr_atoi(freq_string);
472     else
473         freq = -1;
474
475     for (child = node->children; child ; child = child->next)
476     {
477         if (child->type == XML_TEXT_NODE)
478             wrbuf_puts(wrbuf, (const char *) child->content);
479     }
480     facet_term = facet_term_create_cstr(odr, wrbuf_cstr(wrbuf), freq);
481     wrbuf_destroy(wrbuf);
482     return facet_term;
483 }
484
485 static Z_FacetField *yaz_sru_proxy_decode_facet_field(ODR odr, xmlNodePtr ptr)
486 {
487     Z_AttributeList *list;
488     Z_FacetField *facet_field;
489     int num_terms = 0;
490     int index = 0;
491     xmlNodePtr node;
492     /* USE attribute */
493     const char* name = yaz_element_attribute_value_get(ptr, "facet", "code");
494     yaz_log(YLOG_DEBUG, "sru-proxy facet type: %s", name);
495
496     list = yaz_use_attribute_create(odr, name);
497     for (node = ptr->children; node; node = node->next) {
498         if (match_element(node, "facetvalue"))
499             num_terms++;
500     }
501     facet_field = facet_field_create(odr, list, num_terms);
502     index = 0;
503     for (node = ptr->children; node; node = node->next)
504     {
505         if (match_element(node, "facetvalue"))
506         {
507             facet_field_term_set(odr, facet_field,
508                                  yaz_sru_proxy_get_facet_term_count(odr, node),
509                                  index);
510             index++;
511         }
512     }
513     return facet_field;
514 }
515
516 static int yaz_sru_proxy_decode_facets(ODR o, xmlNodePtr root,
517                                        Z_FacetList **facetList)
518 {
519     xmlNodePtr ptr;
520
521     for (ptr = root->children; ptr; ptr = ptr->next)
522     {
523         if (match_element(ptr, "facets"))
524         {
525             xmlNodePtr node;
526             Z_FacetList *facet_list;
527             int num_facets = 0;
528             for (node = ptr->children; node; node= node->next)
529             {
530                 if (node->type == XML_ELEMENT_NODE)
531                     num_facets++;
532             }
533             facet_list = facet_list_create(o, num_facets);
534             num_facets = 0;
535             for (node = ptr->children; node; node= node->next)
536             {
537                 if (match_element(node, "facet"))
538                 {
539                     facet_list_field_set(
540                         o, facet_list,
541                         yaz_sru_proxy_decode_facet_field(o, node), num_facets);
542                     num_facets++;
543                 }
544             }
545             *facetList = facet_list;
546             break;
547         }
548     }
549     return 0;
550 }
551
552
553
554 static int yaz_srw_decode_diagnostics(ODR o, xmlNodePtr pptr,
555                                       Z_SRW_diagnostic **recs, int *num,
556                                       void *client_data, const char *ns)
557 {
558     int i;
559     xmlNodePtr ptr;
560     *num = 0;
561     for (ptr = pptr; ptr; ptr = ptr->next)
562     {
563         if (ptr->type == XML_ELEMENT_NODE &&
564             !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
565             (*num)++;
566     }
567     if (!*num)
568         return 1;
569     *recs = (Z_SRW_diagnostic *) odr_malloc(o, *num * sizeof(**recs));
570     for (i = 0; i < *num; i++)
571     {
572         (*recs)[i].uri = 0;
573         (*recs)[i].details = 0;
574         (*recs)[i].message = 0;
575     }
576     for (i = 0, ptr = pptr; ptr; ptr = ptr->next)
577     {
578         if (ptr->type == XML_ELEMENT_NODE &&
579             !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
580         {
581             xmlNodePtr rptr;
582             (*recs)[i].uri = 0;
583             (*recs)[i].details = 0;
584             (*recs)[i].message = 0;
585             for (rptr = ptr->children; rptr; rptr = rptr->next)
586             {
587                 if (match_xsd_string(rptr, "uri", o,
588                                      &(*recs)[i].uri))
589                     ;
590                 else if (match_xsd_string(rptr, "details", o,
591                                           &(*recs)[i].details))
592                     ;
593                 else
594                     match_xsd_string(rptr, "message", o, &(*recs)[i].message);
595             }
596             i++;
597         }
598     }
599     return 0;
600 }
601
602 int sru_decode_surrogate_diagnostics(const char *buf, size_t len,
603                                      Z_SRW_diagnostic **diag,
604                                      int *num, ODR odr)
605 {
606     int ret = 0;
607     xmlDocPtr doc = xmlParseMemory(buf, len);
608     if (doc)
609     {
610         xmlNodePtr ptr = xmlDocGetRootElement(doc);
611         while (ptr && ptr->type != XML_ELEMENT_NODE)
612             ptr = ptr->next;
613         if (ptr && ptr->ns
614             && !xmlStrcmp(ptr->ns->href,
615                           BAD_CAST "http://www.loc.gov/zing/srw/diagnostic/"))
616         {
617             ret = yaz_srw_decode_diagnostics(odr, ptr, diag, num, 0, 0);
618         }
619         xmlFreeDoc(doc);
620     }
621     return ret;
622 }
623
624 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
625                                int *num, void *client_data, const char *ns)
626 {
627     if (o->direction == ODR_DECODE)
628     {
629         return yaz_srw_decode_diagnostics(o, pptr->children, recs, num, client_data, ns);
630     }
631     else if (o->direction == ODR_ENCODE)
632     {
633         int i;
634         xmlNsPtr ns_diag =
635             xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1, BAD_CAST "diag" );
636         for (i = 0; i < *num; i++)
637         {
638             const char *std_diag = "info:srw/diagnostic/1/";
639             const char *ucp_diag = "info:srw/diagnostic/12/";
640             xmlNodePtr rptr = xmlNewChild(pptr, ns_diag,
641                                           BAD_CAST "diagnostic", 0);
642             add_xsd_string(rptr, "uri", (*recs)[i].uri);
643             if ((*recs)[i].message)
644                 add_xsd_string(rptr, "message", (*recs)[i].message);
645             else if ((*recs)[i].uri )
646             {
647                 if (!strncmp((*recs)[i].uri, std_diag, strlen(std_diag)))
648                 {
649                     int no = atoi((*recs)[i].uri + strlen(std_diag));
650                     const char *message = yaz_diag_srw_str(no);
651                     if (message)
652                         add_xsd_string(rptr, "message", message);
653                 }
654                 else if (!strncmp((*recs)[i].uri, ucp_diag, strlen(ucp_diag)))
655                 {
656                     int no = atoi((*recs)[i].uri + strlen(ucp_diag));
657                     const char *message = yaz_diag_sru_update_str(no);
658                     if (message)
659                         add_xsd_string(rptr, "message", message);
660                 }
661             }
662             add_xsd_string(rptr, "details", (*recs)[i].details);
663         }
664     }
665     return 0;
666 }
667
668 static int yaz_srw_term(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm *term,
669                         void *client_data, const char *ns)
670 {
671     if (o->direction == ODR_DECODE)
672     {
673         xmlNodePtr ptr;
674         term->value = 0;
675         term->numberOfRecords = 0;
676         term->displayTerm = 0;
677         term->whereInList = 0;
678         for (ptr = pptr->children; ptr; ptr = ptr->next)
679         {
680             if (match_xsd_string(ptr, "value", o,  &term->value))
681                 ;
682             else if (match_xsd_integer(ptr, "numberOfRecords", o,
683                                        &term->numberOfRecords))
684                 ;
685             else if (match_xsd_string(ptr, "displayTerm", o,
686                                       &term->displayTerm))
687                 ;
688             else
689                 match_xsd_string(ptr, "whereInList", o, &term->whereInList);
690         }
691     }
692     else if (o->direction == ODR_ENCODE)
693     {
694         xmlNodePtr ptr = pptr;
695         add_xsd_string(ptr, "value", term->value);
696         add_xsd_integer(ptr, "numberOfRecords", term->numberOfRecords);
697         add_xsd_string(ptr, "displayTerm", term->displayTerm);
698         add_xsd_string(ptr, "whereInList", term->whereInList);
699     }
700     return 0;
701 }
702
703 static int yaz_srw_terms(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm **terms,
704                          int *num, void *client_data, const char *ns)
705 {
706     if (o->direction == ODR_DECODE)
707     {
708         int i;
709         xmlNodePtr ptr;
710         *num = 0;
711         for (ptr = pptr->children; ptr; ptr = ptr->next)
712         {
713             if (ptr->type == XML_ELEMENT_NODE &&
714                 !xmlStrcmp(ptr->name, BAD_CAST "term"))
715                 (*num)++;
716         }
717         if (!*num)
718             return 1;
719         *terms = (Z_SRW_scanTerm *) odr_malloc(o, *num * sizeof(**terms));
720         for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
721         {
722             if (ptr->type == XML_ELEMENT_NODE &&
723                 !xmlStrcmp(ptr->name, BAD_CAST "term"))
724                 yaz_srw_term(o, ptr, (*terms)+i, client_data, ns);
725         }
726     }
727     else if (o->direction == ODR_ENCODE)
728     {
729         int i;
730         for (i = 0; i < *num; i++)
731         {
732             xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "term", 0);
733             yaz_srw_term(o, rptr, (*terms)+i, client_data, ns);
734         }
735     }
736     return 0;
737 }
738
739 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
740                   void *client_data, const char *ns)
741 {
742     xmlNodePtr pptr = (xmlNodePtr) vptr;
743     if (o->direction == ODR_DECODE)
744     {
745         Z_SRW_PDU **p = handler_data;
746         xmlNodePtr method = pptr->children;
747         char *neg_version;
748
749         while (method && method->type == XML_TEXT_NODE)
750             method = method->next;
751
752         if (!method)
753             return -1;
754         if (method->type != XML_ELEMENT_NODE)
755             return -1;
756
757         *p = yaz_srw_get_core_v_2_0(o);
758
759         if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveRequest"))
760         {
761             xmlNodePtr ptr = method->children;
762             Z_SRW_searchRetrieveRequest *req;
763             char *recordPacking = 0;
764             char *recordXMLEscaping = 0;
765
766             (*p)->which = Z_SRW_searchRetrieve_request;
767             req = (*p)->u.request = (Z_SRW_searchRetrieveRequest *)
768                 odr_malloc(o, sizeof(*req));
769             req->queryType = "cql";
770             req->query = 0;
771             req->sort_type = Z_SRW_sort_type_none;
772             req->sort.none = 0;
773             req->startRecord = 0;
774             req->maximumRecords = 0;
775             req->recordSchema = 0;
776             req->recordPacking = 0;
777             req->packing = 0;
778             req->recordXPath = 0;
779             req->resultSetTTL = 0;
780             req->stylesheet = 0;
781             req->database = 0;
782
783             for (; ptr; ptr = ptr->next)
784             {
785                 if (match_xsd_string(ptr, "version", o,
786                                      &(*p)->srw_version))
787                     ;
788                 else if (match_xsd_string(ptr, "queryType", o,
789                                           &req->queryType))
790                     ;
791                 else if (match_xsd_string(ptr, "query", o,
792                                           &req->query))
793                     ;
794                 else if (match_xsd_string(ptr, "pQuery", o,
795                                           &req->query))
796                     req->queryType = "pqf";
797                 else if (match_xsd_string(ptr, "xQuery", o,
798                                           &req->query))
799                     req->queryType = "xcql";
800                 else if (match_xsd_integer(ptr, "startRecord", o,
801                                            &req->startRecord))
802                     ;
803                 else if (match_xsd_integer(ptr, "maximumRecords", o,
804                                            &req->maximumRecords))
805                     ;
806                 else if (match_xsd_string(ptr, "recordPacking", o,
807                                           &recordPacking))
808                     ;
809                 else if (match_xsd_string(ptr, "recordXMLEscaping", o,
810                                           &recordXMLEscaping))
811                     ;
812                 else if (match_xsd_string(ptr, "recordSchema", o,
813                                           &req->recordSchema))
814                     ;
815                 else if (match_xsd_string(ptr, "recordXPath", o,
816                                           &req->recordXPath))
817                     ;
818                 else if (match_xsd_integer(ptr, "resultSetTTL", o,
819                                            &req->resultSetTTL))
820                     ;
821                 else if (match_xsd_string(ptr, "sortKeys", o,
822                                           &req->sort.sortKeys))
823                     req->sort_type = Z_SRW_sort_type_sort;
824                 else if (match_xsd_string(ptr, "stylesheet", o,
825                                           &req->stylesheet))
826                     ;
827                 else
828                     match_xsd_string(ptr, "database", o, &req->database);
829             }
830             if (!req->query)
831             {
832                 /* should put proper diagnostic here */
833                 return -1;
834             }
835             if (!strcmp((*p)->srw_version, "2.0"))
836             {
837                 req->recordPacking = recordXMLEscaping;
838                 req->packing = recordPacking;
839             }
840             else
841             {
842                 req->recordPacking = recordPacking;
843             }
844         }
845         else if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveResponse"))
846         {
847             xmlNodePtr ptr = method->children;
848             Z_SRW_searchRetrieveResponse *res;
849
850             (*p)->which = Z_SRW_searchRetrieve_response;
851             res = (*p)->u.response = (Z_SRW_searchRetrieveResponse *)
852                 odr_malloc(o, sizeof(*res));
853
854             res->numberOfRecords = 0;
855             res->resultCountPrecision = 0;
856             res->resultSetId = 0;
857             res->resultSetIdleTime = 0;
858             res->records = 0;
859             res->num_records = 0;
860             res->diagnostics = 0;
861             res->num_diagnostics = 0;
862             res->nextRecordPosition = 0;
863             res->facetList = 0;
864             res->suggestions = 0;
865
866             for (; ptr; ptr = ptr->next)
867             {
868                 if (match_xsd_string(ptr, "version", o,
869                                      &(*p)->srw_version))
870                     ;
871                 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
872                                          &(*p)->extraResponseData_buf,
873                                          &(*p)->extraResponseData_len))
874                     ;
875                 else if (match_xsd_integer(ptr, "numberOfRecords", o,
876                                            &res->numberOfRecords))
877                     ;
878                 else if (match_xsd_string(ptr, "resultCountPrecision", o,
879                                            &res->resultCountPrecision))
880                     ;
881                 else if (match_xsd_string(ptr, "resultSetId", o,
882                                           &res->resultSetId))
883                     ;
884                 else if (match_xsd_integer(ptr, "resultSetIdleTime", o,
885                                            &res->resultSetIdleTime))
886                     ;
887                 else if (match_xsd_integer(ptr, "resultSetTTL", o,
888                                            &res->resultSetIdleTime))
889                     ;
890                 else if (match_element(ptr, "records"))
891                     yaz_srw_records(o, ptr, &res->records,
892                                     &res->extra_records,
893                                     &res->num_records, client_data, 0);
894                 else if (match_xsd_integer(ptr, "nextRecordPosition", o,
895                                            &res->nextRecordPosition))
896                     ;
897                 else if (match_element(ptr, "diagnostics"))
898                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
899                                         &res->num_diagnostics,
900                                         client_data, ns);
901                 else if (match_element(ptr, "facet_analysis"))
902                     yaz_sru_proxy_decode_facets(o, ptr, &res->facetList);
903             }
904         }
905         else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest"))
906         {
907             Z_SRW_explainRequest *req;
908             xmlNodePtr ptr = method->children;
909
910             (*p)->which = Z_SRW_explain_request;
911             req = (*p)->u.explain_request = (Z_SRW_explainRequest *)
912                 odr_malloc(o, sizeof(*req));
913             req->recordPacking = 0;
914             req->packing = 0;
915             req->database = 0;
916             req->stylesheet = 0;
917             for (; ptr; ptr = ptr->next)
918             {
919                 if (match_xsd_string(ptr, "version", o,
920                                      &(*p)->srw_version))
921                     ;
922                 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
923                                          &(*p)->extraResponseData_buf,
924                                          &(*p)->extraResponseData_len))
925                     ;
926                 else if (match_xsd_string(ptr, "stylesheet", o,
927                                           &req->stylesheet))
928                     ;
929                 else if (match_xsd_string(ptr, "recordPacking", o,
930                                           &req->recordPacking))
931                     ;
932                 else
933                     match_xsd_string(ptr, "database", o, &req->database);
934             }
935         }
936         else if (!xmlStrcmp(method->name, BAD_CAST "explainResponse"))
937         {
938             Z_SRW_explainResponse *res;
939             xmlNodePtr ptr = method->children;
940
941             (*p)->which = Z_SRW_explain_response;
942             res = (*p)->u.explain_response = (Z_SRW_explainResponse*)
943                 odr_malloc(o, sizeof(*res));
944             res->diagnostics = 0;
945             res->num_diagnostics = 0;
946             res->record.recordSchema = 0;
947             res->record.recordData_buf = 0;
948             res->record.recordData_len = 0;
949             res->record.recordPosition = 0;
950
951             for (; ptr; ptr = ptr->next)
952             {
953                 if (match_xsd_string(ptr, "version", o,
954                                      &(*p)->srw_version))
955                     ;
956                 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
957                                          &(*p)->extraResponseData_buf,
958                                          &(*p)->extraResponseData_len))
959                     ;
960                 else if (match_element(ptr, "record"))
961                     yaz_srw_record(o, ptr, &res->record, &res->extra_record,
962                                    client_data, 0);
963                 else if (match_element(ptr, "diagnostics"))
964                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
965                                         &res->num_diagnostics,
966                                         client_data, ns);
967                 ;
968             }
969         }
970         else if (!xmlStrcmp(method->name, BAD_CAST "scanRequest"))
971         {
972             Z_SRW_scanRequest *req;
973             xmlNodePtr ptr = method->children;
974
975             (*p)->which = Z_SRW_scan_request;
976             req = (*p)->u.scan_request = (Z_SRW_scanRequest *)
977                 odr_malloc(o, sizeof(*req));
978             req->queryType = "cql";
979             req->scanClause = 0;
980             req->responsePosition = 0;
981             req->maximumTerms = 0;
982             req->stylesheet = 0;
983             req->database = 0;
984
985             for (; ptr; ptr = ptr->next)
986             {
987                 if (match_xsd_string(ptr, "version", o,
988                                      &(*p)->srw_version))
989                     ;
990                 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
991                                          &(*p)->extraResponseData_buf,
992                                          &(*p)->extraResponseData_len))
993                     ;
994                 else if (match_xsd_string(ptr, "scanClause", o,
995                                           &req->scanClause))
996                     ;
997                 else if (match_xsd_string(ptr, "pScanClause", o,
998                                           &req->scanClause))
999                 {
1000                     req->queryType = "pqf";
1001                 }
1002                 else if (match_xsd_integer(ptr, "responsePosition", o,
1003                                            &req->responsePosition))
1004                     ;
1005                 else if (match_xsd_integer(ptr, "maximumTerms", o,
1006                                            &req->maximumTerms))
1007                     ;
1008                 else if (match_xsd_string(ptr, "stylesheet", o,
1009                                           &req->stylesheet))
1010                     ;
1011                 else
1012                     match_xsd_string(ptr, "database", o, &req->database);
1013             }
1014         }
1015         else if (!xmlStrcmp(method->name, BAD_CAST "scanResponse"))
1016         {
1017             Z_SRW_scanResponse *res;
1018             xmlNodePtr ptr = method->children;
1019
1020             (*p)->which = Z_SRW_scan_response;
1021             res = (*p)->u.scan_response = (Z_SRW_scanResponse *)
1022                 odr_malloc(o, sizeof(*res));
1023             res->terms = 0;
1024             res->num_terms = 0;
1025             res->diagnostics = 0;
1026             res->num_diagnostics = 0;
1027
1028             for (; ptr; ptr = ptr->next)
1029             {
1030                 if (match_xsd_string(ptr, "version", o,
1031                                      &(*p)->srw_version))
1032                     ;
1033                 else if (match_xsd_XML_n(ptr, "extraResponseData", o,
1034                                          &(*p)->extraResponseData_buf,
1035                                          &(*p)->extraResponseData_len))
1036                     ;
1037                 else if (match_element(ptr, "terms"))
1038                     yaz_srw_terms(o, ptr, &res->terms,
1039                                   &res->num_terms, client_data,
1040                                   ns);
1041                 else if (match_element(ptr, "diagnostics"))
1042                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1043                                         &res->num_diagnostics,
1044                                         client_data, ns);
1045             }
1046         }
1047         else
1048         {
1049             *p = 0;
1050             return -1;
1051         }
1052         neg_version = yaz_negotiate_sru_version((*p)->srw_version);
1053         if (neg_version)
1054             (*p)->srw_version = neg_version;
1055     }
1056     else if (o->direction == ODR_ENCODE)
1057     {
1058         Z_SRW_PDU **p = handler_data;
1059         xmlNsPtr ns_srw;
1060         xmlNodePtr ptr = 0;
1061         int version2 = !(*p)->srw_version ||
1062             strcmp((*p)->srw_version, "2.") > 0;
1063         if ((*p)->which == Z_SRW_searchRetrieve_request)
1064         {
1065             Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
1066             const char *queryType = req->queryType;
1067             if (version2)
1068                 ns = "http://docs.oasis-open.org/ns/search-ws/sruRequest";
1069             ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveRequest", 0);
1070             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1071             xmlSetNs(ptr, ns_srw);
1072
1073             if (!version2)
1074                 add_xsd_string(ptr, "version", (*p)->srw_version);
1075
1076             if (version2)
1077             {
1078                 if (queryType)
1079                     add_xsd_string(ptr, "queryType", queryType);
1080                 add_xsd_string(ptr, "query", req->query);
1081             }
1082             else
1083             {
1084                 if (!queryType || !strcmp(queryType, "cql"))
1085                     add_xsd_string(ptr, "query", req->query);
1086                 else if (!strcmp(queryType, "xcql"))
1087                     add_xsd_string(ptr, "xQuery", req->query);
1088                 else if (!strcmp(queryType, "pqf"))
1089                     add_xsd_string(ptr, "pQuery", req->query);
1090             }
1091             add_xsd_integer(ptr, "startRecord", req->startRecord);
1092             add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
1093             if (version2)
1094             {
1095                 add_xsd_string(ptr, "recordXMLEscaping", req->recordPacking);
1096                 add_xsd_string(ptr, "recordPacking", req->packing);
1097             }
1098             else
1099                 add_xsd_string(ptr, "recordPacking", req->recordPacking);
1100             add_xsd_string(ptr, "recordSchema", req->recordSchema);
1101             add_xsd_string(ptr, "recordXPath", req->recordXPath);
1102             add_xsd_integer(ptr, "resultSetTTL", req->resultSetTTL);
1103             switch (req->sort_type)
1104             {
1105             case Z_SRW_sort_type_none:
1106                 break;
1107             case Z_SRW_sort_type_sort:
1108                 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
1109                 break;
1110             case Z_SRW_sort_type_xSort:
1111                 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
1112                 break;
1113             }
1114             add_xsd_string(ptr, "stylesheet", req->stylesheet);
1115             add_xsd_string(ptr, "database", req->database);
1116         }
1117         else if ((*p)->which == Z_SRW_searchRetrieve_response)
1118         {
1119             Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
1120             if (version2)
1121                 ns = "http://docs.oasis-open.org/ns/search-ws/sruResponse";
1122             ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveResponse", 0);
1123             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1124             xmlSetNs(ptr, ns_srw);
1125
1126             if (!version2)
1127                 add_xsd_string(ptr, "version", (*p)->srw_version);
1128             add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
1129             add_xsd_string(ptr, "resultSetId", res->resultSetId);
1130             add_xsd_integer(ptr,
1131                             version2 ? "resultSetTTL" : "resultSetIdleTime" ,
1132                             res->resultSetIdleTime);
1133             if (res->num_records)
1134             {
1135                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "records", 0);
1136                 yaz_srw_records(o, rptr, &res->records, &res->extra_records,
1137                                 &res->num_records,
1138                                 client_data, version2);
1139             }
1140             add_xsd_integer(ptr, "nextRecordPosition",
1141                             res->nextRecordPosition);
1142             if (res->num_diagnostics)
1143             {
1144                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1145                                               0);
1146                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1147                                     &res->num_diagnostics, client_data, ns);
1148             }
1149             if (res->resultCountPrecision)
1150                 add_xsd_string(ptr, "resultCountPrecision",
1151                                res->resultCountPrecision);
1152         }
1153         else if ((*p)->which == Z_SRW_explain_request)
1154         {
1155             Z_SRW_explainRequest *req = (*p)->u.explain_request;
1156             ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest", 0);
1157             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1158             xmlSetNs(ptr, ns_srw);
1159
1160             if (!version2)
1161                 add_xsd_string(ptr, "version", (*p)->srw_version);
1162             if (version2)
1163             {
1164                 add_xsd_string(ptr, "recordXMLEscaping", req->recordPacking);
1165                 add_xsd_string(ptr, "recordPacking", req->packing);
1166             }
1167             else
1168                 add_xsd_string(ptr, "recordPacking", req->recordPacking);
1169             add_xsd_string(ptr, "stylesheet", req->stylesheet);
1170             add_xsd_string(ptr, "database", req->database);
1171         }
1172         else if ((*p)->which == Z_SRW_explain_response)
1173         {
1174             Z_SRW_explainResponse *res = (*p)->u.explain_response;
1175             ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse", 0);
1176             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1177             xmlSetNs(ptr, ns_srw);
1178
1179             add_xsd_string(ptr, "version", (*p)->srw_version);
1180             if (1)
1181             {
1182                 xmlNodePtr ptr1 = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1183                 yaz_srw_record(o, ptr1, &res->record, &res->extra_record,
1184                                client_data, version2);
1185             }
1186             if (res->num_diagnostics)
1187             {
1188                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1189                                               0);
1190                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1191                                     &res->num_diagnostics, client_data, ns);
1192             }
1193         }
1194         else if ((*p)->which == Z_SRW_scan_request)
1195         {
1196             Z_SRW_scanRequest *req = (*p)->u.scan_request;
1197             const char *queryType = req->queryType;
1198             if (version2)
1199                 ns = "http://docs.oasis-open.org/ns/search-ws/scan";
1200             ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
1201             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1202             xmlSetNs(ptr, ns_srw);
1203
1204             if (!version2)
1205                 add_xsd_string(ptr, "version", (*p)->srw_version);
1206
1207             if (version2)
1208             {
1209                 if (queryType && strcmp(queryType, "cql"))
1210                     add_xsd_string(ptr, "queryType", queryType);
1211                 add_xsd_string(ptr, "scanClause", req->scanClause);
1212             }
1213             else
1214             {
1215                 if (!queryType || !strcmp(queryType, "cql"))
1216                     add_xsd_string(ptr, "scanClause", req->scanClause);
1217                 else if (!strcmp(queryType, "pqf"))
1218                     add_xsd_string(ptr, "pScanClause", req->scanClause);
1219             }
1220             add_xsd_integer(ptr, "responsePosition", req->responsePosition);
1221             add_xsd_integer(ptr, "maximumTerms", req->maximumTerms);
1222             add_xsd_string(ptr, "stylesheet", req->stylesheet);
1223             add_xsd_string(ptr, "database", req->database);
1224         }
1225         else if ((*p)->which == Z_SRW_scan_response)
1226         {
1227             Z_SRW_scanResponse *res = (*p)->u.scan_response;
1228             if (version2)
1229                 ns = "http://docs.oasis-open.org/ns/search-ws/scan";
1230             ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
1231             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1232             xmlSetNs(ptr, ns_srw);
1233
1234             if (!version2)
1235                 add_xsd_string(ptr, "version", (*p)->srw_version);
1236
1237             if (res->num_terms)
1238             {
1239                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "terms", 0);
1240                 yaz_srw_terms(o, rptr, &res->terms, &res->num_terms,
1241                               client_data, ns);
1242             }
1243             if (res->num_diagnostics)
1244             {
1245                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1246                                               0);
1247                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1248                                     &res->num_diagnostics, client_data, ns);
1249             }
1250         }
1251         else
1252             return -1;
1253         if (ptr && (*p)->extraResponseData_len)
1254             add_XML_n(ptr, "extraResponseData",
1255                       (*p)->extraResponseData_buf,
1256                       (*p)->extraResponseData_len, ns_srw);
1257
1258
1259     }
1260     return 0;
1261 }
1262
1263 int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
1264                   void *client_data, const char *ns_ucp_str)
1265 {
1266     xmlNodePtr pptr = (xmlNodePtr) vptr;
1267     const char *ns_srw_str = YAZ_XMLNS_SRU_v1_1;
1268     if (o->direction == ODR_DECODE)
1269     {
1270         Z_SRW_PDU **p = handler_data;
1271         xmlNodePtr method = pptr->children;
1272
1273         while (method && method->type == XML_TEXT_NODE)
1274             method = method->next;
1275
1276         if (!method)
1277             return -1;
1278         if (method->type != XML_ELEMENT_NODE)
1279             return -1;
1280
1281         *p = yaz_srw_get_core_v_2_0(o);
1282
1283         if (!xmlStrcmp(method->name, BAD_CAST "updateRequest"))
1284         {
1285             xmlNodePtr ptr = method->children;
1286             Z_SRW_updateRequest *req;
1287             char *oper = 0;
1288
1289             (*p)->which = Z_SRW_update_request;
1290             req = (*p)->u.update_request = (Z_SRW_updateRequest *)
1291                 odr_malloc(o, sizeof(*req));
1292             req->database = 0;
1293             req->operation = 0;
1294             req->recordId = 0;
1295             req->recordVersions = 0;
1296             req->num_recordVersions = 0;
1297             req->record = 0;
1298             req->extra_record = 0;
1299             req->extraRequestData_buf = 0;
1300             req->extraRequestData_len = 0;
1301             req->stylesheet = 0;
1302
1303             for (; ptr; ptr = ptr->next)
1304             {
1305                 if (match_xsd_string(ptr, "version", o,
1306                                      &(*p)->srw_version))
1307                     ;
1308                 else if (match_xsd_string(ptr, "action", o,
1309                                           &oper)){
1310                     if (oper)
1311                     {
1312                         if (!strcmp(oper, "info:srw/action/1/delete"))
1313                             req->operation = "delete";
1314                         else if (!strcmp(oper,"info:srw/action/1/replace" ))
1315                             req->operation = "replace";
1316                         else if (!strcmp(oper, "info:srw/action/1/create"))
1317                             req->operation = "insert";
1318                     }
1319                 }
1320                 else if (match_xsd_string(ptr, "recordIdentifier", o,
1321                                           &req->recordId))
1322                     ;
1323                 else if (match_element(ptr, "recordVersions" ) )
1324                     yaz_srw_versions( o, ptr, &req->recordVersions,
1325                                       &req->num_recordVersions, client_data,
1326                                       ns_ucp_str);
1327                 else if (match_element(ptr, "record"))
1328                 {
1329                     req->record = yaz_srw_get_record(o);
1330                     yaz_srw_record(o, ptr, req->record, &req->extra_record,
1331                                    client_data, 0);
1332                 }
1333                 else if (match_xsd_string(ptr, "stylesheet", o,
1334                                           &req->stylesheet))
1335                     ;
1336                 else
1337                     match_xsd_string(ptr, "database", o, &req->database);
1338             }
1339         }
1340         else if (!xmlStrcmp(method->name, BAD_CAST "updateResponse"))
1341         {
1342             xmlNodePtr ptr = method->children;
1343             Z_SRW_updateResponse *res;
1344
1345             (*p)->which = Z_SRW_update_response;
1346             res = (*p)->u.update_response = (Z_SRW_updateResponse *)
1347                 odr_malloc(o, sizeof(*res));
1348
1349             res->operationStatus = 0;
1350             res->recordId = 0;
1351             res->recordVersions = 0;
1352             res->num_recordVersions = 0;
1353             res->diagnostics = 0;
1354             res->num_diagnostics = 0;
1355             res->record = 0;
1356             res->extra_record = 0;
1357             res->extraResponseData_buf = 0;
1358             res->extraResponseData_len = 0;
1359
1360             for (; ptr; ptr = ptr->next)
1361             {
1362                 if (match_xsd_string(ptr, "version", o,
1363                                      &(*p)->srw_version))
1364                     ;
1365                 else if (match_xsd_string(ptr, "operationStatus", o,
1366                                           &res->operationStatus ))
1367                     ;
1368                 else if (match_xsd_string(ptr, "recordIdentifier", o,
1369                                           &res->recordId))
1370                     ;
1371                 else if (match_element(ptr, "recordVersions" ))
1372                     yaz_srw_versions(o, ptr, &res->recordVersions,
1373                                      &res->num_recordVersions,
1374                                      client_data, ns_ucp_str);
1375                 else if (match_element(ptr, "record"))
1376                 {
1377                     res->record = yaz_srw_get_record(o);
1378                     yaz_srw_record(o, ptr, res->record, &res->extra_record,
1379                                    client_data, 0);
1380                 }
1381                 else if (match_element(ptr, "diagnostics"))
1382                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1383                                         &res->num_diagnostics,
1384                                         client_data, ns_ucp_str);
1385             }
1386         }
1387         else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateRequest"))
1388         {
1389         }
1390         else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateResponse"))
1391         {
1392         }
1393         else
1394         {
1395             *p = 0;
1396             return -1;
1397         }
1398     }
1399     else if (o->direction == ODR_ENCODE)
1400     {
1401         Z_SRW_PDU **p = handler_data;
1402         xmlNsPtr ns_ucp, ns_srw;
1403
1404         if ((*p)->which == Z_SRW_update_request)
1405         {
1406             Z_SRW_updateRequest *req = (*p)->u.update_request;
1407             xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "updateRequest", 0);
1408             ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1409             xmlSetNs(ptr, ns_ucp);
1410             ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1411
1412             add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1413             add_xsd_string(ptr, "action", req->operation);
1414             add_xsd_string(ptr, "recordIdentifier", req->recordId );
1415             if (req->recordVersions)
1416                 yaz_srw_versions( o, ptr, &req->recordVersions,
1417                                   &req->num_recordVersions,
1418                                   client_data, ns_ucp_str);
1419             if (req->record && req->record->recordData_len)
1420             {
1421                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1422                 xmlSetNs(rptr, ns_srw);
1423                 yaz_srw_record(o, rptr, req->record, &req->extra_record,
1424                                client_data, 0);
1425             }
1426             if (req->extraRequestData_len)
1427             {
1428                 add_XML_n(ptr, "extraRequestData",
1429                           req->extraRequestData_buf,
1430                           req->extraRequestData_len, ns_srw);
1431             }
1432             add_xsd_string(ptr, "stylesheet", req->stylesheet);
1433             add_xsd_string(ptr, "database", req->database);
1434         }
1435         else if ((*p)->which == Z_SRW_update_response)
1436         {
1437             Z_SRW_updateResponse *res = (*p)->u.update_response;
1438             xmlNodePtr ptr = xmlNewChild(pptr, 0, (xmlChar *)
1439                                          "updateResponse", 0);
1440             ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1441             xmlSetNs(ptr, ns_ucp);
1442             ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1443
1444             add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1445             add_xsd_string(ptr, "operationStatus", res->operationStatus );
1446             add_xsd_string(ptr, "recordIdentifier", res->recordId );
1447             if (res->recordVersions)
1448                 yaz_srw_versions(o, ptr, &res->recordVersions,
1449                                  &res->num_recordVersions,
1450                                  client_data, ns_ucp_str);
1451             if (res->record && res->record->recordData_len)
1452             {
1453                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1454                 xmlSetNs(rptr, ns_srw);
1455                 yaz_srw_record(o, rptr, res->record, &res->extra_record,
1456                                client_data, 0);
1457             }
1458             if (res->num_diagnostics)
1459             {
1460                 xmlNsPtr ns_diag =
1461                     xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1,
1462                              BAD_CAST "diag" );
1463
1464                 xmlNodePtr rptr = xmlNewChild(ptr, ns_diag, BAD_CAST "diagnostics", 0);
1465                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1466                                     &res->num_diagnostics, client_data,
1467                                     ns_ucp_str);
1468             }
1469             if (res->extraResponseData_len)
1470                 add_XML_n(ptr, "extraResponseData",
1471                           res->extraResponseData_buf,
1472                           res->extraResponseData_len, ns_srw);
1473         }
1474         else
1475             return -1;
1476     }
1477     return 0;
1478 }
1479
1480 #endif
1481
1482
1483 /*
1484  * Local variables:
1485  * c-basic-offset: 4
1486  * c-file-style: "Stroustrup"
1487  * indent-tabs-mode: nil
1488  * End:
1489  * vim: shiftwidth=4 tabstop=8 expandtab
1490  */
1491