+const char *tag_value_extract(const char *name, char tag_buffer[5])
+{
+ size_t length = strlen(name);
+ if (length == 3)
+ {
+ strcpy(tag_buffer, name);
+ return tag_buffer;
+ }
+ return 0;
+}
+
+// Given a xmlNode ptr, extract a value from either a element name or from a given attribute
+char *element_attribute_value_extract(const xmlNode *ptr,
+ const char *attribute_name,
+ NMEM nmem)
+{
+ const char *name = (const char *) ptr->name;
+ size_t length = strlen(name);
+ xmlAttr *attr;
+ if (length > 1 )
+ return nmem_strdup(nmem, name+1);
+ // TODO Extract from attribute where matches attribute_name
+ for (attr = ptr->properties; attr; attr = attr->next)
+ if (!strcmp((const char *)attr->name, attribute_name))
+ return nmem_text_node_cdata(attr->children, nmem);
+ return 0;
+}
+
+
+int yaz_marc_read_turbo_xml_subfields(yaz_marc_t mt, const xmlNode *ptr)
+{
+ for (; ptr; ptr = ptr->next)
+ {
+ if (ptr->type == XML_ELEMENT_NODE)
+ {
+ if (!strncmp((const char *) ptr->name, "s", 1))
+ {
+ NMEM nmem = yaz_marc_get_nmem(mt);
+ xmlNode *p;
+ size_t ctrl_data_len = 0;
+ char *ctrl_data_buf = 0;
+ const char *tag_value = element_attribute_value_extract(ptr, "code", nmem);
+ if (!tag_value)
+ {
+ yaz_marc_cprintf(
+ mt, "Missing 'code' value for 'subfield'" );
+ return -1;
+ }
+
+ ctrl_data_len = strlen((const char *) tag_value);
+ // Extract (length) from CDATA
+ for (p = ptr->children; p ; p = p->next)
+ if (p->type == XML_TEXT_NODE)
+ ctrl_data_len += strlen((const char *)p->content);
+ // Allocate memory for code value (1 character (can be multi-byte) and data
+ ctrl_data_buf = (char *) nmem_malloc(nmem, ctrl_data_len+1);
+ // Build a string with "<Code><data>"
+ strcpy(ctrl_data_buf, (const char *) tag_value);
+ for (p = ptr->children; p ; p = p->next)
+ if (p->type == XML_TEXT_NODE)
+ strcat(ctrl_data_buf, (const char *)p->content);
+ yaz_marc_add_subfield(mt, ctrl_data_buf, ctrl_data_len);
+ }
+ else
+ {
+ yaz_marc_cprintf(
+ mt, "Expected element 'subfield', got '%.80s'", ptr->name);
+ return -1;
+ }
+ }
+ }
+ return 0;
+}
+
+