+ /* if identifier length is 2 (most MARCs),
+ the code is a single character .. However we've
+ seen multibyte codes, so see how big it really is */
+ size_t using_code_len =
+ (identifier_length != 2) ? identifier_length - 1
+ :
+ cdata_one_character(mt, s->code_data);
+
+ wrbuf_puts (wr, mt->subfield_str);
+ wrbuf_iconv_write(wr, mt->iconv_cd, s->code_data,
+ using_code_len);
+ wrbuf_iconv_puts(wr, mt->iconv_cd, " ");
+ wrbuf_iconv_puts(wr, mt->iconv_cd,
+ s->code_data + using_code_len);
+ marc_iconv_reset(mt, wr);
+ }
+ wrbuf_puts (wr, mt->endline_str);
+ break;
+ case YAZ_MARC_CONTROLFIELD:
+ wrbuf_printf(wr, "%s", n->u.controlfield.tag);
+ wrbuf_iconv_puts(wr, mt->iconv_cd, " ");
+ wrbuf_iconv_puts(wr, mt->iconv_cd, n->u.controlfield.data);
+ marc_iconv_reset(mt, wr);
+ wrbuf_puts (wr, mt->endline_str);
+ break;
+ case YAZ_MARC_COMMENT:
+ wrbuf_puts(wr, "(");
+ wrbuf_iconv_write(wr, mt->iconv_cd,
+ n->u.comment, strlen(n->u.comment));
+ marc_iconv_reset(mt, wr);
+ wrbuf_puts(wr, ")\n");
+ break;
+ case YAZ_MARC_LEADER:
+ wrbuf_printf(wr, "%s\n", n->u.leader);
+ }
+ }
+ wrbuf_puts(wr, "\n");
+ return 0;
+}
+
+int yaz_marc_write_mode(yaz_marc_t mt, WRBUF wr)
+{
+ switch(mt->xml)
+ {
+ case YAZ_MARC_LINE:
+ return yaz_marc_write_line(mt, wr);
+ case YAZ_MARC_MARCXML:
+ return yaz_marc_write_marcxml(mt, wr);
+ case YAZ_MARC_XCHANGE:
+ return yaz_marc_write_marcxchange(mt, wr, 0, 0); /* no format, type */
+ case YAZ_MARC_ISO2709:
+ return yaz_marc_write_iso2709(mt, wr);
+ case YAZ_MARC_CHECK:
+ return yaz_marc_write_check(mt, wr);
+ }
+ return -1;
+}
+
+/** \brief common MARC XML/Xchange writer
+ \param mt handle
+ \param wr WRBUF output
+ \param ns XMLNS for the elements
+ \param format record format (e.g. "MARC21")
+ \param type record type (e.g. "Bibliographic")
+*/
+static int yaz_marc_write_marcxml_ns1(yaz_marc_t mt, WRBUF wr,
+ const char *ns,
+ const char *format,
+ const char *type)
+{
+ struct yaz_marc_node *n;
+ int identifier_length;
+ const char *leader = 0;
+
+ for (n = mt->nodes; n; n = n->next)
+ if (n->which == YAZ_MARC_LEADER)
+ {
+ leader = n->u.leader;
+ break;
+ }
+
+ if (!leader)
+ return -1;
+ if (!atoi_n_check(leader+11, 1, &identifier_length))
+ return -1;
+
+ wrbuf_printf(wr, "<record xmlns=\"%s\"", ns);
+ if (format)
+ wrbuf_printf(wr, " format=\"%.80s\"", format);
+ if (type)
+ wrbuf_printf(wr, " type=\"%.80s\"", type);
+ wrbuf_printf(wr, ">\n");
+ for (n = mt->nodes; n; n = n->next)
+ {
+ struct yaz_marc_subfield *s;
+
+ switch(n->which)
+ {
+ case YAZ_MARC_DATAFIELD:
+ wrbuf_printf(wr, " <datafield tag=\"");
+ wrbuf_iconv_write_cdata(wr, mt->iconv_cd, n->u.datafield.tag,
+ strlen(n->u.datafield.tag));
+ wrbuf_printf(wr, "\"");
+ if (n->u.datafield.indicator)
+ {
+ int i;
+ for (i = 0; n->u.datafield.indicator[i]; i++)