XPATH fixes - leaf attributes part of tag path
[idzebra-moved-to-github.git] / recctrl / recgrs.c
1 /*
2  * Copyright (C) 1994-2002, Index Data
3  * All rights reserved.
4  *
5  * $Id: recgrs.c,v 1.58 2002-08-02 10:07:48 adam Exp $
6  */
7
8 #include <stdio.h>
9 #include <assert.h>
10 #include <sys/types.h>
11 #ifndef WIN32
12 #include <unistd.h>
13 #endif
14
15 #include <yaz/log.h>
16 #include <yaz/oid.h>
17
18 #include <recctrl.h>
19 #include "grsread.h"
20
21 #define GRS_MAX_WORD 512
22
23 struct grs_handler {
24     RecTypeGrs type;
25     void *clientData;
26     int initFlag;
27     struct grs_handler *next;
28 };
29
30 struct grs_handlers {
31     struct grs_handler *handlers;
32 };
33
34 static int read_grs_type (struct grs_handlers *h,
35                           struct grs_read_info *p, const char *type,
36                           data1_node **root)
37 {
38     struct grs_handler *gh = h->handlers;
39     const char *cp = strchr (type, '.');
40
41     if (cp == NULL || cp == type)
42     {
43         cp = strlen(type) + type;
44         *p->type = 0;
45     }
46     else
47         strcpy (p->type, cp+1);
48     for (gh = h->handlers; gh; gh = gh->next)
49     {
50         if (!memcmp (type, gh->type->type, cp-type))
51         {
52             if (!gh->initFlag)
53             {
54                 gh->initFlag = 1;
55                 gh->clientData = (*gh->type->init)();
56             }
57             p->clientData = gh->clientData;
58             *root = (gh->type->read)(p);
59             gh->clientData = p->clientData;
60             return 0;
61         }
62     }
63     return 1;
64 }
65
66 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
67 {
68     struct grs_handler *gh = (struct grs_handler *) xmalloc (sizeof(*gh));
69     gh->next = h->handlers;
70     h->handlers = gh;
71     gh->initFlag = 0;
72     gh->clientData = 0;
73     gh->type = t;
74 }
75
76 static void *grs_init(RecType recType)
77 {
78     struct grs_handlers *h = (struct grs_handlers *) xmalloc (sizeof(*h));
79     h->handlers = 0;
80
81     grs_add_handler (h, recTypeGrs_sgml);
82     grs_add_handler (h, recTypeGrs_regx);
83 #if HAVE_TCL_H
84     grs_add_handler (h, recTypeGrs_tcl);
85 #endif
86     grs_add_handler (h, recTypeGrs_marc);
87 #if YAZ_HAVE_EXPAT
88     grs_add_handler (h, recTypeGrs_xml);
89 #endif
90     return h;
91 }
92
93 static void grs_destroy(void *clientData)
94 {
95     struct grs_handlers *h = (struct grs_handlers *) clientData;
96     struct grs_handler *gh = h->handlers, *gh_next;
97     while (gh)
98     {
99         gh_next = gh->next;
100         if (gh->initFlag)
101             (*gh->type->destroy)(gh->clientData);
102         xfree (gh);
103         gh = gh_next;
104     }
105     xfree (h);
106 }
107
108 /* use
109      1   start element (tag)
110      2   end element
111      3   start attr (and attr-exact)
112      4   end attr
113
114   1016   cdata
115   1015   attr data
116 */
117
118 static void index_xpath (data1_node *n, struct recExtractCtrl *p,
119                          int level, RecWord *wrd, int use)
120 {
121     int i;
122     char tag_path_full[1024];
123     size_t flen = 0;
124     data1_node *nn;
125
126     switch (n->which)
127     {
128     case DATA1N_data:
129         wrd->reg_type = 'w';
130         wrd->string = n->u.data.data;
131         wrd->length = n->u.data.len;
132         wrd->attrSet = VAL_IDXPATH,
133         wrd->attrUse = use;
134         if (p->flagShowRecords)
135         {
136             printf("%*s data=", (level + 1) * 4, "");
137             for (i = 0; i<wrd->length && i < 8; i++)
138                 fputc (wrd->string[i], stdout);
139             printf("\n");
140         }
141         else
142         {
143             (*p->tokenAdd)(wrd);
144         }
145         break;
146     case DATA1N_tag:
147         for (nn = n; nn; nn = nn->parent)
148         {
149             if (nn->which == DATA1N_tag)
150             {
151                 size_t tlen = strlen(nn->u.tag.tag);
152                 if (tlen + flen > (sizeof(tag_path_full)-2))
153                     return;
154                 memcpy (tag_path_full + flen, nn->u.tag.tag, tlen);
155                 flen += tlen;
156                 tag_path_full[flen++] = '/';
157             }
158             else if (nn->which == DATA1N_root)
159                 break;
160         }
161         wrd->reg_type = '0';
162         wrd->string = tag_path_full;
163         wrd->length = flen;
164         wrd->attrSet = VAL_IDXPATH;
165         wrd->attrUse = use;
166         if (p->flagShowRecords)
167         {
168             printf("%*s tag=", (level + 1) * 4, "");
169             for (i = 0; i<wrd->length && i < 40; i++)
170                 fputc (wrd->string[i], stdout);
171             if (i == 40)
172                 printf (" ..");
173             printf("\n");
174         }
175         else
176         {
177             data1_xattr *xp;
178             (*p->tokenAdd)(wrd);   /* index element pag (AKA tag path) */
179             if (use == 1)
180             {
181                 for (xp = n->u.tag.attributes; xp; xp = xp->next)
182                 {
183                     char comb[512];
184                     /* attribute  (no value) */
185                     wrd->reg_type = '0';
186                     wrd->attrUse = 3;
187                     wrd->string = xp->name;
188                     wrd->length = strlen(xp->name);
189                     
190                     wrd->seqno--;
191                     (*p->tokenAdd)(wrd);
192
193                     if (xp->value &&
194                         strlen(xp->name) + strlen(xp->value) < sizeof(comb)-2)
195                     {
196                         /* attribute value exact */
197                         strcpy (comb, xp->name);
198                         strcat (comb, "=");
199                         strcat (comb, xp->value);
200                         
201                         wrd->attrUse = 3;
202                         wrd->reg_type = '0';
203                         wrd->string = comb;
204                         wrd->length = strlen(comb);
205                         wrd->seqno--;
206                         
207                         (*p->tokenAdd)(wrd);
208                     }
209                 }                
210                 for (xp = n->u.tag.attributes; xp; xp = xp->next)
211                 {
212                     char attr_tag_path_full[1024];
213                     
214                     sprintf (attr_tag_path_full, "@%s/%.*s",
215                              xp->name, flen, tag_path_full);
216
217                     wrd->reg_type = '0';
218                     wrd->attrUse = 1;
219                     wrd->string = attr_tag_path_full;
220                     wrd->length = strlen(attr_tag_path_full);
221                     (*p->tokenAdd)(wrd);
222                     
223                     wrd->attrUse = 1015;
224                     wrd->reg_type = 'w';
225                     wrd->string = xp->value;
226                     wrd->length = strlen(xp->value);
227                     
228                     (*p->tokenAdd)(wrd);
229                     
230                     wrd->reg_type = '0';
231                     wrd->attrUse = 2;
232                     wrd->string = attr_tag_path_full;
233                     wrd->length = strlen(attr_tag_path_full);
234                     (*p->tokenAdd)(wrd);
235                 }
236             }
237         }
238     }
239 }
240
241 static void index_termlist (data1_node *par, data1_node *n,
242                             struct recExtractCtrl *p, int level, RecWord *wrd)
243 {
244     data1_termlist *tlist = 0;
245     data1_datatype dtype = DATA1K_string;
246     /*
247      * cycle up towards the root until we find a tag with an att..
248      * this has the effect of indexing locally defined tags with
249      * the attribute of their ancestor in the record.
250      */
251     
252     while (!par->u.tag.element)
253         if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
254             break;
255     if (!par || !(tlist = par->u.tag.element->termlists))
256         return;
257     if (par->u.tag.element->tag)
258         dtype = par->u.tag.element->tag->kind;
259     
260     for (; tlist; tlist = tlist->next)
261     {
262         char xattr[512];
263         /* consider source */
264         wrd->string = 0;
265         
266         if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
267         {
268             wrd->string = n->u.data.data;
269             wrd->length = n->u.data.len;
270         }
271         else if (!strcmp (tlist->source, "tag") && n->which == DATA1N_tag)
272         {
273             wrd->string = n->u.tag.tag;
274             wrd->length = strlen(n->u.tag.tag);
275         }
276         else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
277             n->which == DATA1N_tag)
278         {
279             data1_xattr *p = n->u.tag.attributes;
280             while (p && strcmp (p->name, xattr))
281                 p = p->next;
282             if (p)
283             {
284                 wrd->string = p->value;
285                 wrd->length = strlen(p->value);
286             }
287         }
288         if (wrd->string)
289         {
290             if (p->flagShowRecords)
291             {
292                 int i;
293                 printf("%*sIdx: [%s]", (level + 1) * 4, "",
294                        tlist->structure);
295                 printf("%s:%s [%d] %s",
296                        tlist->att->parent->name,
297                        tlist->att->name, tlist->att->value,
298                        tlist->source);
299                 printf (" data=\"");
300                 for (i = 0; i<wrd->length && i < 8; i++)
301                     fputc (wrd->string[i], stdout);
302                 fputc ('"', stdout);
303                 if (wrd->length > 8)
304                     printf (" ...");
305                 fputc ('\n', stdout);
306             }
307             else
308             {
309                 wrd->reg_type = *tlist->structure;
310                 wrd->attrSet = (int) (tlist->att->parent->reference);
311                 wrd->attrUse = tlist->att->locals->local;
312                 (*p->tokenAdd)(wrd);
313             }
314         }
315     }
316 }
317
318 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level,
319                     RecWord *wrd)
320 {
321     for (; n; n = n->next)
322     {
323         if (p->flagShowRecords) /* display element description to user */
324         {
325             if (n->which == DATA1N_root)
326             {
327                 printf("%*s", level * 4, "");
328                 printf("Record type: '%s'\n", n->u.root.type);
329             }
330             else if (n->which == DATA1N_tag)
331             {
332                 data1_element *e;
333
334                 printf("%*s", level * 4, "");
335                 if (!(e = n->u.tag.element))
336                     printf("Local tag: '%s'\n", n->u.tag.tag);
337                 else
338                 {
339                     printf("Elm: '%s' ", e->name);
340                     if (e->tag)
341                     {
342                         data1_tag *t = e->tag;
343
344                         printf("TagNam: '%s' ", t->names->name);
345                         printf("(");
346                         if (t->tagset)
347                             printf("%s[%d],", t->tagset->name, t->tagset->type);
348                         else
349                             printf("?,");
350                         if (t->which == DATA1T_numeric)
351                             printf("%d)", t->value.numeric);
352                         else
353                             printf("'%s')", t->value.string);
354                     }
355                     printf("\n");
356                 }
357             }
358         }
359
360         if (n->which == DATA1N_tag)
361         {
362             index_termlist (n, n, p, level, wrd);
363             /* index start tag */
364             if (!n->root->u.root.absyn)
365                 index_xpath (n, p, level, wrd, 1);
366         }
367
368         if (n->child)
369             if (dumpkeys(n->child, p, level + 1, wrd) < 0)
370                 return -1;
371
372
373         if (n->which == DATA1N_data)
374         {
375             data1_node *par = get_parent_tag(p->dh, n);
376
377             if (p->flagShowRecords)
378             {
379                 printf("%*s", level * 4, "");
380                 printf("Data: ");
381                 if (n->u.data.len > 32)
382                     printf("'%.24s ... %.6s'\n", n->u.data.data,
383                            n->u.data.data + n->u.data.len-6);
384                 else if (n->u.data.len > 0)
385                     printf("'%.*s'\n", n->u.data.len, n->u.data.data);
386                 else
387                     printf("NULL\n");
388             }
389
390             if (par)
391                 index_termlist (par, n, p, level, wrd);
392             if (!n->root->u.root.absyn)
393                 index_xpath (n, p, level, wrd, 1016);
394
395         }
396
397         if (n->which == DATA1N_tag)
398         {
399             /* index end tag */
400             if (!n->root->u.root.absyn)
401                 index_xpath (n, p, level, wrd, 2);
402         }
403
404
405         if (p->flagShowRecords && n->which == DATA1N_root)
406         {
407             printf("%*s-------------\n\n", level * 4, "");
408         }
409     }
410     return 0;
411 }
412
413 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
414 {
415     oident oe;
416     int oidtmp[OID_SIZE];
417     RecWord wrd;
418
419     oe.proto = PROTO_Z3950;
420     oe.oclass = CLASS_SCHEMA;
421     if (n->u.root.absyn)
422     {
423         oe.value = n->u.root.absyn->reference;
424         
425         if ((oid_ent_to_oid (&oe, oidtmp)))
426             (*p->schemaAdd)(p, oidtmp);
427     }
428     (*p->init)(p, &wrd);
429
430     return dumpkeys(n, p, 0, &wrd);
431 }
432
433 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
434                            NMEM mem)
435 {
436     data1_node *n;
437     struct grs_read_info gri;
438     oident oe;
439     int oidtmp[OID_SIZE];
440     RecWord wrd;
441
442     gri.readf = p->readf;
443     gri.seekf = p->seekf;
444     gri.tellf = p->tellf;
445     gri.endf = p->endf;
446     gri.fh = p->fh;
447     gri.offset = p->offset;
448     gri.mem = mem;
449     gri.dh = p->dh;
450
451     if (read_grs_type (h, &gri, p->subType, &n))
452         return RECCTRL_EXTRACT_ERROR;
453     if (!n)
454         return RECCTRL_EXTRACT_EOF;
455     oe.proto = PROTO_Z3950;
456     oe.oclass = CLASS_SCHEMA;
457 #if 0
458     if (!n->u.root.absyn)
459         return RECCTRL_EXTRACT_ERROR;
460 #endif
461     if (n->u.root.absyn)
462     {
463         oe.value = n->u.root.absyn->reference;
464         if ((oid_ent_to_oid (&oe, oidtmp)))
465             (*p->schemaAdd)(p, oidtmp);
466     }
467
468     /* ensure our data1 tree is UTF-8 */
469     data1_iconv (p->dh, mem, n, "UTF-8", data1_get_encoding(p->dh, n));
470
471 #if 0
472     data1_pr_tree (p->dh, n, stdout);
473 #endif
474
475     (*p->init)(p, &wrd);
476     if (dumpkeys(n, p, 0, &wrd) < 0)
477     {
478         data1_free_tree(p->dh, n);
479         return RECCTRL_EXTRACT_ERROR;
480     }
481     data1_free_tree(p->dh, n);
482     return RECCTRL_EXTRACT_OK;
483 }
484
485 static int grs_extract(void *clientData, struct recExtractCtrl *p)
486 {
487     int ret;
488     NMEM mem = nmem_create ();
489     struct grs_handlers *h = (struct grs_handlers *) clientData;
490
491     ret = grs_extract_sub(h, p, mem);
492     nmem_destroy(mem);
493     return ret;
494 }
495
496 /*
497  * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
498  */
499 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
500 {
501     data1_esetname *eset;
502     Z_Espec1 *espec = 0;
503     Z_ElementSpec *p;
504
505     switch (c->which)
506     {
507     case Z_RecordComp_simple:
508         if (c->u.simple->which != Z_ElementSetNames_generic)
509             return 26; /* only generic form supported. Fix this later */
510         if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
511                                          c->u.simple->u.generic)))
512         {
513             logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
514             return 25; /* invalid esetname */
515         }
516         logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
517              c->u.simple->u.generic);
518         espec = eset->spec;
519         break;
520     case Z_RecordComp_complex:
521         if (c->u.complex->generic)
522         {
523             /* insert check for schema */
524             if ((p = c->u.complex->generic->elementSpec))
525             {
526                 switch (p->which)
527                 {
528                 case Z_ElementSpec_elementSetName:
529                     if (!(eset =
530                           data1_getesetbyname(dh, n->u.root.absyn,
531                                               p->u.elementSetName)))
532                     {
533                         logf(LOG_LOG, "Unknown esetname '%s'",
534                              p->u.elementSetName);
535                         return 25; /* invalid esetname */
536                     }
537                     logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
538                          p->u.elementSetName);
539                     espec = eset->spec;
540                     break;
541                 case Z_ElementSpec_externalSpec:
542                     if (p->u.externalSpec->which == Z_External_espec1)
543                     {
544                         logf(LOG_DEBUG, "Got Espec-1");
545                         espec = p->u.externalSpec-> u.espec1;
546                     }
547                     else
548                     {
549                         logf(LOG_LOG, "Unknown external espec.");
550                         return 25; /* bad. what is proper diagnostic? */
551                     }
552                     break;
553                 }
554             }
555         }
556         else
557             return 26; /* fix */
558     }
559     if (espec)
560     {
561         logf (LOG_DEBUG, "Element: Espec-1 match");
562         return data1_doespec1(dh, n, espec);
563     }
564     else
565     {
566         logf (LOG_DEBUG, "Element: all match");
567         return -1;
568     }
569 }
570
571 static void add_idzebra_info (struct recRetrieveCtrl *p, data1_node *top,
572                               NMEM mem)
573 {
574     const char *idzebra_ns[7];
575
576     idzebra_ns[0] = "xmlns:idzebra";
577     idzebra_ns[1] = "http://www.indexdata.dk/zebra/";
578     idzebra_ns[2] = 0;
579
580     data1_tag_add_attr (p->dh, mem, top, idzebra_ns);
581
582     data1_mk_tag_data_int (p->dh, top, "idzebra:size", p->recordSize,
583                            mem);
584     if (p->score != -1)
585         data1_mk_tag_data_int (p->dh, top, "idzebra:score",
586                                p->score, mem);
587     
588     data1_mk_tag_data_int (p->dh, top, "idzebra:localnumber", p->localno,
589                            mem);
590     if (p->fname)
591         data1_mk_tag_data_text(p->dh, top, "idzebra:filename",
592                                p->fname, mem);
593 }
594
595 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
596 {
597     data1_node *node = 0, *onode = 0, *top;
598     data1_node *dnew;
599     data1_maptab *map;
600     int res, selected = 0;
601     NMEM mem;
602     struct grs_read_info gri;
603     char *tagname;
604     struct grs_handlers *h = (struct grs_handlers *) clientData;
605     int requested_schema = VAL_NONE;
606     data1_marctab *marctab;
607     int dummy;
608     
609     mem = nmem_create();
610     gri.readf = p->readf;
611     gri.seekf = p->seekf;
612     gri.tellf = p->tellf;
613     gri.endf = NULL;
614     gri.fh = p->fh;
615     gri.offset = 0;
616     gri.mem = mem;
617     gri.dh = p->dh;
618
619     logf (LOG_DEBUG, "grs_retrieve");
620     if (read_grs_type (h, &gri, p->subType, &node))
621     {
622         p->diagnostic = 14;
623         nmem_destroy (mem);
624         return 0;
625     }
626     if (!node)
627     {
628         p->diagnostic = 14;
629         nmem_destroy (mem);
630         return 0;
631     }
632     /* ensure our data1 tree is UTF-8 */
633     data1_iconv (p->dh, mem, node, "UTF-8", data1_get_encoding(p->dh, node));
634
635 #if 0
636     data1_pr_tree (p->dh, node, stdout);
637 #endif
638     top = data1_get_root_tag (p->dh, node);
639
640     logf (LOG_DEBUG, "grs_retrieve: size");
641     if ((dnew = data1_mk_tag_data_wd(p->dh, top, "size", mem)))
642     {
643         dnew->u.data.what = DATA1I_text;
644         dnew->u.data.data = dnew->lbuf;
645         sprintf(dnew->u.data.data, "%d", p->recordSize);
646         dnew->u.data.len = strlen(dnew->u.data.data);
647     }
648
649     tagname = res_get_def(p->res, "tagrank", "rank");
650     if (strcmp(tagname, "0") && p->score >= 0 &&
651         (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
652     {
653         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
654         dnew->u.data.what = DATA1I_num;
655         dnew->u.data.data = dnew->lbuf;
656         sprintf(dnew->u.data.data, "%d", p->score);
657         dnew->u.data.len = strlen(dnew->u.data.data);
658     }
659
660     tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
661     if (strcmp(tagname, "0") && p->localno > 0 &&
662          (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
663     {
664         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
665         dnew->u.data.what = DATA1I_text;
666         dnew->u.data.data = dnew->lbuf;
667
668         sprintf(dnew->u.data.data, "%d", p->localno);
669         dnew->u.data.len = strlen(dnew->u.data.data);
670     }
671 #if 0
672     data1_pr_tree (p->dh, node, stdout);
673 #endif
674     if (p->comp && p->comp->which == Z_RecordComp_complex &&
675         p->comp->u.complex->generic &&
676         p->comp->u.complex->generic->schema)
677     {
678         oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
679         if (oe)
680             requested_schema = oe->value;
681     }
682
683     /* If schema has been specified, map if possible, then check that
684      * we got the right one 
685      */
686     if (requested_schema != VAL_NONE)
687     {
688         logf (LOG_DEBUG, "grs_retrieve: schema mapping");
689         for (map = node->u.root.absyn->maptabs; map; map = map->next)
690         {
691             if (map->target_absyn_ref == requested_schema)
692             {
693                 onode = node;
694                 if (!(node = data1_map_record(p->dh, onode, map, mem)))
695                 {
696                     p->diagnostic = 14;
697                     nmem_destroy (mem);
698                     return 0;
699                 }
700                 break;
701             }
702         }
703         if (node->u.root.absyn &&
704             requested_schema != node->u.root.absyn->reference)
705         {
706             p->diagnostic = 238;
707             nmem_destroy (mem);
708             return 0;
709         }
710     }
711     /*
712      * Does the requested format match a known syntax-mapping? (this reflects
713      * the overlap of schema and formatting which is inherent in the MARC
714      * family)
715      */
716     yaz_log (LOG_DEBUG, "grs_retrieve: syntax mapping");
717     if (node->u.root.absyn)
718         for (map = node->u.root.absyn->maptabs; map; map = map->next)
719         {
720             if (map->target_absyn_ref == p->input_format)
721             {
722                 onode = node;
723                 if (!(node = data1_map_record(p->dh, onode, map, mem)))
724                 {
725                     p->diagnostic = 14;
726                     nmem_destroy (mem);
727                     return 0;
728                 }
729                 break;
730             }
731         }
732     yaz_log (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
733     if (node->u.root.absyn &&
734         node->u.root.absyn->reference != VAL_NONE &&
735         p->input_format == VAL_GRS1)
736     {
737         oident oe;
738         Odr_oid *oid;
739         int oidtmp[OID_SIZE];
740         
741         oe.proto = PROTO_Z3950;
742         oe.oclass = CLASS_SCHEMA;
743         oe.value = node->u.root.absyn->reference;
744         
745         if ((oid = oid_ent_to_oid (&oe, oidtmp)))
746         {
747             char tmp[128];
748             data1_handle dh = p->dh;
749             char *p = tmp;
750             int *ii;
751             
752             for (ii = oid; *ii >= 0; ii++)
753             {
754                 if (p != tmp)
755                         *(p++) = '.';
756                 sprintf(p, "%d", *ii);
757                 p += strlen(p);
758             }
759             *(p++) = '\0';
760                 
761             if ((dnew = data1_mk_tag_data_wd(dh, node, 
762                                              "schemaIdentifier", mem)))
763             {
764                 dnew->u.data.what = DATA1I_oid;
765                 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
766                 memcpy(dnew->u.data.data, tmp, p - tmp);
767                 dnew->u.data.len = p - tmp;
768             }
769         }
770     }
771
772     logf (LOG_DEBUG, "grs_retrieve: element spec");
773     if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
774     {
775         p->diagnostic = res;
776         if (onode)
777             data1_free_tree(p->dh, onode);
778         data1_free_tree(p->dh, node);
779         nmem_destroy(mem);
780         return 0;
781     }
782     else if (p->comp && !res)
783         selected = 1;
784
785 #if 0
786     data1_pr_tree (p->dh, node, stdout);
787 #endif
788     logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
789     switch (p->output_format = (p->input_format != VAL_NONE ?
790                                 p->input_format : VAL_SUTRS))
791     {
792     case VAL_TEXT_XML:
793         add_idzebra_info (p, top, mem);
794
795         if (p->encoding)
796             data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
797
798         if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
799                                               &p->rec_len)))
800             p->diagnostic = 238;
801         else
802         {
803             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
804             memcpy (new_buf, p->rec_buf, p->rec_len);
805             p->rec_buf = new_buf;
806         }
807         break;
808     case VAL_GRS1:
809         dummy = 0;
810         if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
811                                           p->odr, &dummy)))
812             p->diagnostic = 238; /* not available in requested syntax */
813         else
814             p->rec_len = (size_t) (-1);
815         break;
816     case VAL_EXPLAIN:
817         if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
818                                                p->odr)))
819             p->diagnostic = 238;
820         else
821             p->rec_len = (size_t) (-1);
822         break;
823     case VAL_SUMMARY:
824         if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
825                                                p->odr)))
826             p->diagnostic = 238;
827         else
828             p->rec_len = (size_t) (-1);
829         break;
830     case VAL_SUTRS:
831         if (p->encoding)
832             data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
833         if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
834                                            &p->rec_len)))
835             p->diagnostic = 238;
836         else
837         {
838             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
839             memcpy (new_buf, p->rec_buf, p->rec_len);
840             p->rec_buf = new_buf;
841         }
842         break;
843     case VAL_SOIF:
844         if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
845                                             &p->rec_len)))
846             p->diagnostic = 238;
847         else
848         {
849             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
850             memcpy (new_buf, p->rec_buf, p->rec_len);
851             p->rec_buf = new_buf;
852         }
853         break;
854     default:
855         if (!node->u.root.absyn)
856         {
857             p->diagnostic = 238;
858             break;
859         }
860         for (marctab = node->u.root.absyn->marc; marctab;
861              marctab = marctab->next)
862             if (marctab->reference == p->input_format)
863                 break;
864         if (!marctab)
865         {
866             p->diagnostic = 238;
867             break;
868         }
869         if (p->encoding)
870             data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
871         if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
872                                         selected, &p->rec_len)))
873             p->diagnostic = 238;
874         else
875         {
876             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
877             memcpy (new_buf, p->rec_buf, p->rec_len);
878                 p->rec_buf = new_buf;
879         }
880     }
881     if (node)
882         data1_free_tree(p->dh, node);
883     if (onode)
884         data1_free_tree(p->dh, onode);
885     nmem_destroy(mem);
886     return 0;
887 }
888
889 static struct recType grs_type =
890 {
891     "grs",
892     grs_init,
893     grs_destroy,
894     grs_extract,
895     grs_retrieve
896 };
897
898 RecType recTypeGrs = &grs_type;