| 1 |
/* $Id: xml_select.c,v 1.67 2005/01/07 02:02:13 mgrouch Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
|
| 5 |
XMLStarlet: Command Line Toolkit to query/edit/check/transform XML documents |
| 6 |
|
| 7 |
Copyright (c) 2002-2004 Mikhail Grushinskiy. All Rights Reserved. |
| 8 |
|
| 9 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 |
of this software and associated documentation files (the "Software"), to deal |
| 11 |
in the Software without restriction, including without limitation the rights |
| 12 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 |
copies of the Software, and to permit persons to whom the Software is |
| 14 |
furnished to do so, subject to the following conditions: |
| 15 |
|
| 16 |
The above copyright notice and this permission notice shall be included in |
| 17 |
all copies or substantial portions of the Software. |
| 18 |
|
| 19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 |
THE SOFTWARE. |
| 26 |
|
| 27 |
*/ |
| 28 |
|
| 29 |
#include <config.h> |
| 30 |
|
| 31 |
#include <assert.h> |
| 32 |
#include <string.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <limits.h> |
| 36 |
#include <ctype.h> |
| 37 |
|
| 38 |
#include <libxml/tree.h> |
| 39 |
#include <libxslt/templates.h> |
| 40 |
|
| 41 |
#include "xmlstar.h" |
| 42 |
#include "trans.h" |
| 43 |
|
| 44 |
/* max length of xmlstarlet supplied (ie not from command line) namespaces |
| 45 |
* currently xalanredirect is longest, at 13 characters*/ |
| 46 |
#define MAX_NS_PREFIX_LEN 20 |
| 47 |
|
| 48 |
typedef struct { |
| 49 |
const xmlChar *href, *prefix; |
| 50 |
} NsEntry; |
| 51 |
|
| 52 |
static const NsEntry ns_entries[] = { |
| 53 |
{ BAD_CAST "http://exslt.org/common", BAD_CAST "exslt" }, |
| 54 |
{ BAD_CAST "http://exslt.org/math", BAD_CAST "math" }, |
| 55 |
{ BAD_CAST "http://exslt.org/dates-and-times", BAD_CAST "date" }, |
| 56 |
{ BAD_CAST "http://exslt.org/functions", BAD_CAST "func" }, |
| 57 |
{ BAD_CAST "http://exslt.org/sets", BAD_CAST "set" }, |
| 58 |
{ BAD_CAST "http://exslt.org/strings", BAD_CAST "str" }, |
| 59 |
{ BAD_CAST "http://exslt.org/dynamic", BAD_CAST "dyn" }, |
| 60 |
{ BAD_CAST "http://icl.com/saxon", BAD_CAST "saxon" }, |
| 61 |
{ BAD_CAST "org.apache.xalan.xslt.extensions.Redirect", |
| 62 |
BAD_CAST "xalanredirect"}, /* see MAX_NS_PREFIX_LEN */ |
| 63 |
{ BAD_CAST "http://www.jclark.com/xt", BAD_CAST "xt" }, |
| 64 |
{ BAD_CAST "http://xmlsoft.org/XSLT/namespace", BAD_CAST "libxslt" }, |
| 65 |
{ BAD_CAST "http://xmlsoft.org/XSLT/", BAD_CAST "test" }, |
| 66 |
}; |
| 67 |
|
| 68 |
static const NsEntry* |
| 69 |
lookup_ns_entry(const char *prefix, int len) { |
| 70 |
int i; |
| 71 |
for (i = 0; i < COUNT_OF(ns_entries); i++) { |
| 72 |
if (xmlStrncmp(BAD_CAST prefix, ns_entries[i].prefix, len) == 0) |
| 73 |
return &ns_entries[i]; |
| 74 |
} |
| 75 |
return NULL; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
typedef struct _selOptions { |
| 80 |
int quiet; /* No output */ |
| 81 |
int printXSLT; /* Display prepared XSLT */ |
| 82 |
int printRoot; /* Print root element in output (if XML) */ |
| 83 |
int outText; /* Output is text */ |
| 84 |
int indent; /* Indent output */ |
| 85 |
int noblanks; /* Remove insignificant spaces from XML tree */ |
| 86 |
int no_omit_decl; /* Print XML declaration line <?xml version="1.0"?> */ |
| 87 |
int nonet; /* refuse to fetch DTDs or entities over network */ |
| 88 |
const xmlChar *encoding; /* the "encoding" attribute on the stylesheet's <xsl:output/> */ |
| 89 |
} selOptions; |
| 90 |
|
| 91 |
typedef selOptions *selOptionsPtr; |
| 92 |
|
| 93 |
typedef enum { TARG_NONE = 0, TARG_SORT_OP, TARG_XPATH, |
| 94 |
TARG_ATTR_STRING, TARG_STRING, TARG_VAR, |
| 95 |
/* template args below don't consume any command line args */ |
| 96 |
TARG_NEWLINE, TARG_NO_CMDLINE = TARG_NEWLINE, |
| 97 |
TARG_INP_NAME, TARG_STR_NAME_SELECT |
| 98 |
} template_argument_type; |
| 99 |
typedef struct { |
| 100 |
const xmlChar *attrname; |
| 101 |
template_argument_type type; |
| 102 |
} template_option_argument; |
| 103 |
|
| 104 |
#define TEMPLATE_OPT_MAX_ARGS 2 |
| 105 |
|
| 106 |
typedef struct { |
| 107 |
char shortopt; |
| 108 |
const char *longopt; |
| 109 |
const xmlChar *xslname; |
| 110 |
template_option_argument arguments[TEMPLATE_OPT_MAX_ARGS]; |
| 111 |
int nest; |
| 112 |
} template_option; |
| 113 |
|
| 114 |
static const template_option |
| 115 |
OPT_TEMPLATE = { 't', "template" }, |
| 116 |
OPT_COPY_OF = { 'c', "copy-of", BAD_CAST "copy-of", {{BAD_CAST "select", TARG_XPATH}}, 0 }, |
| 117 |
OPT_VALUE_OF = { 'v', "value-of", BAD_CAST "with-param", {{BAD_CAST "name", TARG_STR_NAME_SELECT}, |
| 118 |
{BAD_CAST "select", TARG_XPATH}}, -1 }, |
| 119 |
OPT_OUTPUT = { 'o', "output", BAD_CAST "text", {{NULL, TARG_STRING}}, 0 }, |
| 120 |
OPT_NL = { 'n', "nl", BAD_CAST "value-of", {{NULL, TARG_NEWLINE}}, 0 }, |
| 121 |
OPT_INP_NAME = { 'f', "inp-name", BAD_CAST "copy-of", {{NULL, TARG_INP_NAME}}, 0 }, |
| 122 |
OPT_MATCH = { 'm', "match", BAD_CAST "for-each", {{BAD_CAST "select", TARG_XPATH}}, 1 }, |
| 123 |
OPT_IF = { 'i', "if", BAD_CAST"when", {{BAD_CAST "test", TARG_XPATH}}, 1 }, |
| 124 |
OPT_ELIF = { 0, "elif", BAD_CAST "when", {{BAD_CAST "test", TARG_XPATH}}, 1 }, |
| 125 |
OPT_ELSE = { 0, "else", BAD_CAST "otherwise", {{NULL}}, 1 }, |
| 126 |
OPT_ELEM = { 'e', "elem", BAD_CAST "element", {{BAD_CAST "name", TARG_ATTR_STRING}}, 1 }, |
| 127 |
OPT_ATTR = { 'a', "attr", BAD_CAST "attribute", {{BAD_CAST "name", TARG_ATTR_STRING}}, 1 }, |
| 128 |
OPT_BREAK = { 'b', "break", NULL, {{NULL}}, -1 }, |
| 129 |
OPT_SORT = { 's', "sort", BAD_CAST "sort", {{NULL, TARG_SORT_OP}, {BAD_CAST "select", TARG_XPATH}}, 0 }, |
| 130 |
OPT_VAR = { 0, "var", BAD_CAST "variable", {{BAD_CAST "name", TARG_VAR}}, 1}, |
| 131 |
|
| 132 |
*TEMPLATE_OPTIONS[] = { |
| 133 |
&OPT_TEMPLATE, |
| 134 |
&OPT_COPY_OF, |
| 135 |
&OPT_VALUE_OF, |
| 136 |
&OPT_OUTPUT, |
| 137 |
&OPT_NL, |
| 138 |
&OPT_INP_NAME, |
| 139 |
&OPT_MATCH, |
| 140 |
&OPT_IF, |
| 141 |
&OPT_ELIF, |
| 142 |
&OPT_ELSE, |
| 143 |
&OPT_ELEM, |
| 144 |
&OPT_ATTR, |
| 145 |
&OPT_BREAK, |
| 146 |
&OPT_SORT, |
| 147 |
&OPT_VAR |
| 148 |
}; |
| 149 |
|
| 150 |
void |
| 151 |
caseSortFunction(xsltTransformContextPtr ctxt, xmlNodePtr *sorts, |
| 152 |
int nbsorts); |
| 153 |
|
| 154 |
/** |
| 155 |
* Print small help for command line options |
| 156 |
*/ |
| 157 |
void |
| 158 |
selUsage(const char *argv0, exit_status status) |
| 159 |
{ |
| 160 |
extern void fprint_select_usage(FILE* out, const char* argv0); |
| 161 |
extern const char more_info[]; |
| 162 |
extern const char libxslt_more_info[]; |
| 163 |
FILE *o = (status == EXIT_SUCCESS)? stdout : stderr; |
| 164 |
fprint_select_usage(o, argv0); |
| 165 |
fprintf(o, "%s", more_info); |
| 166 |
fprintf(o, "%s", libxslt_more_info); |
| 167 |
exit(status); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Initialize global command line options |
| 172 |
*/ |
| 173 |
void |
| 174 |
selInitOptions(selOptionsPtr ops) |
| 175 |
{ |
| 176 |
ops->quiet = globalOptions.quiet; |
| 177 |
ops->printXSLT = 0; |
| 178 |
ops->printRoot = 0; |
| 179 |
ops->outText = 0; |
| 180 |
ops->indent = 0; |
| 181 |
ops->noblanks = 0; |
| 182 |
ops->no_omit_decl = 0; |
| 183 |
ops->nonet = 1; |
| 184 |
ops->encoding = NULL; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Parse global command line options |
| 189 |
*/ |
| 190 |
int |
| 191 |
selParseOptions(selOptionsPtr ops, int argc, char **argv) |
| 192 |
{ |
| 193 |
int i; |
| 194 |
|
| 195 |
i = 2; |
| 196 |
while((i < argc) && (strcmp(argv[i], "-t")) && strcmp(argv[i], "--template")) |
| 197 |
{ |
| 198 |
if (!strcmp(argv[i], "-C")) |
| 199 |
{ |
| 200 |
ops->printXSLT = 1; |
| 201 |
} |
| 202 |
else if (!strcmp(argv[i], "-Q") || !strcmp(argv[i], "--quiet")) |
| 203 |
{ |
| 204 |
ops->quiet = 1; |
| 205 |
} |
| 206 |
else if (!strcmp(argv[i], "-B") || !strcmp(argv[i], "--noblanks")) |
| 207 |
{ |
| 208 |
ops->noblanks = 1; |
| 209 |
} |
| 210 |
else if (!strcmp(argv[i], "-T") || !strcmp(argv[i], "--text")) |
| 211 |
{ |
| 212 |
ops->outText = 1; |
| 213 |
} |
| 214 |
else if (!strcmp(argv[i], "-R") || !strcmp(argv[i], "--root")) |
| 215 |
{ |
| 216 |
ops->printRoot = 1; |
| 217 |
} |
| 218 |
else if (!strcmp(argv[i], "-I") || !strcmp(argv[i], "--indent")) |
| 219 |
{ |
| 220 |
ops->indent = 1; |
| 221 |
} |
| 222 |
else if (!strcmp(argv[i], "-D") || !strcmp(argv[i], "--xml-decl")) |
| 223 |
{ |
| 224 |
ops->no_omit_decl = 1; |
| 225 |
} |
| 226 |
else if (!strcmp(argv[i], "-E") || !strcmp(argv[i], "--encode")) |
| 227 |
{ |
| 228 |
if ((i+1) < argc) |
| 229 |
{ |
| 230 |
if (argv[i + 1][0] == '-') |
| 231 |
{ |
| 232 |
fprintf(stderr, "-E option requires argument <encoding> ex: (utf-8, unicode...)\n"); |
| 233 |
exit(EXIT_BAD_ARGS); |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
ops->encoding = BAD_CAST argv[i + 1]; |
| 238 |
} |
| 239 |
} |
| 240 |
else |
| 241 |
{ |
| 242 |
fprintf(stderr, "-E option requires argument <encoding> ex: (utf-8, unicode...)\n"); |
| 243 |
exit(EXIT_BAD_ARGS); |
| 244 |
} |
| 245 |
|
| 246 |
} |
| 247 |
else if (!strcmp(argv[i], "--net")) |
| 248 |
{ |
| 249 |
ops->nonet = 0; |
| 250 |
} |
| 251 |
else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h") || |
| 252 |
!strcmp(argv[i], "-?") || !strcmp(argv[i], "-Z")) |
| 253 |
{ |
| 254 |
selUsage(argv[0], EXIT_SUCCESS); |
| 255 |
} |
| 256 |
i++; |
| 257 |
} |
| 258 |
|
| 259 |
return i; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
/** |
| 264 |
* Search for namespace references in @xpath and add them to @root. Note that we |
| 265 |
* might pickup things that aren't actually namespace references because we |
| 266 |
* don't have a full XPath parser. That's okay, an extra namespace definition |
| 267 |
* won't hurt anyone. |
| 268 |
*/ |
| 269 |
static void |
| 270 |
checkNsRefs(xmlNodePtr root, const char *xpath) |
| 271 |
{ |
| 272 |
const char *colon; |
| 273 |
for (colon = xpath; colon; colon++) { |
| 274 |
int ns_idx = -1; |
| 275 |
|
| 276 |
colon = strchr(colon, ':'); |
| 277 |
if (!colon) break; |
| 278 |
|
| 279 |
for (;; ns_idx--) { |
| 280 |
if (&colon[ns_idx] < xpath |
| 281 |
||!isalnum(colon[ns_idx])) { |
| 282 |
const NsEntry *ns; |
| 283 |
ns_idx++; |
| 284 |
ns = lookup_ns_entry(&colon[ns_idx], -ns_idx); |
| 285 |
if (ns) xmlNewNs(root, ns->href, ns->prefix); |
| 286 |
break; |
| 287 |
} |
| 288 |
if (-ns_idx >= MAX_NS_PREFIX_LEN) break; |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Prepare XSLT template based on command line options |
| 295 |
* Assumes start points to -t option |
| 296 |
*/ |
| 297 |
int |
| 298 |
selGenTemplate(xmlNodePtr root, xmlNodePtr template_node, |
| 299 |
xmlNsPtr xslns, selOptionsPtr ops, int* use_inputfile, int* use_value_of, |
| 300 |
int* lastTempl, int start, int argc, char **argv) |
| 301 |
{ |
| 302 |
int i; |
| 303 |
int templateEmpty; |
| 304 |
int nextTempl; |
| 305 |
const template_option *targ = NULL; |
| 306 |
xmlNodePtr node = template_node; |
| 307 |
|
| 308 |
if (strcmp(argv[start], "-t") != 0 && |
| 309 |
strcmp(argv[start], "--template") != 0) |
| 310 |
{ |
| 311 |
fprintf(stderr, "not at the beginning of template\n"); |
| 312 |
abort(); |
| 313 |
} |
| 314 |
|
| 315 |
*lastTempl = 0; |
| 316 |
templateEmpty = 1; |
| 317 |
nextTempl = 0; |
| 318 |
i = start + 1; |
| 319 |
|
| 320 |
while(i < argc) |
| 321 |
{ |
| 322 |
xmlNodePtr newnode = NULL; |
| 323 |
const template_option *newtarg = NULL; |
| 324 |
int j; |
| 325 |
int nesting; |
| 326 |
|
| 327 |
if (argv[i][0] == '-' && argv[i][1] != '\0') |
| 328 |
{ |
| 329 |
for (j = 0; j < sizeof(TEMPLATE_OPTIONS)/sizeof(*TEMPLATE_OPTIONS); j++) |
| 330 |
{ |
| 331 |
newtarg = TEMPLATE_OPTIONS[j]; |
| 332 |
if (argv[i][1] == '-' && strcmp(newtarg->longopt, &argv[i][2]) == 0) |
| 333 |
goto found_option; /* long option */ |
| 334 |
else if(newtarg->shortopt == argv[i][1]) |
| 335 |
goto found_option; /* short option */ |
| 336 |
} |
| 337 |
fprintf(stderr, "unrecognized option: %s\n", argv[i]); |
| 338 |
exit(EXIT_BAD_ARGS); |
| 339 |
} |
| 340 |
else |
| 341 |
{ |
| 342 |
break; |
| 343 |
} |
| 344 |
|
| 345 |
found_option: |
| 346 |
if (newtarg == &OPT_SORT && (targ != &OPT_MATCH && targ != &OPT_SORT)) |
| 347 |
{ |
| 348 |
fprintf(stderr, "sort(s) must follow match\n"); |
| 349 |
exit(EXIT_BAD_ARGS); |
| 350 |
} |
| 351 |
else if (newtarg == &OPT_TEMPLATE) |
| 352 |
{ |
| 353 |
nextTempl = 1; |
| 354 |
i--; |
| 355 |
break; |
| 356 |
} |
| 357 |
else if (newtarg == &OPT_IF) |
| 358 |
{ |
| 359 |
node = xmlNewChild(node, xslns, BAD_CAST "choose", NULL); |
| 360 |
node->_private = (void*) &OPT_IF; |
| 361 |
} |
| 362 |
else if (newtarg == &OPT_ELIF || newtarg == &OPT_ELSE) |
| 363 |
{ |
| 364 |
node = node->parent; |
| 365 |
if (node->_private != &OPT_IF) { |
| 366 |
fprintf(stderr, "else without if\n"); |
| 367 |
exit(EXIT_BAD_ARGS); |
| 368 |
} |
| 369 |
} |
| 370 |
else if (newtarg == &OPT_VALUE_OF) |
| 371 |
{ |
| 372 |
node = xmlNewChild(node, xslns, BAD_CAST "call-template", NULL); |
| 373 |
xmlNewProp(node, BAD_CAST "name", BAD_CAST "value-of-template"); |
| 374 |
node->_private = (void*) &OPT_VALUE_OF; |
| 375 |
*use_value_of = 1; |
| 376 |
/* value-of-template uses exslt:node-set */ |
| 377 |
checkNsRefs(root, "exslt:node-set"); |
| 378 |
} |
| 379 |
|
| 380 |
i++; |
| 381 |
templateEmpty = 0; |
| 382 |
nesting = newtarg->nest; |
| 383 |
|
| 384 |
if (newtarg->xslname) |
| 385 |
newnode = xmlNewChild(node, xslns, newtarg->xslname, NULL); |
| 386 |
|
| 387 |
for (j = 0; j < TEMPLATE_OPT_MAX_ARGS && newtarg->arguments[j].type; j++) |
| 388 |
{ |
| 389 |
if (i >= argc && newtarg->arguments[j].type < TARG_NO_CMDLINE) |
| 390 |
selUsage(argv[0], EXIT_BAD_ARGS); |
| 391 |
switch (newtarg->arguments[j].type) |
| 392 |
{ |
| 393 |
case TARG_VAR: { |
| 394 |
char *equals = strchr(argv[i], '='); |
| 395 |
if (equals) { |
| 396 |
*equals = '\0'; |
| 397 |
xmlNewProp(newnode, BAD_CAST "select", BAD_CAST (&equals[1])); |
| 398 |
nesting = 0; |
| 399 |
} |
| 400 |
xmlNewProp(newnode, newtarg->arguments[j].attrname, BAD_CAST argv[i]); |
| 401 |
break; |
| 402 |
} |
| 403 |
|
| 404 |
case TARG_XPATH: |
| 405 |
checkNsRefs(root, argv[i]); |
| 406 |
case TARG_ATTR_STRING: |
| 407 |
xmlNewProp(newnode, newtarg->arguments[j].attrname, BAD_CAST argv[i]); |
| 408 |
break; |
| 409 |
|
| 410 |
case TARG_STRING: |
| 411 |
xmlNodeAddContent(newnode, BAD_CAST argv[i]); |
| 412 |
break; |
| 413 |
|
| 414 |
case TARG_NEWLINE: |
| 415 |
xmlNewProp(newnode, BAD_CAST "select", BAD_CAST "'\n'"); |
| 416 |
break; |
| 417 |
case TARG_STR_NAME_SELECT: |
| 418 |
xmlNewProp(newnode, BAD_CAST "name", BAD_CAST "select"); |
| 419 |
break; |
| 420 |
|
| 421 |
case TARG_INP_NAME: |
| 422 |
*use_inputfile = 1; |
| 423 |
xmlNewProp(newnode, BAD_CAST "select", BAD_CAST "$inputFile"); |
| 424 |
break; |
| 425 |
|
| 426 |
case TARG_SORT_OP: { |
| 427 |
char order, data_type, case_order; |
| 428 |
int nread; |
| 429 |
nread = sscanf(argv[i], "%c:%c:%c", &order, &data_type, &case_order); |
| 430 |
if (nread != 3) selUsage(argv[0], EXIT_BAD_ARGS); /* TODO: allow missing letters */ |
| 431 |
|
| 432 |
if (order == 'A' || order == 'D') |
| 433 |
xmlNewProp(newnode, BAD_CAST "order", |
| 434 |
BAD_CAST (order == 'A'? "ascending" : "descending")); |
| 435 |
if (data_type == 'N' || data_type == 'T') |
| 436 |
xmlNewProp(newnode, BAD_CAST "data-type", |
| 437 |
BAD_CAST (data_type == 'N'? "number" : "text")); |
| 438 |
if (case_order == 'U' || case_order == 'L') |
| 439 |
xmlNewProp(newnode, BAD_CAST "case-order", |
| 440 |
BAD_CAST (case_order == 'U'? "upper-first" : "lower-first")); |
| 441 |
} break; |
| 442 |
|
| 443 |
default: |
| 444 |
assert(0); |
| 445 |
} |
| 446 |
if (newtarg->arguments[j].type < TARG_NO_CMDLINE) i++; |
| 447 |
} |
| 448 |
|
| 449 |
switch (nesting) { |
| 450 |
case -1: |
| 451 |
do { node = node->parent; } while(node->_private); |
| 452 |
break; |
| 453 |
case 0: |
| 454 |
break; |
| 455 |
case 1: |
| 456 |
node = newnode; |
| 457 |
break; |
| 458 |
default: |
| 459 |
assert(0); |
| 460 |
} |
| 461 |
targ = newtarg; |
| 462 |
} |
| 463 |
|
| 464 |
if (templateEmpty) |
| 465 |
{ |
| 466 |
fprintf(stderr, "error in arguments:"); |
| 467 |
fprintf(stderr, " -t or --template option must be followed by"); |
| 468 |
fprintf(stderr, " --match or other options\n"); |
| 469 |
exit(EXIT_BAD_ARGS); |
| 470 |
} |
| 471 |
|
| 472 |
if (!nextTempl) |
| 473 |
{ |
| 474 |
if (i >= argc || argv[i][0] != '-' || strcmp(argv[i], "-") == 0) |
| 475 |
{ |
| 476 |
*lastTempl = 1; |
| 477 |
return i; /* return index of next input filename */ |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
/* return index to beginning of the next template */ |
| 482 |
return ++i; |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* Prepare XSLT stylesheet based on command line options |
| 487 |
*/ |
| 488 |
int |
| 489 |
selPrepareXslt(xmlDocPtr style, selOptionsPtr ops, xmlChar *ns_arr[], |
| 490 |
int start, int argc, char **argv) |
| 491 |
{ |
| 492 |
int i, t, ns, use_inputfile = 0, use_value_of = 0; |
| 493 |
xmlNodePtr root, root_template = NULL; |
| 494 |
xmlNsPtr xslns; |
| 495 |
xmlBufferPtr attr_buf; |
| 496 |
|
| 497 |
root = xmlNewDocRawNode(style, NULL, BAD_CAST "stylesheet", NULL); |
| 498 |
xmlDocSetRootElement(style, root); |
| 499 |
xmlNewProp(root, BAD_CAST "version", BAD_CAST "1.0"); |
| 500 |
xslns = xmlNewNs(root, XSLT_NAMESPACE, BAD_CAST "xsl"); |
| 501 |
xmlSetNs(root, xslns); |
| 502 |
|
| 503 |
ns = 0; |
| 504 |
while(ns_arr[ns]) |
| 505 |
{ |
| 506 |
xmlNewNs(root, ns_arr[ns+1], xmlStrlen(ns_arr[ns])?ns_arr[ns] : NULL); |
| 507 |
ns += 2; |
| 508 |
} |
| 509 |
cleanupNSArr(ns_arr); |
| 510 |
|
| 511 |
{ |
| 512 |
xmlNodePtr output; |
| 513 |
output = xmlNewChild(root, xslns, BAD_CAST "output", NULL); |
| 514 |
xmlNewProp(output, BAD_CAST "omit-xml-declaration", |
| 515 |
BAD_CAST ((ops->no_omit_decl)?"no":"yes")); |
| 516 |
xmlNewProp(output, BAD_CAST "indent", |
| 517 |
BAD_CAST ((ops->indent)?"yes":"no")); |
| 518 |
if (ops->encoding) xmlNewProp(output, BAD_CAST "encoding", ops->encoding); |
| 519 |
if (ops->outText) xmlNewProp(output, BAD_CAST "method", BAD_CAST "text"); |
| 520 |
} |
| 521 |
|
| 522 |
for (i = start, t = 0; i < argc; i++) |
| 523 |
if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--template")) |
| 524 |
t++; |
| 525 |
|
| 526 |
/* |
| 527 |
* At least one -t option must be found |
| 528 |
*/ |
| 529 |
if (t == 0) |
| 530 |
{ |
| 531 |
fprintf(stderr, "error in arguments:"); |
| 532 |
fprintf(stderr, " no -t or --template options found\n"); |
| 533 |
exit(EXIT_BAD_ARGS); |
| 534 |
} |
| 535 |
|
| 536 |
if (t > 1) |
| 537 |
root_template = xmlNewChild(root, xslns, BAD_CAST "template", NULL); |
| 538 |
|
| 539 |
t = 0; |
| 540 |
i = start; |
| 541 |
while(i < argc) |
| 542 |
{ |
| 543 |
if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--template")) |
| 544 |
{ |
| 545 |
xmlNodePtr call_template, template; |
| 546 |
int lastTempl = 0; |
| 547 |
t++; |
| 548 |
template = xmlNewChild(root, xslns, BAD_CAST "template", NULL); |
| 549 |
|
| 550 |
if (root_template) { |
| 551 |
xmlChar num_buf[1+10+1]; /* t+maxnumber+NUL */ |
| 552 |
xmlStrPrintf(num_buf, sizeof num_buf, BAD_CAST "t%d", t); |
| 553 |
|
| 554 |
call_template = xmlNewChild(root_template, xslns, |
| 555 |
BAD_CAST "call-template", NULL); |
| 556 |
xmlNewProp(call_template, BAD_CAST "name", num_buf); |
| 557 |
xmlNewProp(template, BAD_CAST "name", num_buf); |
| 558 |
} else { |
| 559 |
root_template = template; |
| 560 |
} |
| 561 |
|
| 562 |
i = selGenTemplate(root, template, |
| 563 |
xslns, ops, &use_inputfile, &use_value_of, |
| 564 |
&lastTempl, i, argc, argv); |
| 565 |
if (lastTempl) break; |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
if (!ops->outText && ops->printRoot) { |
| 570 |
xmlNodePtr result_root = root_template; |
| 571 |
xmlNodeSetName(result_root, BAD_CAST "xsl-select"); |
| 572 |
xmlSetNs(result_root, NULL); |
| 573 |
xmlUnlinkNode(result_root); |
| 574 |
|
| 575 |
root_template = xmlNewChild(root, xslns, BAD_CAST "template", NULL); |
| 576 |
xmlAddChild(root_template, result_root); |
| 577 |
} |
| 578 |
|
| 579 |
xmlNewProp(root_template, BAD_CAST "match", BAD_CAST "/"); |
| 580 |
|
| 581 |
attr_buf = xmlBufferCreate(); |
| 582 |
for (ns = 0; ns < COUNT_OF(ns_entries); ns++) { |
| 583 |
if (xmlSearchNs(NULL, root, ns_entries[ns].prefix)) { |
| 584 |
if (xmlBufferLength(attr_buf) != 0) |
| 585 |
xmlBufferWriteChar(attr_buf, " "); |
| 586 |
xmlBufferCat(attr_buf, ns_entries[ns].prefix); |
| 587 |
} |
| 588 |
} |
| 589 |
if (xmlBufferLength(attr_buf) != 0) |
| 590 |
xmlNewProp(root, BAD_CAST "extension-element-prefixes", |
| 591 |
xmlBufferContent(attr_buf)); |
| 592 |
|
| 593 |
xmlBufferFree(attr_buf); |
| 594 |
|
| 595 |
if (use_inputfile) { |
| 596 |
xmlNodePtr param; |
| 597 |
param = xmlNewChild(root, xslns, BAD_CAST "param", BAD_CAST "-"); |
| 598 |
xmlNewProp(param, BAD_CAST "name", BAD_CAST "inputFile"); |
| 599 |
} |
| 600 |
|
| 601 |
if (use_value_of) { |
| 602 |
xmlNodePtr value_of_template, for_each, value_of, param; |
| 603 |
/* <xsl:template name='value-of-template' xmlns:xsl=XSLT_NS> */ |
| 604 |
value_of_template = xmlNewChild(root, xslns, BAD_CAST "template", NULL); |
| 605 |
xmlNewProp(value_of_template, BAD_CAST "name", BAD_CAST "value-of-template"); |
| 606 |
/* <xsl:param name='select'/> */ |
| 607 |
param = xmlNewChild(value_of_template, xslns, BAD_CAST "param", NULL); |
| 608 |
xmlNewProp(param, BAD_CAST "name", BAD_CAST "select"); |
| 609 |
/* <xsl:value-of select='$select'/> */ |
| 610 |
value_of = xmlNewChild(value_of_template, xslns, BAD_CAST "value-of", NULL); |
| 611 |
xmlNewProp(value_of, BAD_CAST "select", BAD_CAST "$select"); |
| 612 |
/* <xsl:for-each select='exslt:node-set($select)[position()>1]'> */ |
| 613 |
for_each = xmlNewChild(value_of_template, xslns, BAD_CAST "for-each", NULL); |
| 614 |
xmlNewProp(for_each, BAD_CAST "select", BAD_CAST "exslt:node-set($select)[position()>1]"); |
| 615 |
/* <xsl:value-of select='\" \"'/> */ |
| 616 |
value_of = xmlNewChild(for_each, xslns, BAD_CAST "value-of", NULL); |
| 617 |
xmlNewProp(value_of, BAD_CAST "select", BAD_CAST "'\n'"); |
| 618 |
/* <xsl:value-of select='.'/> */ |
| 619 |
value_of = xmlNewChild(for_each, xslns, BAD_CAST "value-of", NULL); |
| 620 |
xmlNewProp(value_of, BAD_CAST "select", BAD_CAST "."); |
| 621 |
} |
| 622 |
|
| 623 |
return i; |
| 624 |
} |
| 625 |
|
| 626 |
/** |
| 627 |
* copy namespace definitions from @root to @style_tree |
| 628 |
*/ |
| 629 |
static void |
| 630 |
extract_ns_defs(xmlNodePtr root, xmlDocPtr style_tree) |
| 631 |
{ |
| 632 |
xmlNsPtr nsDef; |
| 633 |
xmlNodePtr style_root = xmlDocGetRootElement(style_tree); |
| 634 |
if (!root) return; |
| 635 |
|
| 636 |
for (nsDef = root->nsDef; nsDef; nsDef = nsDef->next) { |
| 637 |
xmlNewNs(style_root, nsDef->href, nsDef->prefix); |
| 638 |
if (nsDef->prefix == NULL) |
| 639 |
default_ns = nsDef->href; |
| 640 |
} |
| 641 |
if (default_ns) { |
| 642 |
xmlNewNs(style_root, default_ns, BAD_CAST "_"); |
| 643 |
xmlNewNs(style_root, default_ns, BAD_CAST "DEFAULT"); |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
static void |
| 648 |
do_file(const char *filename, xmlDocPtr style_tree, |
| 649 |
int xml_options, const selOptions *ops, xsltOptions *xsltOps, |
| 650 |
int *status) |
| 651 |
{ |
| 652 |
xmlChar *value; |
| 653 |
xmlDocPtr doc; |
| 654 |
|
| 655 |
/* Pass input file name as predefined parameter 'inputFile' */ |
| 656 |
const char *params[2+1] = { "inputFile" }; |
| 657 |
value = xmlStrdup((const xmlChar *)"'"); |
| 658 |
value = xmlStrcat(value, (const xmlChar *)filename); |
| 659 |
value = xmlStrcat(value, (const xmlChar *)"'"); |
| 660 |
params[1] = (char *) value; |
| 661 |
|
| 662 |
|
| 663 |
doc = xmlReadFile(filename, NULL, xml_options); |
| 664 |
if (doc != NULL) { |
| 665 |
xmlDocPtr res; |
| 666 |
|
| 667 |
static xsltStylesheetPtr style = NULL; |
| 668 |
if (!style) { |
| 669 |
if (globalOptions.doc_namespace) |
| 670 |
extract_ns_defs(xmlDocGetRootElement(doc), style_tree); |
| 671 |
/* Parse XSLT stylesheet */ |
| 672 |
style = xsltParseStylesheetDoc(style_tree); |
| 673 |
if (!style) exit(EXIT_LIB_ERROR); |
| 674 |
} |
| 675 |
|
| 676 |
res = xsltTransform(xsltOps, doc, params, style, filename); |
| 677 |
if (!ops->quiet && (!res || xsltSaveResultToFile(stdout, res, style) < 0)) |
| 678 |
{ |
| 679 |
*status = EXIT_LIB_ERROR; |
| 680 |
} |
| 681 |
else if ((ops->quiet || *status == EXIT_FAILURE) && res && res->children) |
| 682 |
{ |
| 683 |
*status = EXIT_SUCCESS; |
| 684 |
if (ops->quiet) exit(EXIT_SUCCESS); |
| 685 |
} |
| 686 |
} else { |
| 687 |
*status = EXIT_BAD_FILE; |
| 688 |
} |
| 689 |
|
| 690 |
xmlFree(value); |
| 691 |
} |
| 692 |
|
| 693 |
/** |
| 694 |
* This is the main function for 'select' option |
| 695 |
*/ |
| 696 |
int |
| 697 |
selMain(int argc, char **argv) |
| 698 |
{ |
| 699 |
static xsltOptions xsltOps; |
| 700 |
static selOptions ops; |
| 701 |
int start, i, n, status = EXIT_FAILURE; |
| 702 |
int nCount = 0; |
| 703 |
xmlDocPtr style_tree; |
| 704 |
int xml_options = 0; |
| 705 |
|
| 706 |
if (argc <= 2) selUsage(argv[0], EXIT_BAD_ARGS); |
| 707 |
|
| 708 |
selInitOptions(&ops); |
| 709 |
xsltInitOptions(&xsltOps); |
| 710 |
start = selParseOptions(&ops, argc, argv); |
| 711 |
xml_options |= XML_PARSE_NOENT; /* substitute entities */ |
| 712 |
xml_options |= XML_PARSE_DTDATTR; /* use default attrib values */ |
| 713 |
xml_options |= ops.nonet? XML_PARSE_NONET : 0; |
| 714 |
xsltOps.nonet = ops.nonet; |
| 715 |
xsltOps.noblanks = ops.noblanks; |
| 716 |
xsltInitLibXml(&xsltOps); |
| 717 |
xsltSetSortFunc(caseSortFunction); |
| 718 |
|
| 719 |
/* set parameters */ |
| 720 |
parseNSArr(ns_arr, &nCount, start, argv+2); |
| 721 |
|
| 722 |
style_tree = xmlNewDoc(NULL); |
| 723 |
i = selPrepareXslt(style_tree, &ops, ns_arr, start, argc, argv); |
| 724 |
|
| 725 |
if (ops.printXSLT) |
| 726 |
{ |
| 727 |
if (i < argc) { |
| 728 |
xmlTextReaderPtr reader = xmlReaderForFile(argv[i], NULL, xml_options); |
| 729 |
xmlTextReaderRead(reader); |
| 730 |
extract_ns_defs(xmlTextReaderCurrentNode(reader), style_tree); |
| 731 |
xmlTextReaderClose(reader); |
| 732 |
} |
| 733 |
xmlDocFormatDump(stdout, style_tree, 1); |
| 734 |
exit(EXIT_SUCCESS); |
| 735 |
} |
| 736 |
|
| 737 |
for (n=i; n<argc; n++) |
| 738 |
do_file(argv[n], style_tree, xml_options, &ops, &xsltOps, &status); |
| 739 |
|
| 740 |
if (i == argc) |
| 741 |
do_file("-", style_tree, xml_options, &ops, &xsltOps, &status); |
| 742 |
|
| 743 |
/* |
| 744 |
* Shutdown libxml |
| 745 |
*/ |
| 746 |
xsltCleanupGlobals(); |
| 747 |
xmlCleanupParser(); |
| 748 |
|
| 749 |
return status; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
/****************************************************************************/ |
| 756 |
|
| 757 |
/** |
| 758 |
* @number: compare numerically? |
| 759 |
* @returns: negative if @obj1 compares less than @obj2 |
| 760 |
*/ |
| 761 |
static int |
| 762 |
compareFunction(xmlXPathObjectPtr obj1, xmlXPathObjectPtr obj2, |
| 763 |
int number, int lower_first, int descending) |
| 764 |
{ |
| 765 |
int tst; |
| 766 |
|
| 767 |
if (number) { |
| 768 |
/* We make NaN smaller than number in accordance |
| 769 |
with XSLT spec */ |
| 770 |
if (xmlXPathIsNaN(obj1->floatval)) { |
| 771 |
if (xmlXPathIsNaN(obj2->floatval)) |
| 772 |
tst = 0; |
| 773 |
else |
| 774 |
tst = -1; |
| 775 |
} else if (xmlXPathIsNaN(obj2->floatval)) |
| 776 |
tst = 1; |
| 777 |
else if (obj1->floatval == obj2->floatval) |
| 778 |
tst = 0; |
| 779 |
else if (obj1->floatval > obj2->floatval) |
| 780 |
tst = 1; |
| 781 |
else tst = -1; |
| 782 |
} else { |
| 783 |
tst = xmlStrcasecmp(obj1->stringval, obj2->stringval); |
| 784 |
if (tst == 0) { |
| 785 |
tst = xmlStrcmp(obj1->stringval, obj2->stringval); |
| 786 |
if (lower_first) |
| 787 |
tst = -tst; |
| 788 |
} |
| 789 |
} |
| 790 |
if (descending) |
| 791 |
tst = -tst; |
| 792 |
|
| 793 |
return tst; |
| 794 |
} |
| 795 |
|
| 796 |
/** |
| 797 |
* xsltSortFunction: |
| 798 |
* @ctxt: a XSLT process context |
| 799 |
* @sorts: array of sort nodes |
| 800 |
* @nbsorts: the number of sorts in the array |
| 801 |
* |
| 802 |
* reorder the current node list accordingly to the set of sorting |
| 803 |
* requirement provided by the arry of nodes. |
| 804 |
* |
| 805 |
* like xsltDefaultSortFunction, but respect case-order attribute |
| 806 |
*/ |
| 807 |
void |
| 808 |
caseSortFunction(xsltTransformContextPtr ctxt, xmlNodePtr *sorts, |
| 809 |
int nbsorts) { |
| 810 |
#ifdef XSLT_REFACTORED |
| 811 |
xsltStyleItemSortPtr comp; |
| 812 |
#else |
| 813 |
xsltStylePreCompPtr comp; |
| 814 |
#endif |
| 815 |
xmlXPathObjectPtr *resultsTab[XSLT_MAX_SORT]; |
| 816 |
xmlXPathObjectPtr *results = NULL, *res; |
| 817 |
xmlNodeSetPtr list = NULL; |
| 818 |
int descending, number, desc, numb; |
| 819 |
int len = 0; |
| 820 |
int i, j, incr; |
| 821 |
int tst; |
| 822 |
int depth; |
| 823 |
xmlNodePtr node; |
| 824 |
xmlXPathObjectPtr tmp; |
| 825 |
int tempstype[XSLT_MAX_SORT], temporder[XSLT_MAX_SORT], |
| 826 |
tempcaseorder[XSLT_MAX_SORT]; |
| 827 |
|
| 828 |
if ((ctxt == NULL) || (sorts == NULL) || (nbsorts <= 0) || |
| 829 |
(nbsorts >= XSLT_MAX_SORT)) |
| 830 |
return; |
| 831 |
if (sorts[0] == NULL) |
| 832 |
return; |
| 833 |
comp = sorts[0]->psvi; |
| 834 |
if (comp == NULL) |
| 835 |
return; |
| 836 |
|
| 837 |
list = ctxt->nodeList; |
| 838 |
if ((list == NULL) || (list->nodeNr <= 1)) |
| 839 |
return; /* nothing to do */ |
| 840 |
|
| 841 |
for (j = 0; j < nbsorts; j++) { |
| 842 |
comp = sorts[j]->psvi; |
| 843 |
tempstype[j] = 0; |
| 844 |
if ((comp->stype == NULL) && (comp->has_stype != 0)) { |
| 845 |
comp->stype = |
| 846 |
xsltEvalAttrValueTemplate(ctxt, sorts[j], |
| 847 |
(const xmlChar *) "data-type", |
| 848 |
XSLT_NAMESPACE); |
| 849 |
if (comp->stype != NULL) { |
| 850 |
tempstype[j] = 1; |
| 851 |
if (xmlStrEqual(comp->stype, (const xmlChar *) "text")) |
| 852 |
comp->number = 0; |
| 853 |
else if (xmlStrEqual(comp->stype, (const xmlChar *) "number")) |
| 854 |
comp->number = 1; |
| 855 |
else { |
| 856 |
xsltTransformError(ctxt, NULL, sorts[j], |
| 857 |
"xsltDoSortFunction: no support for data-type = %s\n", |
| 858 |
comp->stype); |
| 859 |
comp->number = 0; /* use default */ |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
temporder[j] = 0; |
| 864 |
if ((comp->order == NULL) && (comp->has_order != 0)) { |
| 865 |
comp->order = xsltEvalAttrValueTemplate(ctxt, sorts[j], |
| 866 |
(const xmlChar *) "order", |
| 867 |
XSLT_NAMESPACE); |
| 868 |
if (comp->order != NULL) { |
| 869 |
temporder[j] = 1; |
| 870 |
if (xmlStrEqual(comp->order, (const xmlChar *) "ascending")) |
| 871 |
comp->descending = 0; |
| 872 |
else if (xmlStrEqual(comp->order, |
| 873 |
(const xmlChar *) "descending")) |
| 874 |
comp->descending = 1; |
| 875 |
else { |
| 876 |
xsltTransformError(ctxt, NULL, sorts[j], |
| 877 |
"xsltDoSortFunction: invalid value %s for order\n", |
| 878 |
comp->order); |
| 879 |
comp->descending = 0; /* use default */ |
| 880 |
} |
| 881 |
} |
| 882 |
} |
| 883 |
|
| 884 |
tempcaseorder[j] = 0; |
| 885 |
if ((comp->case_order == NULL) /* && (comp->has_case_order != 0) */) { |
| 886 |
comp->case_order = xsltEvalAttrValueTemplate(ctxt, sorts[j], |
| 887 |
(const xmlChar *) "case-order", XSLT_NAMESPACE); |
| 888 |
if (comp->case_order != NULL) { |
| 889 |
tempcaseorder[j] = 1; |
| 890 |
if (xmlStrEqual(comp->case_order, BAD_CAST "upper-first")) |
| 891 |
comp->lower_first = 0; |
| 892 |
else if (xmlStrEqual(comp->case_order, BAD_CAST "lower-first")) |
| 893 |
comp->lower_first = 1; |
| 894 |
else { |
| 895 |
xsltTransformError(ctxt, NULL, sorts[j], |
| 896 |
"xsltDoSortFunction: invalid value %s for case-order\n", |
| 897 |
comp->case_order); |
| 898 |
comp->lower_first = 0; /* use default */ |
| 899 |
} |
| 900 |
} |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
len = list->nodeNr; |
| 905 |
|
| 906 |
resultsTab[0] = xsltComputeSortResult(ctxt, sorts[0]); |
| 907 |
for (i = 1;i < XSLT_MAX_SORT;i++) |
| 908 |
resultsTab[i] = NULL; |
| 909 |
|
| 910 |
results = resultsTab[0]; |
| 911 |
|
| 912 |
comp = sorts[0]->psvi; |
| 913 |
descending = comp->descending; |
| 914 |
number = comp->number; |
| 915 |
if (results == NULL) |
| 916 |
return; |
| 917 |
|
| 918 |
/* Shell's sort of node-set */ |
| 919 |
for (incr = len / 2; incr > 0; incr /= 2) { |
| 920 |
for (i = incr; i < len; i++) { |
| 921 |
j = i - incr; |
| 922 |
if (results[i] == NULL) |
| 923 |
continue; |
| 924 |
|
| 925 |
while (j >= 0) { |
| 926 |
if (results[j] == NULL) |
| 927 |
tst = 1; |
| 928 |
else |
| 929 |
tst = compareFunction(results[j], results[j + incr], |
| 930 |
number, comp->lower_first, descending); |
| 931 |
|
| 932 |
if (tst == 0) { |
| 933 |
/* |
| 934 |
* Okay we need to use multi level sorts |
| 935 |
*/ |
| 936 |
depth = 1; |
| 937 |
while (depth < nbsorts) { |
| 938 |
if (sorts[depth] == NULL) |
| 939 |
break; |
| 940 |
comp = sorts[depth]->psvi; |
| 941 |
if (comp == NULL) |
| 942 |
break; |
| 943 |
desc = comp->descending; |
| 944 |
numb = comp->number; |
| 945 |
|
| 946 |
/* |
| 947 |
* Compute the result of the next level for the |
| 948 |
* full set, this might be optimized ... or not |
| 949 |
*/ |
| 950 |
if (resultsTab[depth] == NULL) |
| 951 |
resultsTab[depth] = xsltComputeSortResult(ctxt, |
| 952 |
sorts[depth]); |
| 953 |
res = resultsTab[depth]; |
| 954 |
if (res == NULL) |
| 955 |
break; |
| 956 |
if (res[j] == NULL) { |
| 957 |
if (res[j+incr] != NULL) |
| 958 |
tst = 1; |
| 959 |
} else { |
| 960 |
tst = compareFunction(res[j], res[j+incr], |
| 961 |
numb, comp->lower_first, desc); |
| 962 |
} |
| 963 |
|
| 964 |
/* |
| 965 |
* if we still can't differenciate at this level |
| 966 |
* try one level deeper. |
| 967 |
*/ |
| 968 |
if (tst != 0) |
| 969 |
break; |
| 970 |
depth++; |
| 971 |
} |
| 972 |
} |
| 973 |
if (tst == 0) { |
| 974 |
tst = results[j]->index > results[j + incr]->index; |
| 975 |
} |
| 976 |
if (tst > 0) { |
| 977 |
tmp = results[j]; |
| 978 |
results[j] = results[j + incr]; |
| 979 |
results[j + incr] = tmp; |
| 980 |
node = list->nodeTab[j]; |
| 981 |
list->nodeTab[j] = list->nodeTab[j + incr]; |
| 982 |
list->nodeTab[j + incr] = node; |
| 983 |
depth = 1; |
| 984 |
while (depth < nbsorts) { |
| 985 |
if (sorts[depth] == NULL) |
| 986 |
break; |
| 987 |
if (resultsTab[depth] == NULL) |
| 988 |
break; |
| 989 |
res = resultsTab[depth]; |
| 990 |
tmp = res[j]; |
| 991 |
res[j] = res[j + incr]; |
| 992 |
res[j + incr] = tmp; |
| 993 |
depth++; |
| 994 |
} |
| 995 |
j -= incr; |
| 996 |
} else |
| 997 |
break; |
| 998 |
} |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
for (j = 0; j < nbsorts; j++) { |
| 1003 |
comp = sorts[j]->psvi; |
| 1004 |
if (tempstype[j] == 1) { |
| 1005 |
/* The data-type needs to be recomputed each time */ |
| 1006 |
xmlFree((void *)(comp->stype)); |
| 1007 |
comp->stype = NULL; |
| 1008 |
} |
| 1009 |
if (temporder[j] == 1) { |
| 1010 |
/* The order needs to be recomputed each time */ |
| 1011 |
xmlFree((void *)(comp->order)); |
| 1012 |
comp->order = NULL; |
| 1013 |
} |
| 1014 |
if (tempcaseorder[j] == 1) { |
| 1015 |
/* The case-order needs to be recomputed each time */ |
| 1016 |
xmlFree((void *)(comp->case_order)); |
| 1017 |
comp->case_order = NULL; |
| 1018 |
} |
| 1019 |
if (resultsTab[j] != NULL) { |
| 1020 |
for (i = 0;i < len;i++) |
| 1021 |
xmlXPathFreeObject(resultsTab[j][i]); |
| 1022 |
xmlFree(resultsTab[j]); |
| 1023 |
} |
| 1024 |
} |
| 1025 |
} |