2 * xsltproc.c: user program for the XSL Transformation 1.0 engine
4 * See Copyright for the status of this software.
9 #include "libxslt/libxslt.h"
10 #include "libexslt/exslt.h"
14 #ifdef HAVE_SYS_TIME_H
23 #include <libxml/xmlmemory.h>
24 #include <libxml/debugXML.h>
25 #include <libxml/HTMLtree.h>
26 #include <libxml/xmlIO.h>
27 #ifdef LIBXML_DOCB_ENABLED
28 #include <libxml/DOCBparser.h>
30 #ifdef LIBXML_XINCLUDE_ENABLED
31 #include <libxml/xinclude.h>
33 #ifdef LIBXML_CATALOG_ENABLED
34 #include <libxml/catalog.h>
36 #include <libxml/parserInternals.h>
38 #include <libxslt/xslt.h>
39 #include <libxslt/xsltInternals.h>
40 #include <libxslt/transform.h>
41 #include <libxslt/xsltutils.h>
42 #include <libxslt/extensions.h>
44 #include <libexslt/exsltconfig.h>
49 #pragma comment(lib, "ws2_32.lib")
50 #define gettimeofday(p1,p2)
58 static int repeat = 0;
59 static int timing = 0;
60 static int novalid = 0;
62 #ifdef LIBXML_DOCB_ENABLED
63 static int docbook = 0;
65 #ifdef LIBXML_HTML_ENABLED
68 #ifdef LIBXML_XINCLUDE_ENABLED
69 static int xinclude = 0;
71 static int profile = 0;
73 static xmlExternalEntityLoader defaultLoader = NULL;
75 static struct timeval begin, end;
76 static const char *params[16 + 1];
77 static int nbparams = 0;
78 static const char *output = NULL;
80 static xmlParserInputPtr
81 xsltNoNetExternalEntityLoader(const char *URL, const char *ID,
82 xmlParserCtxtPtr ctxt) {
84 if ((!xmlStrncasecmp((const xmlChar *) URL,
85 (const xmlChar *) "ftp://", 6)) ||
86 (!xmlStrncasecmp((const xmlChar *) URL,
87 (const xmlChar *) "http://", 7))) {
88 fprintf(stderr, "Attempt to load network entity %s \n", URL);
93 if (defaultLoader != NULL) {
94 return(defaultLoader(URL, ID, ctxt));
100 xsltProcess(xmlDocPtr doc, xsltStylesheetPtr cur, const char *filename) {
103 #ifdef LIBXML_XINCLUDE_ENABLED
106 gettimeofday(&begin, NULL);
107 xmlXIncludeProcess(doc);
111 gettimeofday(&end, NULL);
112 msec = end.tv_sec - begin.tv_sec;
114 msec += (end.tv_usec - begin.tv_usec) / 1000;
115 fprintf(stderr, "XInclude processing %s took %ld ms\n",
121 gettimeofday(&begin, NULL);
122 if (output == NULL) {
126 for (j = 1; j < repeat; j++) {
127 res = xsltApplyStylesheet(cur, doc, params);
130 #ifdef LIBXML_HTML_ENABLED
132 doc = htmlParseFile(filename, NULL);
135 #ifdef LIBXML_DOCB_ENABLED
137 doc = docbParseFile(filename, NULL);
140 doc = xmlParseFile(filename);
144 res = xsltProfileStylesheet(cur, doc, params, stderr);
146 res = xsltApplyStylesheet(cur, doc, params);
151 gettimeofday(&end, NULL);
152 msec = end.tv_sec - begin.tv_sec;
154 msec += (end.tv_usec - begin.tv_usec) / 1000;
157 "Applying stylesheet %d times took %ld ms\n",
161 "Applying stylesheet took %ld ms\n", msec);
165 fprintf(stderr, "no result for %s\n", filename);
172 #ifdef LIBXML_DEBUG_ENABLED
174 xmlDebugDumpDocument(stdout, res);
177 if (cur->methodURI == NULL) {
179 gettimeofday(&begin, NULL);
180 xsltSaveResultToFile(stdout, res, cur);
184 gettimeofday(&end, NULL);
185 msec = end.tv_sec - begin.tv_sec;
187 msec += (end.tv_usec - begin.tv_usec) / 1000;
188 fprintf(stderr, "Saving result took %ld ms\n",
193 (cur->method, (const xmlChar *) "xhtml")) {
194 fprintf(stderr, "non standard output xhtml\n");
196 gettimeofday(&begin, NULL);
197 xsltSaveResultToFile(stdout, res, cur);
201 gettimeofday(&end, NULL);
202 msec = end.tv_sec - begin.tv_sec;
205 (end.tv_usec - begin.tv_usec) / 1000;
207 "Saving result took %ld ms\n",
212 "Unsupported non standard output %s\n",
216 #ifdef LIBXML_DEBUG_ENABLED
222 xsltRunStylesheet(cur, doc, params, output, NULL, NULL);
226 gettimeofday(&end, NULL);
227 msec = end.tv_sec - begin.tv_sec;
229 msec += (end.tv_usec - begin.tv_usec) / 1000;
231 "Running stylesheet and saving result took %ld ms\n",
238 static void usage(const char *name) {
239 printf("Usage: %s [options] stylesheet file [file ...]\n", name);
240 printf(" Options:\n");
241 printf(" --version or -V: show the version of libxml and libxslt used\n");
242 printf(" --verbose or -v: show logs of what's happening\n");
243 printf(" --output file or -o file: save to a given file\n");
244 printf(" --timing: display the time used\n");
245 printf(" --repeat: run the transformation 20 times\n");
246 printf(" --debug: dump the tree of the result instead\n");
247 printf(" --novalid: skip the Dtd loading phase\n");
248 printf(" --noout: do not dump the result\n");
249 printf(" --maxdepth val : increase the maximum depth\n");
250 #ifdef LIBXML_HTML_ENABLED
251 printf(" --html: the input document is(are) an HTML file(s)\n");
253 #ifdef LIBXML_DOCB_ENABLED
254 printf(" --docbook: the input document is SGML docbook\n");
256 printf(" --param name value : pass a (parameter,value) pair\n");
257 printf(" --nonet refuse to fetch DTDs or entities over network\n");
258 printf(" --warnnet warn against fetching over the network\n");
259 #ifdef LIBXML_CATALOG_ENABLED
260 printf(" --catalogs : use the catalogs from $SGML_CATALOG_FILES\n");
262 #ifdef LIBXML_XINCLUDE_ENABLED
263 printf(" --xinclude : do XInclude processing on document intput\n");
265 printf(" --profile or --norman : dump profiling informations \n");
269 main(int argc, char **argv)
272 xsltStylesheetPtr cur = NULL;
273 xmlDocPtr doc, style;
284 defaultLoader = xmlGetExternalEntityLoader();
285 xmlLineNumbersDefault(1);
288 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
290 xmlLoadExtDtdDefaultValue = 0;
291 for (i = 1; i < argc; i++) {
292 if (!strcmp(argv[i], "-"))
295 if (argv[i][0] != '-')
297 #ifdef LIBXML_DEBUG_ENABLED
298 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) {
302 if ((!strcmp(argv[i], "-v")) ||
303 (!strcmp(argv[i], "-verbose")) ||
304 (!strcmp(argv[i], "--verbose"))) {
305 xsltSetGenericDebugFunc(stderr, NULL);
306 } else if ((!strcmp(argv[i], "-o")) ||
307 (!strcmp(argv[i], "-output")) ||
308 (!strcmp(argv[i], "--output"))) {
311 } else if ((!strcmp(argv[i], "-V")) ||
312 (!strcmp(argv[i], "-version")) ||
313 (!strcmp(argv[i], "--version"))) {
314 printf("Using libxml %s, libxslt %s and libexslt %s\n",
315 xmlParserVersion, xsltEngineVersion, exsltLibraryVersion);
317 ("xsltproc was compiled against libxml %d, libxslt %d and libexslt %d\n",
318 LIBXML_VERSION, LIBXSLT_VERSION, LIBEXSLT_VERSION);
319 printf("libxslt %d was compiled against libxml %d\n",
320 xsltLibxsltVersion, xsltLibxmlVersion);
321 printf("libexslt %d was compiled against libxml %d\n",
322 exsltLibexsltVersion, exsltLibxmlVersion);
323 } else if ((!strcmp(argv[i], "-repeat"))
324 || (!strcmp(argv[i], "--repeat"))) {
329 } else if ((!strcmp(argv[i], "-novalid")) ||
330 (!strcmp(argv[i], "--novalid"))) {
332 } else if ((!strcmp(argv[i], "-noout")) ||
333 (!strcmp(argv[i], "--noout"))) {
335 #ifdef LIBXML_DOCB_ENABLED
336 } else if ((!strcmp(argv[i], "-docbook")) ||
337 (!strcmp(argv[i], "--docbook"))) {
340 #ifdef LIBXML_HTML_ENABLED
341 } else if ((!strcmp(argv[i], "-html")) ||
342 (!strcmp(argv[i], "--html"))) {
345 } else if ((!strcmp(argv[i], "-timing")) ||
346 (!strcmp(argv[i], "--timing"))) {
348 } else if ((!strcmp(argv[i], "-profile")) ||
349 (!strcmp(argv[i], "--profile"))) {
351 } else if ((!strcmp(argv[i], "-norman")) ||
352 (!strcmp(argv[i], "--norman"))) {
354 } else if ((!strcmp(argv[i], "-warnnet")) ||
355 (!strcmp(argv[i], "--warnnet"))) {
356 xmlSetExternalEntityLoader(xsltNoNetExternalEntityLoader);
357 } else if ((!strcmp(argv[i], "-nonet")) ||
358 (!strcmp(argv[i], "--nonet"))) {
359 xmlSetExternalEntityLoader(xsltNoNetExternalEntityLoader);
361 #ifdef LIBXML_CATALOG_ENABLED
362 } else if ((!strcmp(argv[i], "-catalogs")) ||
363 (!strcmp(argv[i], "--catalogs"))) {
364 const char *catalogs;
366 catalogs = getenv("SGML_CATALOG_FILES");
367 if (catalogs == NULL) {
368 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
370 xmlLoadCatalogs(catalogs);
373 #ifdef LIBXML_XINCLUDE_ENABLED
374 } else if ((!strcmp(argv[i], "-xinclude")) ||
375 (!strcmp(argv[i], "--xinclude"))) {
377 xsltSetXIncludeDefault(1);
379 } else if ((!strcmp(argv[i], "-param")) ||
380 (!strcmp(argv[i], "--param"))) {
382 params[nbparams++] = argv[i++];
383 params[nbparams++] = argv[i];
384 if (nbparams >= 16) {
385 fprintf(stderr, "too many params\n");
388 } else if ((!strcmp(argv[i], "-maxdepth")) ||
389 (!strcmp(argv[i], "--maxdepth"))) {
393 if (sscanf(argv[i], "%d", &value) == 1) {
395 xsltMaxDepth = value;
398 fprintf(stderr, "Unknown option %s\n", argv[i]);
403 params[nbparams] = NULL;
406 * Replace entities with their content.
408 xmlSubstituteEntitiesDefault(1);
411 * Register the EXSLT extensions and the test module
414 xsltRegisterTestModule();
416 for (i = 1; i < argc; i++) {
417 if ((!strcmp(argv[i], "-maxdepth")) ||
418 (!strcmp(argv[i], "--maxdepth"))) {
421 } else if ((!strcmp(argv[i], "-o")) ||
422 (!strcmp(argv[i], "-output")) ||
423 (!strcmp(argv[i], "--output"))) {
427 if ((!strcmp(argv[i], "-param")) || (!strcmp(argv[i], "--param"))) {
431 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
433 gettimeofday(&begin, NULL);
434 style = xmlParseFile((const char *) argv[i]);
438 gettimeofday(&end, NULL);
439 msec = end.tv_sec - begin.tv_sec;
441 msec += (end.tv_usec - begin.tv_usec) / 1000;
442 fprintf(stderr, "Parsing stylesheet %s took %ld ms\n",
446 fprintf(stderr, "cannot parse %s\n", argv[i]);
449 cur = xsltLoadStylesheetPI(style);
451 /* it is an embedded stylesheet */
452 xsltProcess(style, cur, argv[i]);
453 xsltFreeStylesheet(cur);
456 cur = xsltParseStylesheetDoc(style);
458 if (cur->indent == 1)
459 xmlIndentTreeOutput = 1;
461 xmlIndentTreeOutput = 0;
471 * disable CDATA from being built in the document tree
473 xmlDefaultSAXHandlerInit();
474 xmlDefaultSAXHandler.cdataBlock = NULL;
476 if ((cur != NULL) && (cur->errors == 0)) {
477 for (; i < argc; i++) {
480 gettimeofday(&begin, NULL);
481 #ifdef LIBXML_HTML_ENABLED
483 doc = htmlParseFile(argv[i], NULL);
486 #ifdef LIBXML_DOCB_ENABLED
488 doc = docbParseFile(argv[i], NULL);
491 doc = xmlParseFile(argv[i]);
493 fprintf(stderr, "unable to parse %s\n", argv[i]);
499 gettimeofday(&end, NULL);
500 msec = end.tv_sec - begin.tv_sec;
502 msec += (end.tv_usec - begin.tv_usec) / 1000;
503 fprintf(stderr, "Parsing document %s took %ld ms\n",
506 xsltProcess(doc, cur, argv[i]);
508 xsltFreeStylesheet(cur);
510 xsltCleanupGlobals();