+ if (n->type != XML_ELEMENT_NODE)
+ continue;
+ if (!strcmp(n->name, "metadata"))
+ {
+ struct conf_metadata *md = 0;
+ struct conf_sortkey *sk = 0;
+ struct record_metadata **wheretoput, *newm;
+ int imeta;
+ int first, last;
+
+ type = xmlGetProp(n, "type");
+ value = xmlNodeListGetString(xdoc, n->children, 0);
+
+ if (!type || !value)
+ continue;
+
+ // First, find out what field we're looking at
+ for (imeta = 0; imeta < service->num_metadata; imeta++)
+ if (!strcmp(type, service->metadata[imeta].name))
+ {
+ md = &service->metadata[imeta];
+ if (md->sortkey_offset >= 0)
+ sk = &service->sortkeys[md->sortkey_offset];
+ break;
+ }
+ if (!md)
+ {
+ yaz_log(YLOG_WARN, "Ignoring unknown metadata element: %s", type);
+ continue;
+ }
+
+ // Find out where we are putting it
+ if (md->merge == Metadata_merge_no)
+ wheretoput = &res->metadata[imeta];
+ else
+ wheretoput = &cluster->metadata[imeta];
+
+ // Put it there
+ newm = nmem_malloc(se->nmem, sizeof(struct record_metadata));
+ newm->next = 0;
+ if (md->type == Metadata_type_generic)
+ {
+ char *p;
+ newm->data.text = nmem_strdup(se->nmem, value);
+ for (p = newm->data.text + strlen(newm->data.text) - 1;
+ p > newm->data.text && strchr(" ,/.", *p); p--)
+ *p = '\0';
+
+ }
+ else if (md->type == Metadata_type_year)
+ {
+ if (extract_years(value, &first, &last) < 0)
+ continue;
+ }
+ else
+ {
+ yaz_log(YLOG_WARN, "Unknown type in metadata element %s", type);
+ continue;
+ }
+ if (md->type == Metadata_type_year && md->merge != Metadata_merge_range)
+ {
+ yaz_log(YLOG_WARN, "Only range merging supported for years");
+ continue;
+ }
+ if (md->merge == Metadata_merge_unique)
+ {
+ struct record_metadata *mnode;
+ for (mnode = *wheretoput; mnode; mnode = mnode->next)
+ if (!strcmp(mnode->data.text, newm->data.text))
+ break;
+ if (!mnode)
+ {
+ newm->next = *wheretoput;
+ *wheretoput = newm;
+ }
+ }
+ else if (md->merge == Metadata_merge_longest)
+ {
+ if (!*wheretoput ||
+ strlen(newm->data.text) > strlen((*wheretoput)->data.text))
+ {
+ *wheretoput = newm;
+ if (sk)
+ {
+ char *s = nmem_strdup(se->nmem, newm->data.text);
+ if (!cluster->sortkeys[md->sortkey_offset])
+ cluster->sortkeys[md->sortkey_offset] =
+ nmem_malloc(se->nmem, sizeof(union data_types));
+ normalize_mergekey(s,
+ (sk->type == Metadata_sortkey_skiparticle));
+ cluster->sortkeys[md->sortkey_offset]->text = s;
+ }
+ }
+ }
+ else if (md->merge == Metadata_merge_all || md->merge == Metadata_merge_no)
+ {
+ newm->next = *wheretoput;
+ *wheretoput = newm;
+ }
+ else if (md->merge == Metadata_merge_range)
+ {
+ assert(md->type == Metadata_type_year);
+ if (!*wheretoput)
+ {
+ *wheretoput = newm;
+ (*wheretoput)->data.number.min = first;
+ (*wheretoput)->data.number.max = last;
+ if (sk)
+ cluster->sortkeys[md->sortkey_offset] = &newm->data;
+ }
+ else
+ {
+ if (first < (*wheretoput)->data.number.min)
+ (*wheretoput)->data.number.min = first;
+ if (last > (*wheretoput)->data.number.max)
+ (*wheretoput)->data.number.max = last;
+ }
+#ifdef GAGA
+ if (sk)
+ {
+ union data_types *sdata = cluster->sortkeys[md->sortkey_offset];
+ yaz_log(YLOG_LOG, "SK range: %d-%d", sdata->number.min, sdata->number.max);
+ }
+#endif
+ }
+ else
+ yaz_log(YLOG_WARN, "Don't know how to merge on element name %s", md->name);