2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marcdump.c,v 1.30 2005-04-20 13:17:29 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] [-e] [-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:xOeXIf: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_XCHANGE;
153 xml = YAZ_MARC_MARCXML;
156 xml = YAZ_MARC_ISO2709;
165 inf = fopen (arg, "rb");
169 fprintf (stderr, "%s: cannot open %s:%s\n",
170 prog, arg, strerror (errno));
174 fprintf (cfile, "char *marc_records[] = {\n");
177 yaz_marc_t mt = yaz_marc_create();
182 cd = yaz_iconv_open(to, from);
185 fprintf(stderr, "conversion from %s to %s "
186 "unsupported\n", from, to);
189 yaz_marc_iconv(mt, cd);
191 yaz_marc_xml(mt, xml);
192 yaz_marc_debug(mt, verbose);
199 r = fread (buf, 1, 5, inf);
202 if (r && print_offset && verbose)
203 printf ("<!-- Extra %d bytes at end of file -->\n", r);
206 while (*buf < '0' || *buf > '9')
209 long off = ftell(inf) - 5;
210 if (verbose || print_offset)
211 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
213 *buf & 0xff, *buf & 0xff,
215 for (i = 0; i<4; i++)
217 r = fread(buf+4, 1, 1, inf);
223 if (verbose || print_offset)
224 printf ("<!-- End of file with data -->\n");
229 long off = ftell(inf) - 5;
230 printf ("<!-- Record %d offset %ld (0x%lx) -->\n",
233 len = atoi_n(buf, 5);
234 if (len < 25 || len > 100000)
236 long off = ftell(inf) - 5;
237 printf("Bad Length %d read at offset %ld (%lx)\n",
238 len, (long) off, (long) off);
242 r = fread (buf + 5, 1, len, inf);
245 r = yaz_marc_decode_buf (mt, buf, -1, &result, &rlen);
247 fwrite (result, rlen, 1, stdout);
249 if (r > 0 && libxml_dom_test)
251 xmlDocPtr doc = xmlParseMemory(result, rlen);
253 fprintf(stderr, "xmLParseMemory failed\n");
257 xmlXPathContextPtr xpathCtx;
258 xmlXPathObjectPtr xpathObj;
259 static const char *xpathExpr[] = {
260 "/record/datafield[@tag='245']/subfield[@code='a']",
261 "/record/datafield[@tag='100']/subfield",
262 "/record/datafield[@tag='245']/subfield[@code='a']",
263 "/record/datafield[@tag='650']/subfield",
264 "/record/datafield[@tag='650']",
267 xpathCtx = xmlXPathNewContext(doc);
269 for (i = 0; xpathExpr[i]; i++) {
270 xpathObj = xmlXPathEvalExpression(xpathExpr[i], xpathCtx);
271 if(xpathObj == NULL) {
272 fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr[i]);
276 print_xpath_nodes(xpathObj->nodesetval, stdout);
277 xmlXPathFreeObject(xpathObj);
280 xmlXPathFreeContext(xpathCtx);
290 fprintf (cfile, ",");
291 fprintf (cfile, "\n");
292 for (i = 0; i < r; i++)
295 fprintf (cfile, " \"");
296 fprintf (cfile, "\\x%02X", p[i] & 255);
298 if (i < r - 1 && (i & 15) == 15)
299 fprintf (cfile, "\"\n");
302 fprintf (cfile, "\"\n");
311 yaz_marc_destroy(mt);
314 fprintf (cfile, "};\n");