2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marc_read_xml.c,v 1.3 2007-05-06 20:12:20 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);
126 yaz_marc_cprintf(mt, "Missing element 'leader'");
129 if (strlen(leader) != 24)
131 yaz_marc_cprintf(mt, "Bad length %d of leader data."
132 " Must have length of 24 characters", strlen(leader));
135 yaz_marc_set_leader(mt, leader,
141 &length_implementation);
146 static int yaz_marc_read_xml_fields(yaz_marc_t mt, const xmlNode *ptr)
148 for(; ptr; ptr = ptr->next)
149 if (ptr->type == XML_ELEMENT_NODE)
151 if (!strcmp((const char *) ptr->name, "controlfield"))
153 const xmlNode *ptr_tag = 0;
154 struct _xmlAttr *attr;
155 for (attr = ptr->properties; attr; attr = attr->next)
156 if (!strcmp((const char *)attr->name, "tag"))
157 ptr_tag = attr->children;
161 mt, "Bad attribute '%.80s' for 'controlfield'",
168 mt, "Missing attribute 'tag' for 'controlfield'" );
171 yaz_marc_add_controlfield_xml(mt, ptr_tag, ptr->children);
173 else if (!strcmp((const char *) ptr->name, "datafield"))
175 char indstr[11]; /* 0(unused), 1,....9, + zero term */
176 const xmlNode *ptr_tag = 0;
177 struct _xmlAttr *attr;
179 for (i = 0; i<11; i++)
181 for (attr = ptr->properties; attr; attr = attr->next)
182 if (!strcmp((const char *)attr->name, "tag"))
183 ptr_tag = attr->children;
184 else if (strlen((const char *)attr->name) == 4 &&
185 !memcmp(attr->name, "ind", 3))
187 int no = atoi((const char *)attr->name+3);
189 && attr->children->type == XML_TEXT_NODE)
190 indstr[no] = attr->children->content[0];
195 mt, "Bad attribute '%.80s' for 'datafield'",
202 mt, "Missing attribute 'tag' for 'datafield'" );
205 /* note that indstr[0] is unused so we use indstr[1..] */
206 yaz_marc_add_datafield_xml(mt, ptr_tag,
207 indstr+1, strlen(indstr+1));
209 if (yaz_marc_read_xml_subfields(mt, ptr->children))
215 "Expected element controlfield or datafield,"
216 " got %.80s", ptr->name);
224 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