Correction of namespeaces
[xmldoc.git] / xp.c
1 /*
2  * xsltproc.c: user program for the XSL Transformation 1.0 engine
3  *
4  * See Copyright for the status of this software.
5  *
6  * daniel@veillard.com
7  */
8
9 //#include <libxslt/libxslt.h>
10 //#include <libexslt/exslt.h>
11 #include <string.h>
12 #ifdef HAVE_SYS_STAT_H
13 #include <sys/stat.h>
14 #endif
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <stdlib.h>
19 #ifdef HAVE_STDARG_H
20 #include <stdarg.h>
21 #endif
22 #include <libxml/xmlmemory.h>
23 #include <libxml/debugXML.h>
24 #include <libxml/HTMLtree.h>
25 #include <libxml/xmlIO.h>
26 #ifdef LIBXML_XINCLUDE_ENABLED
27 #include <libxml/xinclude.h>
28 #endif
29 #ifdef LIBXML_CATALOG_ENABLED
30 #include <libxml/catalog.h>
31 #endif
32 #include <libxml/parserInternals.h>
33
34 #include <libxslt/xslt.h>
35 #include <libxslt/xsltInternals.h>
36 #include <libxslt/transform.h>
37 #include <libxslt/xsltutils.h>
38 #include <libxslt/extensions.h>
39
40 #include <libexslt/exslt.h>
41
42
43 #ifndef HAVE_STAT
44 #  ifdef HAVE__STAT
45      /* MS C library seems to define stat and _stat. The definition
46       *         is identical. Still, mapping them to each other causes a warning. */
47 #    ifndef _MSC_VER
48 #      define stat(x,y) _stat(x,y)
49 #    endif
50 #    define HAVE_STAT
51 #  endif
52 #endif
53
54 xmlParserInputPtr xmlNoNetExternalEntityLoader(const char *URL,
55                                                const char *ID,
56                                                xmlParserCtxtPtr ctxt);
57
58 #define LIBXML_DEBUG_ENABLED
59 //#define DEBUG
60
61 static int debug = 0;
62 static int novalid = 0;
63 static int noout = 0;
64 static int latex = 0;
65 #ifdef LIBXML_DOCB_ENABLED
66 static int docbook = 0;
67 #endif
68 #ifdef LIBXML_HTML_ENABLED
69 static int html = 0;
70 #endif
71
72 static const char *params[16 + 1];
73 static int nbparams = 0;
74 static const char *output = NULL;
75
76 void LatexEscape(xmlNodePtr node);
77
78 /*
79  *  The LaTeX translation table. Translate unicodes to LaTeX escape sequences.
80  */
81
82 //#define DEBUG
83
84 struct
85 {
86    int           unicode;
87    unsigned char *sequence;
88 } textab[] =
89 {
90    {  '#',     "\\#"              },
91    {  '$',     "\\$"              },
92    {  '%',     "\\%"              },
93    {  '&',     "\\&"              },
94    {  '<',     "\\verb+<+"        },
95    {  '>',     "\\verb+>+"        },
96    {  '\\',    "$\\backslash$"    },
97    {  '^',     "\\verb+^+"        },
98    {  '_',     "\\_"              },
99    {  '{',     "\\{"              },
100    {  '}',     "\\}"              },
101    {  '~',     "$\\sim$"          },
102    {  160,     "~"                },
103    {  161,     "!`"               },
104    {  162,     "..."              },
105    {  163,     "\\pounds"         },
106    {  164,     "..."              },
107    {  165,     "..."              },
108    {  166,     "..."              },
109    {  167,     "\\S"              },
110    {  168,     "\\\"{ }"          },
111    {  169,     "\\copyright"      },
112    {  170,     "..."              },
113    {  171,     "..."              },
114    {  172,     "$\\neg$"          },
115    {  173,     "..."              },
116    {  174,     "..."              },
117    {  175,     "\\={ }"           },
118    {  176,     "$^{\\circ}$"      },
119    {  177,     "$\\pm$"           },
120    {  178,     "$^{2}$"           },
121    {  179,     "$^{3}$"           },
122    {  180,     "\\'{ }"           },
123    {  181,     "$\\mu$"           },
124    {  182,     "\\P"              },
125    {  183,     "$\\cdot$"         },
126    {  184,     "\\c{ }"           },
127    {  185,     "$^{1}$"           },
128    {  186,     "..."              },
129    {  187,     "..."              },
130    {  188,     "$\\frac{1}{4}$"   },
131    {  189,     "$\\frac{1}{2}$"   },
132    {  190,     "$\\frac{3}{4}$"   },
133    {  191,     "?`"               },
134    {  192,     "\\`{A}"           },
135    {  193,     "\\'{A}"           },
136    {  194,     "\\^{A}"           },
137    {  195,     "\\~{A}"           },
138    {  196,     "\\\"{A}"          },
139    {  197,     "\\AA"             },
140    {  198,     "\\AE"             },
141    {  199,     "\\c{C}"           },
142    {  200,     "\\`{E}"           },
143    {  201,     "\\'{E}"           },
144    {  202,     "\\^{E}"           },
145    {  203,     "\\\"{E}"          },
146    {  204,     "\\`{I}"           },
147    {  205,     "\\'{I}"           },
148    {  206,     "\\^{I}"           },
149    {  207,     "\\\"{I}"          },
150    {  208,     "..."              },
151    {  209,     "\\~{N}"           },
152    {  210,     "\\`{O}"           },
153    {  211,     "\\'{O}"           },
154    {  212,     "\\^{O}"           },
155    {  213,     "\\~{O}"           },
156    {  214,     "\\\"{O}"          },
157    {  215,     "$\\times$"        },
158    {  216,     "\\O"              },
159    {  217,     "\\`{U}"           },
160    {  218,     "\\'{U}"           },
161    {  219,     "\\^{U}"           },
162    {  220,     "\\\"{U}"          },
163    {  221,     "\\'{Y}"           },
164    {  222,     "..."              },
165    {  223,     "\\ss"             },
166    {  224,     "\\`{a}"           },
167    {  225,     "\\'{a}"           },
168    {  226,     "\\^{a}"           },
169    {  227,     "\\~{a}"           },
170    {  228,     "\\\"{a}"          },
171    {  229,     "\\aa"             },
172    {  230,     "\\ae"             },
173    {  231,     "\\c{c}"           },
174    {  232,     "\\`{e}"           },
175    {  233,     "\\'{e}"           },
176    {  234,     "\\^{e}"           },
177    {  235,     "\\\"{e}"          },
178    {  236,     "\\`{i}"           },
179    {  237,     "\\'{i}"           },
180    {  238,     "\\^{i}"           },
181    {  239,     "\\\"{i}"          },
182    {  240,     "..."              },
183    {  241,     "\\~{n}"           },
184    {  242,     "\\`{o}"           },
185    {  243,     "\\'{o}"           },
186    {  244,     "\\^{o}"           },
187    {  245,     "\\~{o}"           },
188    {  246,     "\\\"{o}"          },
189    {  247,     "$\\div$"          },
190    {  248,     "\\o"              },
191    {  249,     "\\`{u}"           },
192    {  250,     "\\'{u}"           },
193    {  251,     "\\^{u}"           },
194    {  252,     "\\\"{u}"          },
195    {  253,     "\\'{y}"           },
196    {  254,     "..."              },
197    {  255,     "\\\"{y}"          },
198    {  338,     "\\OE"             },
199    {  339,     "\\oe"             },
200    {  352,     "\\u{S}"           },
201    {  353,     "\\u{s}"           },
202    {  376,     "\\\"{Y}"          },
203    {  913,     "A"                },
204    {  914,     "B"                },
205    {  915,     "$\\Gamma$"        },
206    {  916,     "$\\Delta$"        },
207    {  917,     "E"                },
208    {  918,     "Z"                },
209    {  919,     "H"                },
210    {  920,     "$\\Theta$"        },
211    {  921,     "I"                },
212    {  922,     "K"                },
213    {  923,     "$\\Lambda$"       },
214    {  924,     "M"                },
215    {  925,     "N"                },
216    {  926,     "$\\Xi$"           },
217    {  927,     "O"                },
218    {  928,     "$\\Pi$"           },
219    {  929,     "P"                },
220    {  931,     "$\\Sigma$"        },
221    {  932,     "T"                },
222    {  933,     "$\\Upsilon$"      },
223    {  934,     "$\\Phi$"          },
224    {  935,     "X"                },
225    {  936,     "$\\Psi$"          },
226    {  937,     "$\\Omega$"        },
227    {  945,     "$\\alpha$"        },
228    {  946,     "$\\beta$"         },
229    {  947,     "$\\gamma$"        },
230    {  948,     "$\\delta$"        },
231    {  949,     "$\\epsilon$"      },
232    {  950,     "$\\zeta$"         },
233    {  951,     "$\\eta$"          },
234    {  952,     "$\\theta$"        },
235    {  953,     "$\\iota$"         },
236    {  954,     "$\\kappa$"        },
237    {  955,     "$\\lambda$"       },
238    {  956,     "$\\mu$"           },
239    {  957,     "$\\nu$"           },
240    {  958,     "$\\xi$"           },
241    {  959,     "o"                },
242    {  960,     "$\\pi$"           },
243    {  961,     "$\\rho$"          },
244    {  962,     "$\\varsigma$"     },
245    {  963,     "$\\sigma$"        },
246    {  964,     "$\\tau$"          },
247    {  965,     "$\\upsilon$"      },
248    {  966,     "$\\phi$"          },
249    {  967,     "$\\chi$"          },
250    {  968,     "$\\psi$"          },
251    {  969,     "$\\omega$"        },
252    { 8224,     "\\dag"            },
253    { 8225,     "\\ddag"           },
254    { 8226,     "$\\bullet$"       },
255    { 8230,     "$\\ldots$"        },
256    { 8242,     "$\\prime$"        },
257    { 8243,     "$\\prime\\prime$" },
258    { 8254,     "\\={ }"           },
259    { 8364,     "\\EUR"            },
260    { 8465,     "$\\Im$"           },
261    { 8472,     "$\\wp$"           },
262    { 8476,     "$\\Re$"           },
263    { 8482,     "$^{TM}$"          },
264    { 8501,     "$\\aleph$"        },
265    { 8592,     "$\\leftarrow$"    },
266    { 8593,     "$\\uparrow$"      },
267    { 8594,     "$\\rightarrow$"   },
268    { 8595,     "$\\downarrow$"    },
269    { 8596,     "$\\leftrightarrow$" },
270    { 8629,     "$\\hookleftarrow$"},
271    { 8656,     "$\\Leftarrow$"    },
272    { 8657,     "$\\Uparrow$"      },
273    { 8658,     "$\\Rightarrow$"   },
274    { 8659,     "$\\Downarrow$"    },
275    { 8660,     "$\\Leftrightarrow$" },
276    { 8704,     "$\\forall$"       },
277    { 8706,     "$\\partial$"      },
278    { 8707,     "$\\exists$"       },
279    { 8709,     "$\\emptyset$"     },
280    { 8711,     "$\\nabla$"        },
281    { 8712,     "$\\in$"           },
282    { 8713,     "..."              },
283    { 8715,     "$\\ni$"           },
284    { 8719,     "$\\prod$"         },
285    { 8721,     "$\\sum$"          },
286    { 8722,     "$-$"              },
287    { 8727,     "$\\ast$"          },
288    { 8730,     "$\\surd$"         },
289    { 8733,     "$\\propto$"       },
290    { 8734,     "$\\infty$"        },
291    { 8736,     "$\\angle$"        },
292    { 8743,     "$\\wedge$"        },
293    { 8744,     "$\\vee$"          },
294    { 8745,     "$\\cap$"          },
295    { 8746,     "$\\cup$"          },
296    { 8747,     "$\\int$"          },
297    { 8756,     "$\\leadsto$"      },
298    { 8764,     "$\\sim$"          },
299    { 8773,     "$\\cong$"         },
300    { 8776,     "$\\approx$"       },
301    { 8800,     "$\\neq$"          },
302    { 8801,     "$\\equiv$"        },
303    { 8804,     "$\\leq$"          },
304    { 8805,     "$\\geq$"          },
305    { 8834,     "$\\subset$"       },
306    { 8835,     "$\\supset$"       },
307    { 8836,     "..."              },
308    { 8838,     "$\\subseteq$"     },
309    { 8839,     "$\\supseteq$"     },
310    { 8853,     "$\\oplus$"        },
311    { 8855,     "$\\otimes$"       },
312    { 8869,     "$\\perp$"         },
313    { 8901,     "$\\cdot$"         },
314    { 8968,     "$\\lceil$"        },
315    { 8969,     "$\\rceil$"        },
316    { 8970,     "$\\lfloor$"       },
317    { 8971,     "$\\rfloor$"       },
318    { 9001,     "$\\langle$"       },
319    { 9002,     "$\\rangle$"       },
320    { 9674,     "$\\Diamond$"      },
321    { 9824,     "$\\spadesuit$"    },
322    { 9827,     "$\\clubsuit$"     },
323    { 9829,     "$\\heartsuit$"    },
324    { 9830,     "$\\diamondsuit$"  },
325 };
326
327 unsigned char *latex_translate(int code)
328 {
329    char   *translation;
330    int    i;
331
332    translation = 0;
333
334    for (i = 0; translation == 0 && textab[i].unicode <= code
335                && i < sizeof(textab) / sizeof(textab[0]); i++)
336    {
337       if (textab[i].unicode == code)
338       {
339          translation = textab[i].sequence;
340 #ifdef DEBUG
341          fprintf(stderr, "\n*** translation for 0x%04x is %s\n", code, translation);
342 #endif
343       }
344    }
345    return  translation;
346 }
347
348 static void
349 xsltProcess(xmlDocPtr doc, xsltStylesheetPtr cur, const char *filename) {
350     xmlDocPtr res;
351
352     xmlXIncludeProcessFlags(doc, XSLT_PARSE_OPTIONS);
353
354 #ifdef DEBUG
355     xmlDebugDumpDocument(stdout, doc);
356 #endif
357
358     if (latex)
359     {
360        LatexEscape(xmlDocGetRootElement(doc));
361     }
362
363     if (output == NULL) {
364         res = xsltApplyStylesheet(cur, doc, params);
365
366         xmlFreeDoc(doc);
367         if (res == NULL) {
368             fprintf(stderr, "no result for %s\n", filename);
369             return;
370         }
371         if (noout) {
372             xmlFreeDoc(res);
373             return;
374         }
375 #ifdef LIBXML_DEBUG_ENABLED
376         if (debug)
377             xmlDebugDumpDocument(stdout, res);
378         else {
379 #endif
380             if (cur->methodURI == NULL) {
381                 xsltSaveResultToFile(stdout, res, cur);
382             } else {
383                 if (xmlStrEqual
384                     (cur->method, (const xmlChar *) "xhtml")) {
385                     fprintf(stderr, "non standard output xhtml\n");
386                     xsltSaveResultToFile(stdout, res, cur);
387                 } else {
388                     fprintf(stderr,
389                             "Unsupported non standard output %s\n",
390                             cur->method);
391                     xsltSaveResultToFile(stdout, res, cur);
392                 }
393             }
394 #ifdef LIBXML_DEBUG_ENABLED
395         }
396 #endif
397
398         xmlFreeDoc(res);
399     } else {
400         xsltRunStylesheet(cur, doc, params, output, NULL, NULL);
401         xmlFreeDoc(doc);
402     }
403 }
404
405
406 void LatexEscape(xmlNodePtr node)
407 {
408    xmlNodePtr  child;
409    xmlAttrPtr  attribute;
410    xmlChar     *byte;
411    int         unicode;
412
413    unsigned char  *translation, *replacement;
414    int            textlen;
415    int            i;
416
417 #ifdef DEBUG
418       fprintf(stderr, "\n-------- Node type %d\n", node->type);
419 #endif
420    if (xmlNodeIsText(node))
421    {
422 #ifdef DEBUG
423       fprintf(stderr, "\n-------- Text node at %p (%d bytes):\n%s",
424                       node, strlen(node->content), node->content);
425 #endif
426
427       textlen = strlen(node->content);
428       if (textlen < 100)
429       {
430          textlen = 100;
431       }
432       replacement = malloc(textlen);
433       i           = 0;
434       
435       for (byte = node->content; *byte != '\0'; byte++)
436       {
437          /* transform from utf-8 to unicode */
438
439          if ((*byte & 0x80) == 0)
440          {
441             unicode = *byte;
442          }
443          else if ((*byte & 0xe0) == 0xc0)
444          {
445 #ifdef DEBUG
446             fprintf(stderr, "2-byte unicode: 0x%02x, 0x%02x\n",byte[0], byte[1]);
447 #endif
448             unicode = (*byte & 0x1f) << 6;
449             byte++;
450             unicode |= *byte & 0x3f;
451 #ifdef DEBUG
452             fprintf(stderr, "Unicode = 0x%04x\n", unicode);
453 #endif
454          }
455          else if ((*byte & 0xf0) == 0xe0)
456          {
457              /*  3-byte unicode */
458
459             unicode = (*byte & 0x0f) << 12;
460             byte++;
461             unicode |= (*byte & 0x3f) << 6;
462             byte++;
463             unicode |= *byte & 0x3f;
464          }
465          else
466          {
467             fprintf(stderr, "More than 3 bytes utf-8 is not supported.\n");
468          }
469
470          translation = latex_translate(unicode);
471
472          /*  Append the original byte or the translation to the replacment text */
473
474          if (translation == 0)
475          {
476             if (i + 1 >= textlen)
477             {
478                textlen += 100;
479                replacement = realloc(replacement, textlen);
480             }
481             replacement[i++] = *byte;
482          }
483          else
484          {
485             int   j;
486
487             if (i + strlen(translation) + 1 >= textlen)
488             {
489                textlen += 100;
490                replacement = realloc(replacement, textlen);
491             }
492             for (j = 0; translation[j]; j++)
493             {
494                replacement[i++] = translation[j];
495             } 
496          }
497       }
498       replacement[i] = '\0';
499       xmlNodeSetContent(node, replacement);
500 #ifdef DEBUG
501       fprintf(stderr, "\n-------- Replacement text:\n%s", replacement);
502 #endif
503       free(replacement);
504    }
505    else
506    {
507       for (attribute = node->properties; attribute != 0; attribute = attribute->next)
508       {
509          LatexEscape(attribute->children);
510       }
511       for (child = node->children; child != 0; child = child->next)
512       {
513          if (strcmp(child->name, "verbatim") != 0 )
514          {
515             LatexEscape(child);
516          }
517       }
518    }
519 }
520
521 static void usage(const char *name)
522 {
523     printf("Usage: %s [options] stylesheet file [file ...]\n", name);
524     printf("   Options:\n");
525     printf("\t--version or -V: show the version of libxml and libxslt used\n");
526     printf("\t--verbose or -v: show logs of what's happening\n");
527     printf("\t--output file or -o file: save to a given file\n");
528     printf("\t--debug: dump the tree of the result instead\n");
529     printf("\t--latex: transform special characters for latex\n");
530     printf("\t--novalid: skip the Dtd loading phase\n");
531     printf("\t--noout: do not dump the result\n");
532     printf("\t--maxdepth val : increase the maximum depth\n");
533 #ifdef LIBXML_HTML_ENABLED
534     printf("\t--html: the input document is(are) an HTML file(s)\n");
535 #endif
536 #ifdef LIBXML_DOCB_ENABLED
537     printf("\t--docbook: the input document is SGML docbook\n");
538 #endif
539     printf("\t--param name value : pass a (parameter,value) pair\n");
540     printf("\t      string values must be quoted like \"'string'\"\n");
541     printf("\t--nonet refuse to fetch DTDs or entities over network\n");
542 #ifdef LIBXML_CATALOG_ENABLED
543     printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
544     printf("\t             otherwise XML Catalogs starting from \n");
545     printf("\t         file:///etc/xml/catalog are activated by default\n");
546 #endif
547 }
548
549 int
550 main(int argc, char **argv)
551 {
552     int i;
553     xsltStylesheetPtr cur = NULL;
554     xmlDocPtr doc, style;
555
556     if (argc <= 1) {
557         usage(argv[0]);
558         return (1);
559     }
560
561     xmlInitMemory();
562
563     LIBXML_TEST_VERSION
564
565     xmlLineNumbersDefault(1);
566
567     if (novalid == 0)
568         xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
569     else
570         xmlLoadExtDtdDefaultValue = 0;
571     for (i = 1; i < argc; i++) {
572         if (!strcmp(argv[i], "-"))
573             break;
574
575         if (argv[i][0] != '-')
576             continue;
577 #ifdef LIBXML_DEBUG_ENABLED
578         if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) {
579             debug++;
580         } else
581 #endif
582         if ((!strcmp(argv[i], "-v")) ||
583                 (!strcmp(argv[i], "-verbose")) ||
584                 (!strcmp(argv[i], "--verbose"))) {
585             xsltSetGenericDebugFunc(stderr, NULL);
586         } else if ((!strcmp(argv[i], "-o")) ||
587                    (!strcmp(argv[i], "-output")) ||
588                    (!strcmp(argv[i], "--output"))) {
589             i++;
590             output = argv[i++];
591         } else if ((!strcmp(argv[i], "-V")) ||
592                    (!strcmp(argv[i], "-version")) ||
593                    (!strcmp(argv[i], "--version")))
594         {
595
596         } else if ((!strcmp(argv[i], "-novalid")) ||
597                    (!strcmp(argv[i], "--novalid"))) {
598             novalid++;
599         } else if ((!strcmp(argv[i], "-latex")) ||
600                    (!strcmp(argv[i], "--latex"))) {
601             latex++;
602         } else if ((!strcmp(argv[i], "-noout")) ||
603                    (!strcmp(argv[i], "--noout"))) {
604             noout++;
605 #ifdef LIBXML_HTML_ENABLED
606         } else if ((!strcmp(argv[i], "-html")) ||
607                    (!strcmp(argv[i], "--html"))) {
608             html++;
609 #endif
610         } else if ((!strcmp(argv[i], "-nonet")) ||
611                    (!strcmp(argv[i], "--nonet"))) {
612             xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader);
613 #ifdef LIBXML_CATALOG_ENABLED
614         } else if ((!strcmp(argv[i], "-catalogs")) ||
615                    (!strcmp(argv[i], "--catalogs"))) {
616             const char *catalogs;
617
618             catalogs = getenv("SGML_CATALOG_FILES");
619             if (catalogs == NULL) {
620                 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
621             } else {
622                 xmlLoadCatalogs(catalogs);
623             }
624 #endif
625         } else if ((!strcmp(argv[i], "-param")) ||
626                    (!strcmp(argv[i], "--param"))) {
627             i++;
628             params[nbparams++] = argv[i++];
629             params[nbparams++] = argv[i];
630             if (nbparams >= 16) {
631                 fprintf(stderr, "too many params\n");
632                 return (1);
633             }
634         } else if ((!strcmp(argv[i], "-maxdepth")) ||
635                    (!strcmp(argv[i], "--maxdepth"))) {
636             int value;
637
638             i++;
639             if (sscanf(argv[i], "%d", &value) == 1) {
640                 if (value > 0)
641                     xsltMaxDepth = value;
642             }
643         } else {
644             fprintf(stderr, "Unknown option %s\n", argv[i]);
645             usage(argv[0]);
646             return (1);
647         }
648     }
649     params[nbparams] = NULL;
650
651     xsltSetXIncludeDefault(1);
652     /*
653      * Replace entities with their content.
654      */
655     xmlSubstituteEntitiesDefault(1);
656
657     /*
658      * Register the EXSLT extensions and the test module
659      */
660     exsltRegisterAll();
661     //xsltRegisterTestModule();
662
663     for (i = 1; i < argc; i++) {
664         if ((!strcmp(argv[i], "-maxdepth")) ||
665             (!strcmp(argv[i], "--maxdepth"))) {
666             i++;
667             continue;
668         } else if ((!strcmp(argv[i], "-o")) ||
669                    (!strcmp(argv[i], "-output")) ||
670                    (!strcmp(argv[i], "--output"))) {
671             i++;
672             continue;
673         }
674         if ((!strcmp(argv[i], "-param")) || (!strcmp(argv[i], "--param"))) {
675             i += 2;
676             continue;
677         }
678         if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
679             style = xmlParseFile((const char *) argv[i]);
680             if (style == NULL) {
681                 fprintf(stderr,  "cannot parse %s\n", argv[i]);
682                 cur = NULL;
683             } else {
684                 cur = xsltLoadStylesheetPI(style);
685                 if (cur != NULL) {
686                     /* it is an embedded stylesheet */
687                     xsltProcess(style, cur, argv[i]);
688                     xsltFreeStylesheet(cur);
689                     goto done;
690                 }
691                 cur = xsltParseStylesheetDoc(style);
692                 if (cur != NULL) {
693                     if (cur->indent == 1)
694                         xmlIndentTreeOutput = 1;
695                     else
696                         xmlIndentTreeOutput = 0;
697                     i++;
698                 } else {
699                     xmlFreeDoc(style);
700                     goto done;
701                 }
702             }
703             break;
704
705         }
706     }
707
708     /*
709      * disable CDATA from being built in the document tree
710      */
711     xmlDefaultSAXHandlerInit();
712     xmlDefaultSAXHandler.cdataBlock = NULL;
713
714     if ((cur != NULL) && (cur->errors == 0)) {
715         for (; i < argc; i++) {
716             doc = NULL;
717 #ifdef LIBXML_HTML_ENABLED
718             if (html)
719                 doc = htmlParseFile(argv[i], NULL);
720             else
721 #endif
722                 doc = xmlParseFile(argv[i]);
723             if (doc == NULL) {
724                 fprintf(stderr, "unable to parse %s\n", argv[i]);
725                 continue;
726             }
727             xsltProcess(doc, cur, argv[i]);
728         }
729     }
730     if (cur != NULL)
731         xsltFreeStylesheet(cur);
732 done:
733     //xsltCleanupGlobals();
734     xmlCleanupParser();
735     xmlMemoryDump();
736     return (0);
737 }
738