1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
7 * \file marc_read_xml.c
8 * \brief Implements reading of MARC as XML
22 #include <yaz/marcdisp.h>
23 #include <yaz/wrbuf.h>
24 #include <yaz/yaz-util.h>
25 #include <yaz/nmem_xml.h>
28 #include <libxml/tree.h>
32 int yaz_marc_read_xml_subfields(yaz_marc_t mt, const xmlNode *ptr)
34 NMEM nmem = yaz_marc_get_nmem(mt);
35 for (; ptr; ptr = ptr->next)
37 if (ptr->type == XML_ELEMENT_NODE)
39 if (!strcmp((const char *) ptr->name, "subfield"))
41 size_t ctrl_data_len = 0;
42 char *ctrl_data_buf = 0;
43 const xmlNode *p = 0, *ptr_code = 0;
44 struct _xmlAttr *attr;
45 for (attr = ptr->properties; attr; attr = attr->next)
46 if (!strcmp((const char *)attr->name, "code"))
47 ptr_code = attr->children;
51 mt, "Bad attribute '%.80s' for 'subfield'",
58 mt, "Missing attribute 'code' for 'subfield'" );
61 if (ptr_code->type == XML_TEXT_NODE)
64 strlen((const char *)ptr_code->content);
69 mt, "Missing value for 'code' in 'subfield'" );
72 for (p = ptr->children; p ; p = p->next)
73 if (p->type == XML_TEXT_NODE)
74 ctrl_data_len += strlen((const char *)p->content);
75 ctrl_data_buf = (char *) nmem_malloc(nmem, ctrl_data_len+1);
76 strcpy(ctrl_data_buf, (const char *)ptr_code->content);
77 for (p = ptr->children; p ; p = p->next)
78 if (p->type == XML_TEXT_NODE)
79 strcat(ctrl_data_buf, (const char *)p->content);
80 yaz_marc_add_subfield(mt, ctrl_data_buf, ctrl_data_len);
85 mt, "Expected element 'subfield', got '%.80s'", ptr->name);
93 static int yaz_marc_read_xml_leader(yaz_marc_t mt, const xmlNode **ptr_p)
96 int identifier_length;
98 int length_data_entry;
100 int length_implementation;
101 const char *leader = 0;
102 const xmlNode *ptr = *ptr_p;
104 for(; ptr; ptr = ptr->next)
105 if (ptr->type == XML_ELEMENT_NODE)
107 if (!strcmp((const char *) ptr->name, "leader"))
109 xmlNode *p = ptr->children;
110 for(; p; p = p->next)
111 if (p->type == XML_TEXT_NODE)
112 leader = (const char *) p->content;
118 mt, "Expected element 'leader', got '%.80s'", ptr->name);
123 yaz_marc_cprintf(mt, "Missing element 'leader'");
126 if (strlen(leader) != 24)
128 yaz_marc_cprintf(mt, "Bad length %d of leader data."
129 " Must have length of 24 characters", strlen(leader));
132 yaz_marc_set_leader(mt, leader,
138 &length_implementation);
143 static int yaz_marc_read_xml_fields(yaz_marc_t mt, const xmlNode *ptr)
145 for(; ptr; ptr = ptr->next)
146 if (ptr->type == XML_ELEMENT_NODE)
148 if (!strcmp((const char *) ptr->name, "controlfield"))
150 const xmlNode *ptr_tag = 0;
151 struct _xmlAttr *attr;
152 for (attr = ptr->properties; attr; attr = attr->next)
153 if (!strcmp((const char *)attr->name, "tag"))
154 ptr_tag = attr->children;
158 mt, "Bad attribute '%.80s' for 'controlfield'",
165 mt, "Missing attribute 'tag' for 'controlfield'" );
168 yaz_marc_add_controlfield_xml(mt, ptr_tag, ptr->children);
170 else if (!strcmp((const char *) ptr->name, "datafield"))
172 char indstr[11]; /* 0(unused), 1,....9, + zero term */
173 const xmlNode *ptr_tag = 0;
174 struct _xmlAttr *attr;
176 for (i = 0; i<11; i++)
178 for (attr = ptr->properties; attr; attr = attr->next)
179 if (!strcmp((const char *)attr->name, "tag"))
180 ptr_tag = attr->children;
181 else if (strlen((const char *)attr->name) == 4 &&
182 !memcmp(attr->name, "ind", 3))
184 int no = atoi((const char *)attr->name+3);
186 && attr->children->type == XML_TEXT_NODE)
187 indstr[no] = attr->children->content[0];
192 mt, "Bad attribute '%.80s' for 'datafield'",
198 mt, "Missing attribute 'tag' for 'datafield'" );
201 /* note that indstr[0] is unused so we use indstr[1..] */
202 yaz_marc_add_datafield_xml(mt, ptr_tag,
203 indstr+1, strlen(indstr+1));
205 if (yaz_marc_read_xml_subfields(mt, ptr->children))
211 "Expected element controlfield or datafield,"
212 " got %.80s", ptr->name);
221 int yaz_marc_read_xml(yaz_marc_t mt, const xmlNode *ptr)
225 for(; ptr; ptr = ptr->next)
226 if (ptr->type == XML_ELEMENT_NODE)
228 if (!strcmp((const char *) ptr->name, "record"))
233 mt, "Unknown element '%.80s' in MARC XML reader",
240 yaz_marc_cprintf(mt, "Missing element 'record' in MARC XML record");
243 /* ptr points to record node now */
245 if (yaz_marc_read_xml_leader(mt, &ptr))
247 return yaz_marc_read_xml_fields(mt, ptr->next);
255 * c-file-style: "Stroustrup"
256 * indent-tabs-mode: nil
258 * vim: shiftwidth=4 tabstop=8 expandtab