2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marcdump.c,v 1.26 2005-01-15 19:47:15 adam Exp $
13 #include <libxml/parser.h>
14 #include <libxml/tree.h>
16 #include <libxml/xpath.h>
17 #include <libxml/xpathInternals.h>
34 #include <yaz/marcdisp.h>
35 #include <yaz/yaz-util.h>
36 #include <yaz/xmalloc.h>
37 #include <yaz/options.h>
46 static void usage(const char *prog)
48 fprintf (stderr, "Usage: %s [-c cfile] [-f from] [-t to] [-x] [-O] [-X] [-I] [-v] file...\n",
53 void print_xpath_nodes(xmlNodeSetPtr nodes, FILE* output) {
59 size = (nodes) ? nodes->nodeNr : 0;
61 fprintf(output, "Result (%d nodes):\n", size);
62 for(i = 0; i < size; ++i) {
63 assert(nodes->nodeTab[i]);
65 if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL)
69 ns = (xmlNsPtr)nodes->nodeTab[i];
70 cur = (xmlNodePtr)ns->next;
72 fprintf(output, "= namespace \"%s\"=\"%s\" for node %s:%s\n",
73 ns->prefix, ns->href, cur->ns->href, cur->name);
75 fprintf(output, "= namespace \"%s\"=\"%s\" for node %s\n",
76 ns->prefix, ns->href, cur->name);
79 else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE)
81 cur = nodes->nodeTab[i];
83 fprintf(output, "= element node \"%s:%s\"\n",
84 cur->ns->href, cur->name);
88 fprintf(output, "= element node \"%s\"\n",
94 cur = nodes->nodeTab[i];
95 fprintf(output, "= node \"%s\": type %d\n", cur->name, cur->type);
101 int main (int argc, char **argv)
104 int libxml_dom_test = 0;
105 int print_offset = 0;
114 char *from = 0, *to = 0;
118 setlocale(LC_CTYPE, "");
122 to = nl_langinfo(CODESET);
126 while ((r = options("pvc:xOXIf:t:2", argv, argc, &arg)) != -2)
141 cfile = fopen (arg, "w");
144 xml = YAZ_MARC_SIMPLEXML;
147 xml = YAZ_MARC_OAIMARC;
150 xml = YAZ_MARC_MARCXML;
153 xml = YAZ_MARC_ISO2709;
162 inf = fopen (arg, "rb");
166 fprintf (stderr, "%s: cannot open %s:%s\n",
167 prog, arg, strerror (errno));
171 fprintf (cfile, "char *marc_records[] = {\n");
174 yaz_marc_t mt = yaz_marc_create();
179 cd = yaz_iconv_open(to, from);
182 fprintf(stderr, "conversion from %s to %s "
183 "unsupported\n", from, to);
186 yaz_marc_iconv(mt, cd);
188 yaz_marc_xml(mt, xml);
189 yaz_marc_debug(mt, verbose);
196 r = fread (buf, 1, 5, inf);
199 if (r && print_offset)
200 printf ("Extra %d bytes", r);
205 long off = ftell(inf);
206 printf ("Record %d offset %ld\n", num, (long) off);
208 len = atoi_n(buf, 5);
209 if (len < 25 || len > 100000)
212 r = fread (buf + 5, 1, len, inf);
215 r = yaz_marc_decode_buf (mt, buf, -1, &result, &rlen);
218 fwrite (result, rlen, 1, stdout);
222 xmlDocPtr doc = xmlParseMemory(result, rlen);
224 fprintf(stderr, "xmLParseMemory failed\n");
228 xmlXPathContextPtr xpathCtx;
229 xmlXPathObjectPtr xpathObj;
230 static const char *xpathExpr[] = {
231 "/record/datafield[@tag='245']/subfield[@code='a']",
232 "/record/datafield[@tag='100']/subfield",
233 "/record/datafield[@tag='245']/subfield[@code='a']",
234 "/record/datafield[@tag='650']/subfield",
235 "/record/datafield[@tag='650']",
238 xpathCtx = xmlXPathNewContext(doc);
240 for (i = 0; xpathExpr[i]; i++) {
241 xpathObj = xmlXPathEvalExpression(xpathExpr[i], xpathCtx);
242 if(xpathObj == NULL) {
243 fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr[i]);
247 print_xpath_nodes(xpathObj->nodesetval, stdout);
248 xmlXPathFreeObject(xpathObj);
251 xmlXPathFreeContext(xpathCtx);
261 fprintf (cfile, ",");
262 fprintf (cfile, "\n");
263 for (i = 0; i < r; i++)
266 fprintf (cfile, " \"");
267 fprintf (cfile, "\\x%02X", p[i] & 255);
269 if (i < r - 1 && (i & 15) == 15)
270 fprintf (cfile, "\"\n");
273 fprintf (cfile, "\"\n");
280 yaz_marc_destroy(mt);
283 fprintf (cfile, "};\n");