1 /* $Id: srw-xslt.c,v 1.1 2003-01-06 08:20:28 adam Exp $
2 Copyright (C) 2002-2003
5 This file is part of the YAZ toolkit.
11 #include <libxml/parser.h>
12 #include <libxml/tree.h>
13 #include <libxslt/xsltutils.h>
14 #include <libxslt/transform.h>
17 #include <yaz/srw-util.h>
19 struct xslt_maps_info {
27 struct xslt_map_result_info {
37 xslt_maps xslt_maps_create()
39 xslt_maps m = malloc(sizeof(*m));
46 void xslt_maps_free(xslt_maps m)
54 int xslt_maps_file(xslt_maps m, const char *f)
59 m->doc = xmlParseFile(f);
68 void xslt_map_free (xslt_map_result res)
80 xslt_map_result xslt_map (xslt_maps m, const char *schema_source,
81 const char *schema_target,
82 const char *in_buf, int in_len)
85 const char *map_ns = "http://indexdata.dk/srw/schema-mappings/v1.0/";
90 ptr = xmlDocGetRootElement(m->doc);
91 while (ptr && ptr->type == XML_ELEMENT_NODE)
93 if (!strcmp(ptr->name, "schema-mappings"))
99 for (; ptr; ptr = ptr->next)
101 if (ptr->type == XML_ELEMENT_NODE && !strcmp(ptr->name, "map")
102 && !strcmp(ptr->ns->href, map_ns) && ptr->children)
104 xmlNodePtr src = ptr->children;
107 const char *full_target = 0;
108 const char *filename = 0;
110 for (; src; src = src->next)
112 if (src->type == XML_ELEMENT_NODE &&
113 !strcmp(src->name, "schema") &&
114 !strcmp(src->ns->href, map_ns))
116 struct _xmlAttr *attr = src->properties;
117 for (; attr; attr = attr->next)
118 if (!strcmp(attr->name, "target") &&
120 attr->children->type == XML_TEXT_NODE)
122 full_target = attr->children->content;
123 if (!strcmp(attr->children->content,
127 else if (!strcmp(attr->name, "source")
129 && attr->children->type == XML_TEXT_NODE)
132 if (!strcmp(schema_source,
133 attr->children->content))
136 else if (!strcmp(attr->name, "alias")
138 && attr->children->type == XML_TEXT_NODE)
140 if (!strcmp(attr->children->content, schema_target))
144 if (src->type == XML_ELEMENT_NODE &&
145 !strcmp(src->name, "stylesheet") &&
146 !strcmp(src->ns->href, map_ns))
148 struct _xmlAttr *attr = src->properties;
149 for (; attr; attr = attr->next)
150 if (!strcmp(attr->name, "filename") &&
152 attr->children->type == XML_TEXT_NODE)
154 filename = attr->children->content;
158 if (source_ok && target_ok)
162 xslt_map_result out = malloc(sizeof(*out));
163 xmlDocPtr res, doc = xmlParseMemory(in_buf, in_len);
164 xmlDocPtr xslt_doc = xmlParseFile(filename);
165 xsltStylesheetPtr xsp;
167 xsp = xsltParseStylesheetDoc(xslt_doc);
169 res = xsltApplyStylesheet(xsp, doc, 0);
171 xmlDocDumpMemory (res, &out->buf, &out->len);
173 xsltFreeStylesheet(xsp);
178 out->schema = strdup(full_target);
183 xslt_map_result out = malloc(sizeof(*out));
184 out->buf = xmlMalloc(in_len);
185 memcpy (out->buf, in_buf, in_len);
187 out->schema = strdup(full_target);
197 char *xslt_map_result_buf(xslt_map_result res)
201 int xslt_map_result_len(xslt_map_result res)
206 char *xslt_map_result_schema(xslt_map_result res)