Fixed bug #723: Enable EXSLT for alvis. We use whatever YAZ
[idzebra-moved-to-github.git] / index / alvis.c
1 /* $Id: alvis.c,v 1.4 2006-11-03 23:17:08 adam Exp $
2    Copyright (C) 1995-2006
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <stdio.h>
24 #include <assert.h>
25 #include <ctype.h>
26
27 #include <yaz/diagbib1.h>
28 #include <yaz/tpath.h>
29
30 #include <libxml/xmlversion.h>
31 #include <libxml/parser.h>
32 #include <libxml/tree.h>
33 #include <libxml/xmlIO.h>
34 #include <libxml/xmlreader.h>
35 #include <libxslt/transform.h>
36 #include <libxslt/xsltutils.h>
37
38 #if YAZ_HAVE_EXSLT
39 #include <libexslt/exslt.h>
40 #endif
41
42 #include <idzebra/util.h>
43 #include <idzebra/recctrl.h>
44
45 struct filter_schema {
46     const char *name;
47     const char *identifier;
48     const char *stylesheet;
49     struct filter_schema *next;
50     const char *default_schema;
51     /* char default_schema; */
52     const char *include_snippet;
53     xsltStylesheetPtr stylesheet_xsp;
54 };
55
56 struct filter_info {
57     xmlDocPtr doc;
58     char *fname;
59     char *full_name;
60     const char *profile_path;
61     const char *split_level;
62     const char *split_path;
63     ODR odr;
64     struct filter_schema *schemas;
65     xmlTextReaderPtr reader;
66 };
67
68 #define ZEBRA_SCHEMA_XSLT_NS "http://indexdata.dk/zebra/xslt/1"
69
70 #define XML_STRCMP(a,b)   strcmp((char*)a, b)
71 #define XML_STRLEN(a) strlen((char*)a)
72
73 static const char *zebra_xslt_ns = ZEBRA_SCHEMA_XSLT_NS;
74
75 static void set_param_xml(const char **params, const char *name,
76                           const char *value, ODR odr)
77 {
78     while (*params)
79         params++;
80     params[0] = name;
81     params[1] = value;
82     params[2] = 0;
83 }
84
85 static void set_param_str(const char **params, const char *name,
86                           const char *value, ODR odr)
87 {
88     char *quoted = odr_malloc(odr, 3 + strlen(value));
89     sprintf(quoted, "'%s'", value);
90     while (*params)
91         params++;
92     params[0] = name;
93     params[1] = quoted;
94     params[2] = 0;
95 }
96
97 static void set_param_int(const char **params, const char *name,
98                           zint value, ODR odr)
99 {
100     char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
101     while (*params)
102         params++;
103     sprintf(quoted, "'" ZINT_FORMAT "'", value);
104     params[0] = name;
105     params[1] = quoted;
106     params[2] = 0;
107 }
108
109 #define ENABLE_INPUT_CALLBACK 0
110
111 #if ENABLE_INPUT_CALLBACK
112 static int zebra_xmlInputMatchCallback (char const *filename)
113 {
114     yaz_log(YLOG_LOG, "match %s", filename);
115     return 0;
116 }
117
118 static void * zebra_xmlInputOpenCallback (char const *filename)
119 {
120     return 0;
121 }
122
123 static int zebra_xmlInputReadCallback (void * context, char * buffer, int len)
124 {
125     return 0;
126 }
127
128 static int zebra_xmlInputCloseCallback (void * context)
129 {
130     return 0;
131 }
132 #endif
133
134 static void *filter_init(Res res, RecType recType)
135 {
136     struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
137     tinfo->reader = 0;
138     tinfo->fname = 0;
139     tinfo->full_name = 0;
140     tinfo->profile_path = 0;
141     tinfo->split_level = 0;
142     tinfo->split_path = 0;
143     tinfo->odr = odr_createmem(ODR_ENCODE);
144     tinfo->doc = 0;
145     tinfo->schemas = 0;
146
147 #if YAZ_HAVE_EXSLT
148     exsltRegisterAll(); 
149 #endif
150
151 #if ENABLE_INPUT_CALLBACK
152     xmlRegisterDefaultInputCallbacks();
153     xmlRegisterInputCallbacks(zebra_xmlInputMatchCallback,
154                               zebra_xmlInputOpenCallback,
155                               zebra_xmlInputReadCallback,
156                               zebra_xmlInputCloseCallback);
157 #endif
158     return tinfo;
159 }
160
161 static int attr_content(struct _xmlAttr *attr, const char *name,
162                         const char **dst_content)
163 {
164     if (!XML_STRCMP(attr->name, name) && attr->children &&
165         attr->children->type == XML_TEXT_NODE)
166     {
167         *dst_content = (const char *)(attr->children->content);
168         return 1;
169     }
170     return 0;
171 }
172
173 static void destroy_schemas(struct filter_info *tinfo)
174 {
175     struct filter_schema *schema = tinfo->schemas;
176     while (schema)
177     {
178         struct filter_schema *schema_next = schema->next;
179         if (schema->stylesheet_xsp)
180             xsltFreeStylesheet(schema->stylesheet_xsp);
181         xfree(schema);
182         schema = schema_next;
183     }
184     tinfo->schemas = 0;
185     xfree(tinfo->fname);
186     if (tinfo->doc)
187         xmlFreeDoc(tinfo->doc);    
188     tinfo->doc = 0;
189 }
190
191 static ZEBRA_RES create_schemas(struct filter_info *tinfo, const char *fname)
192 {
193     char tmp_full_name[1024];
194     xmlNodePtr ptr;
195     tinfo->fname = xstrdup(fname);
196
197    if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path, 
198                              NULL, tmp_full_name))
199       tinfo->full_name = xstrdup(tmp_full_name);
200     else
201       tinfo->full_name = xstrdup(tinfo->fname);
202
203     yaz_log(YLOG_LOG, "alvis filter: loading config file %s", tinfo->full_name);
204
205     tinfo->doc = xmlParseFile(tinfo->full_name);
206
207     if (!tinfo->doc){
208         yaz_log(YLOG_WARN, "alvis filter: could not parse config file %s", 
209                 tinfo->full_name);
210
211         return ZEBRA_FAIL;
212     }
213     
214     ptr = xmlDocGetRootElement(tinfo->doc);
215     if (!ptr || ptr->type != XML_ELEMENT_NODE ||
216         XML_STRCMP(ptr->name, "schemaInfo")){
217         yaz_log(YLOG_WARN, 
218                 "alvis filter:  config file %s :" 
219                 " expected root element <schemaInfo>", 
220                 tinfo->full_name);  
221         return ZEBRA_FAIL;
222     }
223
224     for (ptr = ptr->children; ptr; ptr = ptr->next)
225     {
226         if (ptr->type != XML_ELEMENT_NODE)
227             continue;
228         if (!XML_STRCMP(ptr->name, "schema"))
229         {  
230             char tmp_xslt_full_name[1024];
231             struct _xmlAttr *attr;
232             struct filter_schema *schema = xmalloc(sizeof(*schema));
233             schema->name = 0;
234             schema->identifier = 0;
235             schema->stylesheet = 0;
236             schema->default_schema = 0;
237             schema->next = tinfo->schemas;
238             schema->stylesheet_xsp = 0;
239             schema->include_snippet = 0;
240             tinfo->schemas = schema;
241             for (attr = ptr->properties; attr; attr = attr->next)
242             {
243                 attr_content(attr, "identifier", &schema->identifier);
244                 attr_content(attr, "name", &schema->name);
245                 attr_content(attr, "stylesheet", &schema->stylesheet);
246                 attr_content(attr, "default", &schema->default_schema);
247                 attr_content(attr, "snippet", &schema->include_snippet);
248             }
249             /*yaz_log(YLOG_LOG, "XSLT add %s %s %s", 
250               schema->name, schema->identifier, schema->stylesheet); */
251
252             /* find requested schema */
253
254             if (schema->stylesheet){
255               yaz_filepath_resolve(schema->stylesheet, tinfo->profile_path, 
256                                    NULL, tmp_xslt_full_name);
257               schema->stylesheet_xsp 
258                 = xsltParseStylesheetFile((const xmlChar*) tmp_xslt_full_name);
259               if (!schema->stylesheet_xsp)
260                 yaz_log(YLOG_WARN, 
261                         "alvis filter: could not parse xslt stylesheet %s", 
262                         tmp_xslt_full_name);
263             }
264             
265                 
266         }
267         else if (!XML_STRCMP(ptr->name, "split"))
268         {
269             struct _xmlAttr *attr;
270             for (attr = ptr->properties; attr; attr = attr->next)
271             {
272                 attr_content(attr, "level", &tinfo->split_level);
273                 attr_content(attr, "path", &tinfo->split_path);
274             }
275         }
276         else
277         {
278             yaz_log(YLOG_WARN, "Bad element %s in %s", ptr->name, fname);
279             return ZEBRA_FAIL;
280         }
281     }
282     return ZEBRA_OK;
283 }
284
285 static struct filter_schema *lookup_schema(struct filter_info *tinfo,
286                                            const char *est)
287 {
288     struct filter_schema *schema;
289
290     for (schema = tinfo->schemas; schema; schema = schema->next)
291     { 
292         /* find requested schema */
293         if (est) 
294         {    
295             if (schema->identifier && !strcmp(schema->identifier, est))
296                 return schema;
297             
298             if (schema->name && !strcmp(schema->name, est))
299                 return schema;
300         } 
301         /* or return default schema if defined */
302         else if (schema->default_schema)
303             return schema;
304     }
305
306     /* return first schema if no default schema defined */
307     if (tinfo->schemas)
308         return tinfo->schemas;
309     
310     return 0;
311 }
312
313 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
314 {
315     struct filter_info *tinfo = clientData;
316     if (!args || !*args){
317       yaz_log(YLOG_WARN, "alvis filter: need config file");
318       return ZEBRA_FAIL;
319     }
320
321     if (tinfo->fname && !strcmp(args, tinfo->fname))
322         return ZEBRA_OK;
323     
324     tinfo->profile_path 
325       /* = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH); */
326       = res_get(res, "profilePath");
327     yaz_log(YLOG_LOG, "alvis filter: profilePath %s", tinfo->profile_path);
328
329     destroy_schemas(tinfo);
330     create_schemas(tinfo, args);
331     return ZEBRA_OK;
332 }
333
334 static void filter_destroy(void *clientData)
335 {
336     struct filter_info *tinfo = clientData;
337     destroy_schemas(tinfo);
338     if (tinfo->reader)
339         xmlFreeTextReader(tinfo->reader);
340     odr_destroy(tinfo->odr);
341     xfree(tinfo);
342 }
343
344 static int ioread_ex(void *context, char *buffer, int len)
345 {
346     struct recExtractCtrl *p = context;
347     return p->stream->readf(p->stream, buffer, len);
348 }
349
350 static int ioclose_ex(void *context)
351 {
352     return 0;
353 }
354
355 static void index_cdata(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
356                         xmlNodePtr ptr, RecWord *recWord)
357 {
358     for(; ptr; ptr = ptr->next)
359     {
360         index_cdata(tinfo, ctrl, ptr->children, recWord);
361         if (ptr->type != XML_TEXT_NODE)
362             continue;
363         recWord->term_buf = (const char *)ptr->content;
364         recWord->term_len = XML_STRLEN(ptr->content);
365         (*ctrl->tokenAdd)(recWord);
366     }
367 }
368
369 static void index_node(struct filter_info *tinfo,  struct recExtractCtrl *ctrl,
370                        xmlNodePtr ptr, RecWord *recWord)
371 {
372     for(; ptr; ptr = ptr->next)
373     {
374         index_node(tinfo, ctrl, ptr->children, recWord);
375         if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
376             XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
377             continue;
378         if (!XML_STRCMP(ptr->name, "index"))
379         {
380             const char *name_str = 0;
381             const char *type_str = 0;
382             const char *xpath_str = 0;
383             struct _xmlAttr *attr;
384             for (attr = ptr->properties; attr; attr = attr->next)
385             {
386                 attr_content(attr, "name", &name_str);
387                 attr_content(attr, "xpath", &xpath_str);
388                 attr_content(attr, "type", &type_str);
389             }
390             if (name_str)
391             {
392                 int prev_type = recWord->index_type; /* save default type */
393
394                 if (type_str && *type_str)
395                     recWord->index_type = *type_str; /* type was given */
396                 recWord->index_name = name_str;
397                 index_cdata(tinfo, ctrl, ptr->children, recWord);
398
399                 recWord->index_type = prev_type;     /* restore it again */
400             }
401         }
402     }
403 }
404
405 static void index_record(struct filter_info *tinfo,struct recExtractCtrl *ctrl,
406                          xmlNodePtr ptr, RecWord *recWord)
407 {
408     const char *type_str = "update";
409
410     if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
411         !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
412         && !XML_STRCMP(ptr->name, "record"))
413     {
414         const char *id_str = 0;
415         const char *rank_str = 0;
416         struct _xmlAttr *attr;
417         for (attr = ptr->properties; attr; attr = attr->next)
418         {
419             attr_content(attr, "type", &type_str);
420             attr_content(attr, "id", &id_str);
421             attr_content(attr, "rank", &rank_str);
422         }
423         if (id_str)
424             sscanf(id_str, "%255s", ctrl->match_criteria);
425
426         if (rank_str)
427             ctrl->staticrank = atoi(rank_str);
428         
429         ptr = ptr->children;
430     }
431
432     if (!strcmp("update", type_str))
433         index_node(tinfo, ctrl, ptr, recWord);
434     else if (!strcmp("delete", type_str))
435          yaz_log(YLOG_WARN, "alvis filter delete: to be implemented");
436     else
437          yaz_log(YLOG_WARN, "alvis filter: unknown record type '%s'", 
438                  type_str);
439 }
440     
441 static int extract_doc(struct filter_info *tinfo, struct recExtractCtrl *p,
442                        xmlDocPtr doc)
443 {
444     RecWord recWord;
445     const char *params[10];
446     xmlChar *buf_out;
447     int len_out;
448
449     struct filter_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
450
451     params[0] = 0;
452     set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
453
454     (*p->init)(p, &recWord);
455
456     if (schema && schema->stylesheet_xsp)
457     {
458         xmlNodePtr root_ptr;
459         xmlDocPtr resDoc = 
460             xsltApplyStylesheet(schema->stylesheet_xsp,
461                                 doc, params);
462         if (p->flagShowRecords)
463         {
464             xmlDocDumpMemory(resDoc, &buf_out, &len_out);
465             fwrite(buf_out, len_out, 1, stdout);
466             xmlFree(buf_out);
467         }
468         root_ptr = xmlDocGetRootElement(resDoc);
469         if (root_ptr)
470             index_record(tinfo, p, root_ptr, &recWord);
471         else
472         {
473             yaz_log(YLOG_WARN, "No root for index XML record."
474                     " split_level=%s stylesheet=%s",
475                     tinfo->split_level, schema->stylesheet);
476         }
477         xmlFreeDoc(resDoc);
478     }
479     xmlDocDumpMemory(doc, &buf_out, &len_out);
480     if (p->flagShowRecords)
481         fwrite(buf_out, len_out, 1, stdout);
482     (*p->setStoreData)(p, buf_out, len_out);
483     xmlFree(buf_out);
484     
485     xmlFreeDoc(doc);
486     return RECCTRL_EXTRACT_OK;
487 }
488
489 static int extract_split(struct filter_info *tinfo, struct recExtractCtrl *p)
490 {
491     int ret;
492     int split_depth = 0;
493     if (p->first_record)
494     {
495         if (tinfo->reader)
496             xmlFreeTextReader(tinfo->reader);
497         tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
498                                        p /* I/O handler */,
499                                        0 /* URL */, 
500                                        0 /* encoding */,
501                                        XML_PARSE_XINCLUDE);
502     }
503     if (!tinfo->reader)
504         return RECCTRL_EXTRACT_ERROR_GENERIC;
505
506     if (tinfo->split_level)
507         split_depth = atoi(tinfo->split_level);
508     ret = xmlTextReaderRead(tinfo->reader);
509     while (ret == 1) {
510         int type = xmlTextReaderNodeType(tinfo->reader);
511         int depth = xmlTextReaderDepth(tinfo->reader);
512         if (split_depth == 0 ||
513             (split_depth > 0 &&
514              type == XML_READER_TYPE_ELEMENT && split_depth == depth))
515         {
516             xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
517             xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
518             xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
519
520             xmlDocSetRootElement(doc, ptr2);
521
522             return extract_doc(tinfo, p, doc);   
523         }
524         ret = xmlTextReaderRead(tinfo->reader);
525     }
526     xmlFreeTextReader(tinfo->reader);
527     tinfo->reader = 0;
528     return RECCTRL_EXTRACT_EOF;
529 }
530
531 static int extract_full(struct filter_info *tinfo, struct recExtractCtrl *p)
532 {
533     if (p->first_record) /* only one record per stream */
534     {
535         xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
536                                   0 /* URL */,
537                                   0 /* encoding */,
538                                   XML_PARSE_XINCLUDE);
539         if (!doc)
540         {
541             return RECCTRL_EXTRACT_ERROR_GENERIC;
542         }
543         return extract_doc(tinfo, p, doc);
544     }
545     else
546         return RECCTRL_EXTRACT_EOF;
547 }
548
549 static int filter_extract(void *clientData, struct recExtractCtrl *p)
550 {
551     struct filter_info *tinfo = clientData;
552
553     odr_reset(tinfo->odr);
554
555     if (tinfo->split_level == 0 && tinfo->split_path == 0)
556         return extract_full(tinfo, p);
557     else
558     {
559         return extract_split(tinfo, p);
560     }
561 }
562
563 static int ioread_ret(void *context, char *buffer, int len)
564 {
565     struct recRetrieveCtrl *p = context;
566     return p->stream->readf(p->stream, buffer, len);
567 }
568
569 static int ioclose_ret(void *context)
570 {
571     return 0;
572 }
573
574
575 static const char *snippet_doc(struct recRetrieveCtrl *p, int text_mode,
576                                int window_size)
577 {
578     const char *xml_doc_str;
579     int ord = 0;
580     WRBUF wrbuf = wrbuf_alloc();
581     zebra_snippets *res = 
582         zebra_snippets_window(p->doc_snippet, p->hit_snippet, window_size);
583     zebra_snippet_word *w = zebra_snippets_list(res);
584
585     if (text_mode)
586         wrbuf_printf(wrbuf, "\'");
587     else
588         wrbuf_printf(wrbuf, "<snippet xmlns='%s'>\n", zebra_xslt_ns);
589     for (; w; w = w->next)
590     {
591         if (ord == 0)
592             ord = w->ord;
593         else if (ord != w->ord)
594
595             break;
596         if (text_mode)
597             wrbuf_printf(wrbuf, "%s%s%s ", 
598                          w->match ? "*" : "",
599                          w->term,
600                          w->match ? "*" : "");
601         else
602         {
603             wrbuf_printf(wrbuf, " <term ord='%d' seqno='" ZINT_FORMAT "' %s>", 
604                          w->ord, w->seqno,
605                          (w->match ? "match='1'" : ""));
606             wrbuf_xmlputs(wrbuf, w->term);
607             wrbuf_printf(wrbuf, "</term>\n");
608         }
609     }
610     if (text_mode)
611         wrbuf_printf(wrbuf, "\'");
612     else
613         wrbuf_printf(wrbuf, "</snippet>\n");
614
615     xml_doc_str = odr_strdup(p->odr, wrbuf_buf(wrbuf));
616
617     zebra_snippets_destroy(res);
618     wrbuf_free(wrbuf, 1);
619     return xml_doc_str;
620 }
621
622 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
623 {
624     /* const char *esn = zebra_xslt_ns; */
625     const char *esn = 0;
626     const char *params[32];
627     struct filter_info *tinfo = clientData;
628     xmlDocPtr resDoc;
629     xmlDocPtr doc;
630     struct filter_schema *schema;
631     int window_size = -1;
632
633     if (p->comp)
634     {
635         if (p->comp->which == Z_RecordComp_simple
636             && p->comp->u.simple->which == Z_ElementSetNames_generic)
637         {
638             esn = p->comp->u.simple->u.generic;
639         }
640         else if (p->comp->which == Z_RecordComp_complex 
641                  && p->comp->u.complex->generic->elementSpec
642                  && p->comp->u.complex->generic->elementSpec->which ==
643                  Z_ElementSpec_elementSetName)
644         {
645             esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
646         }
647     }
648     schema = lookup_schema(tinfo, esn);
649     if (!schema)
650     {
651         p->diagnostic =
652             YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
653         return 0;
654     }
655
656     if (schema->include_snippet)
657         window_size = atoi(schema->include_snippet);
658
659     params[0] = 0;
660     set_param_int(params, "id", p->localno, p->odr);
661     if (p->fname)
662         set_param_str(params, "filename", p->fname, p->odr);
663     if (p->staticrank >= 0)
664         set_param_int(params, "rank", p->staticrank, p->odr);
665
666     if (esn)
667         set_param_str(params, "schema", esn, p->odr);
668     else
669         if (schema->name)
670             set_param_str(params, "schema", schema->name, p->odr);
671         else if (schema->identifier)
672             set_param_str(params, "schema", schema->identifier, p->odr);
673         else
674             set_param_str(params, "schema", "", p->odr);
675
676     if (p->score >= 0)
677         set_param_int(params, "score", p->score, p->odr);
678     set_param_int(params, "size", p->recordSize, p->odr);
679
680     if (window_size >= 0)
681         set_param_xml(params, "snippet", snippet_doc(p, 1, window_size),
682                       p->odr);
683     doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
684                     0 /* URL */,
685                     0 /* encoding */,
686                     XML_PARSE_XINCLUDE);
687     if (!doc)
688     {
689         p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
690         return 0;
691     }
692
693     if (window_size >= 0)
694     {
695         xmlNodePtr node = xmlDocGetRootElement(doc);
696         const char *snippet_str = snippet_doc(p, 0, window_size);
697         xmlDocPtr snippet_doc = xmlParseMemory(snippet_str, strlen(snippet_str));
698         xmlAddChild(node, xmlDocGetRootElement(snippet_doc));
699     }
700     if (!schema->stylesheet_xsp)
701         resDoc = doc;
702     else
703     {
704         resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
705                                      doc, params);
706         xmlFreeDoc(doc);
707     }
708     if (!resDoc)
709     {
710         p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
711     }
712     else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
713     {
714         xmlChar *buf_out;
715         int len_out;
716
717         xsltSaveResultToString(&buf_out, &len_out, resDoc,
718                                schema->stylesheet_xsp); 
719
720         p->output_format = VAL_TEXT_XML;
721         p->rec_len = len_out;
722         p->rec_buf = odr_malloc(p->odr, p->rec_len);
723         memcpy(p->rec_buf, buf_out, p->rec_len);
724         xmlFree(buf_out);
725     }
726     else if (p->output_format == VAL_SUTRS)
727     {
728         xmlChar *buf_out;
729         int len_out;
730
731         xsltSaveResultToString(&buf_out, &len_out, resDoc,
732                                schema->stylesheet_xsp); 
733
734         p->output_format = VAL_SUTRS;
735         p->rec_len = len_out;
736         p->rec_buf = odr_malloc(p->odr, p->rec_len);
737         memcpy(p->rec_buf, buf_out, p->rec_len);
738         
739         xmlFree(buf_out);
740     }
741     else
742     {
743         p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
744     }
745     xmlFreeDoc(resDoc);
746     return 0;
747 }
748
749 static struct recType filter_type = {
750     0,
751     "alvis",
752     filter_init,
753     filter_config,
754     filter_destroy,
755     filter_extract,
756     filter_retrieve
757 };
758
759 RecType
760 #ifdef IDZEBRA_STATIC_ALVIS
761 idzebra_filter_alvis
762 #else
763 idzebra_filter
764 #endif
765
766 [] = {
767     &filter_type,
768     0,
769 };
770 /*
771  * Local variables:
772  * c-basic-offset: 4
773  * indent-tabs-mode: nil
774  * End:
775  * vim: shiftwidth=4 tabstop=8 expandtab
776  */
777