2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marc_read_xml.c,v 1.4 2007-12-17 20:59:30 adam Exp $
9 * \file marc_read_xml.c
10 * \brief Implements reading of MARC as XML
24 #include <yaz/marcdisp.h>
25 #include <yaz/wrbuf.h>
26 #include <yaz/yaz-util.h>
27 #include <yaz/nmem_xml.h>
30 #include <libxml/tree.h>
34 int yaz_marc_read_xml_subfields(yaz_marc_t mt, const xmlNode *ptr)
36 NMEM nmem = yaz_marc_get_nmem(mt);
37 for (; ptr; ptr = ptr->next)
39 if (ptr->type == XML_ELEMENT_NODE)
41 if (!strcmp((const char *) ptr->name, "subfield"))
43 size_t ctrl_data_len = 0;
44 char *ctrl_data_buf = 0;
45 const xmlNode *p = 0, *ptr_code = 0;
46 struct _xmlAttr *attr;
47 for (attr = ptr->properties; attr; attr = attr->next)
48 if (!strcmp((const char *)attr->name, "code"))
49 ptr_code = attr->children;
53 mt, "Bad attribute '%.80s' for 'subfield'",
60 mt, "Missing attribute 'code' for 'subfield'" );
63 if (ptr_code->type == XML_TEXT_NODE)
66 strlen((const char *)ptr_code->content);
71 mt, "Missing value for 'code' in 'subfield'" );
74 for (p = ptr->children; p ; p = p->next)
75 if (p->type == XML_TEXT_NODE)
76 ctrl_data_len += strlen((const char *)p->content);
77 ctrl_data_buf = (char *) nmem_malloc(nmem, ctrl_data_len+1);
78 strcpy(ctrl_data_buf, (const char *)ptr_code->content);
79 for (p = ptr->children; p ; p = p->next)
80 if (p->type == XML_TEXT_NODE)
81 strcat(ctrl_data_buf, (const char *)p->content);
82 yaz_marc_add_subfield(mt, ctrl_data_buf, ctrl_data_len);
87 mt, "Expected element 'subfield', got '%.80s'", ptr->name);
95 static int yaz_marc_read_xml_leader(yaz_marc_t mt, const xmlNode **ptr_p)
98 int identifier_length;
100 int length_data_entry;
102 int length_implementation;
103 const char *leader = 0;
104 const xmlNode *ptr = *ptr_p;
106 for(; ptr; ptr = ptr->next)
107 if (ptr->type == XML_ELEMENT_NODE)
109 if (!strcmp((const char *) ptr->name, "leader"))
111 xmlNode *p = ptr->children;
112 for(; p; p = p->next)
113 if (p->type == XML_TEXT_NODE)
114 leader = (const char *) p->content;
120 mt, "Expected element 'leader', got '%.80s'", ptr->name);
125 yaz_marc_cprintf(mt, "Missing element 'leader'");
128 if (strlen(leader) != 24)
130 yaz_marc_cprintf(mt, "Bad length %d of leader data."
131 " Must have length of 24 characters", strlen(leader));
134 yaz_marc_set_leader(mt, leader,
140 &length_implementation);
145 static int yaz_marc_read_xml_fields(yaz_marc_t mt, const xmlNode *ptr)
147 for(; ptr; ptr = ptr->next)
148 if (ptr->type == XML_ELEMENT_NODE)
150 if (!strcmp((const char *) ptr->name, "controlfield"))
152 const xmlNode *ptr_tag = 0;
153 struct _xmlAttr *attr;
154 for (attr = ptr->properties; attr; attr = attr->next)
155 if (!strcmp((const char *)attr->name, "tag"))
156 ptr_tag = attr->children;
160 mt, "Bad attribute '%.80s' for 'controlfield'",
167 mt, "Missing attribute 'tag' for 'controlfield'" );
170 yaz_marc_add_controlfield_xml(mt, ptr_tag, ptr->children);
172 else if (!strcmp((const char *) ptr->name, "datafield"))
174 char indstr[11]; /* 0(unused), 1,....9, + zero term */
175 const xmlNode *ptr_tag = 0;
176 struct _xmlAttr *attr;
178 for (i = 0; i<11; i++)
180 for (attr = ptr->properties; attr; attr = attr->next)
181 if (!strcmp((const char *)attr->name, "tag"))
182 ptr_tag = attr->children;
183 else if (strlen((const char *)attr->name) == 4 &&
184 !memcmp(attr->name, "ind", 3))
186 int no = atoi((const char *)attr->name+3);
188 && attr->children->type == XML_TEXT_NODE)
189 indstr[no] = attr->children->content[0];
194 mt, "Bad attribute '%.80s' for 'datafield'",
200 mt, "Missing attribute 'tag' for 'datafield'" );
203 /* note that indstr[0] is unused so we use indstr[1..] */
204 yaz_marc_add_datafield_xml(mt, ptr_tag,
205 indstr+1, strlen(indstr+1));
207 if (yaz_marc_read_xml_subfields(mt, ptr->children))
213 "Expected element controlfield or datafield,"
214 " got %.80s", ptr->name);
222 int yaz_marc_read_xml(yaz_marc_t mt, const xmlNode *ptr)
227 for(; ptr; ptr = ptr->next)
228 if (ptr->type == XML_ELEMENT_NODE)
230 if (!strcmp((const char *) ptr->name, "record"))
235 mt, "Unknown element '%.80s' in MARC XML reader",
242 yaz_marc_cprintf(mt, "Missing element 'record' in MARC XML record");
245 /* ptr points to record node now */
247 if (yaz_marc_read_xml_leader(mt, &ptr))
249 return yaz_marc_read_xml_fields(mt, ptr->next);
259 * indent-tabs-mode: nil
261 * vim: shiftwidth=4 tabstop=8 expandtab