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