Coverage Report

Created: 2026-12-01 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxslt/libxslt/transform.c
Line
Count
Source
1
/*
2
 * transform.c: Implementation of the XSL Transformation 1.0 engine
3
 *              transform part, i.e. applying a Stylesheet to a document
4
 *
5
 * References:
6
 *   http://www.w3.org/TR/1999/REC-xslt-19991116
7
 *
8
 *   Michael Kay "XSLT Programmer's Reference" pp 637-643
9
 *   Writing Multiple Output Files
10
 *
11
 *   XSLT-1.1 Working Draft
12
 *   http://www.w3.org/TR/xslt11#multiple-output
13
 *
14
 * See Copyright for the status of this software.
15
 *
16
 * daniel@veillard.com
17
 */
18
19
#define IN_LIBXSLT
20
#include "libxslt.h"
21
22
#include <limits.h>
23
#include <string.h>
24
#include <stdio.h>
25
#include <stddef.h>
26
27
#include <libxml/xmlmemory.h>
28
#include <libxml/parser.h>
29
#include <libxml/tree.h>
30
#include <libxml/valid.h>
31
#include <libxml/hash.h>
32
#include <libxml/encoding.h>
33
#include <libxml/xmlerror.h>
34
#include <libxml/xpath.h>
35
#include <libxml/parserInternals.h>
36
#include <libxml/xpathInternals.h>
37
#include <libxml/HTMLtree.h>
38
#include <libxml/debugXML.h>
39
#include <libxml/uri.h>
40
#include "xslt.h"
41
#include "xsltInternals.h"
42
#include "xsltutils.h"
43
#include "xsltlocale.h"
44
#include "pattern.h"
45
#include "transform.h"
46
#include "transformInternals.h"
47
#include "variables.h"
48
#include "numbersInternals.h"
49
#include "namespaces.h"
50
#include "attributes.h"
51
#include "templates.h"
52
#include "imports.h"
53
#include "keys.h"
54
#include "documents.h"
55
#include "extensions.h"
56
#include "extra.h"
57
#include "preproc.h"
58
#include "security.h"
59
60
#ifdef WITH_XSLT_DEBUG
61
#define WITH_XSLT_DEBUG_EXTRA
62
#define WITH_XSLT_DEBUG_PROCESS
63
#define WITH_XSLT_DEBUG_VARIABLE
64
#endif
65
66
#define XSLT_GENERATE_HTML_DOCTYPE
67
#ifdef XSLT_GENERATE_HTML_DOCTYPE
68
static int xsltGetHTMLIDs(const xmlChar *version, const xmlChar **publicID,
69
        const xmlChar **systemID);
70
#endif
71
72
int xsltMaxDepth = 3000;
73
int xsltMaxVars = 15000;
74
75
/*
76
 * Useful macros
77
 */
78
79
#ifndef FALSE
80
# define FALSE (0 == 1)
81
# define TRUE (!FALSE)
82
#endif
83
84
#define IS_BLANK_NODE(n)            \
85
0
    (((n)->type == XML_TEXT_NODE) && (xsltIsBlank((n)->content)))
86
87
88
/*
89
* Forward declarations
90
*/
91
92
static xmlNsPtr
93
xsltCopyNamespaceListInternal(xmlNodePtr node, xmlNsPtr cur);
94
95
static xmlNodePtr
96
xsltCopyTree(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
97
       xmlNodePtr node, xmlNodePtr insert, int isLRE,
98
       int topElemVisited);
99
100
static void
101
xsltApplySequenceConstructor(xsltTransformContextPtr ctxt,
102
           xmlNodePtr contextNode, xmlNodePtr list,
103
           xsltTemplatePtr templ);
104
105
static void
106
xsltApplyXSLTTemplate(xsltTransformContextPtr ctxt,
107
          xmlNodePtr contextNode,
108
          xmlNodePtr list,
109
          xsltTemplatePtr templ,
110
          xsltStackElemPtr withParams);
111
112
/**
113
 * templPush:
114
 * @ctxt: the transformation context
115
 * @value:  the template to push on the stack
116
 *
117
 * Push a template on the stack
118
 *
119
 * Returns the new index in the stack or 0 in case of error
120
 */
121
static int
122
templPush(xsltTransformContextPtr ctxt, xsltTemplatePtr value)
123
0
{
124
0
    if (ctxt->templNr >= ctxt->templMax) {
125
0
        xsltTemplatePtr *tmp;
126
0
        int newMax = ctxt->templMax == 0 ? 4 : ctxt->templMax * 2;
127
128
0
        tmp = (xsltTemplatePtr *) xmlRealloc(ctxt->templTab,
129
0
                newMax * sizeof(*tmp));
130
0
        if (tmp == NULL) {
131
0
            xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
132
0
            return (0);
133
0
        }
134
0
        ctxt->templTab = tmp;
135
0
        ctxt->templMax = newMax;
136
0
    }
137
0
    ctxt->templTab[ctxt->templNr] = value;
138
0
    ctxt->templ = value;
139
0
    return (ctxt->templNr++);
140
0
}
141
/**
142
 * templPop:
143
 * @ctxt: the transformation context
144
 *
145
 * Pop a template value from the stack
146
 *
147
 * Returns the stored template value
148
 */
149
static xsltTemplatePtr
150
templPop(xsltTransformContextPtr ctxt)
151
0
{
152
0
    xsltTemplatePtr ret;
153
154
0
    if (ctxt->templNr <= 0)
155
0
        return (0);
156
0
    ctxt->templNr--;
157
0
    if (ctxt->templNr > 0)
158
0
        ctxt->templ = ctxt->templTab[ctxt->templNr - 1];
159
0
    else
160
0
        ctxt->templ = (xsltTemplatePtr) 0;
161
0
    ret = ctxt->templTab[ctxt->templNr];
162
0
    ctxt->templTab[ctxt->templNr] = 0;
163
0
    return (ret);
164
0
}
165
166
/**
167
 * xsltLocalVariablePop:
168
 * @ctxt: the transformation context
169
 * @limitNr: number of variables which should remain
170
 * @level: the depth in the xsl:template's tree
171
 *
172
 * Pops all variable values at the given @depth from the stack.
173
 *
174
 * Returns the stored variable value
175
 * **NOTE:**
176
 * This is an internal routine and should not be called by users!
177
 */
178
void
179
xsltLocalVariablePop(xsltTransformContextPtr ctxt, int limitNr, int level)
180
0
{
181
0
    xsltStackElemPtr variable;
182
183
0
    if (ctxt->varsNr <= 0)
184
0
        return;
185
186
0
    do {
187
0
  if (ctxt->varsNr <= limitNr)
188
0
      break;
189
0
  variable = ctxt->varsTab[ctxt->varsNr - 1];
190
0
  if (variable->level <= level)
191
0
      break;
192
0
  if (variable->level >= 0)
193
0
      xsltFreeStackElemList(variable);
194
0
  ctxt->varsNr--;
195
0
    } while (ctxt->varsNr != 0);
196
0
    if (ctxt->varsNr > 0)
197
0
        ctxt->vars = ctxt->varsTab[ctxt->varsNr - 1];
198
0
    else
199
0
        ctxt->vars = NULL;
200
0
}
201
202
/**
203
 * xsltTemplateParamsCleanup:
204
 *
205
 * Removes xsl:param and xsl:with-param items from the
206
 * variable-stack. Only xsl:with-param items are not freed.
207
 */
208
static void
209
xsltTemplateParamsCleanup(xsltTransformContextPtr ctxt)
210
0
{
211
0
    xsltStackElemPtr param;
212
213
0
    for (; ctxt->varsNr > ctxt->varsBase; ctxt->varsNr--) {
214
0
  param = ctxt->varsTab[ctxt->varsNr -1];
215
  /*
216
  * Free xsl:param items.
217
  * xsl:with-param items will have a level of -1 or -2.
218
  */
219
0
  if (param->level >= 0) {
220
0
      xsltFreeStackElemList(param);
221
0
  }
222
0
    }
223
0
    if (ctxt->varsNr > 0)
224
0
        ctxt->vars = ctxt->varsTab[ctxt->varsNr - 1];
225
0
    else
226
0
        ctxt->vars = NULL;
227
0
}
228
229
#ifdef WITH_PROFILER
230
231
/**
232
 * profPush:
233
 * @ctxt: the transformation context
234
 * @value:  the profiling value to push on the stack
235
 *
236
 * Push a profiling value on the stack
237
 *
238
 * Returns the new index in the stack or 0 in case of error
239
 */
240
static int
241
profPush(xsltTransformContextPtr ctxt, long value)
242
{
243
    if (ctxt->profMax == 0) {
244
        ctxt->profMax = 4;
245
        ctxt->profTab =
246
            (long *) xmlMalloc(ctxt->profMax * sizeof(ctxt->profTab[0]));
247
        if (ctxt->profTab == NULL) {
248
            xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
249
            return (0);
250
        }
251
    }
252
    else if (ctxt->profNr >= ctxt->profMax) {
253
        ctxt->profMax *= 2;
254
        ctxt->profTab =
255
            (long *) xmlRealloc(ctxt->profTab,
256
                                ctxt->profMax * sizeof(ctxt->profTab[0]));
257
        if (ctxt->profTab == NULL) {
258
            xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
259
            return (0);
260
        }
261
    }
262
    ctxt->profTab[ctxt->profNr] = value;
263
    ctxt->prof = value;
264
    return (ctxt->profNr++);
265
}
266
/**
267
 * profPop:
268
 * @ctxt: the transformation context
269
 *
270
 * Pop a profiling value from the stack
271
 *
272
 * Returns the stored profiling value
273
 */
274
static long
275
profPop(xsltTransformContextPtr ctxt)
276
{
277
    long ret;
278
279
    if (ctxt->profNr <= 0)
280
        return (0);
281
    ctxt->profNr--;
282
    if (ctxt->profNr > 0)
283
        ctxt->prof = ctxt->profTab[ctxt->profNr - 1];
284
    else
285
        ctxt->prof = (long) 0;
286
    ret = ctxt->profTab[ctxt->profNr];
287
    ctxt->profTab[ctxt->profNr] = 0;
288
    return (ret);
289
}
290
291
static void
292
profCallgraphAdd(xsltTemplatePtr templ, xsltTemplatePtr parent)
293
{
294
    int i;
295
296
    if (templ->templMax == 0) {
297
        templ->templMax = 4;
298
        templ->templCalledTab =
299
            (xsltTemplatePtr *) xmlMalloc(templ->templMax *
300
                                          sizeof(templ->templCalledTab[0]));
301
        templ->templCountTab =
302
            (int *) xmlMalloc(templ->templMax *
303
                                          sizeof(templ->templCountTab[0]));
304
        if (templ->templCalledTab == NULL || templ->templCountTab == NULL) {
305
            xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
306
            return;
307
        }
308
    }
309
    else if (templ->templNr >= templ->templMax) {
310
        templ->templMax *= 2;
311
        templ->templCalledTab =
312
            (xsltTemplatePtr *) xmlRealloc(templ->templCalledTab,
313
                                           templ->templMax *
314
                                           sizeof(templ->templCalledTab[0]));
315
        templ->templCountTab =
316
            (int *) xmlRealloc(templ->templCountTab,
317
                                           templ->templMax *
318
                                           sizeof(templ->templCountTab[0]));
319
        if (templ->templCalledTab == NULL || templ->templCountTab == NULL) {
320
            xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
321
            return;
322
        }
323
    }
324
325
    for (i = 0; i < templ->templNr; i++) {
326
        if (templ->templCalledTab[i] == parent) {
327
            templ->templCountTab[i]++;
328
            break;
329
        }
330
    }
331
    if (i == templ->templNr) {
332
        /* not found, add new one */
333
        templ->templCalledTab[templ->templNr] = parent;
334
        templ->templCountTab[templ->templNr] = 1;
335
        templ->templNr++;
336
    }
337
}
338
339
#endif /* WITH_PROFILER */
340
341
/**
342
 * xsltPreCompEval:
343
 * @ctxt: transform context
344
 * @node: context node
345
 * @comp: precompiled expression
346
 *
347
 * Evaluate a precompiled XPath expression.
348
 */
349
static xmlXPathObjectPtr
350
xsltPreCompEval(xsltTransformContextPtr ctxt, xmlNodePtr node,
351
0
                xsltStylePreCompPtr comp) {
352
0
    xmlXPathObjectPtr res;
353
0
    xmlXPathContextPtr xpctxt;
354
0
    xmlNodePtr oldXPContextNode;
355
0
    xmlNsPtr *oldXPNamespaces;
356
0
    int oldXPProximityPosition, oldXPContextSize, oldXPNsNr;
357
358
0
    xpctxt = ctxt->xpathCtxt;
359
0
    oldXPContextNode = xpctxt->node;
360
0
    oldXPProximityPosition = xpctxt->proximityPosition;
361
0
    oldXPContextSize = xpctxt->contextSize;
362
0
    oldXPNsNr = xpctxt->nsNr;
363
0
    oldXPNamespaces = xpctxt->namespaces;
364
365
0
    xpctxt->node = node;
366
#ifdef XSLT_REFACTORED
367
    if (comp->inScopeNs != NULL) {
368
        xpctxt->namespaces = comp->inScopeNs->list;
369
        xpctxt->nsNr = comp->inScopeNs->xpathNumber;
370
    } else {
371
        xpctxt->namespaces = NULL;
372
        xpctxt->nsNr = 0;
373
    }
374
#else
375
0
    xpctxt->namespaces = comp->nsList;
376
0
    xpctxt->nsNr = comp->nsNr;
377
0
#endif
378
379
0
    res = xmlXPathCompiledEval(comp->comp, xpctxt);
380
381
0
    xpctxt->node = oldXPContextNode;
382
0
    xpctxt->proximityPosition = oldXPProximityPosition;
383
0
    xpctxt->contextSize = oldXPContextSize;
384
0
    xpctxt->nsNr = oldXPNsNr;
385
0
    xpctxt->namespaces = oldXPNamespaces;
386
387
0
    return(res);
388
0
}
389
390
/**
391
 * xsltPreCompEvalToBoolean:
392
 * @ctxt: transform context
393
 * @node: context node
394
 * @comp: precompiled expression
395
 *
396
 * Evaluate a precompiled XPath expression as boolean.
397
 */
398
static int
399
xsltPreCompEvalToBoolean(xsltTransformContextPtr ctxt, xmlNodePtr node,
400
0
                         xsltStylePreCompPtr comp) {
401
0
    int res;
402
0
    xmlXPathContextPtr xpctxt;
403
0
    xmlNodePtr oldXPContextNode;
404
0
    xmlNsPtr *oldXPNamespaces;
405
0
    int oldXPProximityPosition, oldXPContextSize, oldXPNsNr;
406
407
0
    xpctxt = ctxt->xpathCtxt;
408
0
    oldXPContextNode = xpctxt->node;
409
0
    oldXPProximityPosition = xpctxt->proximityPosition;
410
0
    oldXPContextSize = xpctxt->contextSize;
411
0
    oldXPNsNr = xpctxt->nsNr;
412
0
    oldXPNamespaces = xpctxt->namespaces;
413
414
0
    xpctxt->node = node;
415
#ifdef XSLT_REFACTORED
416
    if (comp->inScopeNs != NULL) {
417
        xpctxt->namespaces = comp->inScopeNs->list;
418
        xpctxt->nsNr = comp->inScopeNs->xpathNumber;
419
    } else {
420
        xpctxt->namespaces = NULL;
421
        xpctxt->nsNr = 0;
422
    }
423
#else
424
0
    xpctxt->namespaces = comp->nsList;
425
0
    xpctxt->nsNr = comp->nsNr;
426
0
#endif
427
428
0
    res = xmlXPathCompiledEvalToBoolean(comp->comp, xpctxt);
429
430
0
    xpctxt->node = oldXPContextNode;
431
0
    xpctxt->proximityPosition = oldXPProximityPosition;
432
0
    xpctxt->contextSize = oldXPContextSize;
433
0
    xpctxt->nsNr = oldXPNsNr;
434
0
    xpctxt->namespaces = oldXPNamespaces;
435
436
0
    return(res);
437
0
}
438
439
/************************************************************************
440
 *                  *
441
 *      XInclude default settings     *
442
 *                  *
443
 ************************************************************************/
444
445
static int xsltDoXIncludeDefault = 0;
446
447
/**
448
 * xsltSetXIncludeDefault:
449
 * @xinclude: whether to do XInclude processing
450
 *
451
 * Set whether XInclude should be processed on document being loaded by default
452
 */
453
void
454
0
xsltSetXIncludeDefault(int xinclude) {
455
0
    xsltDoXIncludeDefault = (xinclude != 0);
456
0
}
457
458
/**
459
 * xsltGetXIncludeDefault:
460
 *
461
 * Provides the default state for XInclude processing
462
 *
463
 * Returns 0 if there is no processing 1 otherwise
464
 */
465
int
466
0
xsltGetXIncludeDefault(void) {
467
0
    return(xsltDoXIncludeDefault);
468
0
}
469
470
static unsigned long xsltDefaultTrace = (unsigned long) XSLT_TRACE_ALL;
471
472
/**
473
 * xsltDebugSetDefaultTrace:
474
 * @val: tracing level mask
475
 *
476
 * Set the default debug tracing level mask
477
 */
478
0
void xsltDebugSetDefaultTrace(xsltDebugTraceCodes val) {
479
0
  xsltDefaultTrace = val;
480
0
}
481
482
/**
483
 * xsltDebugGetDefaultTrace:
484
 *
485
 * Get the current default debug tracing level mask
486
 *
487
 * Returns the current default debug tracing level mask
488
 */
489
0
xsltDebugTraceCodes xsltDebugGetDefaultTrace(void) {
490
0
  return xsltDefaultTrace;
491
0
}
492
493
/************************************************************************
494
 *                  *
495
 *      Handling of Transformation Contexts   *
496
 *                  *
497
 ************************************************************************/
498
499
static xsltTransformCachePtr
500
xsltTransformCacheCreate(void)
501
0
{
502
0
    xsltTransformCachePtr ret;
503
504
0
    ret = (xsltTransformCachePtr) xmlMalloc(sizeof(xsltTransformCache));
505
0
    if (ret == NULL) {
506
0
  xsltTransformError(NULL, NULL, NULL,
507
0
      "xsltTransformCacheCreate : malloc failed\n");
508
0
  return(NULL);
509
0
    }
510
0
    memset(ret, 0, sizeof(xsltTransformCache));
511
0
    return(ret);
512
0
}
513
514
static void
515
xsltTransformCacheFree(xsltTransformCachePtr cache)
516
0
{
517
0
    if (cache == NULL)
518
0
  return;
519
0
    if (cache->failedDocLoads != NULL)
520
0
  xmlHashFree(cache->failedDocLoads, NULL);
521
    /*
522
    * Free tree fragments.
523
    */
524
0
    if (cache->RVT) {
525
0
  xmlDocPtr tmp, cur = cache->RVT;
526
0
  while (cur) {
527
0
      tmp = cur;
528
0
      cur = (xmlDocPtr) cur->next;
529
0
      if (tmp->_private != NULL) {
530
    /*
531
    * Tree the document info.
532
    */
533
0
    xsltFreeDocumentKeys((xsltDocumentPtr) tmp->_private);
534
0
    xmlFree(tmp->_private);
535
0
      }
536
0
      xmlFreeDoc(tmp);
537
0
  }
538
0
    }
539
    /*
540
    * Free vars/params.
541
    */
542
0
    if (cache->stackItems) {
543
0
  xsltStackElemPtr tmp, cur = cache->stackItems;
544
0
  while (cur) {
545
0
      tmp = cur;
546
0
      cur = cur->next;
547
      /*
548
      * REVISIT TODO: Should be call a destruction-function
549
      * instead?
550
      */
551
0
      xmlFree(tmp);
552
0
  }
553
0
    }
554
0
    xmlFree(cache);
555
0
}
556
557
/**
558
 * xsltNewTransformContext:
559
 * @style:  a parsed XSLT stylesheet
560
 * @doc:  the input document
561
 *
562
 * Create a new XSLT TransformContext
563
 *
564
 * Returns the newly allocated xsltTransformContextPtr or NULL in case of error
565
 */
566
xsltTransformContextPtr
567
0
xsltNewTransformContext(xsltStylesheetPtr style, xmlDocPtr doc) {
568
0
    xsltTransformContextPtr cur;
569
0
    xsltDocumentPtr docu;
570
0
    int i;
571
572
0
    xsltInitGlobals();
573
574
0
    cur = (xsltTransformContextPtr) xmlMalloc(sizeof(xsltTransformContext));
575
0
    if (cur == NULL) {
576
0
  xsltTransformError(NULL, NULL, (xmlNodePtr)doc,
577
0
    "xsltNewTransformContext : malloc failed\n");
578
0
  return(NULL);
579
0
    }
580
0
    memset(cur, 0, sizeof(xsltTransformContext));
581
582
0
    cur->cache = xsltTransformCacheCreate();
583
0
    if (cur->cache == NULL)
584
0
  goto internal_err;
585
    /*
586
     * setup of the dictionary must be done early as some of the
587
     * processing later like key handling may need it.
588
     */
589
0
    cur->dict = xmlDictCreateSub(style->dict);
590
0
    cur->internalized = ((style->internalized) && (cur->dict != NULL));
591
#ifdef WITH_XSLT_DEBUG
592
    xsltGenericDebug(xsltGenericDebugContext,
593
       "Creating sub-dictionary from stylesheet for transformation\n");
594
#endif
595
596
    /*
597
     * initialize the template stack
598
     */
599
0
    cur->templTab = (xsltTemplatePtr *)
600
0
          xmlMalloc(10 * sizeof(xsltTemplatePtr));
601
0
    if (cur->templTab == NULL) {
602
0
  xsltTransformError(NULL, NULL, (xmlNodePtr) doc,
603
0
    "xsltNewTransformContext: out of memory\n");
604
0
  goto internal_err;
605
0
    }
606
0
    cur->templNr = 0;
607
0
    cur->templMax = 5;
608
0
    cur->templ = NULL;
609
0
    cur->maxTemplateDepth = xsltMaxDepth;
610
611
    /*
612
     * initialize the variables stack
613
     */
614
0
    cur->varsTab = (xsltStackElemPtr *)
615
0
          xmlMalloc(10 * sizeof(xsltStackElemPtr));
616
0
    if (cur->varsTab == NULL) {
617
0
        xmlGenericError(xmlGenericErrorContext,
618
0
    "xsltNewTransformContext: out of memory\n");
619
0
  goto internal_err;
620
0
    }
621
0
    cur->varsNr = 0;
622
0
    cur->varsMax = 10;
623
0
    cur->vars = NULL;
624
0
    cur->varsBase = 0;
625
0
    cur->maxTemplateVars = xsltMaxVars;
626
627
    /*
628
     * the profiling stack is not initialized by default
629
     */
630
0
    cur->profTab = NULL;
631
0
    cur->profNr = 0;
632
0
    cur->profMax = 0;
633
0
    cur->prof = 0;
634
635
0
    cur->style = style;
636
0
    cur->xpathCtxt = xmlXPathNewContext(doc);
637
0
    if (cur->xpathCtxt == NULL) {
638
0
  xsltTransformError(NULL, NULL, (xmlNodePtr) doc,
639
0
    "xsltNewTransformContext : xmlXPathNewContext failed\n");
640
0
  goto internal_err;
641
0
    }
642
    /*
643
    * Create an XPath cache.
644
    */
645
0
    if (xmlXPathContextSetCache(cur->xpathCtxt, 1, -1, 0) == -1)
646
0
  goto internal_err;
647
    /*
648
     * Initialize the extras array
649
     */
650
0
    if (style->extrasNr != 0) {
651
0
  cur->extrasMax = style->extrasNr + 20;
652
0
  cur->extras = (xsltRuntimeExtraPtr)
653
0
      xmlMalloc(cur->extrasMax * sizeof(xsltRuntimeExtra));
654
0
  if (cur->extras == NULL) {
655
0
      xmlGenericError(xmlGenericErrorContext,
656
0
        "xsltNewTransformContext: out of memory\n");
657
0
      goto internal_err;
658
0
  }
659
0
  cur->extrasNr = style->extrasNr;
660
0
  for (i = 0;i < cur->extrasMax;i++) {
661
0
      cur->extras[i].info = NULL;
662
0
      cur->extras[i].deallocate = NULL;
663
0
      cur->extras[i].val.ptr = NULL;
664
0
  }
665
0
    } else {
666
0
  cur->extras = NULL;
667
0
  cur->extrasNr = 0;
668
0
  cur->extrasMax = 0;
669
0
    }
670
671
0
    XSLT_REGISTER_VARIABLE_LOOKUP(cur);
672
0
    XSLT_REGISTER_FUNCTION_LOOKUP(cur);
673
0
    cur->xpathCtxt->nsHash = style->nsHash;
674
    /*
675
     * Initialize the registered external modules
676
     */
677
0
    xsltInitCtxtExts(cur);
678
    /*
679
     * Setup document element ordering for later efficiencies
680
     * (bug 133289)
681
     */
682
0
    if (xslDebugStatus == XSLT_DEBUG_NONE)
683
0
        xmlXPathOrderDocElems(doc);
684
    /*
685
     * Must set parserOptions before calling xsltNewDocument
686
     * (bug 164530)
687
     */
688
0
    cur->parserOptions = XSLT_PARSE_OPTIONS;
689
0
    docu = xsltNewDocument(cur, doc);
690
0
    if (docu == NULL) {
691
0
  xsltTransformError(cur, NULL, (xmlNodePtr)doc,
692
0
    "xsltNewTransformContext : xsltNewDocument failed\n");
693
0
  goto internal_err;
694
0
    }
695
0
    docu->main = 1;
696
0
    cur->document = docu;
697
0
    cur->inst = NULL;
698
0
    cur->outputFile = NULL;
699
0
    cur->sec = xsltGetDefaultSecurityPrefs();
700
0
    cur->debugStatus = xslDebugStatus;
701
0
    cur->traceCode = (unsigned long*) &xsltDefaultTrace;
702
0
    cur->xinclude = xsltGetXIncludeDefault();
703
0
    cur->keyInitLevel = 0;
704
705
0
    cur->newLocale = xsltNewLocale;
706
0
    cur->freeLocale = xsltFreeLocale;
707
0
    cur->genSortKey = xsltStrxfrm;
708
709
0
    return(cur);
710
711
0
internal_err:
712
0
    if (cur != NULL)
713
0
  xsltFreeTransformContext(cur);
714
0
    return(NULL);
715
0
}
716
717
/**
718
 * xsltFreeTransformContext:
719
 * @ctxt:  an XSLT transform context
720
 *
721
 * Free up the memory allocated by @ctxt
722
 */
723
void
724
0
xsltFreeTransformContext(xsltTransformContextPtr ctxt) {
725
0
    if (ctxt == NULL)
726
0
  return;
727
728
    /*
729
     * Shutdown the extension modules associated to the stylesheet
730
     * used if needed.
731
     */
732
0
    xsltShutdownCtxtExts(ctxt);
733
734
0
    if (ctxt->xpathCtxt != NULL) {
735
0
  ctxt->xpathCtxt->nsHash = NULL;
736
0
  xmlXPathFreeContext(ctxt->xpathCtxt);
737
0
    }
738
0
    if (ctxt->templTab != NULL)
739
0
  xmlFree(ctxt->templTab);
740
0
    if (ctxt->varsTab != NULL)
741
0
  xmlFree(ctxt->varsTab);
742
0
    if (ctxt->profTab != NULL)
743
0
  xmlFree(ctxt->profTab);
744
0
    if ((ctxt->extrasNr > 0) && (ctxt->extras != NULL)) {
745
0
  int i;
746
747
0
  for (i = 0;i < ctxt->extrasNr;i++) {
748
0
      if ((ctxt->extras[i].deallocate != NULL) &&
749
0
    (ctxt->extras[i].info != NULL))
750
0
    ctxt->extras[i].deallocate(ctxt->extras[i].info);
751
0
  }
752
0
  xmlFree(ctxt->extras);
753
0
    }
754
0
    xsltFreeGlobalVariables(ctxt);
755
0
    xsltFreeDocuments(ctxt);
756
0
    xsltFreeCtxtExts(ctxt);
757
0
    xsltFreeRVTs(ctxt);
758
0
    xsltTransformCacheFree(ctxt->cache);
759
0
    xmlDictFree(ctxt->dict);
760
#ifdef WITH_XSLT_DEBUG
761
    xsltGenericDebug(xsltGenericDebugContext,
762
                     "freeing transformation dictionary\n");
763
#endif
764
0
    memset(ctxt, -1, sizeof(xsltTransformContext));
765
0
    xmlFree(ctxt);
766
0
}
767
768
/************************************************************************
769
 *                  *
770
 *      Copy of Nodes in an XSLT fashion    *
771
 *                  *
772
 ************************************************************************/
773
774
/**
775
 * xsltAddChild:
776
 * @parent:  the parent node
777
 * @cur:  the child node
778
 *
779
 * Wrapper version of xmlAddChild with a more consistent behaviour on
780
 * error. One expect the use to be child = xsltAddChild(parent, child);
781
 * and the routine will take care of not leaking on errors or node merge
782
 *
783
 * Returns the child is successfully attached or NULL if merged or freed
784
 */
785
static xmlNodePtr
786
0
xsltAddChild(xmlNodePtr parent, xmlNodePtr cur) {
787
0
   xmlNodePtr ret;
788
789
0
   if (cur == NULL)
790
0
       return(NULL);
791
0
   if (parent == NULL) {
792
0
       xmlFreeNode(cur);
793
0
       return(NULL);
794
0
   }
795
0
   ret = xmlAddChild(parent, cur);
796
797
0
   return(ret);
798
0
}
799
800
/**
801
 * xsltAddTextString:
802
 * @ctxt:  a XSLT process context
803
 * @target:  the text node where the text will be attached
804
 * @string:  the text string
805
 * @len:  the string length in byte
806
 *
807
 * Extend the current text node with the new string, it handles coalescing
808
 *
809
 * Returns: the text node
810
 */
811
static xmlNodePtr
812
xsltAddTextString(xsltTransformContextPtr ctxt, xmlNodePtr target,
813
0
      const xmlChar *string, int len) {
814
    /*
815
     * optimization
816
     */
817
0
    if ((len <= 0) || (string == NULL) || (target == NULL))
818
0
        return(target);
819
820
0
    if (ctxt->lasttext == target->content) {
821
0
        int minSize;
822
823
        /* Check for integer overflow accounting for NUL terminator. */
824
0
        if (len >= INT_MAX - ctxt->lasttuse) {
825
0
            xsltTransformError(ctxt, NULL, target,
826
0
                "xsltCopyText: text allocation failed\n");
827
0
            return(NULL);
828
0
        }
829
0
        minSize = ctxt->lasttuse + len + 1;
830
831
0
        if (ctxt->lasttsize < minSize) {
832
0
      xmlChar *newbuf;
833
0
      int size;
834
0
            int extra;
835
836
            /* Double buffer size but increase by at least 100 bytes. */
837
0
            extra = minSize < 100 ? 100 : minSize;
838
839
            /* Check for integer overflow. */
840
0
            if (extra > INT_MAX - ctxt->lasttsize) {
841
0
                size = INT_MAX;
842
0
            }
843
0
            else {
844
0
                size = ctxt->lasttsize + extra;
845
0
            }
846
847
0
      newbuf = (xmlChar *) xmlRealloc(target->content,size);
848
0
      if (newbuf == NULL) {
849
0
    xsltTransformError(ctxt, NULL, target,
850
0
     "xsltCopyText: text allocation failed\n");
851
0
    return(NULL);
852
0
      }
853
0
      ctxt->lasttsize = size;
854
0
      ctxt->lasttext = newbuf;
855
0
      target->content = newbuf;
856
0
  }
857
0
  memcpy(&(target->content[ctxt->lasttuse]), string, len);
858
0
  ctxt->lasttuse += len;
859
0
  target->content[ctxt->lasttuse] = 0;
860
0
    } else {
861
0
  xmlNodeAddContent(target, string);
862
0
  ctxt->lasttext = target->content;
863
0
  len = xmlStrlen(target->content);
864
0
  ctxt->lasttsize = len;
865
0
  ctxt->lasttuse = len;
866
0
    }
867
0
    return(target);
868
0
}
869
870
/**
871
 * xsltCopyTextString:
872
 * @ctxt:  a XSLT process context
873
 * @target:  the element where the text will be attached
874
 * @string:  the text string
875
 * @noescape:  should disable-escaping be activated for this text node.
876
 *
877
 * Adds @string to a newly created or an existent text node child of
878
 * @target.
879
 *
880
 * Returns: the text node, where the text content of @cur is copied to.
881
 *          NULL in case of API or internal errors.
882
 */
883
xmlNodePtr
884
xsltCopyTextString(xsltTransformContextPtr ctxt, xmlNodePtr target,
885
             const xmlChar *string, int noescape)
886
0
{
887
0
    xmlNodePtr copy;
888
0
    int len;
889
890
0
    if (string == NULL)
891
0
  return(NULL);
892
893
#ifdef WITH_XSLT_DEBUG_PROCESS
894
    XSLT_TRACE(ctxt,XSLT_TRACE_COPY_TEXT,xsltGenericDebug(xsltGenericDebugContext,
895
         "xsltCopyTextString: copy text %s\n",
896
         string));
897
#endif
898
899
    /*
900
    * Play safe and reset the merging mechanism for every new
901
    * target node.
902
    */
903
0
    if ((target == NULL) || (target->children == NULL)) {
904
0
  ctxt->lasttext = NULL;
905
0
    }
906
907
    /* handle coalescing of text nodes here */
908
0
    len = xmlStrlen(string);
909
0
    if ((ctxt->type == XSLT_OUTPUT_XML) &&
910
0
  (ctxt->style->cdataSection != NULL) &&
911
0
  (target != NULL) &&
912
0
  (target->type == XML_ELEMENT_NODE) &&
913
0
  (((target->ns == NULL) &&
914
0
    (xmlHashLookup2(ctxt->style->cdataSection,
915
0
              target->name, NULL) != NULL)) ||
916
0
   ((target->ns != NULL) &&
917
0
    (xmlHashLookup2(ctxt->style->cdataSection,
918
0
                    target->name, target->ns->href) != NULL))))
919
0
    {
920
  /*
921
  * Process "cdata-section-elements".
922
  */
923
0
  if ((target->last != NULL) &&
924
0
      (target->last->type == XML_CDATA_SECTION_NODE))
925
0
  {
926
0
      return(xsltAddTextString(ctxt, target->last, string, len));
927
0
  }
928
0
  copy = xmlNewCDataBlock(ctxt->output, string, len);
929
0
    } else if (noescape) {
930
  /*
931
  * Process "disable-output-escaping".
932
  */
933
0
  if ((target != NULL) && (target->last != NULL) &&
934
0
      (target->last->type == XML_TEXT_NODE) &&
935
0
      (target->last->name == xmlStringTextNoenc))
936
0
  {
937
0
      return(xsltAddTextString(ctxt, target->last, string, len));
938
0
  }
939
0
  copy = xmlNewTextLen(string, len);
940
0
  if (copy != NULL)
941
0
      copy->name = xmlStringTextNoenc;
942
0
    } else {
943
  /*
944
  * Default processing.
945
  */
946
0
  if ((target != NULL) && (target->last != NULL) &&
947
0
      (target->last->type == XML_TEXT_NODE) &&
948
0
      (target->last->name == xmlStringText)) {
949
0
      return(xsltAddTextString(ctxt, target->last, string, len));
950
0
  }
951
0
  copy = xmlNewTextLen(string, len);
952
0
    }
953
0
    if (copy != NULL && target != NULL)
954
0
  copy = xsltAddChild(target, copy);
955
0
    if (copy != NULL) {
956
0
  ctxt->lasttext = copy->content;
957
0
  ctxt->lasttsize = len;
958
0
  ctxt->lasttuse = len;
959
0
    } else {
960
0
  xsltTransformError(ctxt, NULL, target,
961
0
       "xsltCopyTextString: text copy failed\n");
962
0
  ctxt->lasttext = NULL;
963
0
    }
964
0
    return(copy);
965
0
}
966
967
/**
968
 * xsltCopyText:
969
 * @ctxt:  a XSLT process context
970
 * @target:  the element where the text will be attached
971
 * @cur:  the text or CDATA node
972
 * @interned:  the string is in the target doc dictionary
973
 *
974
 * Copy the text content of @cur and append it to @target's children.
975
 *
976
 * Returns: the text node, where the text content of @cur is copied to.
977
 *          NULL in case of API or internal errors.
978
 */
979
static xmlNodePtr
980
xsltCopyText(xsltTransformContextPtr ctxt, xmlNodePtr target,
981
       xmlNodePtr cur, int interned)
982
0
{
983
0
    xmlNodePtr copy;
984
985
0
    if ((cur->type != XML_TEXT_NODE) &&
986
0
  (cur->type != XML_CDATA_SECTION_NODE))
987
0
  return(NULL);
988
0
    if (cur->content == NULL)
989
0
  return(NULL);
990
991
#ifdef WITH_XSLT_DEBUG_PROCESS
992
    if (cur->type == XML_CDATA_SECTION_NODE) {
993
  XSLT_TRACE(ctxt,XSLT_TRACE_COPY_TEXT,xsltGenericDebug(xsltGenericDebugContext,
994
       "xsltCopyText: copy CDATA text %s\n",
995
       cur->content));
996
    } else if (cur->name == xmlStringTextNoenc) {
997
  XSLT_TRACE(ctxt,XSLT_TRACE_COPY_TEXT,xsltGenericDebug(xsltGenericDebugContext,
998
         "xsltCopyText: copy unescaped text %s\n",
999
       cur->content));
1000
    } else {
1001
  XSLT_TRACE(ctxt,XSLT_TRACE_COPY_TEXT,xsltGenericDebug(xsltGenericDebugContext,
1002
       "xsltCopyText: copy text %s\n",
1003
       cur->content));
1004
    }
1005
#endif
1006
1007
    /*
1008
    * Play save and reset the merging mechanism for every new
1009
    * target node.
1010
    */
1011
0
    if ((target == NULL) || (target->children == NULL)) {
1012
0
  ctxt->lasttext = NULL;
1013
0
    }
1014
1015
0
    if ((ctxt->style->cdataSection != NULL) &&
1016
0
  (ctxt->type == XSLT_OUTPUT_XML) &&
1017
0
  (target != NULL) &&
1018
0
  (target->type == XML_ELEMENT_NODE) &&
1019
0
  (((target->ns == NULL) &&
1020
0
    (xmlHashLookup2(ctxt->style->cdataSection,
1021
0
              target->name, NULL) != NULL)) ||
1022
0
   ((target->ns != NULL) &&
1023
0
    (xmlHashLookup2(ctxt->style->cdataSection,
1024
0
                    target->name, target->ns->href) != NULL))))
1025
0
    {
1026
  /*
1027
  * Process "cdata-section-elements".
1028
  */
1029
  /*
1030
  * OPTIMIZE TODO: xsltCopyText() is also used for attribute content.
1031
  */
1032
  /*
1033
  * TODO: Since this doesn't merge adjacent CDATA-section nodes,
1034
  * we'll get: <![CDATA[x]]><!CDATA[y]]>.
1035
  * TODO: Reported in #321505.
1036
  */
1037
0
  if ((target->last != NULL) &&
1038
0
       (target->last->type == XML_CDATA_SECTION_NODE))
1039
0
  {
1040
      /*
1041
      * Append to existing CDATA-section node.
1042
      */
1043
0
      copy = xsltAddTextString(ctxt, target->last, cur->content,
1044
0
    xmlStrlen(cur->content));
1045
0
      goto exit;
1046
0
  } else {
1047
0
      unsigned int len;
1048
1049
0
      len = xmlStrlen(cur->content);
1050
0
      copy = xmlNewCDataBlock(ctxt->output, cur->content, len);
1051
0
      if (copy == NULL)
1052
0
    goto exit;
1053
0
      ctxt->lasttext = copy->content;
1054
0
      ctxt->lasttsize = len;
1055
0
      ctxt->lasttuse = len;
1056
0
  }
1057
0
    } else if ((target != NULL) &&
1058
0
  (target->last != NULL) &&
1059
  /* both escaped or both non-escaped text-nodes */
1060
0
  (((target->last->type == XML_TEXT_NODE) &&
1061
0
  (target->last->name == cur->name)) ||
1062
        /* non-escaped text nodes and CDATA-section nodes */
1063
0
  (((target->last->type == XML_CDATA_SECTION_NODE) &&
1064
0
  (cur->name == xmlStringTextNoenc)))))
1065
0
    {
1066
  /*
1067
   * we are appending to an existing text node
1068
   */
1069
0
  copy = xsltAddTextString(ctxt, target->last, cur->content,
1070
0
      xmlStrlen(cur->content));
1071
0
  goto exit;
1072
0
    } else if ((interned) && (target != NULL) &&
1073
0
  (target->doc != NULL) &&
1074
0
  (target->doc->dict == ctxt->dict))
1075
0
    {
1076
  /*
1077
  * TODO: DO we want to use this also for "text" output?
1078
  */
1079
0
        copy = xmlNewTextLen(NULL, 0);
1080
0
  if (copy == NULL)
1081
0
      goto exit;
1082
0
  if (cur->name == xmlStringTextNoenc)
1083
0
      copy->name = xmlStringTextNoenc;
1084
1085
  /*
1086
   * Must confirm that content is in dict (bug 302821)
1087
   * TODO: This check should be not needed for text coming
1088
   * from the stylesheets
1089
   */
1090
0
  if (xmlDictOwns(ctxt->dict, cur->content))
1091
0
      copy->content = cur->content;
1092
0
  else {
1093
0
      if ((copy->content = xmlStrdup(cur->content)) == NULL) {
1094
0
                xmlFreeNode(copy);
1095
0
    return NULL;
1096
0
            }
1097
0
  }
1098
1099
0
  ctxt->lasttext = NULL;
1100
0
    } else {
1101
        /*
1102
   * normal processing. keep counters to extend the text node
1103
   * in xsltAddTextString if needed.
1104
   */
1105
0
        unsigned int len;
1106
1107
0
  len = xmlStrlen(cur->content);
1108
0
  copy = xmlNewTextLen(cur->content, len);
1109
0
  if (copy == NULL)
1110
0
      goto exit;
1111
0
  if (cur->name == xmlStringTextNoenc)
1112
0
      copy->name = xmlStringTextNoenc;
1113
0
  ctxt->lasttext = copy->content;
1114
0
  ctxt->lasttsize = len;
1115
0
  ctxt->lasttuse = len;
1116
0
    }
1117
0
    if (copy != NULL) {
1118
0
  if (target != NULL) {
1119
0
      copy->doc = target->doc;
1120
      /*
1121
      * MAYBE TODO: Maybe we should reset the ctxt->lasttext here
1122
      *  to ensure that the optimized text-merging mechanism
1123
      *  won't interfere with normal node-merging in any case.
1124
      */
1125
0
      copy = xsltAddChild(target, copy);
1126
0
  }
1127
0
    } else {
1128
0
  xsltTransformError(ctxt, NULL, target,
1129
0
       "xsltCopyText: text copy failed\n");
1130
0
    }
1131
1132
0
exit:
1133
0
    if ((copy == NULL) || (copy->content == NULL)) {
1134
0
  xsltTransformError(ctxt, NULL, target,
1135
0
      "Internal error in xsltCopyText(): "
1136
0
      "Failed to copy the string.\n");
1137
0
  ctxt->state = XSLT_STATE_STOPPED;
1138
0
    }
1139
0
    return(copy);
1140
0
}
1141
1142
/**
1143
 * xsltShallowCopyAttr:
1144
 * @ctxt:  a XSLT process context
1145
 * @invocNode: responsible node in the stylesheet; used for error reports
1146
 * @target:  the element where the attribute will be grafted
1147
 * @attr: the attribute to be copied
1148
 *
1149
 * Do a copy of an attribute.
1150
 * Called by:
1151
 *  - xsltCopyTree()
1152
 *  - xsltCopyOf()
1153
 *  - xsltCopy()
1154
 *
1155
 * Returns: a new xmlAttrPtr, or NULL in case of error.
1156
 */
1157
static xmlAttrPtr
1158
xsltShallowCopyAttr(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
1159
       xmlNodePtr target, xmlAttrPtr attr)
1160
0
{
1161
0
    xmlAttrPtr copy;
1162
0
    xmlChar *value;
1163
1164
0
    if (attr == NULL)
1165
0
  return(NULL);
1166
1167
0
    if (target->type != XML_ELEMENT_NODE) {
1168
0
  xsltTransformError(ctxt, NULL, invocNode,
1169
0
      "Cannot add an attribute node to a non-element node.\n");
1170
0
  return(NULL);
1171
0
    }
1172
1173
0
    if (target->children != NULL) {
1174
0
  xsltTransformError(ctxt, NULL, invocNode,
1175
0
      "Attribute nodes must be added before "
1176
0
      "any child nodes to an element.\n");
1177
0
  return(NULL);
1178
0
    }
1179
1180
0
    value = xmlNodeListGetString(attr->doc, attr->children, 1);
1181
0
    if (attr->ns != NULL) {
1182
0
  xmlNsPtr ns;
1183
1184
0
  ns = xsltGetSpecialNamespace(ctxt, invocNode,
1185
0
      attr->ns->href, attr->ns->prefix, target);
1186
0
  if (ns == NULL) {
1187
0
      xsltTransformError(ctxt, NULL, invocNode,
1188
0
    "Namespace fixup error: Failed to acquire an in-scope "
1189
0
    "namespace binding of the copied attribute '{%s}%s'.\n",
1190
0
    attr->ns->href, attr->name);
1191
      /*
1192
      * TODO: Should we just stop here?
1193
      */
1194
0
  }
1195
  /*
1196
  * Note that xmlSetNsProp() will take care of duplicates
1197
  * and assigns the new namespace even to a duplicate.
1198
  */
1199
0
  copy = xmlSetNsProp(target, ns, attr->name, value);
1200
0
    } else {
1201
0
  copy = xmlSetNsProp(target, NULL, attr->name, value);
1202
0
    }
1203
0
    if (value != NULL)
1204
0
  xmlFree(value);
1205
1206
0
    if (copy == NULL)
1207
0
  return(NULL);
1208
1209
#if 0
1210
    /*
1211
    * NOTE: This was optimized according to bug #342695.
1212
    * TODO: Can this further be optimized, if source and target
1213
    *  share the same dict and attr->children is just 1 text node
1214
    *  which is in the dict? How probable is such a case?
1215
    */
1216
    /*
1217
    * TODO: Do we need to create an empty text node if the value
1218
    *  is the empty string?
1219
    */
1220
    value = xmlNodeListGetString(attr->doc, attr->children, 1);
1221
    if (value != NULL) {
1222
  txtNode = xmlNewDocText(target->doc, NULL);
1223
  if (txtNode == NULL)
1224
      return(NULL);
1225
  if ((target->doc != NULL) &&
1226
      (target->doc->dict != NULL))
1227
  {
1228
      txtNode->content =
1229
    (xmlChar *) xmlDictLookup(target->doc->dict,
1230
        BAD_CAST value, -1);
1231
      xmlFree(value);
1232
  } else
1233
      txtNode->content = value;
1234
  copy->children = txtNode;
1235
    }
1236
#endif
1237
1238
0
    return(copy);
1239
0
}
1240
1241
/**
1242
 * xsltCopyAttrListNoOverwrite:
1243
 * @ctxt:  a XSLT process context
1244
 * @invocNode: responsible node in the stylesheet; used for error reports
1245
 * @target:  the element where the new attributes will be grafted
1246
 * @attr:  the first attribute in the list to be copied
1247
 *
1248
 * Copies a list of attribute nodes, starting with @attr, over to the
1249
 * @target element node.
1250
 *
1251
 * Called by:
1252
 *  - xsltCopyTree()
1253
 *
1254
 * Returns 0 on success and -1 on errors and internal errors.
1255
 */
1256
static int
1257
xsltCopyAttrListNoOverwrite(xsltTransformContextPtr ctxt,
1258
          xmlNodePtr invocNode,
1259
          xmlNodePtr target, xmlAttrPtr attr)
1260
0
{
1261
0
    xmlAttrPtr copy;
1262
0
    xmlNsPtr origNs = NULL, copyNs = NULL;
1263
0
    xmlChar *value;
1264
1265
    /*
1266
    * Don't use xmlCopyProp() here, since it will try to
1267
    * reconciliate namespaces.
1268
    */
1269
0
    while (attr != NULL) {
1270
  /*
1271
  * Find a namespace node in the tree of @target.
1272
  * Avoid searching for the same ns.
1273
  */
1274
0
  if (attr->ns != origNs) {
1275
0
      origNs = attr->ns;
1276
0
      if (attr->ns != NULL) {
1277
0
    copyNs = xsltGetSpecialNamespace(ctxt, invocNode,
1278
0
        attr->ns->href, attr->ns->prefix, target);
1279
0
    if (copyNs == NULL)
1280
0
        return(-1);
1281
0
      } else
1282
0
    copyNs = NULL;
1283
0
  }
1284
  /*
1285
   * If attribute has a value, we need to copy it (watching out
1286
   * for possible entities)
1287
   */
1288
0
  if ((attr->children) && (attr->children->type == XML_TEXT_NODE) &&
1289
0
            (attr->children->next == NULL)) {
1290
0
            copy = xmlNewNsProp(target, copyNs, attr->name,
1291
0
                                attr->children->content);
1292
0
        } else if (attr->children != NULL) {
1293
0
      value = xmlNodeListGetString(attr->doc, attr->children, 1);
1294
0
            copy = xmlNewNsProp(target, copyNs, attr->name, BAD_CAST value);
1295
0
      xmlFree(value);
1296
0
        } else {
1297
0
            copy = xmlNewNsProp(target, copyNs, attr->name, NULL);
1298
0
        }
1299
1300
0
  if (copy == NULL)
1301
0
      return(-1);
1302
1303
0
  attr = attr->next;
1304
0
    }
1305
0
    return(0);
1306
0
}
1307
1308
/**
1309
 * xsltShallowCopyElem:
1310
 * @ctxt:  the XSLT process context
1311
 * @node:  the element node in the source tree
1312
 *         or the Literal Result Element
1313
 * @insert:  the parent in the result tree
1314
 * @isLRE: if @node is a Literal Result Element
1315
 *
1316
 * Make a copy of the element node @node
1317
 * and insert it as last child of @insert.
1318
 *
1319
 * URGENT TODO: The problem with this one (for the non-refactored code)
1320
 * is that it is used for both, Literal Result Elements *and*
1321
 * copying input nodes.
1322
 *
1323
 * BIG NOTE: This is only called for XML_ELEMENT_NODEs.
1324
 *
1325
 * Called from:
1326
 *   xsltApplySequenceConstructor()
1327
 *    (for Literal Result Elements - which is a problem)
1328
 *   xsltCopy() (for shallow-copying elements via xsl:copy)
1329
 *
1330
 * Returns a pointer to the new node, or NULL in case of error
1331
 */
1332
static xmlNodePtr
1333
xsltShallowCopyElem(xsltTransformContextPtr ctxt, xmlNodePtr node,
1334
        xmlNodePtr insert, int isLRE)
1335
0
{
1336
0
    xmlNodePtr copy;
1337
1338
0
    if ((node->type == XML_DTD_NODE) || (insert == NULL))
1339
0
  return(NULL);
1340
0
    if ((node->type == XML_TEXT_NODE) ||
1341
0
  (node->type == XML_CDATA_SECTION_NODE))
1342
0
  return(xsltCopyText(ctxt, insert, node, 0));
1343
1344
0
    copy = xmlDocCopyNode(node, insert->doc, 0);
1345
0
    if (copy != NULL) {
1346
0
  copy->doc = ctxt->output;
1347
0
  copy = xsltAddChild(insert, copy);
1348
0
        if (copy == NULL) {
1349
0
             xsltTransformError(ctxt, NULL, node,
1350
0
                "xsltShallowCopyElem: copy failed\n");
1351
0
             return (copy);
1352
0
        }
1353
1354
0
  if (node->type == XML_ELEMENT_NODE) {
1355
      /*
1356
       * Add namespaces as they are needed
1357
       */
1358
0
      if (node->nsDef != NULL) {
1359
    /*
1360
    * TODO: Remove the LRE case in the refactored code
1361
    * gets enabled.
1362
    */
1363
0
    if (isLRE)
1364
0
        xsltCopyNamespaceList(ctxt, copy, node->nsDef);
1365
0
    else
1366
0
        xsltCopyNamespaceListInternal(copy, node->nsDef);
1367
0
      }
1368
1369
      /*
1370
      * URGENT TODO: The problem with this is that it does not
1371
      *  copy over all namespace nodes in scope.
1372
      *  The damn thing about this is, that we would need to
1373
      *  use the xmlGetNsList(), for every single node; this is
1374
      *  also done in xsltCopyTree(), but only for the top node.
1375
      */
1376
0
      if (node->ns != NULL) {
1377
0
    if (isLRE) {
1378
        /*
1379
        * REVISIT TODO: Since the non-refactored code still does
1380
        *  ns-aliasing, we need to call xsltGetNamespace() here.
1381
        *  Remove this when ready.
1382
        */
1383
0
        copy->ns = xsltGetNamespace(ctxt, node, node->ns, copy);
1384
0
    } else {
1385
0
        copy->ns = xsltGetSpecialNamespace(ctxt,
1386
0
      node, node->ns->href, node->ns->prefix, copy);
1387
1388
0
    }
1389
0
      } else if ((insert->type == XML_ELEMENT_NODE) &&
1390
0
           (insert->ns != NULL))
1391
0
      {
1392
    /*
1393
    * "Undeclare" the default namespace.
1394
    */
1395
0
    xsltGetSpecialNamespace(ctxt, node, NULL, NULL, copy);
1396
0
      }
1397
0
  }
1398
0
    } else {
1399
0
  xsltTransformError(ctxt, NULL, node,
1400
0
    "xsltShallowCopyElem: copy %s failed\n", node->name);
1401
0
    }
1402
0
    return(copy);
1403
0
}
1404
1405
/**
1406
 * xsltCopyTreeList:
1407
 * @ctxt:  a XSLT process context
1408
 * @invocNode: responsible node in the stylesheet; used for error reports
1409
 * @list:  the list of element nodes in the source tree.
1410
 * @insert:  the parent in the result tree.
1411
 * @isLRE:  is this a literal result element list
1412
 * @topElemVisited: indicates if a top-most element was already processed
1413
 *
1414
 * Make a copy of the full list of tree @list
1415
 * and insert it as last children of @insert
1416
 *
1417
 * NOTE: Not to be used for Literal Result Elements.
1418
 *
1419
 * Used by:
1420
 *  - xsltCopyOf()
1421
 *
1422
 * Returns a pointer to the new list, or NULL in case of error
1423
 */
1424
static xmlNodePtr
1425
xsltCopyTreeList(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
1426
     xmlNodePtr list,
1427
     xmlNodePtr insert, int isLRE, int topElemVisited)
1428
0
{
1429
0
    xmlNodePtr copy, ret = NULL;
1430
1431
0
    while (list != NULL) {
1432
0
  copy = xsltCopyTree(ctxt, invocNode,
1433
0
      list, insert, isLRE, topElemVisited);
1434
0
  if (copy != NULL) {
1435
0
      if (ret == NULL) {
1436
0
    ret = copy;
1437
0
      }
1438
0
  }
1439
0
  list = list->next;
1440
0
    }
1441
0
    return(ret);
1442
0
}
1443
1444
/**
1445
 * xsltCopyNamespaceListInternal:
1446
 * @node:  the target node
1447
 * @cur:  the first namespace
1448
 *
1449
 * Do a copy of a namespace list. If @node is non-NULL the
1450
 * new namespaces are added automatically.
1451
 * Called by:
1452
 *   xsltCopyTree()
1453
 *
1454
 * QUESTION: What is the exact difference between this function
1455
 *  and xsltCopyNamespaceList() in "namespaces.c"?
1456
 * ANSWER: xsltCopyNamespaceList() tries to apply ns-aliases.
1457
 *
1458
 * Returns: a new xmlNsPtr, or NULL in case of error.
1459
 */
1460
static xmlNsPtr
1461
0
xsltCopyNamespaceListInternal(xmlNodePtr elem, xmlNsPtr ns) {
1462
0
    xmlNsPtr ret = NULL;
1463
0
    xmlNsPtr p = NULL, q, luNs;
1464
1465
0
    if (ns == NULL)
1466
0
  return(NULL);
1467
    /*
1468
     * One can add namespaces only on element nodes
1469
     */
1470
0
    if ((elem != NULL) && (elem->type != XML_ELEMENT_NODE))
1471
0
  elem = NULL;
1472
1473
0
    do {
1474
0
  if (ns->type != XML_NAMESPACE_DECL)
1475
0
      break;
1476
  /*
1477
   * Avoid duplicating namespace declarations on the tree.
1478
   */
1479
0
  if (elem != NULL) {
1480
0
      if ((elem->ns != NULL) &&
1481
0
    xmlStrEqual(elem->ns->prefix, ns->prefix) &&
1482
0
    xmlStrEqual(elem->ns->href, ns->href))
1483
0
      {
1484
0
    ns = ns->next;
1485
0
    continue;
1486
0
      }
1487
0
      luNs = xmlSearchNs(elem->doc, elem, ns->prefix);
1488
0
      if ((luNs != NULL) && (xmlStrEqual(luNs->href, ns->href)))
1489
0
      {
1490
0
    ns = ns->next;
1491
0
    continue;
1492
0
      }
1493
0
  }
1494
0
  q = xmlNewNs(elem, ns->href, ns->prefix);
1495
0
  if (p == NULL) {
1496
0
      ret = p = q;
1497
0
  } else if (q != NULL) {
1498
0
      p->next = q;
1499
0
      p = q;
1500
0
  }
1501
0
  ns = ns->next;
1502
0
    } while (ns != NULL);
1503
0
    return(ret);
1504
0
}
1505
1506
/**
1507
 * xsltShallowCopyNsNode:
1508
 * @ctxt:  the XSLT transformation context
1509
 * @invocNode: responsible node in the stylesheet; used for error reports
1510
 * @insert:  the target element node in the result tree
1511
 * @ns: the namespace node
1512
 *
1513
 * This is used for copying ns-nodes with xsl:copy-of and xsl:copy.
1514
 *
1515
 * Returns a new/existing ns-node, or NULL.
1516
 */
1517
static xmlNsPtr
1518
xsltShallowCopyNsNode(xsltTransformContextPtr ctxt,
1519
          xmlNodePtr invocNode,
1520
          xmlNodePtr insert,
1521
          xmlNsPtr ns)
1522
0
{
1523
    /*
1524
     * TODO: Contrary to header comments, this is declared as int.
1525
     * be modified to return a node pointer, or NULL if any error
1526
     */
1527
0
    xmlNsPtr tmpns;
1528
1529
0
    if ((insert == NULL) || (insert->type != XML_ELEMENT_NODE))
1530
0
  return(NULL);
1531
1532
0
    if (insert->children != NULL) {
1533
0
  xsltTransformError(ctxt, NULL, invocNode,
1534
0
      "Namespace nodes must be added before "
1535
0
      "any child nodes are added to an element.\n");
1536
0
  return(NULL);
1537
0
    }
1538
    /*
1539
     * BIG NOTE: Xalan-J simply overwrites any ns-decls with
1540
     * an equal prefix. We definitively won't do that.
1541
     *
1542
     * MSXML 4.0 and the .NET ignores ns-decls for which an
1543
     * equal prefix is already in use.
1544
     *
1545
     * Saxon raises an error like:
1546
     * "net.sf.saxon.xpath.DynamicError: Cannot create two namespace
1547
     * nodes with the same name".
1548
     *
1549
     * NOTE: We'll currently follow MSXML here.
1550
     * REVISIT TODO: Check if it's better to follow Saxon here.
1551
     */
1552
0
    if (ns->prefix == NULL) {
1553
  /*
1554
  * If we are adding ns-nodes to an element using e.g.
1555
  * <xsl:copy-of select="/foo/namespace::*">, then we need
1556
  * to ensure that we don't incorrectly declare a default
1557
  * namespace on an element in no namespace, which otherwise
1558
  * would move the element incorrectly into a namespace, if
1559
  * the node tree is serialized.
1560
  */
1561
0
  if (insert->ns == NULL)
1562
0
      goto occupied;
1563
0
    } else if ((ns->prefix[0] == 'x') &&
1564
0
  xmlStrEqual(ns->prefix, BAD_CAST "xml"))
1565
0
    {
1566
  /*
1567
  * The XML namespace is built in.
1568
  */
1569
0
  return(NULL);
1570
0
    }
1571
1572
0
    if (insert->nsDef != NULL) {
1573
0
  tmpns = insert->nsDef;
1574
0
  do {
1575
0
      if ((tmpns->prefix == NULL) == (ns->prefix == NULL)) {
1576
0
    if ((tmpns->prefix == ns->prefix) ||
1577
0
        xmlStrEqual(tmpns->prefix, ns->prefix))
1578
0
    {
1579
        /*
1580
        * Same prefix.
1581
        */
1582
0
        if (xmlStrEqual(tmpns->href, ns->href))
1583
0
      return(NULL);
1584
0
        goto occupied;
1585
0
    }
1586
0
      }
1587
0
      tmpns = tmpns->next;
1588
0
  } while (tmpns != NULL);
1589
0
    }
1590
0
    tmpns = xmlSearchNs(insert->doc, insert, ns->prefix);
1591
0
    if ((tmpns != NULL) && xmlStrEqual(tmpns->href, ns->href))
1592
0
  return(NULL);
1593
    /*
1594
    * Declare a new namespace.
1595
    * TODO: The problem (wrt efficiency) with this xmlNewNs() is
1596
    * that it will again search the already declared namespaces
1597
    * for a duplicate :-/
1598
    */
1599
0
    return(xmlNewNs(insert, ns->href, ns->prefix));
1600
1601
0
occupied:
1602
    /*
1603
    * TODO: We could as well raise an error here (like Saxon does),
1604
    * or at least generate a warning.
1605
    */
1606
0
    return(NULL);
1607
0
}
1608
1609
/**
1610
 * xsltCopyTree:
1611
 * @ctxt:  the XSLT transformation context
1612
 * @invocNode: responsible node in the stylesheet; used for error reports
1613
 * @node:  the element node in the source tree
1614
 * @insert:  the parent in the result tree
1615
 * @isLRE:  indicates if @node is a Literal Result Element
1616
 * @topElemVisited: indicates if a top-most element was already processed
1617
 *
1618
 * Make a copy of the full tree under the element node @node
1619
 * and insert it as last child of @insert
1620
 *
1621
 * NOTE: Not to be used for Literal Result Elements.
1622
 *
1623
 * Used by:
1624
 *  - xsltCopyOf()
1625
 *
1626
 * Returns a pointer to the new tree, or NULL in case of error
1627
 */
1628
static xmlNodePtr
1629
xsltCopyTree(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
1630
       xmlNodePtr node, xmlNodePtr insert, int isLRE,
1631
       int topElemVisited)
1632
0
{
1633
0
    xmlNodePtr copy;
1634
1635
0
    if (node == NULL)
1636
0
  return(NULL);
1637
0
    switch (node->type) {
1638
0
        case XML_ELEMENT_NODE:
1639
0
        case XML_ENTITY_REF_NODE:
1640
0
        case XML_ENTITY_NODE:
1641
0
        case XML_PI_NODE:
1642
0
        case XML_COMMENT_NODE:
1643
0
        case XML_DOCUMENT_NODE:
1644
0
        case XML_HTML_DOCUMENT_NODE:
1645
#ifdef LIBXML_DOCB_ENABLED
1646
        case XML_DOCB_DOCUMENT_NODE:
1647
#endif
1648
0
      break;
1649
0
        case XML_TEXT_NODE: {
1650
0
      int noenc = (node->name == xmlStringTextNoenc);
1651
0
      return(xsltCopyTextString(ctxt, insert, node->content, noenc));
1652
0
      }
1653
0
        case XML_CDATA_SECTION_NODE:
1654
0
      return(xsltCopyTextString(ctxt, insert, node->content, 0));
1655
0
        case XML_ATTRIBUTE_NODE:
1656
0
      return((xmlNodePtr)
1657
0
    xsltShallowCopyAttr(ctxt, invocNode, insert, (xmlAttrPtr) node));
1658
0
        case XML_NAMESPACE_DECL:
1659
0
      return((xmlNodePtr) xsltShallowCopyNsNode(ctxt, invocNode,
1660
0
    insert, (xmlNsPtr) node));
1661
1662
0
        case XML_DOCUMENT_TYPE_NODE:
1663
0
        case XML_DOCUMENT_FRAG_NODE:
1664
0
        case XML_NOTATION_NODE:
1665
0
        case XML_DTD_NODE:
1666
0
        case XML_ELEMENT_DECL:
1667
0
        case XML_ATTRIBUTE_DECL:
1668
0
        case XML_ENTITY_DECL:
1669
0
        case XML_XINCLUDE_START:
1670
0
        case XML_XINCLUDE_END:
1671
0
            return(NULL);
1672
0
    }
1673
0
    if (XSLT_IS_RES_TREE_FRAG(node)) {
1674
0
  if (node->children != NULL)
1675
0
      copy = xsltCopyTreeList(ctxt, invocNode,
1676
0
    node->children, insert, 0, 0);
1677
0
  else
1678
0
      copy = NULL;
1679
0
  return(copy);
1680
0
    }
1681
0
    copy = xmlDocCopyNode(node, insert->doc, 0);
1682
0
    if (copy != NULL) {
1683
0
  copy->doc = ctxt->output;
1684
0
  copy = xsltAddChild(insert, copy);
1685
0
        if (copy == NULL) {
1686
0
            xsltTransformError(ctxt, NULL, invocNode,
1687
0
            "xsltCopyTree: Copying of '%s' failed.\n", node->name);
1688
0
            return (copy);
1689
0
        }
1690
  /*
1691
   * The node may have been coalesced into another text node.
1692
   */
1693
0
  if (insert->last != copy)
1694
0
      return(insert->last);
1695
0
  copy->next = NULL;
1696
1697
0
  if (node->type == XML_ELEMENT_NODE) {
1698
      /*
1699
      * Copy in-scope namespace nodes.
1700
      *
1701
      * REVISIT: Since we try to reuse existing in-scope ns-decls by
1702
      *  using xmlSearchNsByHref(), this will eventually change
1703
      *  the prefix of an original ns-binding; thus it might
1704
      *  break QNames in element/attribute content.
1705
      * OPTIMIZE TODO: If we had a xmlNsPtr * on the transformation
1706
      *  context, plus a ns-lookup function, which writes directly
1707
      *  to a given list, then we wouldn't need to create/free the
1708
      *  nsList every time.
1709
      */
1710
0
      if ((topElemVisited == 0) &&
1711
0
    (node->parent != NULL) &&
1712
0
    (node->parent->type != XML_DOCUMENT_NODE) &&
1713
0
    (node->parent->type != XML_HTML_DOCUMENT_NODE))
1714
0
      {
1715
0
    xmlNsPtr *nsList, *curns, ns;
1716
1717
    /*
1718
    * If this is a top-most element in a tree to be
1719
    * copied, then we need to ensure that all in-scope
1720
    * namespaces are copied over. For nodes deeper in the
1721
    * tree, it is sufficient to reconcile only the ns-decls
1722
    * (node->nsDef entries).
1723
    */
1724
1725
0
    nsList = xmlGetNsList(node->doc, node);
1726
0
    if (nsList != NULL) {
1727
0
        curns = nsList;
1728
0
        do {
1729
      /*
1730
      * Search by prefix first in order to break as less
1731
      * QNames in element/attribute content as possible.
1732
      */
1733
0
      ns = xmlSearchNs(insert->doc, insert,
1734
0
          (*curns)->prefix);
1735
1736
0
      if ((ns == NULL) ||
1737
0
          (! xmlStrEqual(ns->href, (*curns)->href)))
1738
0
      {
1739
0
          ns = NULL;
1740
          /*
1741
          * Search by namespace name.
1742
          * REVISIT TODO: Currently disabled.
1743
          */
1744
#if 0
1745
          ns = xmlSearchNsByHref(insert->doc,
1746
        insert, (*curns)->href);
1747
#endif
1748
0
      }
1749
0
      if (ns == NULL) {
1750
          /*
1751
          * Declare a new namespace on the copied element.
1752
          */
1753
0
          ns = xmlNewNs(copy, (*curns)->href,
1754
0
        (*curns)->prefix);
1755
          /* TODO: Handle errors */
1756
0
      }
1757
0
      if (node->ns == *curns) {
1758
          /*
1759
          * If this was the original's namespace then set
1760
          * the generated counterpart on the copy.
1761
          */
1762
0
          copy->ns = ns;
1763
0
      }
1764
0
      curns++;
1765
0
        } while (*curns != NULL);
1766
0
        xmlFree(nsList);
1767
0
    }
1768
0
      } else if (node->nsDef != NULL) {
1769
    /*
1770
    * Copy over all namespace declaration attributes.
1771
    */
1772
0
    if (node->nsDef != NULL) {
1773
0
        if (isLRE)
1774
0
      xsltCopyNamespaceList(ctxt, copy, node->nsDef);
1775
0
        else
1776
0
      xsltCopyNamespaceListInternal(copy, node->nsDef);
1777
0
    }
1778
0
      }
1779
      /*
1780
      * Set the namespace.
1781
      */
1782
0
      if (node->ns != NULL) {
1783
0
    if (copy->ns == NULL) {
1784
        /*
1785
        * This will map copy->ns to one of the newly created
1786
        * in-scope ns-decls, OR create a new ns-decl on @copy.
1787
        */
1788
0
        copy->ns = xsltGetSpecialNamespace(ctxt, invocNode,
1789
0
      node->ns->href, node->ns->prefix, copy);
1790
0
    }
1791
0
      } else if ((insert->type == XML_ELEMENT_NODE) &&
1792
0
    (insert->ns != NULL))
1793
0
      {
1794
    /*
1795
    * "Undeclare" the default namespace on @copy with xmlns="".
1796
    */
1797
0
    xsltGetSpecialNamespace(ctxt, invocNode, NULL, NULL, copy);
1798
0
      }
1799
      /*
1800
      * Copy attribute nodes.
1801
      */
1802
0
      if (node->properties != NULL) {
1803
0
    xsltCopyAttrListNoOverwrite(ctxt, invocNode,
1804
0
        copy, node->properties);
1805
0
      }
1806
0
      if (topElemVisited == 0)
1807
0
    topElemVisited = 1;
1808
0
  }
1809
  /*
1810
  * Copy the subtree.
1811
  */
1812
0
  if (node->children != NULL) {
1813
0
      xsltCopyTreeList(ctxt, invocNode,
1814
0
    node->children, copy, isLRE, topElemVisited);
1815
0
  }
1816
0
    } else {
1817
0
  xsltTransformError(ctxt, NULL, invocNode,
1818
0
      "xsltCopyTree: Copying of '%s' failed.\n", node->name);
1819
0
    }
1820
0
    return(copy);
1821
0
}
1822
1823
/************************************************************************
1824
 *                  *
1825
 *    Error/fallback processing       *
1826
 *                  *
1827
 ************************************************************************/
1828
1829
/**
1830
 * xsltApplyFallbacks:
1831
 * @ctxt:  a XSLT process context
1832
 * @node:  the node in the source tree.
1833
 * @inst:  the node generating the error
1834
 *
1835
 * Process possible xsl:fallback nodes present under @inst
1836
 *
1837
 * Returns the number of xsl:fallback element found and processed
1838
 */
1839
static int
1840
xsltApplyFallbacks(xsltTransformContextPtr ctxt, xmlNodePtr node,
1841
0
             xmlNodePtr inst) {
1842
1843
0
    xmlNodePtr child;
1844
0
    int ret = 0;
1845
1846
0
    if ((ctxt == NULL) || (node == NULL) || (inst == NULL) ||
1847
0
  (inst->children == NULL))
1848
0
  return(0);
1849
1850
0
    child = inst->children;
1851
0
    while (child != NULL) {
1852
0
        if ((IS_XSLT_ELEM(child)) &&
1853
0
            (xmlStrEqual(child->name, BAD_CAST "fallback"))) {
1854
#ifdef WITH_XSLT_DEBUG_PARSING
1855
      xsltGenericDebug(xsltGenericDebugContext,
1856
           "applying xsl:fallback\n");
1857
#endif
1858
0
      ret++;
1859
0
      xsltApplySequenceConstructor(ctxt, node, child->children,
1860
0
    NULL);
1861
0
  }
1862
0
  child = child->next;
1863
0
    }
1864
0
    return(ret);
1865
0
}
1866
1867
/************************************************************************
1868
 *                  *
1869
 *      Default processing        *
1870
 *                  *
1871
 ************************************************************************/
1872
1873
/**
1874
 * xsltDefaultProcessOneNode:
1875
 * @ctxt:  a XSLT process context
1876
 * @node:  the node in the source tree.
1877
 * @params: extra parameters passed to the template if any
1878
 *
1879
 * Process the source node with the default built-in template rule:
1880
 * <xsl:template match="*|/">
1881
 *   <xsl:apply-templates/>
1882
 * </xsl:template>
1883
 *
1884
 * and
1885
 *
1886
 * <xsl:template match="text()|@*">
1887
 *   <xsl:value-of select="."/>
1888
 * </xsl:template>
1889
 *
1890
 * Note also that namespace declarations are copied directly:
1891
 *
1892
 * the built-in template rule is the only template rule that is applied
1893
 * for namespace nodes.
1894
 */
1895
static void
1896
xsltDefaultProcessOneNode(xsltTransformContextPtr ctxt, xmlNodePtr node,
1897
0
        xsltStackElemPtr params) {
1898
0
    xmlNodePtr copy;
1899
0
    xmlNodePtr cur;
1900
0
    int nbchild = 0, oldSize;
1901
0
    int childno = 0, oldPos;
1902
0
    xsltTemplatePtr template;
1903
1904
0
    CHECK_STOPPED;
1905
    /*
1906
     * Handling of leaves
1907
     */
1908
0
    switch (node->type) {
1909
0
  case XML_DOCUMENT_NODE:
1910
0
  case XML_HTML_DOCUMENT_NODE:
1911
0
  case XML_ELEMENT_NODE:
1912
0
      break;
1913
0
  case XML_CDATA_SECTION_NODE:
1914
#ifdef WITH_XSLT_DEBUG_PROCESS
1915
      XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
1916
       "xsltDefaultProcessOneNode: copy CDATA %s\n",
1917
    node->content));
1918
#endif
1919
0
      copy = xsltCopyText(ctxt, ctxt->insert, node, 0);
1920
0
      if (copy == NULL) {
1921
0
    xsltTransformError(ctxt, NULL, node,
1922
0
     "xsltDefaultProcessOneNode: cdata copy failed\n");
1923
0
      }
1924
0
      return;
1925
0
  case XML_TEXT_NODE:
1926
#ifdef WITH_XSLT_DEBUG_PROCESS
1927
      if (node->content == NULL) {
1928
    XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
1929
     "xsltDefaultProcessOneNode: copy empty text\n"));
1930
    return;
1931
      } else {
1932
    XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
1933
     "xsltDefaultProcessOneNode: copy text %s\n",
1934
      node->content));
1935
            }
1936
#endif
1937
0
      copy = xsltCopyText(ctxt, ctxt->insert, node, 0);
1938
0
      if (copy == NULL) {
1939
0
    xsltTransformError(ctxt, NULL, node,
1940
0
     "xsltDefaultProcessOneNode: text copy failed\n");
1941
0
      }
1942
0
      return;
1943
0
  case XML_ATTRIBUTE_NODE:
1944
0
      cur = node->children;
1945
0
      while ((cur != NULL) && (cur->type != XML_TEXT_NODE))
1946
0
    cur = cur->next;
1947
0
      if (cur == NULL) {
1948
0
    xsltTransformError(ctxt, NULL, node,
1949
0
     "xsltDefaultProcessOneNode: no text for attribute\n");
1950
0
      } else {
1951
#ifdef WITH_XSLT_DEBUG_PROCESS
1952
    if (cur->content == NULL) {
1953
        XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
1954
         "xsltDefaultProcessOneNode: copy empty text\n"));
1955
    } else {
1956
        XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
1957
         "xsltDefaultProcessOneNode: copy text %s\n",
1958
      cur->content));
1959
                }
1960
#endif
1961
0
    copy = xsltCopyText(ctxt, ctxt->insert, cur, 0);
1962
0
    if (copy == NULL) {
1963
0
        xsltTransformError(ctxt, NULL, node,
1964
0
         "xsltDefaultProcessOneNode: text copy failed\n");
1965
0
    }
1966
0
      }
1967
0
      return;
1968
0
  default:
1969
0
      return;
1970
0
    }
1971
    /*
1972
     * Handling of Elements: first pass, counting
1973
     */
1974
0
    cur = node->children;
1975
0
    while (cur != NULL) {
1976
0
  if (IS_XSLT_REAL_NODE(cur))
1977
0
      nbchild++;
1978
0
  cur = cur->next;
1979
0
    }
1980
1981
    /*
1982
     * Handling of Elements: second pass, actual processing
1983
     *
1984
     * Note that params are passed to the next template. This matches
1985
     * XSLT 2.0 behavior but doesn't conform to XSLT 1.0.
1986
     */
1987
0
    oldSize = ctxt->xpathCtxt->contextSize;
1988
0
    oldPos = ctxt->xpathCtxt->proximityPosition;
1989
0
    cur = node->children;
1990
0
    while (cur != NULL) {
1991
0
  childno++;
1992
0
  switch (cur->type) {
1993
0
      case XML_DOCUMENT_NODE:
1994
0
      case XML_HTML_DOCUMENT_NODE:
1995
0
      case XML_ELEMENT_NODE:
1996
0
    ctxt->xpathCtxt->contextSize = nbchild;
1997
0
    ctxt->xpathCtxt->proximityPosition = childno;
1998
1999
0
                if (ctxt->depth >= ctxt->maxTemplateDepth) {
2000
0
                    xsltTransformError(ctxt, NULL, cur,
2001
0
                        "xsltDefaultProcessOneNode: Maximum template depth "
2002
0
                        "exceeded.\n"
2003
0
                        "You can adjust xsltMaxDepth (--maxdepth) in order to "
2004
0
                        "raise the maximum number of nested template calls and "
2005
0
                        "variables/params (currently set to %d).\n",
2006
0
                        ctxt->maxTemplateDepth);
2007
0
                    ctxt->state = XSLT_STATE_STOPPED;
2008
0
                    return;
2009
0
                }
2010
0
                ctxt->depth++;
2011
0
    xsltProcessOneNode(ctxt, cur, params);
2012
0
                ctxt->depth--;
2013
0
    break;
2014
0
      case XML_CDATA_SECTION_NODE:
2015
0
    template = xsltGetTemplate(ctxt, cur, NULL);
2016
0
    if (template) {
2017
#ifdef WITH_XSLT_DEBUG_PROCESS
2018
        XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2019
     "xsltDefaultProcessOneNode: applying template for CDATA %s\n",
2020
             cur->content));
2021
#endif
2022
        /*
2023
        * Instantiate the xsl:template.
2024
        */
2025
0
        xsltApplyXSLTTemplate(ctxt, cur, template->content,
2026
0
      template, params);
2027
0
    } else /* if (ctxt->mode == NULL) */ {
2028
#ifdef WITH_XSLT_DEBUG_PROCESS
2029
        XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2030
         "xsltDefaultProcessOneNode: copy CDATA %s\n",
2031
             cur->content));
2032
#endif
2033
0
        copy = xsltCopyText(ctxt, ctxt->insert, cur, 0);
2034
0
        if (copy == NULL) {
2035
0
      xsltTransformError(ctxt, NULL, cur,
2036
0
          "xsltDefaultProcessOneNode: cdata copy failed\n");
2037
0
        }
2038
0
    }
2039
0
    break;
2040
0
      case XML_TEXT_NODE:
2041
0
    template = xsltGetTemplate(ctxt, cur, NULL);
2042
0
    if (template) {
2043
#ifdef WITH_XSLT_DEBUG_PROCESS
2044
        XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2045
       "xsltDefaultProcessOneNode: applying template for text %s\n",
2046
             cur->content));
2047
#endif
2048
0
        ctxt->xpathCtxt->contextSize = nbchild;
2049
0
        ctxt->xpathCtxt->proximityPosition = childno;
2050
        /*
2051
        * Instantiate the xsl:template.
2052
        */
2053
0
        xsltApplyXSLTTemplate(ctxt, cur, template->content,
2054
0
      template, params);
2055
0
    } else /* if (ctxt->mode == NULL) */ {
2056
#ifdef WITH_XSLT_DEBUG_PROCESS
2057
        if (cur->content == NULL) {
2058
      XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2059
       "xsltDefaultProcessOneNode: copy empty text\n"));
2060
        } else {
2061
      XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2062
         "xsltDefaultProcessOneNode: copy text %s\n",
2063
           cur->content));
2064
                    }
2065
#endif
2066
0
        copy = xsltCopyText(ctxt, ctxt->insert, cur, 0);
2067
0
        if (copy == NULL) {
2068
0
      xsltTransformError(ctxt, NULL, cur,
2069
0
          "xsltDefaultProcessOneNode: text copy failed\n");
2070
0
        }
2071
0
    }
2072
0
    break;
2073
0
      case XML_PI_NODE:
2074
0
      case XML_COMMENT_NODE:
2075
0
    template = xsltGetTemplate(ctxt, cur, NULL);
2076
0
    if (template) {
2077
#ifdef WITH_XSLT_DEBUG_PROCESS
2078
        if (cur->type == XML_PI_NODE) {
2079
      XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2080
         "xsltDefaultProcessOneNode: template found for PI %s\n",
2081
                       cur->name));
2082
        } else if (cur->type == XML_COMMENT_NODE) {
2083
      XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2084
         "xsltDefaultProcessOneNode: template found for comment\n"));
2085
                    }
2086
#endif
2087
0
        ctxt->xpathCtxt->contextSize = nbchild;
2088
0
        ctxt->xpathCtxt->proximityPosition = childno;
2089
        /*
2090
        * Instantiate the xsl:template.
2091
        */
2092
0
        xsltApplyXSLTTemplate(ctxt, cur, template->content,
2093
0
      template, params);
2094
0
    }
2095
0
    break;
2096
0
      default:
2097
0
    break;
2098
0
  }
2099
0
  cur = cur->next;
2100
0
    }
2101
0
    ctxt->xpathCtxt->contextSize = oldSize;
2102
0
    ctxt->xpathCtxt->proximityPosition = oldPos;
2103
0
}
2104
2105
/**
2106
 * xsltProcessOneNode:
2107
 * @ctxt:  a XSLT process context
2108
 * @contextNode:  the "current node" in the source tree
2109
 * @withParams:  extra parameters (e.g. xsl:with-param) passed to the
2110
 *               template if any
2111
 *
2112
 * Process the source node.
2113
 */
2114
void
2115
xsltProcessOneNode(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
2116
             xsltStackElemPtr withParams)
2117
0
{
2118
0
    xsltTemplatePtr templ;
2119
0
    xmlNodePtr oldNode;
2120
2121
0
    templ = xsltGetTemplate(ctxt, contextNode, NULL);
2122
    /*
2123
     * If no template is found, apply the default rule.
2124
     */
2125
0
    if (templ == NULL) {
2126
#ifdef WITH_XSLT_DEBUG_PROCESS
2127
  if (contextNode->type == XML_DOCUMENT_NODE) {
2128
      XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2129
       "xsltProcessOneNode: no template found for /\n"));
2130
  } else if (contextNode->type == XML_CDATA_SECTION_NODE) {
2131
      XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2132
       "xsltProcessOneNode: no template found for CDATA\n"));
2133
  } else if (contextNode->type == XML_ATTRIBUTE_NODE) {
2134
      XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2135
       "xsltProcessOneNode: no template found for attribute %s\n",
2136
                       ((xmlAttrPtr) contextNode)->name));
2137
  } else  {
2138
      XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2139
       "xsltProcessOneNode: no template found for %s\n", contextNode->name));
2140
        }
2141
#endif
2142
0
  oldNode = ctxt->node;
2143
0
  ctxt->node = contextNode;
2144
0
  xsltDefaultProcessOneNode(ctxt, contextNode, withParams);
2145
0
  ctxt->node = oldNode;
2146
0
  return;
2147
0
    }
2148
2149
0
    if (contextNode->type == XML_ATTRIBUTE_NODE) {
2150
0
  xsltTemplatePtr oldCurTempRule = ctxt->currentTemplateRule;
2151
  /*
2152
  * Set the "current template rule".
2153
  */
2154
0
  ctxt->currentTemplateRule = templ;
2155
2156
#ifdef WITH_XSLT_DEBUG_PROCESS
2157
  XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2158
       "xsltProcessOneNode: applying template '%s' for attribute %s\n",
2159
                   templ->match, contextNode->name));
2160
#endif
2161
0
  xsltApplyXSLTTemplate(ctxt, contextNode, templ->content, templ, withParams);
2162
2163
0
  ctxt->currentTemplateRule = oldCurTempRule;
2164
0
    } else {
2165
0
  xsltTemplatePtr oldCurTempRule = ctxt->currentTemplateRule;
2166
  /*
2167
  * Set the "current template rule".
2168
  */
2169
0
  ctxt->currentTemplateRule = templ;
2170
2171
#ifdef WITH_XSLT_DEBUG_PROCESS
2172
  if (contextNode->type == XML_DOCUMENT_NODE) {
2173
      XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2174
       "xsltProcessOneNode: applying template '%s' for /\n",
2175
                       templ->match));
2176
  } else {
2177
      XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
2178
       "xsltProcessOneNode: applying template '%s' for %s\n",
2179
                       templ->match, contextNode->name));
2180
        }
2181
#endif
2182
0
  xsltApplyXSLTTemplate(ctxt, contextNode, templ->content, templ, withParams);
2183
2184
0
  ctxt->currentTemplateRule = oldCurTempRule;
2185
0
    }
2186
0
}
2187
2188
#ifdef WITH_DEBUGGER
2189
static xmlNodePtr
2190
xsltDebuggerStartSequenceConstructor(xsltTransformContextPtr ctxt,
2191
             xmlNodePtr contextNode,
2192
             xmlNodePtr list,
2193
             xsltTemplatePtr templ,
2194
             int *addCallResult)
2195
{
2196
    xmlNodePtr debugedNode = NULL;
2197
2198
    if (ctxt->debugStatus != XSLT_DEBUG_NONE) {
2199
        if (templ) {
2200
            *addCallResult = xslAddCall(templ, templ->elem);
2201
        } else {
2202
            *addCallResult = xslAddCall(NULL, list);
2203
        }
2204
        switch (ctxt->debugStatus) {
2205
            case XSLT_DEBUG_RUN_RESTART:
2206
            case XSLT_DEBUG_QUIT:
2207
                if (*addCallResult)
2208
                    xslDropCall();
2209
                return(NULL);
2210
        }
2211
        if (templ) {
2212
            xslHandleDebugger(templ->elem, contextNode, templ, ctxt);
2213
            debugedNode = templ->elem;
2214
        } else if (list) {
2215
            xslHandleDebugger(list, contextNode, templ, ctxt);
2216
            debugedNode = list;
2217
        } else if (ctxt->inst) {
2218
            xslHandleDebugger(ctxt->inst, contextNode, templ, ctxt);
2219
            debugedNode = ctxt->inst;
2220
        }
2221
    }
2222
    return(debugedNode);
2223
}
2224
#endif /* WITH_DEBUGGER */
2225
2226
/**
2227
 * xsltLocalVariablePush:
2228
 * @ctxt: the transformation context
2229
 * @variable: variable to be pushed to the variable stack
2230
 * @level: new value for variable's level
2231
 *
2232
 * Places the variable onto the local variable stack
2233
 *
2234
 * Returns: 0 for success, -1 for any error
2235
 * **NOTE:**
2236
 * This is an internal routine and should not be called by users!
2237
 */
2238
int
2239
xsltLocalVariablePush(xsltTransformContextPtr ctxt,
2240
          xsltStackElemPtr variable,
2241
          int level)
2242
0
{
2243
0
    if (ctxt->varsNr >= ctxt->varsMax) {
2244
0
        xsltStackElemPtr *tmp;
2245
0
        int newMax = ctxt->varsMax == 0 ? 10 : 2 * ctxt->varsMax;
2246
2247
0
  tmp = (xsltStackElemPtr *) xmlRealloc(ctxt->varsTab,
2248
0
                newMax * sizeof(*tmp));
2249
0
  if (tmp == NULL) {
2250
0
      xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
2251
0
      return (-1);
2252
0
  }
2253
0
        ctxt->varsTab = tmp;
2254
0
        ctxt->varsMax = newMax;
2255
0
    }
2256
0
    ctxt->varsTab[ctxt->varsNr++] = variable;
2257
0
    ctxt->vars = variable;
2258
0
    variable->level = level;
2259
0
    return(0);
2260
0
}
2261
2262
/**
2263
 * xsltReleaseLocalRVTs:
2264
 *
2265
 * Fragments which are results of extension instructions
2266
 * are preserved; all other fragments are freed/cached.
2267
 */
2268
static void
2269
xsltReleaseLocalRVTs(xsltTransformContextPtr ctxt, xmlDocPtr base)
2270
0
{
2271
0
    xmlDocPtr cur = ctxt->localRVT, tmp;
2272
2273
0
    if (cur == base)
2274
0
        return;
2275
0
    if (cur->prev != NULL)
2276
0
        xsltTransformError(ctxt, NULL, NULL, "localRVT not head of list\n");
2277
2278
    /* Reset localRVT early because some RVTs might be registered again. */
2279
0
    ctxt->localRVT = base;
2280
0
    if (base != NULL)
2281
0
        base->prev = NULL;
2282
2283
0
    do {
2284
0
        tmp = cur;
2285
0
        cur = (xmlDocPtr) cur->next;
2286
0
        if (tmp->compression == XSLT_RVT_LOCAL) {
2287
0
            xsltReleaseRVT(ctxt, tmp);
2288
0
        } else if (tmp->compression == XSLT_RVT_GLOBAL) {
2289
0
            xsltRegisterPersistRVT(ctxt, tmp);
2290
0
        } else if (tmp->compression == XSLT_RVT_FUNC_RESULT) {
2291
            /*
2292
             * This will either register the RVT again or move it to the
2293
             * context variable.
2294
             */
2295
0
            xsltRegisterLocalRVT(ctxt, tmp);
2296
0
            tmp->compression = XSLT_RVT_FUNC_RESULT;
2297
0
        } else {
2298
0
            xmlGenericError(xmlGenericErrorContext,
2299
0
                    "xsltReleaseLocalRVTs: Unexpected RVT flag %p\n",
2300
0
                    tmp->psvi);
2301
0
        }
2302
0
    } while (cur != base);
2303
0
}
2304
2305
/**
2306
 * xsltApplySequenceConstructor:
2307
 * @ctxt:  a XSLT process context
2308
 * @contextNode:  the "current node" in the source tree
2309
 * @list:  the nodes of a sequence constructor;
2310
 *         (plus leading xsl:param elements)
2311
 * @templ: the compiled xsl:template (optional)
2312
 *
2313
 * Processes a sequence constructor.
2314
 *
2315
 * NOTE: ctxt->currentTemplateRule was introduced to reflect the
2316
 * semantics of "current template rule". I.e. the field ctxt->templ
2317
 * is not intended to reflect this, thus always pushed onto the
2318
 * template stack.
2319
 */
2320
static void
2321
xsltApplySequenceConstructor(xsltTransformContextPtr ctxt,
2322
           xmlNodePtr contextNode, xmlNodePtr list,
2323
           xsltTemplatePtr templ)
2324
0
{
2325
0
    xmlNodePtr oldInsert, oldInst, oldCurInst, oldContextNode;
2326
0
    xmlNodePtr cur, insert, copy = NULL;
2327
0
    int level = 0, oldVarsNr;
2328
0
    xmlDocPtr oldLocalFragmentTop;
2329
2330
#ifdef XSLT_REFACTORED
2331
    xsltStylePreCompPtr info;
2332
#endif
2333
2334
#ifdef WITH_DEBUGGER
2335
    int addCallResult = 0;
2336
    xmlNodePtr debuggedNode = NULL;
2337
#endif
2338
2339
0
    if (ctxt == NULL)
2340
0
  return;
2341
2342
#ifdef WITH_DEBUGGER
2343
    if (ctxt->debugStatus != XSLT_DEBUG_NONE) {
2344
  debuggedNode =
2345
      xsltDebuggerStartSequenceConstructor(ctxt, contextNode,
2346
    list, templ, &addCallResult);
2347
  if (debuggedNode == NULL)
2348
      return;
2349
    }
2350
#endif
2351
2352
0
    if (list == NULL)
2353
0
        return;
2354
0
    CHECK_STOPPED;
2355
2356
    /*
2357
    * Check for infinite recursion: stop if the maximum of nested templates
2358
    * is excceeded. Adjust xsltMaxDepth if you need more.
2359
    */
2360
0
    if (ctxt->depth >= ctxt->maxTemplateDepth) {
2361
0
        xsltTransformError(ctxt, NULL, list,
2362
0
      "xsltApplySequenceConstructor: A potential infinite template "
2363
0
            "recursion was detected.\n"
2364
0
      "You can adjust xsltMaxDepth (--maxdepth) in order to "
2365
0
      "raise the maximum number of nested template calls and "
2366
0
      "variables/params (currently set to %d).\n",
2367
0
      ctxt->maxTemplateDepth);
2368
0
        xsltDebug(ctxt, contextNode, list, NULL);
2369
0
  ctxt->state = XSLT_STATE_STOPPED;
2370
0
        return;
2371
0
    }
2372
0
    ctxt->depth++;
2373
2374
0
    oldLocalFragmentTop = ctxt->localRVT;
2375
0
    oldInsert = insert = ctxt->insert;
2376
0
    oldInst = oldCurInst = ctxt->inst;
2377
0
    oldContextNode = ctxt->node;
2378
    /*
2379
    * Save current number of variables on the stack; new vars are popped when
2380
    * exiting.
2381
    */
2382
0
    oldVarsNr = ctxt->varsNr;
2383
    /*
2384
    * Process the sequence constructor.
2385
    */
2386
0
    cur = list;
2387
0
    while (cur != NULL) {
2388
0
        if (ctxt->opLimit != 0) {
2389
0
            if (ctxt->opCount >= ctxt->opLimit) {
2390
0
    xsltTransformError(ctxt, NULL, cur,
2391
0
        "xsltApplySequenceConstructor: "
2392
0
                    "Operation limit exceeded\n");
2393
0
          ctxt->state = XSLT_STATE_STOPPED;
2394
0
                goto error;
2395
0
            }
2396
0
            ctxt->opCount += 1;
2397
0
        }
2398
2399
0
        ctxt->inst = cur;
2400
2401
#ifdef WITH_DEBUGGER
2402
        switch (ctxt->debugStatus) {
2403
            case XSLT_DEBUG_RUN_RESTART:
2404
            case XSLT_DEBUG_QUIT:
2405
                break;
2406
2407
        }
2408
#endif
2409
        /*
2410
         * Test; we must have a valid insertion point.
2411
         */
2412
0
        if (insert == NULL) {
2413
2414
#ifdef WITH_XSLT_DEBUG_PROCESS
2415
            XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
2416
    "xsltApplySequenceConstructor: insert == NULL !\n"));
2417
#endif
2418
0
            goto error;
2419
0
        }
2420
2421
#ifdef WITH_DEBUGGER
2422
        if ((ctxt->debugStatus != XSLT_DEBUG_NONE) && (debuggedNode != cur))
2423
            xslHandleDebugger(cur, contextNode, templ, ctxt);
2424
#endif
2425
2426
#ifdef XSLT_REFACTORED
2427
  if (cur->type == XML_ELEMENT_NODE) {
2428
      info = (xsltStylePreCompPtr) cur->psvi;
2429
      /*
2430
      * We expect a compiled representation on:
2431
      * 1) XSLT instructions of this XSLT version (1.0)
2432
      *    (with a few exceptions)
2433
      * 2) Literal result elements
2434
      * 3) Extension instructions
2435
      * 4) XSLT instructions of future XSLT versions
2436
      *    (forwards-compatible mode).
2437
      */
2438
      if (info == NULL) {
2439
    /*
2440
    * Handle the rare cases where we don't expect a compiled
2441
    * representation on an XSLT element.
2442
    */
2443
    if (IS_XSLT_ELEM_FAST(cur) && IS_XSLT_NAME(cur, "message")) {
2444
        xsltMessage(ctxt, contextNode, cur);
2445
        goto skip_children;
2446
    }
2447
    /*
2448
    * Something really went wrong:
2449
    */
2450
    xsltTransformError(ctxt, NULL, cur,
2451
        "Internal error in xsltApplySequenceConstructor(): "
2452
        "The element '%s' in the stylesheet has no compiled "
2453
        "representation.\n",
2454
        cur->name);
2455
                goto skip_children;
2456
            }
2457
2458
      if (info->type == XSLT_FUNC_LITERAL_RESULT_ELEMENT) {
2459
    xsltStyleItemLRElementInfoPtr lrInfo =
2460
        (xsltStyleItemLRElementInfoPtr) info;
2461
    /*
2462
    * Literal result elements
2463
    * --------------------------------------------------------
2464
    */
2465
#ifdef WITH_XSLT_DEBUG_PROCESS
2466
    XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
2467
        xsltGenericDebug(xsltGenericDebugContext,
2468
        "xsltApplySequenceConstructor: copy literal result "
2469
        "element '%s'\n", cur->name));
2470
#endif
2471
    /*
2472
    * Copy the raw element-node.
2473
    * OLD: if ((copy = xsltShallowCopyElem(ctxt, cur, insert))
2474
    *     == NULL)
2475
    *   goto error;
2476
    */
2477
    copy = xmlDocCopyNode(cur, insert->doc, 0);
2478
    if (copy == NULL) {
2479
        xsltTransformError(ctxt, NULL, cur,
2480
      "Internal error in xsltApplySequenceConstructor(): "
2481
      "Failed to copy literal result element '%s'.\n",
2482
      cur->name);
2483
        goto error;
2484
    } else {
2485
        /*
2486
        * Add the element-node to the result tree.
2487
        */
2488
        copy->doc = ctxt->output;
2489
        copy = xsltAddChild(insert, copy);
2490
        /*
2491
        * Create effective namespaces declarations.
2492
        * OLD: xsltCopyNamespaceList(ctxt, copy, cur->nsDef);
2493
        */
2494
        if (lrInfo->effectiveNs != NULL) {
2495
      xsltEffectiveNsPtr effNs = lrInfo->effectiveNs;
2496
      xmlNsPtr ns, lastns = NULL;
2497
2498
      while (effNs != NULL) {
2499
          /*
2500
          * Avoid generating redundant namespace
2501
          * declarations; thus lookup if there is already
2502
          * such a ns-decl in the result.
2503
          */
2504
          ns = xmlSearchNs(copy->doc, copy, effNs->prefix);
2505
          if ((ns != NULL) &&
2506
        (xmlStrEqual(ns->href, effNs->nsName)))
2507
          {
2508
        effNs = effNs->next;
2509
        continue;
2510
          }
2511
          ns = xmlNewNs(copy, effNs->nsName, effNs->prefix);
2512
          if (ns == NULL) {
2513
        xsltTransformError(ctxt, NULL, cur,
2514
            "Internal error in "
2515
            "xsltApplySequenceConstructor(): "
2516
            "Failed to copy a namespace "
2517
            "declaration.\n");
2518
        goto error;
2519
          }
2520
2521
          if (lastns == NULL)
2522
        copy->nsDef = ns;
2523
          else
2524
        lastns->next =ns;
2525
          lastns = ns;
2526
2527
          effNs = effNs->next;
2528
      }
2529
2530
        }
2531
        /*
2532
        * NOTE that we don't need to apply ns-alising: this was
2533
        *  already done at compile-time.
2534
        */
2535
        if (cur->ns != NULL) {
2536
      /*
2537
      * If there's no such ns-decl in the result tree,
2538
      * then xsltGetSpecialNamespace() will
2539
      * create a ns-decl on the copied node.
2540
      */
2541
      copy->ns = xsltGetSpecialNamespace(ctxt, cur,
2542
          cur->ns->href, cur->ns->prefix, copy);
2543
        } else {
2544
      /*
2545
      * Undeclare the default namespace if needed.
2546
      * This can be skipped, if the result element has
2547
      *  no ns-decls, in which case the result element
2548
      *  obviously does not declare a default namespace;
2549
      *  AND there's either no parent, or the parent
2550
      *  element is in no namespace; this means there's no
2551
      *  default namespace is scope to care about.
2552
      *
2553
      * REVISIT: This might result in massive
2554
      *  generation of ns-decls if nodes in a default
2555
      *  namespaces are mixed with nodes in no namespace.
2556
      *
2557
      */
2558
      if (copy->nsDef ||
2559
          ((insert != NULL) &&
2560
           (insert->type == XML_ELEMENT_NODE) &&
2561
           (insert->ns != NULL)))
2562
      {
2563
          xsltGetSpecialNamespace(ctxt, cur,
2564
        NULL, NULL, copy);
2565
      }
2566
        }
2567
    }
2568
    /*
2569
    * SPEC XSLT 2.0 "Each attribute of the literal result
2570
    *  element, other than an attribute in the XSLT namespace,
2571
    *  is processed to produce an attribute for the element in
2572
    *  the result tree."
2573
    * NOTE: See bug #341325.
2574
    */
2575
    if (cur->properties != NULL) {
2576
        xsltAttrListTemplateProcess(ctxt, copy, cur->properties);
2577
    }
2578
      } else if (IS_XSLT_ELEM_FAST(cur)) {
2579
    /*
2580
    * XSLT instructions
2581
    * --------------------------------------------------------
2582
    */
2583
    if (info->type == XSLT_FUNC_UNKOWN_FORWARDS_COMPAT) {
2584
        /*
2585
        * We hit an unknown XSLT element.
2586
        * Try to apply one of the fallback cases.
2587
        */
2588
        ctxt->insert = insert;
2589
        if (!xsltApplyFallbacks(ctxt, contextNode, cur)) {
2590
      xsltTransformError(ctxt, NULL, cur,
2591
          "The is no fallback behaviour defined for "
2592
          "the unknown XSLT element '%s'.\n",
2593
          cur->name);
2594
        }
2595
        ctxt->insert = oldInsert;
2596
    } else if (info->func != NULL) {
2597
        /*
2598
        * Execute the XSLT instruction.
2599
        */
2600
        ctxt->insert = insert;
2601
2602
        info->func(ctxt, contextNode, cur,
2603
      (xsltElemPreCompPtr) info);
2604
2605
        /*
2606
        * Cleanup temporary tree fragments.
2607
        */
2608
        if (oldLocalFragmentTop != ctxt->localRVT)
2609
      xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
2610
2611
        ctxt->insert = oldInsert;
2612
    } else if (info->type == XSLT_FUNC_VARIABLE) {
2613
        xsltStackElemPtr tmpvar = ctxt->vars;
2614
2615
        xsltParseStylesheetVariable(ctxt, cur);
2616
2617
        if (tmpvar != ctxt->vars) {
2618
      /*
2619
      * TODO: Using a @tmpvar is an annoying workaround, but
2620
      *  the current mechanisms do not provide any other way
2621
      *  of knowing if the var was really pushed onto the
2622
      *  stack.
2623
      */
2624
      ctxt->vars->level = level;
2625
        }
2626
    } else if (info->type == XSLT_FUNC_MESSAGE) {
2627
        /*
2628
        * TODO: Won't be hit, since we don't compile xsl:message.
2629
        */
2630
        xsltMessage(ctxt, contextNode, cur);
2631
    } else {
2632
        xsltTransformError(ctxt, NULL, cur,
2633
      "Unexpected XSLT element '%s'.\n", cur->name);
2634
    }
2635
    goto skip_children;
2636
2637
      } else {
2638
    xsltTransformFunction func;
2639
    /*
2640
    * Extension intructions (elements)
2641
    * --------------------------------------------------------
2642
    */
2643
    if (cur->psvi == xsltExtMarker) {
2644
        /*
2645
        * The xsltExtMarker was set during the compilation
2646
        * of extension instructions if there was no registered
2647
        * handler for this specific extension function at
2648
        * compile-time.
2649
        * Libxslt will now lookup if a handler is
2650
        * registered in the context of this transformation.
2651
        */
2652
        func = xsltExtElementLookup(ctxt, cur->name,
2653
                                                cur->ns->href);
2654
    } else
2655
        func = ((xsltElemPreCompPtr) cur->psvi)->func;
2656
2657
    if (func == NULL) {
2658
        /*
2659
        * No handler available.
2660
        * Try to execute fallback behaviour via xsl:fallback.
2661
        */
2662
#ifdef WITH_XSLT_DEBUG_PROCESS
2663
        XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
2664
      xsltGenericDebug(xsltGenericDebugContext,
2665
          "xsltApplySequenceConstructor: unknown extension %s\n",
2666
          cur->name));
2667
#endif
2668
        ctxt->insert = insert;
2669
        if (!xsltApplyFallbacks(ctxt, contextNode, cur)) {
2670
      xsltTransformError(ctxt, NULL, cur,
2671
          "Unknown extension instruction '{%s}%s'.\n",
2672
          cur->ns->href, cur->name);
2673
        }
2674
        ctxt->insert = oldInsert;
2675
    } else {
2676
        /*
2677
        * Execute the handler-callback.
2678
        */
2679
#ifdef WITH_XSLT_DEBUG_PROCESS
2680
        XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
2681
      "xsltApplySequenceConstructor: extension construct %s\n",
2682
      cur->name));
2683
#endif
2684
                    /*
2685
                     * Disable the xsltCopyTextString optimization for
2686
                     * extension elements. Extensions could append text using
2687
                     * xmlAddChild which will free the buffer pointed to by
2688
                     * 'lasttext'. This buffer could later be reallocated with
2689
                     * a different size than recorded in 'lasttsize'. See bug
2690
                     * #777432.
2691
                     */
2692
                    if (cur->psvi == xsltExtMarker) {
2693
                        ctxt->lasttext = NULL;
2694
                    }
2695
2696
        ctxt->insert = insert;
2697
2698
        func(ctxt, contextNode, cur, cur->psvi);
2699
2700
        /*
2701
        * Cleanup temporary tree fragments.
2702
        */
2703
        if (oldLocalFragmentTop != ctxt->localRVT)
2704
      xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
2705
2706
        ctxt->insert = oldInsert;
2707
    }
2708
    goto skip_children;
2709
      }
2710
2711
  } else if (XSLT_IS_TEXT_NODE(cur)) {
2712
      /*
2713
      * Text
2714
      * ------------------------------------------------------------
2715
      */
2716
#ifdef WITH_XSLT_DEBUG_PROCESS
2717
            if (cur->name == xmlStringTextNoenc) {
2718
                XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
2719
        xsltGenericDebug(xsltGenericDebugContext,
2720
        "xsltApplySequenceConstructor: copy unescaped text '%s'\n",
2721
        cur->content));
2722
            } else {
2723
                XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
2724
        xsltGenericDebug(xsltGenericDebugContext,
2725
        "xsltApplySequenceConstructor: copy text '%s'\n",
2726
        cur->content));
2727
            }
2728
#endif
2729
            if (xsltCopyText(ctxt, insert, cur, ctxt->internalized) == NULL)
2730
    goto error;
2731
  }
2732
2733
#else /* XSLT_REFACTORED */
2734
2735
0
        if (IS_XSLT_ELEM(cur)) {
2736
            /*
2737
             * This is an XSLT node
2738
             */
2739
0
            xsltStylePreCompPtr info = (xsltStylePreCompPtr) cur->psvi;
2740
2741
0
            if (info == NULL) {
2742
0
                if (IS_XSLT_NAME(cur, "message")) {
2743
0
                    xsltMessage(ctxt, contextNode, cur);
2744
0
                } else {
2745
                    /*
2746
                     * That's an error try to apply one of the fallback cases
2747
                     */
2748
0
                    ctxt->insert = insert;
2749
0
                    if (!xsltApplyFallbacks(ctxt, contextNode, cur)) {
2750
0
                        xsltGenericError(xsltGenericErrorContext,
2751
0
          "xsltApplySequenceConstructor: %s was not compiled\n",
2752
0
          cur->name);
2753
0
                    }
2754
0
                    ctxt->insert = oldInsert;
2755
0
                }
2756
0
                goto skip_children;
2757
0
            }
2758
2759
0
            if (info->func != NULL) {
2760
0
    oldCurInst = ctxt->inst;
2761
0
    ctxt->inst = cur;
2762
0
                ctxt->insert = insert;
2763
2764
0
                info->func(ctxt, contextNode, cur, (xsltElemPreCompPtr) info);
2765
2766
    /*
2767
    * Cleanup temporary tree fragments.
2768
    */
2769
0
    if (oldLocalFragmentTop != ctxt->localRVT)
2770
0
        xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
2771
2772
0
                ctxt->insert = oldInsert;
2773
0
    ctxt->inst = oldCurInst;
2774
0
                goto skip_children;
2775
0
            }
2776
2777
0
            if (IS_XSLT_NAME(cur, "variable")) {
2778
0
    xsltStackElemPtr tmpvar = ctxt->vars;
2779
2780
0
    oldCurInst = ctxt->inst;
2781
0
    ctxt->inst = cur;
2782
2783
0
    xsltParseStylesheetVariable(ctxt, cur);
2784
2785
0
    ctxt->inst = oldCurInst;
2786
2787
0
    if (tmpvar != ctxt->vars) {
2788
        /*
2789
        * TODO: Using a @tmpvar is an annoying workaround, but
2790
        *  the current mechanisms do not provide any other way
2791
        *  of knowing if the var was really pushed onto the
2792
        *  stack.
2793
        */
2794
0
        ctxt->vars->level = level;
2795
0
    }
2796
0
            } else if (IS_XSLT_NAME(cur, "message")) {
2797
0
                xsltMessage(ctxt, contextNode, cur);
2798
0
            } else {
2799
0
    xsltTransformError(ctxt, NULL, cur,
2800
0
        "Unexpected XSLT element '%s'.\n", cur->name);
2801
0
            }
2802
0
            goto skip_children;
2803
0
        } else if ((cur->type == XML_TEXT_NODE) ||
2804
0
                   (cur->type == XML_CDATA_SECTION_NODE)) {
2805
2806
            /*
2807
             * This text comes from the stylesheet
2808
             * For stylesheets, the set of whitespace-preserving
2809
             * element names consists of just xsl:text.
2810
             */
2811
#ifdef WITH_XSLT_DEBUG_PROCESS
2812
            if (cur->type == XML_CDATA_SECTION_NODE) {
2813
                XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
2814
                                 "xsltApplySequenceConstructor: copy CDATA text %s\n",
2815
                                 cur->content));
2816
            } else if (cur->name == xmlStringTextNoenc) {
2817
                XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
2818
                                 "xsltApplySequenceConstructor: copy unescaped text %s\n",
2819
                                 cur->content));
2820
            } else {
2821
                XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
2822
                                 "xsltApplySequenceConstructor: copy text %s\n",
2823
                                 cur->content));
2824
            }
2825
#endif
2826
0
            if (xsltCopyText(ctxt, insert, cur, ctxt->internalized) == NULL)
2827
0
    goto error;
2828
0
        } else if ((cur->type == XML_ELEMENT_NODE) &&
2829
0
                   (cur->ns != NULL) && (cur->psvi != NULL)) {
2830
0
            xsltTransformFunction function;
2831
2832
0
      oldCurInst = ctxt->inst;
2833
0
      ctxt->inst = cur;
2834
            /*
2835
             * Flagged as an extension element
2836
             */
2837
0
            if (cur->psvi == xsltExtMarker)
2838
0
                function = xsltExtElementLookup(ctxt, cur->name,
2839
0
                                                cur->ns->href);
2840
0
            else
2841
0
                function = ((xsltElemPreCompPtr) cur->psvi)->func;
2842
2843
0
            if (function == NULL) {
2844
0
                xmlNodePtr child;
2845
0
                int found = 0;
2846
2847
#ifdef WITH_XSLT_DEBUG_PROCESS
2848
                XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
2849
        "xsltApplySequenceConstructor: unknown extension %s\n",
2850
                    cur->name));
2851
#endif
2852
                /*
2853
                 * Search if there are fallbacks
2854
                 */
2855
0
                ctxt->insert = insert;
2856
0
                child = cur->children;
2857
0
                while (child != NULL) {
2858
0
                    if ((IS_XSLT_ELEM(child)) &&
2859
0
                        (IS_XSLT_NAME(child, "fallback")))
2860
0
        {
2861
0
                        found = 1;
2862
0
                        xsltApplySequenceConstructor(ctxt, contextNode,
2863
0
          child->children, NULL);
2864
0
                    }
2865
0
                    child = child->next;
2866
0
                }
2867
0
                ctxt->insert = oldInsert;
2868
2869
0
                if (!found) {
2870
0
                    xsltTransformError(ctxt, NULL, cur,
2871
0
      "xsltApplySequenceConstructor: failed to find extension %s\n",
2872
0
      cur->name);
2873
0
                }
2874
0
            } else {
2875
#ifdef WITH_XSLT_DEBUG_PROCESS
2876
                XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
2877
        "xsltApplySequenceConstructor: extension construct %s\n",
2878
                    cur->name));
2879
#endif
2880
2881
                /*
2882
                 * Disable the xsltCopyTextString optimization for
2883
                 * extension elements. Extensions could append text using
2884
                 * xmlAddChild which will free the buffer pointed to by
2885
                 * 'lasttext'. This buffer could later be reallocated with
2886
                 * a different size than recorded in 'lasttsize'. See bug
2887
                 * #777432.
2888
                 */
2889
0
                if (cur->psvi == xsltExtMarker) {
2890
0
              ctxt->lasttext = NULL;
2891
0
                }
2892
2893
0
                ctxt->insert = insert;
2894
2895
0
                function(ctxt, contextNode, cur, cur->psvi);
2896
    /*
2897
    * Cleanup temporary tree fragments.
2898
    */
2899
0
    if (oldLocalFragmentTop != ctxt->localRVT)
2900
0
        xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
2901
2902
0
                ctxt->insert = oldInsert;
2903
2904
0
            }
2905
0
      ctxt->inst = oldCurInst;
2906
0
            goto skip_children;
2907
0
        } else if (cur->type == XML_ELEMENT_NODE) {
2908
#ifdef WITH_XSLT_DEBUG_PROCESS
2909
            XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
2910
    "xsltApplySequenceConstructor: copy node %s\n",
2911
                cur->name));
2912
#endif
2913
0
      oldCurInst = ctxt->inst;
2914
0
      ctxt->inst = cur;
2915
2916
0
            if ((copy = xsltShallowCopyElem(ctxt, cur, insert, 1)) == NULL)
2917
0
    goto error;
2918
            /*
2919
             * Add extra namespaces inherited from the current template
2920
             * if we are in the first level children and this is a
2921
       * "real" template.
2922
             */
2923
0
            if ((templ != NULL) && (oldInsert == insert) &&
2924
0
                (ctxt->templ != NULL) && (ctxt->templ->inheritedNs != NULL)) {
2925
0
                int i;
2926
0
                xmlNsPtr ns, ret;
2927
2928
0
                for (i = 0; i < ctxt->templ->inheritedNsNr; i++) {
2929
0
        const xmlChar *URI = NULL;
2930
0
        xsltStylesheetPtr style;
2931
0
                    ns = ctxt->templ->inheritedNs[i];
2932
2933
        /* Note that the XSLT namespace was already excluded
2934
        * in xsltGetInheritedNsList().
2935
        */
2936
#if 0
2937
        if (xmlStrEqual(ns->href, XSLT_NAMESPACE))
2938
      continue;
2939
#endif
2940
0
        style = ctxt->style;
2941
0
        while (style != NULL) {
2942
0
      if (style->nsAliases != NULL)
2943
0
          URI = (const xmlChar *)
2944
0
        xmlHashLookup(style->nsAliases, ns->href);
2945
0
      if (URI != NULL)
2946
0
          break;
2947
2948
0
      style = xsltNextImport(style);
2949
0
        }
2950
0
        if (URI == UNDEFINED_DEFAULT_NS)
2951
0
      continue;
2952
0
        if (URI == NULL)
2953
0
      URI = ns->href;
2954
        /*
2955
        * TODO: The following will still be buggy for the
2956
        * non-refactored code.
2957
        */
2958
0
        ret = xmlSearchNs(copy->doc, copy, ns->prefix);
2959
0
        if ((ret == NULL) || (!xmlStrEqual(ret->href, URI)))
2960
0
        {
2961
0
      xmlNewNs(copy, URI, ns->prefix);
2962
0
        }
2963
0
                }
2964
0
    if (copy->ns != NULL) {
2965
        /*
2966
         * Fix the node namespace if needed
2967
         */
2968
0
        copy->ns = xsltGetNamespace(ctxt, cur, copy->ns, copy);
2969
0
    }
2970
0
            }
2971
      /*
2972
             * all the attributes are directly inherited
2973
             */
2974
0
            if (cur->properties != NULL) {
2975
0
                xsltAttrListTemplateProcess(ctxt, copy, cur->properties);
2976
0
            }
2977
0
      ctxt->inst = oldCurInst;
2978
0
        }
2979
0
#endif /* else of XSLT_REFACTORED */
2980
2981
        /*
2982
         * Descend into content in document order.
2983
         */
2984
0
        if (cur->children != NULL) {
2985
0
            if (cur->children->type != XML_ENTITY_DECL) {
2986
0
                cur = cur->children;
2987
0
    level++;
2988
0
                if (copy != NULL)
2989
0
                    insert = copy;
2990
0
                continue;
2991
0
            }
2992
0
        }
2993
2994
0
skip_children:
2995
  /*
2996
  * If xslt:message was just processed, we might have hit a
2997
  * terminate='yes'; if so, then break the loop and clean up.
2998
  * TODO: Do we need to check this also before trying to descend
2999
  *  into the content?
3000
  */
3001
0
  if (ctxt->state == XSLT_STATE_STOPPED)
3002
0
      break;
3003
0
        if (cur->next != NULL) {
3004
0
            cur = cur->next;
3005
0
            continue;
3006
0
        }
3007
3008
0
        do {
3009
0
            cur = cur->parent;
3010
0
      level--;
3011
      /*
3012
      * Pop variables/params (xsl:variable and xsl:param).
3013
      */
3014
0
      if ((ctxt->varsNr > oldVarsNr) && (ctxt->vars->level > level)) {
3015
0
    xsltLocalVariablePop(ctxt, oldVarsNr, level);
3016
0
      }
3017
3018
0
            insert = insert->parent;
3019
0
            if (cur == NULL)
3020
0
                break;
3021
0
            if (cur == list->parent) {
3022
0
                cur = NULL;
3023
0
                break;
3024
0
            }
3025
0
            if (cur->next != NULL) {
3026
0
                cur = cur->next;
3027
0
                break;
3028
0
            }
3029
0
        } while (cur != NULL);
3030
0
    }
3031
3032
0
error:
3033
    /*
3034
    * In case of errors: pop remaining variables.
3035
    */
3036
0
    if (ctxt->varsNr > oldVarsNr)
3037
0
  xsltLocalVariablePop(ctxt, oldVarsNr, -1);
3038
3039
0
    ctxt->node = oldContextNode;
3040
0
    ctxt->inst = oldInst;
3041
0
    ctxt->insert = oldInsert;
3042
3043
0
    ctxt->depth--;
3044
3045
#ifdef WITH_DEBUGGER
3046
    if ((ctxt->debugStatus != XSLT_DEBUG_NONE) && (addCallResult)) {
3047
        xslDropCall();
3048
    }
3049
#endif
3050
0
}
3051
3052
/*
3053
* xsltApplyXSLTTemplate:
3054
* @ctxt:  a XSLT transformation context
3055
* @contextNode:  the node in the source tree.
3056
* @list:  the nodes of a sequence constructor;
3057
*         (plus leading xsl:param elements)
3058
* @templ: the compiled xsl:template declaration;
3059
*         NULL if a sequence constructor
3060
* @withParams:  a set of caller-parameters (xsl:with-param) or NULL
3061
*
3062
* Called by:
3063
* - xsltApplyImports()
3064
* - xsltCallTemplate()
3065
* - xsltDefaultProcessOneNode()
3066
* - xsltProcessOneNode()
3067
*/
3068
static void
3069
xsltApplyXSLTTemplate(xsltTransformContextPtr ctxt,
3070
          xmlNodePtr contextNode,
3071
          xmlNodePtr list,
3072
          xsltTemplatePtr templ,
3073
          xsltStackElemPtr withParams)
3074
0
{
3075
0
    int oldVarsBase = 0;
3076
0
    xmlNodePtr cur;
3077
0
    xsltStackElemPtr tmpParam = NULL;
3078
0
    xmlDocPtr oldUserFragmentTop;
3079
#ifdef WITH_PROFILER
3080
    long start = 0;
3081
#endif
3082
3083
#ifdef XSLT_REFACTORED
3084
    xsltStyleItemParamPtr iparam;
3085
#else
3086
0
    xsltStylePreCompPtr iparam;
3087
0
#endif
3088
3089
#ifdef WITH_DEBUGGER
3090
    int addCallResult = 0;
3091
#endif
3092
3093
0
    if (ctxt == NULL)
3094
0
  return;
3095
0
    if (templ == NULL) {
3096
0
  xsltTransformError(ctxt, NULL, list,
3097
0
      "xsltApplyXSLTTemplate: Bad arguments; @templ is mandatory.\n");
3098
0
  return;
3099
0
    }
3100
3101
#ifdef WITH_DEBUGGER
3102
    if (ctxt->debugStatus != XSLT_DEBUG_NONE) {
3103
  if (xsltDebuggerStartSequenceConstructor(ctxt, contextNode,
3104
    list, templ, &addCallResult) == NULL)
3105
      return;
3106
    }
3107
#endif
3108
3109
0
    if (list == NULL)
3110
0
        return;
3111
0
    CHECK_STOPPED;
3112
3113
0
    if (ctxt->varsNr >= ctxt->maxTemplateVars)
3114
0
  {
3115
0
        xsltTransformError(ctxt, NULL, list,
3116
0
      "xsltApplyXSLTTemplate: A potential infinite template recursion "
3117
0
      "was detected.\n"
3118
0
      "You can adjust maxTemplateVars (--maxvars) in order to "
3119
0
      "raise the maximum number of variables/params (currently set to %d).\n",
3120
0
      ctxt->maxTemplateVars);
3121
0
        xsltDebug(ctxt, contextNode, list, NULL);
3122
0
  ctxt->state = XSLT_STATE_STOPPED;
3123
0
        return;
3124
0
  }
3125
3126
0
    oldUserFragmentTop = ctxt->tmpRVT;
3127
0
    ctxt->tmpRVT = NULL;
3128
3129
    /*
3130
    * Initiate a distinct scope of local params/variables.
3131
    */
3132
0
    oldVarsBase = ctxt->varsBase;
3133
0
    ctxt->varsBase = ctxt->varsNr;
3134
3135
0
    ctxt->node = contextNode;
3136
3137
#ifdef WITH_PROFILER
3138
    if (ctxt->profile) {
3139
  templ->nbCalls++;
3140
  start = xsltTimestamp();
3141
  profPush(ctxt, 0);
3142
  profCallgraphAdd(templ, ctxt->templ);
3143
    }
3144
#endif
3145
3146
    /*
3147
    * Push the xsl:template declaration onto the stack.
3148
    */
3149
0
    templPush(ctxt, templ);
3150
3151
#ifdef WITH_XSLT_DEBUG_PROCESS
3152
    if (templ->name != NULL)
3153
  XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
3154
  "applying xsl:template '%s'\n", templ->name));
3155
#endif
3156
    /*
3157
    * Process xsl:param instructions and skip those elements for
3158
    * further processing.
3159
    */
3160
0
    cur = list;
3161
0
    do {
3162
0
  if (cur->type == XML_TEXT_NODE) {
3163
0
      cur = cur->next;
3164
0
      continue;
3165
0
  }
3166
0
  if ((cur->type != XML_ELEMENT_NODE) ||
3167
0
      (cur->name[0] != 'p') ||
3168
0
      (cur->psvi == NULL) ||
3169
0
      (! xmlStrEqual(cur->name, BAD_CAST "param")) ||
3170
0
      (! IS_XSLT_ELEM(cur)))
3171
0
  {
3172
0
      break;
3173
0
  }
3174
3175
0
  list = cur->next;
3176
3177
#ifdef XSLT_REFACTORED
3178
  iparam = (xsltStyleItemParamPtr) cur->psvi;
3179
#else
3180
0
  iparam = (xsltStylePreCompPtr) cur->psvi;
3181
0
#endif
3182
3183
  /*
3184
  * Substitute xsl:param for a given xsl:with-param.
3185
  * Since the XPath expression will reference the params/vars
3186
  * by index, we need to slot the xsl:with-params in the
3187
  * order of encountered xsl:params to keep the sequence of
3188
  * params/variables in the stack exactly as it was at
3189
  * compile time,
3190
  */
3191
0
  tmpParam = NULL;
3192
0
  if (withParams) {
3193
0
      tmpParam = withParams;
3194
0
      do {
3195
0
    if ((tmpParam->name == (iparam->name)) &&
3196
0
        (tmpParam->nameURI == (iparam->ns)))
3197
0
    {
3198
        /*
3199
        * Push the caller-parameter.
3200
        */
3201
0
        xsltLocalVariablePush(ctxt, tmpParam, -1);
3202
0
        break;
3203
0
    }
3204
0
    tmpParam = tmpParam->next;
3205
0
      } while (tmpParam != NULL);
3206
0
  }
3207
  /*
3208
  * Push the xsl:param.
3209
  */
3210
0
  if (tmpParam == NULL) {
3211
      /*
3212
      * Note that we must assume that the added parameter
3213
      * has a @depth of 0.
3214
      */
3215
0
      xsltParseStylesheetParam(ctxt, cur);
3216
0
  }
3217
0
  cur = cur->next;
3218
0
    } while (cur != NULL);
3219
    /*
3220
    * Process the sequence constructor.
3221
    */
3222
0
    xsltApplySequenceConstructor(ctxt, contextNode, list, templ);
3223
3224
    /*
3225
    * Remove remaining xsl:param and xsl:with-param items from
3226
    * the stack. Don't free xsl:with-param items.
3227
    */
3228
0
    if (ctxt->varsNr > ctxt->varsBase)
3229
0
  xsltTemplateParamsCleanup(ctxt);
3230
0
    ctxt->varsBase = oldVarsBase;
3231
3232
    /*
3233
    * Release user-created fragments stored in the scope
3234
    * of xsl:template. Note that this mechanism is deprecated:
3235
    * user code should now use xsltRegisterLocalRVT() instead
3236
    * of the obsolete xsltRegisterTmpRVT().
3237
    */
3238
0
    if (ctxt->tmpRVT) {
3239
0
  xmlDocPtr curdoc = ctxt->tmpRVT, tmp;
3240
3241
0
  while (curdoc != NULL) {
3242
0
      tmp = curdoc;
3243
0
      curdoc = (xmlDocPtr) curdoc->next;
3244
0
      xsltReleaseRVT(ctxt, tmp);
3245
0
  }
3246
0
    }
3247
0
    ctxt->tmpRVT = oldUserFragmentTop;
3248
3249
    /*
3250
    * Pop the xsl:template declaration from the stack.
3251
    */
3252
0
    templPop(ctxt);
3253
3254
#ifdef WITH_PROFILER
3255
    if (ctxt->profile) {
3256
  long spent, child, total, end;
3257
3258
  end = xsltTimestamp();
3259
  child = profPop(ctxt);
3260
  total = end - start;
3261
  spent = total - child;
3262
  if (spent <= 0) {
3263
      /*
3264
      * Not possible unless the original calibration failed
3265
      * we can try to correct it on the fly.
3266
      */
3267
      xsltCalibrateAdjust(spent);
3268
      spent = 0;
3269
  }
3270
3271
  templ->time += spent;
3272
  if (ctxt->profNr > 0)
3273
      ctxt->profTab[ctxt->profNr - 1] += total;
3274
    }
3275
#endif
3276
3277
#ifdef WITH_DEBUGGER
3278
    if ((ctxt->debugStatus != XSLT_DEBUG_NONE) && (addCallResult)) {
3279
        xslDropCall();
3280
    }
3281
#endif
3282
0
}
3283
3284
3285
/**
3286
 * xsltApplyOneTemplate:
3287
 * @ctxt:  a XSLT process context
3288
 * @contextNode:  the node in the source tree.
3289
 * @list:  the nodes of a sequence constructor
3290
 * @templ: not used
3291
 * @params:  a set of parameters (xsl:param) or NULL
3292
 *
3293
 * Processes a sequence constructor on the current node in the source tree.
3294
 *
3295
 * @params are the already computed variable stack items; this function
3296
 * pushes them on the variable stack, and pops them before exiting; it's
3297
 * left to the caller to free or reuse @params afterwards. The initial
3298
 * states of the variable stack will always be restored before this
3299
 * function exits.
3300
 * NOTE that this does *not* initiate a new distinct variable scope; i.e.
3301
 * variables already on the stack are visible to the process. The caller's
3302
 * side needs to start a new variable scope if needed (e.g. in exsl:function).
3303
 *
3304
 * @templ is obsolete and not used anymore (e.g. <exslt:function> does not
3305
 * provide a @templ); a non-NULL @templ might raise an error in the future.
3306
 *
3307
 * BIG NOTE: This function is not intended to process the content of an
3308
 * xsl:template; it does not expect xsl:param instructions in @list and
3309
 * will report errors if found.
3310
 *
3311
 * Called by:
3312
 *  - xsltEvalVariable() (variables.c)
3313
 *  - exsltFuncFunctionFunction() (libexsl/functions.c)
3314
 */
3315
void
3316
xsltApplyOneTemplate(xsltTransformContextPtr ctxt,
3317
         xmlNodePtr contextNode,
3318
                     xmlNodePtr list,
3319
         xsltTemplatePtr templ ATTRIBUTE_UNUSED,
3320
                     xsltStackElemPtr params)
3321
0
{
3322
0
    if ((ctxt == NULL) || (list == NULL))
3323
0
  return;
3324
0
    CHECK_STOPPED;
3325
3326
0
    if (params) {
3327
  /*
3328
   * This code should be obsolete - was previously used
3329
   * by libexslt/functions.c, but due to bug 381319 the
3330
   * logic there was changed.
3331
   */
3332
0
  int oldVarsNr = ctxt->varsNr;
3333
3334
  /*
3335
  * Push the given xsl:param(s) onto the variable stack.
3336
  */
3337
0
  while (params != NULL) {
3338
0
      xsltLocalVariablePush(ctxt, params, -1);
3339
0
      params = params->next;
3340
0
  }
3341
0
  xsltApplySequenceConstructor(ctxt, contextNode, list, templ);
3342
  /*
3343
  * Pop the given xsl:param(s) from the stack but don't free them.
3344
  */
3345
0
  xsltLocalVariablePop(ctxt, oldVarsNr, -2);
3346
0
    } else
3347
0
  xsltApplySequenceConstructor(ctxt, contextNode, list, templ);
3348
0
}
3349
3350
/************************************************************************
3351
 *                  *
3352
 *        XSLT-1.1 extensions         *
3353
 *                  *
3354
 ************************************************************************/
3355
3356
/**
3357
 * xsltDocumentElem:
3358
 * @ctxt:  an XSLT processing context
3359
 * @node:  The current node
3360
 * @inst:  the instruction in the stylesheet
3361
 * @castedComp:  precomputed information
3362
 *
3363
 * Process an EXSLT/XSLT-1.1 document element
3364
 */
3365
void
3366
xsltDocumentElem(xsltTransformContextPtr ctxt, xmlNodePtr node,
3367
                 xmlNodePtr inst, xsltElemPreCompPtr castedComp)
3368
0
{
3369
#ifdef XSLT_REFACTORED
3370
    xsltStyleItemDocumentPtr comp = (xsltStyleItemDocumentPtr) castedComp;
3371
#else
3372
0
    xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
3373
0
#endif
3374
0
    xsltStylesheetPtr style = NULL;
3375
0
    int ret;
3376
0
    xmlChar *filename = NULL, *prop, *elements;
3377
0
    xmlChar *element, *end;
3378
0
    xmlDocPtr res = NULL;
3379
0
    xmlDocPtr oldOutput;
3380
0
    xmlNodePtr oldInsert, root;
3381
0
    const char *oldOutputFile;
3382
0
    xsltOutputType oldType;
3383
0
    xmlChar *URL = NULL;
3384
0
    const xmlChar *method;
3385
0
    const xmlChar *doctypePublic;
3386
0
    const xmlChar *doctypeSystem;
3387
0
    const xmlChar *version;
3388
0
    const xmlChar *encoding;
3389
0
    int redirect_write_append = 0;
3390
3391
0
    if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL))
3392
0
        return;
3393
3394
0
    if (comp->filename == NULL) {
3395
3396
0
        if (xmlStrEqual(inst->name, (const xmlChar *) "output")) {
3397
      /*
3398
      * The element "output" is in the namespace XSLT_SAXON_NAMESPACE
3399
      *   (http://icl.com/saxon)
3400
      * The @file is in no namespace.
3401
      */
3402
#ifdef WITH_XSLT_DEBUG_EXTRA
3403
            xsltGenericDebug(xsltGenericDebugContext,
3404
                             "Found saxon:output extension\n");
3405
#endif
3406
0
            URL = xsltEvalAttrValueTemplate(ctxt, inst,
3407
0
                                                 (const xmlChar *) "file",
3408
0
                                                 XSLT_SAXON_NAMESPACE);
3409
3410
0
      if (URL == NULL)
3411
0
    URL = xsltEvalAttrValueTemplate(ctxt, inst,
3412
0
                                                 (const xmlChar *) "href",
3413
0
                                                 XSLT_SAXON_NAMESPACE);
3414
0
        } else if (xmlStrEqual(inst->name, (const xmlChar *) "write")) {
3415
#ifdef WITH_XSLT_DEBUG_EXTRA
3416
            xsltGenericDebug(xsltGenericDebugContext,
3417
                             "Found xalan:write extension\n");
3418
#endif
3419
0
            URL = xsltEvalAttrValueTemplate(ctxt, inst,
3420
0
                                                 (const xmlChar *)
3421
0
                                                 "select",
3422
0
                                                 XSLT_XALAN_NAMESPACE);
3423
0
      if (URL != NULL) {
3424
0
    xmlXPathCompExprPtr cmp;
3425
0
    xmlChar *val;
3426
3427
    /*
3428
     * Trying to handle bug #59212
3429
     * The value of the "select" attribute is an
3430
     * XPath expression.
3431
     * (see http://xml.apache.org/xalan-j/extensionslib.html#redirect)
3432
     */
3433
0
    cmp = xmlXPathCtxtCompile(ctxt->xpathCtxt, URL);
3434
0
                val = xsltEvalXPathString(ctxt, cmp);
3435
0
    xmlXPathFreeCompExpr(cmp);
3436
0
    xmlFree(URL);
3437
0
    URL = val;
3438
0
      }
3439
0
      if (URL == NULL)
3440
0
    URL = xsltEvalAttrValueTemplate(ctxt, inst,
3441
0
                 (const xmlChar *)
3442
0
                 "file",
3443
0
                 XSLT_XALAN_NAMESPACE);
3444
0
      if (URL == NULL)
3445
0
    URL = xsltEvalAttrValueTemplate(ctxt, inst,
3446
0
                 (const xmlChar *)
3447
0
                 "href",
3448
0
                 XSLT_XALAN_NAMESPACE);
3449
0
        } else if (xmlStrEqual(inst->name, (const xmlChar *) "document")) {
3450
0
            URL = xsltEvalAttrValueTemplate(ctxt, inst,
3451
0
                                                 (const xmlChar *) "href",
3452
0
                                                 NULL);
3453
0
        }
3454
3455
0
    } else {
3456
0
        URL = xmlStrdup(comp->filename);
3457
0
    }
3458
3459
0
    if (URL == NULL) {
3460
0
  xsltTransformError(ctxt, NULL, inst,
3461
0
             "xsltDocumentElem: href/URI-Reference not found\n");
3462
0
  return;
3463
0
    }
3464
3465
    /*
3466
     * If the computation failed, it's likely that the URL wasn't escaped
3467
     */
3468
0
    filename = xmlBuildURI(URL, (const xmlChar *) ctxt->outputFile);
3469
0
    if (filename == NULL) {
3470
0
  xmlChar *escURL;
3471
3472
0
  escURL=xmlURIEscapeStr(URL, BAD_CAST ":/.?,");
3473
0
  if (escURL != NULL) {
3474
0
      filename = xmlBuildURI(escURL, (const xmlChar *) ctxt->outputFile);
3475
0
      xmlFree(escURL);
3476
0
  }
3477
0
    }
3478
3479
0
    if (filename == NULL) {
3480
0
  xsltTransformError(ctxt, NULL, inst,
3481
0
             "xsltDocumentElem: URL computation failed for %s\n",
3482
0
       URL);
3483
0
  xmlFree(URL);
3484
0
  return;
3485
0
    }
3486
3487
    /*
3488
     * Security checking: can we write to this resource
3489
     */
3490
0
    if (ctxt->sec != NULL) {
3491
0
  ret = xsltCheckWrite(ctxt->sec, ctxt, filename);
3492
0
  if (ret <= 0) {
3493
0
            if (ret == 0)
3494
0
                xsltTransformError(ctxt, NULL, inst,
3495
0
                     "xsltDocumentElem: write rights for %s denied\n",
3496
0
                                 filename);
3497
0
      xmlFree(URL);
3498
0
      xmlFree(filename);
3499
0
      return;
3500
0
  }
3501
0
    }
3502
3503
0
    oldOutputFile = ctxt->outputFile;
3504
0
    oldOutput = ctxt->output;
3505
0
    oldInsert = ctxt->insert;
3506
0
    oldType = ctxt->type;
3507
0
    ctxt->outputFile = (const char *) filename;
3508
3509
0
    style = xsltNewStylesheet();
3510
0
    if (style == NULL) {
3511
0
  xsltTransformError(ctxt, NULL, inst,
3512
0
                         "xsltDocumentElem: out of memory\n");
3513
0
        goto error;
3514
0
    }
3515
3516
    /*
3517
     * Version described in 1.1 draft allows full parameterization
3518
     * of the output.
3519
     */
3520
0
    prop = xsltEvalAttrValueTemplate(ctxt, inst,
3521
0
             (const xmlChar *) "version",
3522
0
             NULL);
3523
0
    if (prop != NULL) {
3524
0
  if (style->version != NULL)
3525
0
      xmlFree(style->version);
3526
0
  style->version = prop;
3527
0
    }
3528
0
    prop = xsltEvalAttrValueTemplate(ctxt, inst,
3529
0
             (const xmlChar *) "encoding",
3530
0
             NULL);
3531
0
    if (prop != NULL) {
3532
0
  if (style->encoding != NULL)
3533
0
      xmlFree(style->encoding);
3534
0
  style->encoding = prop;
3535
0
    }
3536
0
    prop = xsltEvalAttrValueTemplate(ctxt, inst,
3537
0
             (const xmlChar *) "method",
3538
0
             NULL);
3539
0
    if (prop != NULL) {
3540
0
  const xmlChar *URI;
3541
3542
0
  if (style->method != NULL)
3543
0
      xmlFree(style->method);
3544
0
  style->method = NULL;
3545
0
  if (style->methodURI != NULL)
3546
0
      xmlFree(style->methodURI);
3547
0
  style->methodURI = NULL;
3548
3549
0
  URI = xsltGetQNameURI(inst, &prop);
3550
0
  if (prop == NULL) {
3551
0
      if (style != NULL) style->errors++;
3552
0
  } else if (URI == NULL) {
3553
0
      if ((xmlStrEqual(prop, (const xmlChar *) "xml")) ||
3554
0
    (xmlStrEqual(prop, (const xmlChar *) "html")) ||
3555
0
    (xmlStrEqual(prop, (const xmlChar *) "text"))) {
3556
0
    style->method = prop;
3557
0
      } else {
3558
0
    xsltTransformError(ctxt, NULL, inst,
3559
0
         "invalid value for method: %s\n", prop);
3560
0
    if (style != NULL) style->warnings++;
3561
0
      }
3562
0
  } else {
3563
0
      style->method = prop;
3564
0
      style->methodURI = xmlStrdup(URI);
3565
0
  }
3566
0
    }
3567
0
    prop = xsltEvalAttrValueTemplate(ctxt, inst,
3568
0
             (const xmlChar *)
3569
0
             "doctype-system", NULL);
3570
0
    if (prop != NULL) {
3571
0
  if (style->doctypeSystem != NULL)
3572
0
      xmlFree(style->doctypeSystem);
3573
0
  style->doctypeSystem = prop;
3574
0
    }
3575
0
    prop = xsltEvalAttrValueTemplate(ctxt, inst,
3576
0
             (const xmlChar *)
3577
0
             "doctype-public", NULL);
3578
0
    if (prop != NULL) {
3579
0
  if (style->doctypePublic != NULL)
3580
0
      xmlFree(style->doctypePublic);
3581
0
  style->doctypePublic = prop;
3582
0
    }
3583
0
    prop = xsltEvalAttrValueTemplate(ctxt, inst,
3584
0
             (const xmlChar *) "standalone",
3585
0
             NULL);
3586
0
    if (prop != NULL) {
3587
0
  if (xmlStrEqual(prop, (const xmlChar *) "yes")) {
3588
0
      style->standalone = 1;
3589
0
  } else if (xmlStrEqual(prop, (const xmlChar *) "no")) {
3590
0
      style->standalone = 0;
3591
0
  } else {
3592
0
      xsltTransformError(ctxt, NULL, inst,
3593
0
           "invalid value for standalone: %s\n",
3594
0
           prop);
3595
0
      if (style != NULL) style->warnings++;
3596
0
  }
3597
0
  xmlFree(prop);
3598
0
    }
3599
3600
0
    prop = xsltEvalAttrValueTemplate(ctxt, inst,
3601
0
             (const xmlChar *) "indent",
3602
0
             NULL);
3603
0
    if (prop != NULL) {
3604
0
  if (xmlStrEqual(prop, (const xmlChar *) "yes")) {
3605
0
      style->indent = 1;
3606
0
  } else if (xmlStrEqual(prop, (const xmlChar *) "no")) {
3607
0
      style->indent = 0;
3608
0
  } else {
3609
0
      xsltTransformError(ctxt, NULL, inst,
3610
0
           "invalid value for indent: %s\n", prop);
3611
0
      if (style != NULL) style->warnings++;
3612
0
  }
3613
0
  xmlFree(prop);
3614
0
    }
3615
3616
0
    prop = xsltEvalAttrValueTemplate(ctxt, inst,
3617
0
             (const xmlChar *)
3618
0
             "omit-xml-declaration",
3619
0
             NULL);
3620
0
    if (prop != NULL) {
3621
0
  if (xmlStrEqual(prop, (const xmlChar *) "yes")) {
3622
0
      style->omitXmlDeclaration = 1;
3623
0
  } else if (xmlStrEqual(prop, (const xmlChar *) "no")) {
3624
0
      style->omitXmlDeclaration = 0;
3625
0
  } else {
3626
0
      xsltTransformError(ctxt, NULL, inst,
3627
0
           "invalid value for omit-xml-declaration: %s\n",
3628
0
           prop);
3629
0
      if (style != NULL) style->warnings++;
3630
0
  }
3631
0
  xmlFree(prop);
3632
0
    }
3633
3634
0
    elements = xsltEvalAttrValueTemplate(ctxt, inst,
3635
0
           (const xmlChar *)
3636
0
           "cdata-section-elements",
3637
0
           NULL);
3638
0
    if (elements != NULL) {
3639
0
  if (style->stripSpaces == NULL)
3640
0
      style->stripSpaces = xmlHashCreate(10);
3641
0
  if (style->stripSpaces == NULL) {
3642
0
      xmlFree(elements);
3643
0
      return;
3644
0
  }
3645
3646
0
  element = elements;
3647
0
  while (*element != 0) {
3648
0
      while (xmlIsBlank_ch(*element))
3649
0
    element++;
3650
0
      if (*element == 0)
3651
0
    break;
3652
0
      end = element;
3653
0
      while ((*end != 0) && (!xmlIsBlank_ch(*end)))
3654
0
    end++;
3655
0
      element = xmlStrndup(element, end - element);
3656
0
      if (element) {
3657
0
    const xmlChar *URI;
3658
3659
#ifdef WITH_XSLT_DEBUG_PARSING
3660
    xsltGenericDebug(xsltGenericDebugContext,
3661
         "add cdata section output element %s\n",
3662
         element);
3663
#endif
3664
0
                URI = xsltGetQNameURI(inst, &element);
3665
3666
0
    xmlHashAddEntry2(style->stripSpaces, element, URI,
3667
0
              (xmlChar *) "cdata");
3668
0
    xmlFree(element);
3669
0
      }
3670
0
      element = end;
3671
0
  }
3672
0
  xmlFree(elements);
3673
0
    }
3674
3675
    /*
3676
     * Create a new document tree and process the element template
3677
     */
3678
0
    XSLT_GET_IMPORT_PTR(method, style, method)
3679
0
    XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
3680
0
    XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
3681
0
    XSLT_GET_IMPORT_PTR(version, style, version)
3682
0
    XSLT_GET_IMPORT_PTR(encoding, style, encoding)
3683
3684
0
    if ((method != NULL) &&
3685
0
  (!xmlStrEqual(method, (const xmlChar *) "xml"))) {
3686
0
  if (xmlStrEqual(method, (const xmlChar *) "html")) {
3687
0
      ctxt->type = XSLT_OUTPUT_HTML;
3688
0
      if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
3689
0
    res = htmlNewDoc(doctypeSystem, doctypePublic);
3690
0
      else {
3691
0
    if (version != NULL) {
3692
0
#ifdef XSLT_GENERATE_HTML_DOCTYPE
3693
0
        xsltGetHTMLIDs(version, &doctypePublic, &doctypeSystem);
3694
0
#endif
3695
0
                }
3696
0
    res = htmlNewDocNoDtD(doctypeSystem, doctypePublic);
3697
0
      }
3698
0
      if (res == NULL)
3699
0
    goto error;
3700
0
      res->dict = ctxt->dict;
3701
0
      xmlDictReference(res->dict);
3702
0
  } else if (xmlStrEqual(method, (const xmlChar *) "xhtml")) {
3703
0
      xsltTransformError(ctxt, NULL, inst,
3704
0
       "xsltDocumentElem: unsupported method xhtml\n");
3705
0
      ctxt->type = XSLT_OUTPUT_HTML;
3706
0
      res = htmlNewDocNoDtD(doctypeSystem, doctypePublic);
3707
0
      if (res == NULL)
3708
0
    goto error;
3709
0
      res->dict = ctxt->dict;
3710
0
      xmlDictReference(res->dict);
3711
0
  } else if (xmlStrEqual(method, (const xmlChar *) "text")) {
3712
0
      ctxt->type = XSLT_OUTPUT_TEXT;
3713
0
      res = xmlNewDoc(style->version);
3714
0
      if (res == NULL)
3715
0
    goto error;
3716
0
      res->dict = ctxt->dict;
3717
0
      xmlDictReference(res->dict);
3718
#ifdef WITH_XSLT_DEBUG
3719
      xsltGenericDebug(xsltGenericDebugContext,
3720
                     "reusing transformation dict for output\n");
3721
#endif
3722
0
  } else {
3723
0
      xsltTransformError(ctxt, NULL, inst,
3724
0
           "xsltDocumentElem: unsupported method (%s)\n",
3725
0
                 method);
3726
0
      goto error;
3727
0
  }
3728
0
    } else {
3729
0
  ctxt->type = XSLT_OUTPUT_XML;
3730
0
  res = xmlNewDoc(style->version);
3731
0
  if (res == NULL)
3732
0
      goto error;
3733
0
  res->dict = ctxt->dict;
3734
0
  xmlDictReference(res->dict);
3735
#ifdef WITH_XSLT_DEBUG
3736
  xsltGenericDebug(xsltGenericDebugContext,
3737
                     "reusing transformation dict for output\n");
3738
#endif
3739
0
    }
3740
0
    res->charset = XML_CHAR_ENCODING_UTF8;
3741
0
    if (encoding != NULL)
3742
0
  res->encoding = xmlStrdup(encoding);
3743
0
    ctxt->output = res;
3744
0
    ctxt->insert = (xmlNodePtr) res;
3745
0
    xsltApplySequenceConstructor(ctxt, node, inst->children, NULL);
3746
3747
    /*
3748
     * Do some post processing work depending on the generated output
3749
     */
3750
0
    root = xmlDocGetRootElement(res);
3751
0
    if (root != NULL) {
3752
0
        const xmlChar *doctype = NULL;
3753
3754
0
        if ((root->ns != NULL) && (root->ns->prefix != NULL))
3755
0
      doctype = xmlDictQLookup(ctxt->dict, root->ns->prefix, root->name);
3756
0
  if (doctype == NULL)
3757
0
      doctype = root->name;
3758
3759
        /*
3760
         * Apply the default selection of the method
3761
         */
3762
0
        if ((method == NULL) &&
3763
0
            (root->ns == NULL) &&
3764
0
            (!xmlStrcasecmp(root->name, (const xmlChar *) "html"))) {
3765
0
            xmlNodePtr tmp;
3766
3767
0
            tmp = res->children;
3768
0
            while ((tmp != NULL) && (tmp != root)) {
3769
0
                if (tmp->type == XML_ELEMENT_NODE)
3770
0
                    break;
3771
0
                if ((tmp->type == XML_TEXT_NODE) && (!xmlIsBlankNode(tmp)))
3772
0
                    break;
3773
0
    tmp = tmp->next;
3774
0
            }
3775
0
            if (tmp == root) {
3776
0
                ctxt->type = XSLT_OUTPUT_HTML;
3777
0
                res->type = XML_HTML_DOCUMENT_NODE;
3778
0
                if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
3779
0
                    res->intSubset = xmlCreateIntSubset(res, doctype,
3780
0
                                                        doctypePublic,
3781
0
                                                        doctypeSystem);
3782
0
#ifdef XSLT_GENERATE_HTML_DOCTYPE
3783
0
    } else if (version != NULL) {
3784
0
                    xsltGetHTMLIDs(version, &doctypePublic,
3785
0
                                   &doctypeSystem);
3786
0
                    if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
3787
0
                        res->intSubset =
3788
0
                            xmlCreateIntSubset(res, doctype,
3789
0
                                               doctypePublic,
3790
0
                                               doctypeSystem);
3791
0
#endif
3792
0
                }
3793
0
            }
3794
3795
0
        }
3796
0
        if (ctxt->type == XSLT_OUTPUT_XML) {
3797
0
            XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
3798
0
                XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
3799
0
                if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
3800
0
                res->intSubset = xmlCreateIntSubset(res, doctype,
3801
0
                                                    doctypePublic,
3802
0
                                                    doctypeSystem);
3803
0
        }
3804
0
    }
3805
3806
    /*
3807
     * Calls to redirect:write also take an optional attribute append.
3808
     * Attribute append="true|yes" which will attempt to simply append
3809
     * to an existing file instead of always opening a new file. The
3810
     * default behavior of always overwriting the file still happens
3811
     * if we do not specify append.
3812
     * Note that append use will forbid use of remote URI target.
3813
     */
3814
0
    prop = xsltEvalAttrValueTemplate(ctxt, inst, (const xmlChar *)"append",
3815
0
             NULL);
3816
0
    if (prop != NULL) {
3817
0
  if (xmlStrEqual(prop, (const xmlChar *) "true") ||
3818
0
      xmlStrEqual(prop, (const xmlChar *) "yes")) {
3819
0
      style->omitXmlDeclaration = 1;
3820
0
      redirect_write_append = 1;
3821
0
  } else
3822
0
      style->omitXmlDeclaration = 0;
3823
0
  xmlFree(prop);
3824
0
    }
3825
3826
0
    if (redirect_write_append) {
3827
0
        FILE *f;
3828
3829
0
  f = fopen((const char *) filename, "ab");
3830
0
  if (f == NULL) {
3831
0
      ret = -1;
3832
0
  } else {
3833
0
      ret = xsltSaveResultToFile(f, res, style);
3834
0
      fclose(f);
3835
0
  }
3836
0
    } else {
3837
0
  ret = xsltSaveResultToFilename((const char *) filename, res, style, 0);
3838
0
    }
3839
0
    if (ret < 0) {
3840
0
  xsltTransformError(ctxt, NULL, inst,
3841
0
                         "xsltDocumentElem: unable to save to %s\n",
3842
0
                         filename);
3843
#ifdef WITH_XSLT_DEBUG_EXTRA
3844
    } else {
3845
        xsltGenericDebug(xsltGenericDebugContext,
3846
                         "Wrote %d bytes to %s\n", ret, filename);
3847
#endif
3848
0
    }
3849
3850
0
  error:
3851
0
    ctxt->output = oldOutput;
3852
0
    ctxt->insert = oldInsert;
3853
0
    ctxt->type = oldType;
3854
0
    ctxt->outputFile = oldOutputFile;
3855
0
    if (URL != NULL)
3856
0
        xmlFree(URL);
3857
0
    if (filename != NULL)
3858
0
        xmlFree(filename);
3859
0
    if (style != NULL)
3860
0
        xsltFreeStylesheet(style);
3861
0
    if (res != NULL)
3862
0
        xmlFreeDoc(res);
3863
0
}
3864
3865
/************************************************************************
3866
 *                  *
3867
 *    Most of the XSLT-1.0 transformations      *
3868
 *                  *
3869
 ************************************************************************/
3870
3871
/**
3872
 * xsltSort:
3873
 * @ctxt:  a XSLT process context
3874
 * @node:  the node in the source tree.
3875
 * @inst:  the xslt sort node
3876
 * @comp:  precomputed information
3877
 *
3878
 * function attached to xsl:sort nodes, but this should not be
3879
 * called directly
3880
 */
3881
void
3882
xsltSort(xsltTransformContextPtr ctxt,
3883
  xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr inst,
3884
0
  xsltElemPreCompPtr comp) {
3885
0
    if (comp == NULL) {
3886
0
  xsltTransformError(ctxt, NULL, inst,
3887
0
       "xsl:sort : compilation failed\n");
3888
0
  return;
3889
0
    }
3890
0
    xsltTransformError(ctxt, NULL, inst,
3891
0
   "xsl:sort : improper use this should not be reached\n");
3892
0
}
3893
3894
/**
3895
 * xsltCopy:
3896
 * @ctxt:  an XSLT process context
3897
 * @node:  the node in the source tree
3898
 * @inst:  the element node of the XSLT-copy instruction
3899
 * @castedComp:  computed information of the XSLT-copy instruction
3900
 *
3901
 * Execute the XSLT-copy instruction on the source node.
3902
 */
3903
void
3904
xsltCopy(xsltTransformContextPtr ctxt, xmlNodePtr node,
3905
   xmlNodePtr inst, xsltElemPreCompPtr castedComp)
3906
0
{
3907
#ifdef XSLT_REFACTORED
3908
    xsltStyleItemCopyPtr comp = (xsltStyleItemCopyPtr) castedComp;
3909
#else
3910
0
    xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
3911
0
#endif
3912
0
    xmlNodePtr copy, oldInsert;
3913
3914
0
    oldInsert = ctxt->insert;
3915
0
    if (ctxt->insert != NULL) {
3916
0
  switch (node->type) {
3917
0
      case XML_TEXT_NODE:
3918
0
      case XML_CDATA_SECTION_NODE:
3919
    /*
3920
     * This text comes from the stylesheet
3921
     * For stylesheets, the set of whitespace-preserving
3922
     * element names consists of just xsl:text.
3923
     */
3924
#ifdef WITH_XSLT_DEBUG_PROCESS
3925
    if (node->type == XML_CDATA_SECTION_NODE) {
3926
        XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
3927
       "xsltCopy: CDATA text %s\n", node->content));
3928
    } else {
3929
        XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
3930
       "xsltCopy: text %s\n", node->content));
3931
                }
3932
#endif
3933
0
    xsltCopyText(ctxt, ctxt->insert, node, 0);
3934
0
    break;
3935
0
      case XML_DOCUMENT_NODE:
3936
0
      case XML_HTML_DOCUMENT_NODE:
3937
0
    break;
3938
0
      case XML_ELEMENT_NODE:
3939
    /*
3940
    * REVISIT NOTE: The "fake" is a doc-node, not an element node.
3941
    * REMOVED:
3942
    *   if (xmlStrEqual(node->name, BAD_CAST " fake node libxslt"))
3943
    *    return;
3944
    */
3945
3946
#ifdef WITH_XSLT_DEBUG_PROCESS
3947
    XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
3948
         "xsltCopy: node %s\n", node->name));
3949
#endif
3950
0
    copy = xsltShallowCopyElem(ctxt, node, ctxt->insert, 0);
3951
0
    ctxt->insert = copy;
3952
0
    if (comp->use != NULL) {
3953
0
        xsltApplyAttributeSet(ctxt, node, inst, comp->use);
3954
0
    }
3955
0
    break;
3956
0
      case XML_ATTRIBUTE_NODE: {
3957
#ifdef WITH_XSLT_DEBUG_PROCESS
3958
    XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
3959
         "xsltCopy: attribute %s\n", node->name));
3960
#endif
3961
    /*
3962
    * REVISIT: We could also raise an error if the parent is not
3963
    * an element node.
3964
    * OPTIMIZE TODO: Can we set the value/children of the
3965
    * attribute without an intermediate copy of the string value?
3966
    */
3967
0
    xsltShallowCopyAttr(ctxt, inst, ctxt->insert, (xmlAttrPtr) node);
3968
0
    break;
3969
0
      }
3970
0
      case XML_PI_NODE:
3971
#ifdef WITH_XSLT_DEBUG_PROCESS
3972
    XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
3973
         "xsltCopy: PI %s\n", node->name));
3974
#endif
3975
0
    copy = xmlNewDocPI(ctxt->insert->doc, node->name,
3976
0
                       node->content);
3977
0
    copy = xsltAddChild(ctxt->insert, copy);
3978
0
    break;
3979
0
      case XML_COMMENT_NODE:
3980
#ifdef WITH_XSLT_DEBUG_PROCESS
3981
    XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
3982
         "xsltCopy: comment\n"));
3983
#endif
3984
0
    copy = xmlNewComment(node->content);
3985
0
    copy = xsltAddChild(ctxt->insert, copy);
3986
0
    break;
3987
0
      case XML_NAMESPACE_DECL:
3988
#ifdef WITH_XSLT_DEBUG_PROCESS
3989
    XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
3990
         "xsltCopy: namespace declaration\n"));
3991
#endif
3992
0
    xsltShallowCopyNsNode(ctxt, inst, ctxt->insert, (xmlNsPtr)node);
3993
0
    break;
3994
0
      default:
3995
0
    break;
3996
3997
0
  }
3998
0
    }
3999
4000
0
    switch (node->type) {
4001
0
  case XML_DOCUMENT_NODE:
4002
0
  case XML_HTML_DOCUMENT_NODE:
4003
0
  case XML_ELEMENT_NODE:
4004
0
      xsltApplySequenceConstructor(ctxt, ctxt->node, inst->children,
4005
0
    NULL);
4006
0
      break;
4007
0
  default:
4008
0
      break;
4009
0
    }
4010
0
    ctxt->insert = oldInsert;
4011
0
}
4012
4013
/**
4014
 * xsltText:
4015
 * @ctxt:  a XSLT process context
4016
 * @node:  the node in the source tree.
4017
 * @inst:  the xslt text node
4018
 * @comp:  precomputed information
4019
 *
4020
 * Process the xslt text node on the source node
4021
 */
4022
void
4023
xsltText(xsltTransformContextPtr ctxt, xmlNodePtr node ATTRIBUTE_UNUSED,
4024
0
      xmlNodePtr inst, xsltElemPreCompPtr comp ATTRIBUTE_UNUSED) {
4025
0
    if ((inst->children != NULL) && (comp != NULL)) {
4026
0
  xmlNodePtr text = inst->children;
4027
0
  xmlNodePtr copy;
4028
4029
0
  while (text != NULL) {
4030
0
      if ((text->type != XML_TEXT_NODE) &&
4031
0
           (text->type != XML_CDATA_SECTION_NODE)) {
4032
0
    xsltTransformError(ctxt, NULL, inst,
4033
0
         "xsl:text content problem\n");
4034
0
    break;
4035
0
      }
4036
0
      copy = xmlNewDocText(ctxt->output, text->content);
4037
0
      if (text->type != XML_CDATA_SECTION_NODE) {
4038
#ifdef WITH_XSLT_DEBUG_PARSING
4039
    xsltGenericDebug(xsltGenericDebugContext,
4040
         "Disable escaping: %s\n", text->content);
4041
#endif
4042
0
    copy->name = xmlStringTextNoenc;
4043
0
      }
4044
0
      copy = xsltAddChild(ctxt->insert, copy);
4045
0
      text = text->next;
4046
0
  }
4047
0
    }
4048
0
}
4049
4050
/**
4051
 * xsltElement:
4052
 * @ctxt:  a XSLT process context
4053
 * @node:  the node in the source tree.
4054
 * @inst:  the xslt element node
4055
 * @castedComp:  precomputed information
4056
 *
4057
 * Process the xslt element node on the source node
4058
 */
4059
void
4060
xsltElement(xsltTransformContextPtr ctxt, xmlNodePtr node,
4061
0
      xmlNodePtr inst, xsltElemPreCompPtr castedComp) {
4062
#ifdef XSLT_REFACTORED
4063
    xsltStyleItemElementPtr comp = (xsltStyleItemElementPtr) castedComp;
4064
#else
4065
0
    xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
4066
0
#endif
4067
0
    xmlChar *prop = NULL;
4068
0
    const xmlChar *name, *prefix = NULL, *nsName = NULL;
4069
0
    xmlNodePtr copy;
4070
0
    xmlNodePtr oldInsert;
4071
4072
0
    if (ctxt->insert == NULL)
4073
0
  return;
4074
4075
    /*
4076
    * A comp->has_name == 0 indicates that we need to skip this instruction,
4077
    * since it was evaluated to be invalid already during compilation.
4078
    */
4079
0
    if (!comp->has_name)
4080
0
        return;
4081
4082
    /*
4083
     * stack and saves
4084
     */
4085
0
    oldInsert = ctxt->insert;
4086
4087
0
    if (comp->name == NULL) {
4088
  /* TODO: fix attr acquisition wrt to the XSLT namespace */
4089
0
        prop = xsltEvalAttrValueTemplate(ctxt, inst,
4090
0
      (const xmlChar *) "name", XSLT_NAMESPACE);
4091
0
        if (prop == NULL) {
4092
0
            xsltTransformError(ctxt, NULL, inst,
4093
0
    "xsl:element: The attribute 'name' is missing.\n");
4094
0
            goto error;
4095
0
        }
4096
0
  if (xmlValidateQName(prop, 0)) {
4097
0
      xsltTransformError(ctxt, NULL, inst,
4098
0
    "xsl:element: The effective name '%s' is not a "
4099
0
    "valid QName.\n", prop);
4100
      /* we fall through to catch any further errors, if possible */
4101
0
  }
4102
0
  name = xsltSplitQName(ctxt->dict, prop, &prefix);
4103
0
  xmlFree(prop);
4104
0
    } else {
4105
  /*
4106
  * The "name" value was static.
4107
  */
4108
#ifdef XSLT_REFACTORED
4109
  prefix = comp->nsPrefix;
4110
  name = comp->name;
4111
#else
4112
0
  name = xsltSplitQName(ctxt->dict, comp->name, &prefix);
4113
0
#endif
4114
0
    }
4115
4116
    /*
4117
     * Create the new element
4118
     */
4119
0
    if (ctxt->output->dict == ctxt->dict) {
4120
0
  copy = xmlNewDocNodeEatName(ctxt->output, NULL, (xmlChar *)name, NULL);
4121
0
    } else {
4122
0
  copy = xmlNewDocNode(ctxt->output, NULL, (xmlChar *)name, NULL);
4123
0
    }
4124
0
    if (copy == NULL) {
4125
0
  xsltTransformError(ctxt, NULL, inst,
4126
0
      "xsl:element : creation of %s failed\n", name);
4127
0
  return;
4128
0
    }
4129
0
    copy = xsltAddChild(ctxt->insert, copy);
4130
0
    if (copy == NULL) {
4131
0
        xsltTransformError(ctxt, NULL, inst,
4132
0
            "xsl:element : xsltAddChild failed\n");
4133
0
        return;
4134
0
    }
4135
4136
    /*
4137
    * Namespace
4138
    * ---------
4139
    */
4140
0
    if (comp->has_ns) {
4141
0
  if (comp->ns != NULL) {
4142
      /*
4143
      * No AVT; just plain text for the namespace name.
4144
      */
4145
0
      if (comp->ns[0] != 0)
4146
0
    nsName = comp->ns;
4147
0
  } else {
4148
0
      xmlChar *tmpNsName;
4149
      /*
4150
      * Eval the AVT.
4151
      */
4152
      /* TODO: check attr acquisition wrt to the XSLT namespace */
4153
0
      tmpNsName = xsltEvalAttrValueTemplate(ctxt, inst,
4154
0
    (const xmlChar *) "namespace", XSLT_NAMESPACE);
4155
      /*
4156
      * SPEC XSLT 1.0:
4157
      *  "If the string is empty, then the expanded-name of the
4158
      *  attribute has a null namespace URI."
4159
      */
4160
0
      if ((tmpNsName != NULL) && (tmpNsName[0] != 0))
4161
0
    nsName = xmlDictLookup(ctxt->dict, BAD_CAST tmpNsName, -1);
4162
0
      xmlFree(tmpNsName);
4163
0
  }
4164
4165
0
        if (xmlStrEqual(nsName, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
4166
0
            xsltTransformError(ctxt, NULL, inst,
4167
0
                "xsl:attribute: Namespace http://www.w3.org/2000/xmlns/ "
4168
0
                "forbidden.\n");
4169
0
            goto error;
4170
0
        }
4171
0
        if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
4172
0
            prefix = BAD_CAST "xml";
4173
0
        } else if (xmlStrEqual(prefix, BAD_CAST "xml")) {
4174
0
            prefix = NULL;
4175
0
        }
4176
0
    } else {
4177
0
  xmlNsPtr ns;
4178
  /*
4179
  * SPEC XSLT 1.0:
4180
  *  "If the namespace attribute is not present, then the QName is
4181
  *  expanded into an expanded-name using the namespace declarations
4182
  *  in effect for the xsl:element element, including any default
4183
  *  namespace declaration.
4184
  */
4185
0
  ns = xmlSearchNs(inst->doc, inst, prefix);
4186
0
  if (ns == NULL) {
4187
      /*
4188
      * TODO: Check this in the compilation layer in case it's a
4189
      * static value.
4190
      */
4191
0
            if (prefix != NULL) {
4192
0
                xsltTransformError(ctxt, NULL, inst,
4193
0
                    "xsl:element: The QName '%s:%s' has no "
4194
0
                    "namespace binding in scope in the stylesheet; "
4195
0
                    "this is an error, since the namespace was not "
4196
0
                    "specified by the instruction itself.\n", prefix, name);
4197
0
            }
4198
0
  } else
4199
0
      nsName = ns->href;
4200
0
    }
4201
    /*
4202
    * Find/create a matching ns-decl in the result tree.
4203
    */
4204
0
    if (nsName != NULL) {
4205
0
  if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
4206
            /* Don't use a prefix of "xmlns" */
4207
0
      xmlChar *pref = xmlStrdup(BAD_CAST "ns_1");
4208
4209
0
      copy->ns = xsltGetSpecialNamespace(ctxt, inst, nsName, pref, copy);
4210
4211
0
      xmlFree(pref);
4212
0
  } else {
4213
0
      copy->ns = xsltGetSpecialNamespace(ctxt, inst, nsName, prefix,
4214
0
    copy);
4215
0
  }
4216
0
    } else if ((copy->parent != NULL) &&
4217
0
  (copy->parent->type == XML_ELEMENT_NODE) &&
4218
0
  (copy->parent->ns != NULL))
4219
0
    {
4220
  /*
4221
  * "Undeclare" the default namespace.
4222
  */
4223
0
  xsltGetSpecialNamespace(ctxt, inst, NULL, NULL, copy);
4224
0
    }
4225
4226
0
    ctxt->insert = copy;
4227
4228
0
    if (comp->has_use) {
4229
0
  if (comp->use != NULL) {
4230
0
      xsltApplyAttributeSet(ctxt, node, inst, comp->use);
4231
0
  } else {
4232
0
      xmlChar *attrSets = NULL;
4233
      /*
4234
      * BUG TODO: use-attribute-sets is not a value template.
4235
      *  use-attribute-sets = qnames
4236
      */
4237
0
      attrSets = xsltEvalAttrValueTemplate(ctxt, inst,
4238
0
    (const xmlChar *)"use-attribute-sets", NULL);
4239
0
      if (attrSets != NULL) {
4240
0
    xsltApplyAttributeSet(ctxt, node, inst, attrSets);
4241
0
    xmlFree(attrSets);
4242
0
      }
4243
0
  }
4244
0
    }
4245
    /*
4246
    * Instantiate the sequence constructor.
4247
    */
4248
0
    if (inst->children != NULL)
4249
0
  xsltApplySequenceConstructor(ctxt, ctxt->node, inst->children,
4250
0
      NULL);
4251
4252
0
error:
4253
0
    ctxt->insert = oldInsert;
4254
0
    return;
4255
0
}
4256
4257
4258
/**
4259
 * xsltComment:
4260
 * @ctxt:  a XSLT process context
4261
 * @node:  the node in the source tree.
4262
 * @inst:  the xslt comment node
4263
 * @comp:  precomputed information
4264
 *
4265
 * Process the xslt comment node on the source node
4266
 */
4267
void
4268
xsltComment(xsltTransformContextPtr ctxt, xmlNodePtr node,
4269
0
             xmlNodePtr inst, xsltElemPreCompPtr comp ATTRIBUTE_UNUSED) {
4270
0
    xmlChar *value = NULL;
4271
0
    xmlNodePtr commentNode;
4272
0
    int len;
4273
4274
0
    value = xsltEvalTemplateString(ctxt, node, inst);
4275
    /* TODO: use or generate the compiled form */
4276
0
    len = xmlStrlen(value);
4277
0
    if (len > 0) {
4278
0
        if ((value[len-1] == '-') ||
4279
0
      (xmlStrstr(value, BAD_CAST "--"))) {
4280
0
      xsltTransformError(ctxt, NULL, inst,
4281
0
        "xsl:comment : '--' or ending '-' not allowed in comment\n");
4282
      /* fall through to try to catch further errors */
4283
0
  }
4284
0
    }
4285
#ifdef WITH_XSLT_DEBUG_PROCESS
4286
    if (value == NULL) {
4287
  XSLT_TRACE(ctxt,XSLT_TRACE_COMMENT,xsltGenericDebug(xsltGenericDebugContext,
4288
       "xsltComment: empty\n"));
4289
    } else {
4290
  XSLT_TRACE(ctxt,XSLT_TRACE_COMMENT,xsltGenericDebug(xsltGenericDebugContext,
4291
       "xsltComment: content %s\n", value));
4292
    }
4293
#endif
4294
4295
0
    commentNode = xmlNewComment(value);
4296
0
    commentNode = xsltAddChild(ctxt->insert, commentNode);
4297
4298
0
    if (value != NULL)
4299
0
  xmlFree(value);
4300
0
}
4301
4302
/**
4303
 * xsltProcessingInstruction:
4304
 * @ctxt:  a XSLT process context
4305
 * @node:  the node in the source tree.
4306
 * @inst:  the xslt processing-instruction node
4307
 * @castedComp:  precomputed information
4308
 *
4309
 * Process the xslt processing-instruction node on the source node
4310
 */
4311
void
4312
xsltProcessingInstruction(xsltTransformContextPtr ctxt, xmlNodePtr node,
4313
0
             xmlNodePtr inst, xsltElemPreCompPtr castedComp) {
4314
#ifdef XSLT_REFACTORED
4315
    xsltStyleItemPIPtr comp = (xsltStyleItemPIPtr) castedComp;
4316
#else
4317
0
    xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
4318
0
#endif
4319
0
    const xmlChar *name;
4320
0
    xmlChar *value = NULL;
4321
0
    xmlNodePtr pi;
4322
4323
4324
0
    if (ctxt->insert == NULL)
4325
0
  return;
4326
0
    if (comp->has_name == 0)
4327
0
  return;
4328
0
    if (comp->name == NULL) {
4329
0
  name = xsltEvalAttrValueTemplate(ctxt, inst,
4330
0
          (const xmlChar *)"name", NULL);
4331
0
  if (name == NULL) {
4332
0
      xsltTransformError(ctxt, NULL, inst,
4333
0
     "xsl:processing-instruction : name is missing\n");
4334
0
      goto error;
4335
0
  }
4336
0
    } else {
4337
0
  name = comp->name;
4338
0
    }
4339
    /* TODO: check that it's both an an NCName and a PITarget. */
4340
4341
4342
0
    value = xsltEvalTemplateString(ctxt, node, inst);
4343
0
    if (xmlStrstr(value, BAD_CAST "?>") != NULL) {
4344
0
  xsltTransformError(ctxt, NULL, inst,
4345
0
       "xsl:processing-instruction: '?>' not allowed within PI content\n");
4346
0
  goto error;
4347
0
    }
4348
#ifdef WITH_XSLT_DEBUG_PROCESS
4349
    if (value == NULL) {
4350
  XSLT_TRACE(ctxt,XSLT_TRACE_PI,xsltGenericDebug(xsltGenericDebugContext,
4351
       "xsltProcessingInstruction: %s empty\n", name));
4352
    } else {
4353
  XSLT_TRACE(ctxt,XSLT_TRACE_PI,xsltGenericDebug(xsltGenericDebugContext,
4354
       "xsltProcessingInstruction: %s content %s\n", name, value));
4355
    }
4356
#endif
4357
4358
0
    pi = xmlNewDocPI(ctxt->insert->doc, name, value);
4359
0
    pi = xsltAddChild(ctxt->insert, pi);
4360
4361
0
error:
4362
0
    if ((name != NULL) && (name != comp->name))
4363
0
        xmlFree((xmlChar *) name);
4364
0
    if (value != NULL)
4365
0
  xmlFree(value);
4366
0
}
4367
4368
/**
4369
 * xsltCopyOf:
4370
 * @ctxt:  an XSLT transformation context
4371
 * @node:  the current node in the source tree
4372
 * @inst:  the element node of the XSLT copy-of instruction
4373
 * @castedComp:  precomputed information of the XSLT copy-of instruction
4374
 *
4375
 * Process the XSLT copy-of instruction.
4376
 */
4377
void
4378
xsltCopyOf(xsltTransformContextPtr ctxt, xmlNodePtr node,
4379
0
             xmlNodePtr inst, xsltElemPreCompPtr castedComp) {
4380
#ifdef XSLT_REFACTORED
4381
    xsltStyleItemCopyOfPtr comp = (xsltStyleItemCopyOfPtr) castedComp;
4382
#else
4383
0
    xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
4384
0
#endif
4385
0
    xmlXPathObjectPtr res = NULL;
4386
0
    xmlNodeSetPtr list = NULL;
4387
0
    int i;
4388
4389
0
    if ((ctxt == NULL) || (node == NULL) || (inst == NULL))
4390
0
  return;
4391
0
    if ((comp == NULL) || (comp->select == NULL) || (comp->comp == NULL)) {
4392
0
  xsltTransformError(ctxt, NULL, inst,
4393
0
       "xsl:copy-of : compilation failed\n");
4394
0
  return;
4395
0
    }
4396
4397
     /*
4398
    * SPEC XSLT 1.0:
4399
    *  "The xsl:copy-of element can be used to insert a result tree
4400
    *  fragment into the result tree, without first converting it to
4401
    *  a string as xsl:value-of does (see [7.6.1 Generating Text with
4402
    *  xsl:value-of]). The required select attribute contains an
4403
    *  expression. When the result of evaluating the expression is a
4404
    *  result tree fragment, the complete fragment is copied into the
4405
    *  result tree. When the result is a node-set, all the nodes in the
4406
    *  set are copied in document order into the result tree; copying
4407
    *  an element node copies the attribute nodes, namespace nodes and
4408
    *  children of the element node as well as the element node itself;
4409
    *  a root node is copied by copying its children. When the result
4410
    *  is neither a node-set nor a result tree fragment, the result is
4411
    *  converted to a string and then inserted into the result tree,
4412
    *  as with xsl:value-of.
4413
    */
4414
4415
#ifdef WITH_XSLT_DEBUG_PROCESS
4416
    XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
4417
   "xsltCopyOf: select %s\n", comp->select));
4418
#endif
4419
4420
    /*
4421
    * Evaluate the "select" expression.
4422
    */
4423
0
    res = xsltPreCompEval(ctxt, node, comp);
4424
4425
0
    if (res != NULL) {
4426
0
  if (res->type == XPATH_NODESET) {
4427
      /*
4428
      * Node-set
4429
      * --------
4430
      */
4431
#ifdef WITH_XSLT_DEBUG_PROCESS
4432
      XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
4433
     "xsltCopyOf: result is a node set\n"));
4434
#endif
4435
0
      list = res->nodesetval;
4436
0
      if (list != NULL) {
4437
0
    xmlNodePtr cur;
4438
    /*
4439
    * The list is already sorted in document order by XPath.
4440
    * Append everything in this order under ctxt->insert.
4441
    */
4442
0
    for (i = 0;i < list->nodeNr;i++) {
4443
0
        cur = list->nodeTab[i];
4444
0
        if (cur == NULL)
4445
0
      continue;
4446
0
        if ((cur->type == XML_DOCUMENT_NODE) ||
4447
0
      (cur->type == XML_HTML_DOCUMENT_NODE))
4448
0
        {
4449
0
      xsltCopyTreeList(ctxt, inst,
4450
0
          cur->children, ctxt->insert, 0, 0);
4451
0
        } else if (cur->type == XML_ATTRIBUTE_NODE) {
4452
0
      xsltShallowCopyAttr(ctxt, inst,
4453
0
          ctxt->insert, (xmlAttrPtr) cur);
4454
0
        } else {
4455
0
      xsltCopyTree(ctxt, inst, cur, ctxt->insert, 0, 0);
4456
0
        }
4457
0
    }
4458
0
      }
4459
0
  } else if (res->type == XPATH_XSLT_TREE) {
4460
      /*
4461
      * Result tree fragment
4462
      * --------------------
4463
      * E.g. via <xsl:variable ...><foo/></xsl:variable>
4464
      * Note that the root node of such trees is an xmlDocPtr in Libxslt.
4465
      */
4466
#ifdef WITH_XSLT_DEBUG_PROCESS
4467
      XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
4468
     "xsltCopyOf: result is a result tree fragment\n"));
4469
#endif
4470
0
      list = res->nodesetval;
4471
0
      if ((list != NULL) && (list->nodeTab != NULL) &&
4472
0
    (list->nodeTab[0] != NULL) &&
4473
0
    (IS_XSLT_REAL_NODE(list->nodeTab[0])))
4474
0
      {
4475
0
    xsltCopyTreeList(ctxt, inst,
4476
0
        list->nodeTab[0]->children, ctxt->insert, 0, 0);
4477
0
      }
4478
0
  } else {
4479
0
      xmlChar *value = NULL;
4480
      /*
4481
      * Convert to a string.
4482
      */
4483
0
      value = xmlXPathCastToString(res);
4484
0
      if (value == NULL) {
4485
0
    xsltTransformError(ctxt, NULL, inst,
4486
0
        "Internal error in xsltCopyOf(): "
4487
0
        "failed to cast an XPath object to string.\n");
4488
0
    ctxt->state = XSLT_STATE_STOPPED;
4489
0
      } else {
4490
0
    if (value[0] != 0) {
4491
        /*
4492
        * Append content as text node.
4493
        */
4494
0
        xsltCopyTextString(ctxt, ctxt->insert, value, 0);
4495
0
    }
4496
0
    xmlFree(value);
4497
4498
#ifdef WITH_XSLT_DEBUG_PROCESS
4499
    XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
4500
        "xsltCopyOf: result %s\n", res->stringval));
4501
#endif
4502
0
      }
4503
0
  }
4504
0
    } else {
4505
0
  ctxt->state = XSLT_STATE_STOPPED;
4506
0
    }
4507
4508
0
    if (res != NULL)
4509
0
  xmlXPathFreeObject(res);
4510
0
}
4511
4512
/**
4513
 * xsltValueOf:
4514
 * @ctxt:  a XSLT process context
4515
 * @node:  the node in the source tree.
4516
 * @inst:  the xslt value-of node
4517
 * @castedComp:  precomputed information
4518
 *
4519
 * Process the xslt value-of node on the source node
4520
 */
4521
void
4522
xsltValueOf(xsltTransformContextPtr ctxt, xmlNodePtr node,
4523
             xmlNodePtr inst, xsltElemPreCompPtr castedComp)
4524
0
{
4525
#ifdef XSLT_REFACTORED
4526
    xsltStyleItemValueOfPtr comp = (xsltStyleItemValueOfPtr) castedComp;
4527
#else
4528
0
    xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
4529
0
#endif
4530
0
    xmlXPathObjectPtr res = NULL;
4531
0
    xmlChar *value = NULL;
4532
4533
0
    if ((ctxt == NULL) || (node == NULL) || (inst == NULL))
4534
0
  return;
4535
4536
0
    if ((comp == NULL) || (comp->select == NULL) || (comp->comp == NULL)) {
4537
0
  xsltTransformError(ctxt, NULL, inst,
4538
0
      "Internal error in xsltValueOf(): "
4539
0
      "The XSLT 'value-of' instruction was not compiled.\n");
4540
0
  return;
4541
0
    }
4542
4543
#ifdef WITH_XSLT_DEBUG_PROCESS
4544
    XSLT_TRACE(ctxt,XSLT_TRACE_VALUE_OF,xsltGenericDebug(xsltGenericDebugContext,
4545
   "xsltValueOf: select %s\n", comp->select));
4546
#endif
4547
4548
0
    res = xsltPreCompEval(ctxt, node, comp);
4549
4550
    /*
4551
    * Cast the XPath object to string.
4552
    */
4553
0
    if (res != NULL) {
4554
0
  value = xmlXPathCastToString(res);
4555
0
  if (value == NULL) {
4556
0
      xsltTransformError(ctxt, NULL, inst,
4557
0
    "Internal error in xsltValueOf(): "
4558
0
    "failed to cast an XPath object to string.\n");
4559
0
      ctxt->state = XSLT_STATE_STOPPED;
4560
0
      goto error;
4561
0
  }
4562
0
  if (value[0] != 0) {
4563
0
      xsltCopyTextString(ctxt, ctxt->insert, value, comp->noescape);
4564
0
  }
4565
0
    } else {
4566
0
  xsltTransformError(ctxt, NULL, inst,
4567
0
      "XPath evaluation returned no result.\n");
4568
0
  ctxt->state = XSLT_STATE_STOPPED;
4569
0
  goto error;
4570
0
    }
4571
4572
#ifdef WITH_XSLT_DEBUG_PROCESS
4573
    if (value) {
4574
  XSLT_TRACE(ctxt,XSLT_TRACE_VALUE_OF,xsltGenericDebug(xsltGenericDebugContext,
4575
       "xsltValueOf: result '%s'\n", value));
4576
    }
4577
#endif
4578
4579
0
error:
4580
0
    if (value != NULL)
4581
0
  xmlFree(value);
4582
0
    if (res != NULL)
4583
0
  xmlXPathFreeObject(res);
4584
0
}
4585
4586
/**
4587
 * xsltNumber:
4588
 * @ctxt:  a XSLT process context
4589
 * @node:  the node in the source tree.
4590
 * @inst:  the xslt number node
4591
 * @castedComp:  precomputed information
4592
 *
4593
 * Process the xslt number node on the source node
4594
 */
4595
void
4596
xsltNumber(xsltTransformContextPtr ctxt, xmlNodePtr node,
4597
     xmlNodePtr inst, xsltElemPreCompPtr castedComp)
4598
0
{
4599
#ifdef XSLT_REFACTORED
4600
    xsltStyleItemNumberPtr comp = (xsltStyleItemNumberPtr) castedComp;
4601
#else
4602
0
    xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
4603
0
#endif
4604
0
    xmlXPathContextPtr xpctxt;
4605
0
    xmlNsPtr *oldXPNamespaces;
4606
0
    int oldXPNsNr;
4607
4608
0
    if (comp == NULL) {
4609
0
  xsltTransformError(ctxt, NULL, inst,
4610
0
       "xsl:number : compilation failed\n");
4611
0
  return;
4612
0
    }
4613
4614
0
    if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL))
4615
0
  return;
4616
4617
0
    comp->numdata.doc = inst->doc;
4618
0
    comp->numdata.node = inst;
4619
4620
0
    xpctxt = ctxt->xpathCtxt;
4621
0
    oldXPNsNr = xpctxt->nsNr;
4622
0
    oldXPNamespaces = xpctxt->namespaces;
4623
4624
#ifdef XSLT_REFACTORED
4625
    if (comp->inScopeNs != NULL) {
4626
        xpctxt->namespaces = comp->inScopeNs->list;
4627
        xpctxt->nsNr = comp->inScopeNs->xpathNumber;
4628
    } else {
4629
        xpctxt->namespaces = NULL;
4630
        xpctxt->nsNr = 0;
4631
    }
4632
#else
4633
0
    xpctxt->namespaces = comp->nsList;
4634
0
    xpctxt->nsNr = comp->nsNr;
4635
0
#endif
4636
4637
0
    xsltNumberFormat(ctxt, &comp->numdata, node);
4638
4639
0
    xpctxt->nsNr = oldXPNsNr;
4640
0
    xpctxt->namespaces = oldXPNamespaces;
4641
0
}
4642
4643
/**
4644
 * xsltApplyImports:
4645
 * @ctxt:  an XSLT transformation context
4646
 * @contextNode:  the current node in the source tree.
4647
 * @inst:  the element node of the XSLT 'apply-imports' instruction
4648
 * @comp:  the compiled instruction
4649
 *
4650
 * Process the XSLT apply-imports element.
4651
 */
4652
void
4653
xsltApplyImports(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
4654
           xmlNodePtr inst,
4655
     xsltElemPreCompPtr comp ATTRIBUTE_UNUSED)
4656
0
{
4657
0
    xsltTemplatePtr templ;
4658
4659
0
    if ((ctxt == NULL) || (inst == NULL))
4660
0
  return;
4661
4662
0
    if (comp == NULL) {
4663
0
  xsltTransformError(ctxt, NULL, inst,
4664
0
      "Internal error in xsltApplyImports(): "
4665
0
      "The XSLT 'apply-imports' instruction was not compiled.\n");
4666
0
  return;
4667
0
    }
4668
    /*
4669
    * NOTE that ctxt->currentTemplateRule and ctxt->templ is not the
4670
    * same; the former is the "Current Template Rule" as defined by the
4671
    * XSLT spec, the latter is simply the template struct being
4672
    * currently processed.
4673
    */
4674
0
    if (ctxt->currentTemplateRule == NULL) {
4675
  /*
4676
  * SPEC XSLT 2.0:
4677
  * "[ERR XTDE0560] It is a non-recoverable dynamic error if
4678
  *  xsl:apply-imports or xsl:next-match is evaluated when the
4679
  *  current template rule is null."
4680
  */
4681
0
  xsltTransformError(ctxt, NULL, inst,
4682
0
       "It is an error to call 'apply-imports' "
4683
0
       "when there's no current template rule.\n");
4684
0
  return;
4685
0
    }
4686
    /*
4687
    * TODO: Check if this is correct.
4688
    */
4689
0
    templ = xsltGetTemplate(ctxt, contextNode,
4690
0
  ctxt->currentTemplateRule->style);
4691
4692
0
    if (templ != NULL) {
4693
0
  xsltTemplatePtr oldCurTemplRule = ctxt->currentTemplateRule;
4694
  /*
4695
  * Set the current template rule.
4696
  */
4697
0
  ctxt->currentTemplateRule = templ;
4698
  /*
4699
  * URGENT TODO: Need xsl:with-param be handled somehow here?
4700
  */
4701
0
  xsltApplyXSLTTemplate(ctxt, contextNode, templ->content,
4702
0
      templ, NULL);
4703
4704
0
  ctxt->currentTemplateRule = oldCurTemplRule;
4705
0
    }
4706
0
    else {
4707
        /* Use built-in templates. */
4708
0
        xsltDefaultProcessOneNode(ctxt, contextNode, NULL);
4709
0
    }
4710
0
}
4711
4712
/**
4713
 * xsltCallTemplate:
4714
 * @ctxt:  a XSLT transformation context
4715
 * @node:  the "current node" in the source tree
4716
 * @inst:  the XSLT 'call-template' instruction
4717
 * @castedComp:  the compiled information of the instruction
4718
 *
4719
 * Processes the XSLT call-template instruction on the source node.
4720
 */
4721
void
4722
xsltCallTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
4723
             xmlNodePtr inst, xsltElemPreCompPtr castedComp)
4724
0
{
4725
#ifdef XSLT_REFACTORED
4726
    xsltStyleItemCallTemplatePtr comp =
4727
  (xsltStyleItemCallTemplatePtr) castedComp;
4728
#else
4729
0
    xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
4730
0
#endif
4731
0
    xsltStackElemPtr withParams = NULL;
4732
4733
0
    if (ctxt->insert == NULL)
4734
0
  return;
4735
0
    if (comp == NULL) {
4736
0
  xsltTransformError(ctxt, NULL, inst,
4737
0
       "The XSLT 'call-template' instruction was not compiled.\n");
4738
0
  return;
4739
0
    }
4740
4741
    /*
4742
     * The template must have been precomputed
4743
     */
4744
0
    if (comp->templ == NULL) {
4745
0
  comp->templ = xsltFindTemplate(ctxt, comp->name, comp->ns);
4746
0
  if (comp->templ == NULL) {
4747
0
      if (comp->ns != NULL) {
4748
0
          xsltTransformError(ctxt, NULL, inst,
4749
0
      "The called template '{%s}%s' was not found.\n",
4750
0
      comp->ns, comp->name);
4751
0
      } else {
4752
0
          xsltTransformError(ctxt, NULL, inst,
4753
0
      "The called template '%s' was not found.\n",
4754
0
      comp->name);
4755
0
      }
4756
0
      return;
4757
0
  }
4758
0
    }
4759
4760
#ifdef WITH_XSLT_DEBUG_PROCESS
4761
    if ((comp != NULL) && (comp->name != NULL))
4762
  XSLT_TRACE(ctxt,XSLT_TRACE_CALL_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
4763
       "call-template: name %s\n", comp->name));
4764
#endif
4765
4766
0
    if (inst->children) {
4767
0
  xmlNodePtr cur;
4768
0
  xsltStackElemPtr param;
4769
4770
0
  cur = inst->children;
4771
0
  while (cur != NULL) {
4772
#ifdef WITH_DEBUGGER
4773
      if (ctxt->debugStatus != XSLT_DEBUG_NONE)
4774
    xslHandleDebugger(cur, node, comp->templ, ctxt);
4775
#endif
4776
0
      if (ctxt->state == XSLT_STATE_STOPPED) break;
4777
      /*
4778
      * TODO: The "with-param"s could be part of the "call-template"
4779
      *   structure. Avoid to "search" for params dynamically
4780
      *   in the XML tree every time.
4781
      */
4782
0
      if (IS_XSLT_ELEM(cur)) {
4783
0
    if (IS_XSLT_NAME(cur, "with-param")) {
4784
0
        param = xsltParseStylesheetCallerParam(ctxt, cur);
4785
0
        if (param != NULL) {
4786
0
      param->next = withParams;
4787
0
      withParams = param;
4788
0
        }
4789
0
    } else {
4790
0
        xsltGenericError(xsltGenericErrorContext,
4791
0
      "xsl:call-template: misplaced xsl:%s\n", cur->name);
4792
0
    }
4793
0
      } else {
4794
0
    xsltGenericError(xsltGenericErrorContext,
4795
0
        "xsl:call-template: misplaced %s element\n", cur->name);
4796
0
      }
4797
0
      cur = cur->next;
4798
0
  }
4799
0
    }
4800
    /*
4801
     * Create a new frame using the params first
4802
     */
4803
0
    xsltApplyXSLTTemplate(ctxt, node, comp->templ->content, comp->templ,
4804
0
  withParams);
4805
0
    if (withParams != NULL)
4806
0
  xsltFreeStackElemList(withParams);
4807
4808
#ifdef WITH_XSLT_DEBUG_PROCESS
4809
    if ((comp != NULL) && (comp->name != NULL))
4810
  XSLT_TRACE(ctxt,XSLT_TRACE_CALL_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
4811
       "call-template returned: name %s\n", comp->name));
4812
#endif
4813
0
}
4814
4815
/**
4816
 * xsltApplyTemplates:
4817
 * @ctxt:  a XSLT transformation context
4818
 * @node:  the 'current node' in the source tree
4819
 * @inst:  the element node of an XSLT 'apply-templates' instruction
4820
 * @castedComp:  the compiled instruction
4821
 *
4822
 * Processes the XSLT 'apply-templates' instruction on the current node.
4823
 */
4824
void
4825
xsltApplyTemplates(xsltTransformContextPtr ctxt, xmlNodePtr node,
4826
             xmlNodePtr inst, xsltElemPreCompPtr castedComp)
4827
0
{
4828
#ifdef XSLT_REFACTORED
4829
    xsltStyleItemApplyTemplatesPtr comp =
4830
  (xsltStyleItemApplyTemplatesPtr) castedComp;
4831
#else
4832
0
    xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
4833
0
#endif
4834
0
    int i;
4835
0
    xmlNodePtr cur, oldContextNode;
4836
0
    xmlNodeSetPtr list = NULL, oldList;
4837
0
    xsltStackElemPtr withParams = NULL;
4838
0
    int oldXPProximityPosition, oldXPContextSize;
4839
0
    const xmlChar *oldMode, *oldModeURI;
4840
0
    xmlDocPtr oldXPDoc;
4841
0
    xsltDocumentPtr oldDocInfo;
4842
0
    xmlXPathContextPtr xpctxt;
4843
4844
0
    if (comp == NULL) {
4845
0
  xsltTransformError(ctxt, NULL, inst,
4846
0
       "xsl:apply-templates : compilation failed\n");
4847
0
  return;
4848
0
    }
4849
0
    if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL))
4850
0
  return;
4851
4852
#ifdef WITH_XSLT_DEBUG_PROCESS
4853
    if ((node != NULL) && (node->name != NULL))
4854
  XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
4855
       "xsltApplyTemplates: node: '%s'\n", node->name));
4856
#endif
4857
4858
0
    xpctxt = ctxt->xpathCtxt;
4859
    /*
4860
    * Save context states.
4861
    */
4862
0
    oldContextNode = ctxt->node;
4863
0
    oldMode = ctxt->mode;
4864
0
    oldModeURI = ctxt->modeURI;
4865
0
    oldDocInfo = ctxt->document;
4866
0
    oldList = ctxt->nodeList;
4867
4868
    /*
4869
     * The xpath context size and proximity position, as
4870
     * well as the xpath and context documents, may be changed
4871
     * so we save their initial state and will restore on exit
4872
     */
4873
0
    oldXPContextSize = xpctxt->contextSize;
4874
0
    oldXPProximityPosition = xpctxt->proximityPosition;
4875
0
    oldXPDoc = xpctxt->doc;
4876
4877
    /*
4878
    * Set up contexts.
4879
    */
4880
0
    ctxt->mode = comp->mode;
4881
0
    ctxt->modeURI = comp->modeURI;
4882
4883
0
    if (comp->select != NULL) {
4884
0
  xmlXPathObjectPtr res = NULL;
4885
4886
0
  if (comp->comp == NULL) {
4887
0
      xsltTransformError(ctxt, NULL, inst,
4888
0
     "xsl:apply-templates : compilation failed\n");
4889
0
      goto error;
4890
0
  }
4891
#ifdef WITH_XSLT_DEBUG_PROCESS
4892
  XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
4893
       "xsltApplyTemplates: select %s\n", comp->select));
4894
#endif
4895
4896
0
  res = xsltPreCompEval(ctxt, node, comp);
4897
4898
0
  if (res != NULL) {
4899
0
      if (res->type == XPATH_NODESET) {
4900
0
    list = res->nodesetval; /* consume the node set */
4901
0
    res->nodesetval = NULL;
4902
0
      } else {
4903
0
    xsltTransformError(ctxt, NULL, inst,
4904
0
        "The 'select' expression did not evaluate to a "
4905
0
        "node set.\n");
4906
0
    ctxt->state = XSLT_STATE_STOPPED;
4907
0
    xmlXPathFreeObject(res);
4908
0
    goto error;
4909
0
      }
4910
0
      xmlXPathFreeObject(res);
4911
      /*
4912
      * Note: An xsl:apply-templates with a 'select' attribute,
4913
      * can change the current source doc.
4914
      */
4915
0
  } else {
4916
0
      xsltTransformError(ctxt, NULL, inst,
4917
0
    "Failed to evaluate the 'select' expression.\n");
4918
0
      ctxt->state = XSLT_STATE_STOPPED;
4919
0
      goto error;
4920
0
  }
4921
0
  if (list == NULL) {
4922
#ifdef WITH_XSLT_DEBUG_PROCESS
4923
      XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
4924
    "xsltApplyTemplates: select didn't evaluate to a node list\n"));
4925
#endif
4926
0
      goto exit;
4927
0
  }
4928
  /*
4929
  *
4930
  * NOTE: Previously a document info (xsltDocument) was
4931
  * created and attached to the Result Tree Fragment.
4932
  * But such a document info is created on demand in
4933
  * xsltKeyFunction() (functions.c), so we need to create
4934
  * it here beforehand.
4935
  * In order to take care of potential keys we need to
4936
  * do some extra work for the case when a Result Tree Fragment
4937
  * is converted into a nodeset (e.g. exslt:node-set()) :
4938
  * We attach a "pseudo-doc" (xsltDocument) to _private.
4939
  * This xsltDocument, together with the keyset, will be freed
4940
  * when the Result Tree Fragment is freed.
4941
  *
4942
  */
4943
#if 0
4944
  if ((ctxt->nbKeys > 0) &&
4945
      (list->nodeNr != 0) &&
4946
      (list->nodeTab[0]->doc != NULL) &&
4947
      XSLT_IS_RES_TREE_FRAG(list->nodeTab[0]->doc))
4948
  {
4949
      /*
4950
      * NOTE that it's also OK if @effectiveDocInfo will be
4951
      * set to NULL.
4952
      */
4953
      isRTF = 1;
4954
      effectiveDocInfo = list->nodeTab[0]->doc->_private;
4955
  }
4956
#endif
4957
0
    } else {
4958
  /*
4959
   * Build an XPath node set with the children
4960
   */
4961
0
  list = xmlXPathNodeSetCreate(NULL);
4962
0
  if (list == NULL)
4963
0
      goto error;
4964
0
  if (node->type != XML_NAMESPACE_DECL)
4965
0
      cur = node->children;
4966
0
  else
4967
0
      cur = NULL;
4968
0
  while (cur != NULL) {
4969
0
            if (IS_XSLT_REAL_NODE(cur))
4970
0
    xmlXPathNodeSetAddUnique(list, cur);
4971
0
      cur = cur->next;
4972
0
  }
4973
0
    }
4974
4975
#ifdef WITH_XSLT_DEBUG_PROCESS
4976
    if (list != NULL)
4977
    XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
4978
  "xsltApplyTemplates: list of %d nodes\n", list->nodeNr));
4979
#endif
4980
4981
0
    if ((list == NULL) || (list->nodeNr == 0))
4982
0
  goto exit;
4983
4984
    /*
4985
    * Set the context's node set and size; this is also needed for
4986
    * for xsltDoSortFunction().
4987
    */
4988
0
    ctxt->nodeList = list;
4989
    /*
4990
    * Process xsl:with-param and xsl:sort instructions.
4991
    * (The code became so verbose just to avoid the
4992
    *  xmlNodePtr sorts[XSLT_MAX_SORT] if there's no xsl:sort)
4993
    * BUG TODO: We are not using namespaced potentially defined on the
4994
    * xsl:sort or xsl:with-param elements; XPath expression might fail.
4995
    */
4996
0
    if (inst->children) {
4997
0
  xsltStackElemPtr param;
4998
4999
0
  cur = inst->children;
5000
0
  while (cur) {
5001
5002
#ifdef WITH_DEBUGGER
5003
      if (ctxt->debugStatus != XSLT_DEBUG_NONE)
5004
    xslHandleDebugger(cur, node, NULL, ctxt);
5005
#endif
5006
0
      if (ctxt->state == XSLT_STATE_STOPPED)
5007
0
    break;
5008
0
      if (cur->type == XML_TEXT_NODE) {
5009
0
    cur = cur->next;
5010
0
    continue;
5011
0
      }
5012
0
      if (! IS_XSLT_ELEM(cur))
5013
0
    break;
5014
0
      if (IS_XSLT_NAME(cur, "with-param")) {
5015
0
    param = xsltParseStylesheetCallerParam(ctxt, cur);
5016
0
    if (param != NULL) {
5017
0
        param->next = withParams;
5018
0
        withParams = param;
5019
0
    }
5020
0
      }
5021
0
      if (IS_XSLT_NAME(cur, "sort")) {
5022
0
    xsltTemplatePtr oldCurTempRule =
5023
0
        ctxt->currentTemplateRule;
5024
0
    int nbsorts = 0;
5025
0
    xmlNodePtr sorts[XSLT_MAX_SORT];
5026
5027
0
    sorts[nbsorts++] = cur;
5028
0
    cur = cur->next;
5029
5030
0
    while (cur) {
5031
5032
#ifdef WITH_DEBUGGER
5033
        if (ctxt->debugStatus != XSLT_DEBUG_NONE)
5034
      xslHandleDebugger(cur, node, NULL, ctxt);
5035
#endif
5036
0
        if (ctxt->state == XSLT_STATE_STOPPED)
5037
0
      break;
5038
5039
0
        if (cur->type == XML_TEXT_NODE) {
5040
0
      cur = cur->next;
5041
0
      continue;
5042
0
        }
5043
5044
0
        if (! IS_XSLT_ELEM(cur))
5045
0
      break;
5046
0
        if (IS_XSLT_NAME(cur, "with-param")) {
5047
0
      param = xsltParseStylesheetCallerParam(ctxt, cur);
5048
0
      if (param != NULL) {
5049
0
          param->next = withParams;
5050
0
          withParams = param;
5051
0
      }
5052
0
        }
5053
0
        if (IS_XSLT_NAME(cur, "sort")) {
5054
0
      if (nbsorts >= XSLT_MAX_SORT) {
5055
0
          xsltTransformError(ctxt, NULL, cur,
5056
0
        "The number (%d) of xsl:sort instructions exceeds the "
5057
0
        "maximum allowed by this processor's settings.\n",
5058
0
        nbsorts);
5059
0
          ctxt->state = XSLT_STATE_STOPPED;
5060
0
          break;
5061
0
      } else {
5062
0
          sorts[nbsorts++] = cur;
5063
0
      }
5064
0
        }
5065
0
        cur = cur->next;
5066
0
    }
5067
    /*
5068
    * The "current template rule" is cleared for xsl:sort.
5069
    */
5070
0
    ctxt->currentTemplateRule = NULL;
5071
    /*
5072
    * Sort.
5073
    */
5074
0
    xsltDoSortFunction(ctxt, sorts, nbsorts);
5075
0
    ctxt->currentTemplateRule = oldCurTempRule;
5076
0
    break;
5077
0
      }
5078
0
      cur = cur->next;
5079
0
  }
5080
0
    }
5081
0
    xpctxt->contextSize = list->nodeNr;
5082
    /*
5083
    * Apply templates for all selected source nodes.
5084
    */
5085
0
    for (i = 0; i < list->nodeNr; i++) {
5086
0
  cur = list->nodeTab[i];
5087
  /*
5088
  * The node becomes the "current node".
5089
  */
5090
0
  ctxt->node = cur;
5091
  /*
5092
  * An xsl:apply-templates can change the current context doc.
5093
  * OPTIMIZE TODO: Get rid of the need to set the context doc.
5094
  */
5095
0
  if ((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL))
5096
0
      xpctxt->doc = cur->doc;
5097
5098
0
  xpctxt->proximityPosition = i + 1;
5099
  /*
5100
  * Find and apply a template for this node.
5101
  */
5102
0
  xsltProcessOneNode(ctxt, cur, withParams);
5103
0
    }
5104
5105
0
exit:
5106
0
error:
5107
    /*
5108
    * Free the parameter list.
5109
    */
5110
0
    if (withParams != NULL)
5111
0
  xsltFreeStackElemList(withParams);
5112
0
    if (list != NULL)
5113
0
  xmlXPathFreeNodeSet(list);
5114
    /*
5115
    * Restore context states.
5116
    */
5117
0
    xpctxt->doc = oldXPDoc;
5118
0
    xpctxt->contextSize = oldXPContextSize;
5119
0
    xpctxt->proximityPosition = oldXPProximityPosition;
5120
5121
0
    ctxt->document = oldDocInfo;
5122
0
    ctxt->nodeList = oldList;
5123
0
    ctxt->node = oldContextNode;
5124
0
    ctxt->mode = oldMode;
5125
0
    ctxt->modeURI = oldModeURI;
5126
0
}
5127
5128
5129
/**
5130
 * xsltChoose:
5131
 * @ctxt:  a XSLT process context
5132
 * @contextNode:  the current node in the source tree
5133
 * @inst:  the xsl:choose instruction
5134
 * @comp:  compiled information of the instruction
5135
 *
5136
 * Processes the xsl:choose instruction on the source node.
5137
 */
5138
void
5139
xsltChoose(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
5140
     xmlNodePtr inst, xsltElemPreCompPtr comp ATTRIBUTE_UNUSED)
5141
0
{
5142
0
    xmlNodePtr cur;
5143
5144
0
    if ((ctxt == NULL) || (contextNode == NULL) || (inst == NULL))
5145
0
  return;
5146
5147
    /*
5148
    * TODO: Content model checks should be done only at compilation
5149
    * time.
5150
    */
5151
0
    cur = inst->children;
5152
0
    if (cur == NULL) {
5153
0
  xsltTransformError(ctxt, NULL, inst,
5154
0
      "xsl:choose: The instruction has no content.\n");
5155
0
  return;
5156
0
    }
5157
5158
#ifdef XSLT_REFACTORED
5159
    /*
5160
    * We don't check the content model during transformation.
5161
    */
5162
#else
5163
0
    if ((! IS_XSLT_ELEM(cur)) || (! IS_XSLT_NAME(cur, "when"))) {
5164
0
  xsltTransformError(ctxt, NULL, inst,
5165
0
       "xsl:choose: xsl:when expected first\n");
5166
0
  return;
5167
0
    }
5168
0
#endif
5169
5170
0
    {
5171
0
  int testRes = 0, res = 0;
5172
5173
#ifdef XSLT_REFACTORED
5174
  xsltStyleItemWhenPtr wcomp = NULL;
5175
#else
5176
0
  xsltStylePreCompPtr wcomp = NULL;
5177
0
#endif
5178
5179
  /*
5180
  * Process xsl:when ---------------------------------------------------
5181
  */
5182
0
  while (IS_XSLT_ELEM(cur) && IS_XSLT_NAME(cur, "when")) {
5183
0
      wcomp = cur->psvi;
5184
5185
0
      if ((wcomp == NULL) || (wcomp->test == NULL) ||
5186
0
    (wcomp->comp == NULL))
5187
0
      {
5188
0
    xsltTransformError(ctxt, NULL, cur,
5189
0
        "Internal error in xsltChoose(): "
5190
0
        "The XSLT 'when' instruction was not compiled.\n");
5191
0
    goto error;
5192
0
      }
5193
5194
5195
#ifdef WITH_DEBUGGER
5196
      if (xslDebugStatus != XSLT_DEBUG_NONE) {
5197
    /*
5198
    * TODO: Isn't comp->templ always NULL for xsl:choose?
5199
    */
5200
    xslHandleDebugger(cur, contextNode, NULL, ctxt);
5201
      }
5202
#endif
5203
#ifdef WITH_XSLT_DEBUG_PROCESS
5204
      XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
5205
    "xsltChoose: test %s\n", wcomp->test));
5206
#endif
5207
5208
0
#ifdef XSLT_FAST_IF
5209
0
      res = xsltPreCompEvalToBoolean(ctxt, contextNode, wcomp);
5210
5211
0
      if (res == -1) {
5212
0
    ctxt->state = XSLT_STATE_STOPPED;
5213
0
    goto error;
5214
0
      }
5215
0
      testRes = (res == 1) ? 1 : 0;
5216
5217
#else /* XSLT_FAST_IF */
5218
5219
      res = xsltPreCompEval(ctxt, cotextNode, wcomp);
5220
5221
      if (res != NULL) {
5222
    if (res->type != XPATH_BOOLEAN)
5223
        res = xmlXPathConvertBoolean(res);
5224
    if (res->type == XPATH_BOOLEAN)
5225
        testRes = res->boolval;
5226
    else {
5227
#ifdef WITH_XSLT_DEBUG_PROCESS
5228
        XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
5229
      "xsltChoose: test didn't evaluate to a boolean\n"));
5230
#endif
5231
        goto error;
5232
    }
5233
    xmlXPathFreeObject(res);
5234
    res = NULL;
5235
      } else {
5236
    ctxt->state = XSLT_STATE_STOPPED;
5237
    goto error;
5238
      }
5239
5240
#endif /* else of XSLT_FAST_IF */
5241
5242
#ifdef WITH_XSLT_DEBUG_PROCESS
5243
      XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
5244
    "xsltChoose: test evaluate to %d\n", testRes));
5245
#endif
5246
0
      if (testRes)
5247
0
    goto test_is_true;
5248
5249
0
      cur = cur->next;
5250
0
  }
5251
5252
  /*
5253
  * Process xsl:otherwise ----------------------------------------------
5254
  */
5255
0
  if (IS_XSLT_ELEM(cur) && IS_XSLT_NAME(cur, "otherwise")) {
5256
5257
#ifdef WITH_DEBUGGER
5258
      if (xslDebugStatus != XSLT_DEBUG_NONE)
5259
    xslHandleDebugger(cur, contextNode, NULL, ctxt);
5260
#endif
5261
5262
#ifdef WITH_XSLT_DEBUG_PROCESS
5263
      XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
5264
    "evaluating xsl:otherwise\n"));
5265
#endif
5266
0
      goto test_is_true;
5267
0
  }
5268
0
  goto exit;
5269
5270
0
test_is_true:
5271
5272
0
  goto process_sequence;
5273
0
    }
5274
5275
0
process_sequence:
5276
5277
    /*
5278
    * Instantiate the sequence constructor.
5279
    */
5280
0
    xsltApplySequenceConstructor(ctxt, ctxt->node, cur->children,
5281
0
  NULL);
5282
5283
0
exit:
5284
0
error:
5285
0
    return;
5286
0
}
5287
5288
/**
5289
 * xsltIf:
5290
 * @ctxt:  a XSLT process context
5291
 * @contextNode:  the current node in the source tree
5292
 * @inst:  the xsl:if instruction
5293
 * @castedComp:  compiled information of the instruction
5294
 *
5295
 * Processes the xsl:if instruction on the source node.
5296
 */
5297
void
5298
xsltIf(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
5299
             xmlNodePtr inst, xsltElemPreCompPtr castedComp)
5300
0
{
5301
0
    int res = 0;
5302
5303
#ifdef XSLT_REFACTORED
5304
    xsltStyleItemIfPtr comp = (xsltStyleItemIfPtr) castedComp;
5305
#else
5306
0
    xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
5307
0
#endif
5308
5309
0
    if ((ctxt == NULL) || (contextNode == NULL) || (inst == NULL))
5310
0
  return;
5311
0
    if ((comp == NULL) || (comp->test == NULL) || (comp->comp == NULL)) {
5312
0
  xsltTransformError(ctxt, NULL, inst,
5313
0
      "Internal error in xsltIf(): "
5314
0
      "The XSLT 'if' instruction was not compiled.\n");
5315
0
  return;
5316
0
    }
5317
5318
#ifdef WITH_XSLT_DEBUG_PROCESS
5319
    XSLT_TRACE(ctxt,XSLT_TRACE_IF,xsltGenericDebug(xsltGenericDebugContext,
5320
   "xsltIf: test %s\n", comp->test));
5321
#endif
5322
5323
0
#ifdef XSLT_FAST_IF
5324
0
    {
5325
0
  xmlDocPtr oldLocalFragmentTop = ctxt->localRVT;
5326
5327
0
  res = xsltPreCompEvalToBoolean(ctxt, contextNode, comp);
5328
5329
  /*
5330
  * Cleanup fragments created during evaluation of the
5331
  * "select" expression.
5332
  */
5333
0
  if (oldLocalFragmentTop != ctxt->localRVT)
5334
0
      xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
5335
0
    }
5336
5337
#ifdef WITH_XSLT_DEBUG_PROCESS
5338
    XSLT_TRACE(ctxt,XSLT_TRACE_IF,xsltGenericDebug(xsltGenericDebugContext,
5339
  "xsltIf: test evaluate to %d\n", res));
5340
#endif
5341
5342
0
    if (res == -1) {
5343
0
  ctxt->state = XSLT_STATE_STOPPED;
5344
0
  goto error;
5345
0
    }
5346
0
    if (res == 1) {
5347
  /*
5348
  * Instantiate the sequence constructor of xsl:if.
5349
  */
5350
0
  xsltApplySequenceConstructor(ctxt,
5351
0
      contextNode, inst->children, NULL);
5352
0
    }
5353
5354
#else /* XSLT_FAST_IF */
5355
    {
5356
  /*
5357
  * OLD CODE:
5358
  */
5359
  xmlXPathObjectPtr xpobj = xsltPreCompEval(ctxt, contextNode, comp);
5360
  if (xpobj != NULL) {
5361
      if (xpobj->type != XPATH_BOOLEAN)
5362
    xpobj = xmlXPathConvertBoolean(xpobj);
5363
      if (xpobj->type == XPATH_BOOLEAN) {
5364
    res = xpobj->boolval;
5365
5366
#ifdef WITH_XSLT_DEBUG_PROCESS
5367
    XSLT_TRACE(ctxt,XSLT_TRACE_IF,xsltGenericDebug(xsltGenericDebugContext,
5368
        "xsltIf: test evaluate to %d\n", res));
5369
#endif
5370
    if (res) {
5371
        xsltApplySequenceConstructor(ctxt,
5372
      contextNode, inst->children, NULL);
5373
    }
5374
      } else {
5375
5376
#ifdef WITH_XSLT_DEBUG_PROCESS
5377
    XSLT_TRACE(ctxt, XSLT_TRACE_IF,
5378
        xsltGenericDebug(xsltGenericDebugContext,
5379
        "xsltIf: test didn't evaluate to a boolean\n"));
5380
#endif
5381
    ctxt->state = XSLT_STATE_STOPPED;
5382
      }
5383
      xmlXPathFreeObject(xpobj);
5384
  } else {
5385
      ctxt->state = XSLT_STATE_STOPPED;
5386
  }
5387
    }
5388
#endif /* else of XSLT_FAST_IF */
5389
5390
0
error:
5391
0
    return;
5392
0
}
5393
5394
/**
5395
 * xsltForEach:
5396
 * @ctxt:  an XSLT transformation context
5397
 * @contextNode:  the "current node" in the source tree
5398
 * @inst:  the element node of the xsl:for-each instruction
5399
 * @castedComp:  the compiled information of the instruction
5400
 *
5401
 * Process the xslt for-each node on the source node
5402
 */
5403
void
5404
xsltForEach(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
5405
      xmlNodePtr inst, xsltElemPreCompPtr castedComp)
5406
0
{
5407
#ifdef XSLT_REFACTORED
5408
    xsltStyleItemForEachPtr comp = (xsltStyleItemForEachPtr) castedComp;
5409
#else
5410
0
    xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
5411
0
#endif
5412
0
    int i;
5413
0
    xmlXPathObjectPtr res = NULL;
5414
0
    xmlNodePtr cur, curInst;
5415
0
    xmlNodeSetPtr list = NULL;
5416
0
    xmlNodeSetPtr oldList;
5417
0
    int oldXPProximityPosition, oldXPContextSize;
5418
0
    xmlNodePtr oldContextNode;
5419
0
    xsltTemplatePtr oldCurTemplRule;
5420
0
    xmlDocPtr oldXPDoc;
5421
0
    xsltDocumentPtr oldDocInfo;
5422
0
    xmlXPathContextPtr xpctxt;
5423
5424
0
    if ((ctxt == NULL) || (contextNode == NULL) || (inst == NULL)) {
5425
0
  xsltGenericError(xsltGenericErrorContext,
5426
0
      "xsltForEach(): Bad arguments.\n");
5427
0
  return;
5428
0
    }
5429
5430
0
    if (comp == NULL) {
5431
0
        xsltTransformError(ctxt, NULL, inst,
5432
0
      "Internal error in xsltForEach(): "
5433
0
      "The XSLT 'for-each' instruction was not compiled.\n");
5434
0
        return;
5435
0
    }
5436
0
    if ((comp->select == NULL) || (comp->comp == NULL)) {
5437
0
  xsltTransformError(ctxt, NULL, inst,
5438
0
      "Internal error in xsltForEach(): "
5439
0
      "The selecting expression of the XSLT 'for-each' "
5440
0
      "instruction was not compiled correctly.\n");
5441
0
  return;
5442
0
    }
5443
0
    xpctxt = ctxt->xpathCtxt;
5444
5445
#ifdef WITH_XSLT_DEBUG_PROCESS
5446
    XSLT_TRACE(ctxt,XSLT_TRACE_FOR_EACH,xsltGenericDebug(xsltGenericDebugContext,
5447
   "xsltForEach: select %s\n", comp->select));
5448
#endif
5449
5450
    /*
5451
    * Save context states.
5452
    */
5453
0
    oldDocInfo = ctxt->document;
5454
0
    oldList = ctxt->nodeList;
5455
0
    oldContextNode = ctxt->node;
5456
    /*
5457
    * The "current template rule" is cleared for the instantiation of
5458
    * xsl:for-each.
5459
    */
5460
0
    oldCurTemplRule = ctxt->currentTemplateRule;
5461
0
    ctxt->currentTemplateRule = NULL;
5462
5463
0
    oldXPDoc = xpctxt->doc;
5464
0
    oldXPProximityPosition = xpctxt->proximityPosition;
5465
0
    oldXPContextSize = xpctxt->contextSize;
5466
5467
    /*
5468
    * Evaluate the 'select' expression.
5469
    */
5470
0
    res = xsltPreCompEval(ctxt, contextNode, comp);
5471
5472
0
    if (res != NULL) {
5473
0
  if (res->type == XPATH_NODESET)
5474
0
      list = res->nodesetval;
5475
0
  else {
5476
0
      xsltTransformError(ctxt, NULL, inst,
5477
0
    "The 'select' expression does not evaluate to a node set.\n");
5478
5479
#ifdef WITH_XSLT_DEBUG_PROCESS
5480
      XSLT_TRACE(ctxt,XSLT_TRACE_FOR_EACH,xsltGenericDebug(xsltGenericDebugContext,
5481
    "xsltForEach: select didn't evaluate to a node list\n"));
5482
#endif
5483
0
      goto error;
5484
0
  }
5485
0
    } else {
5486
0
  xsltTransformError(ctxt, NULL, inst,
5487
0
      "Failed to evaluate the 'select' expression.\n");
5488
0
  ctxt->state = XSLT_STATE_STOPPED;
5489
0
  goto error;
5490
0
    }
5491
5492
0
    if ((list == NULL) || (list->nodeNr <= 0))
5493
0
  goto exit;
5494
5495
#ifdef WITH_XSLT_DEBUG_PROCESS
5496
    XSLT_TRACE(ctxt,XSLT_TRACE_FOR_EACH,xsltGenericDebug(xsltGenericDebugContext,
5497
  "xsltForEach: select evaluates to %d nodes\n", list->nodeNr));
5498
#endif
5499
5500
    /*
5501
    * Set the list; this has to be done already here for xsltDoSortFunction().
5502
    */
5503
0
    ctxt->nodeList = list;
5504
    /*
5505
    * Handle xsl:sort instructions and skip them for further processing.
5506
    * BUG TODO: We are not using namespaced potentially defined on the
5507
    * xsl:sort element; XPath expression might fail.
5508
    */
5509
0
    curInst = inst->children;
5510
0
    if (IS_XSLT_ELEM(curInst) && IS_XSLT_NAME(curInst, "sort")) {
5511
0
  int nbsorts = 0;
5512
0
  xmlNodePtr sorts[XSLT_MAX_SORT];
5513
5514
0
  sorts[nbsorts++] = curInst;
5515
5516
#ifdef WITH_DEBUGGER
5517
  if (xslDebugStatus != XSLT_DEBUG_NONE)
5518
      xslHandleDebugger(curInst, contextNode, NULL, ctxt);
5519
#endif
5520
5521
0
  curInst = curInst->next;
5522
0
  while (IS_XSLT_ELEM(curInst) && IS_XSLT_NAME(curInst, "sort")) {
5523
0
      if (nbsorts >= XSLT_MAX_SORT) {
5524
0
    xsltTransformError(ctxt, NULL, curInst,
5525
0
        "The number of xsl:sort instructions exceeds the "
5526
0
        "maximum (%d) allowed by this processor.\n",
5527
0
        XSLT_MAX_SORT);
5528
0
    goto error;
5529
0
      } else {
5530
0
    sorts[nbsorts++] = curInst;
5531
0
      }
5532
5533
#ifdef WITH_DEBUGGER
5534
      if (xslDebugStatus != XSLT_DEBUG_NONE)
5535
    xslHandleDebugger(curInst, contextNode, NULL, ctxt);
5536
#endif
5537
0
      curInst = curInst->next;
5538
0
  }
5539
0
  xsltDoSortFunction(ctxt, sorts, nbsorts);
5540
0
    }
5541
0
    xpctxt->contextSize = list->nodeNr;
5542
    /*
5543
    * Instantiate the sequence constructor for each selected node.
5544
    */
5545
0
    for (i = 0; i < list->nodeNr; i++) {
5546
0
  cur = list->nodeTab[i];
5547
  /*
5548
  * The selected node becomes the "current node".
5549
  */
5550
0
  ctxt->node = cur;
5551
  /*
5552
  * An xsl:for-each can change the current context doc.
5553
  * OPTIMIZE TODO: Get rid of the need to set the context doc.
5554
  */
5555
0
  if ((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL))
5556
0
      xpctxt->doc = cur->doc;
5557
5558
0
  xpctxt->proximityPosition = i + 1;
5559
5560
0
  xsltApplySequenceConstructor(ctxt, cur, curInst, NULL);
5561
0
    }
5562
5563
0
exit:
5564
0
error:
5565
0
    if (res != NULL)
5566
0
  xmlXPathFreeObject(res);
5567
    /*
5568
    * Restore old states.
5569
    */
5570
0
    ctxt->document = oldDocInfo;
5571
0
    ctxt->nodeList = oldList;
5572
0
    ctxt->node = oldContextNode;
5573
0
    ctxt->currentTemplateRule = oldCurTemplRule;
5574
5575
0
    xpctxt->doc = oldXPDoc;
5576
0
    xpctxt->contextSize = oldXPContextSize;
5577
0
    xpctxt->proximityPosition = oldXPProximityPosition;
5578
0
}
5579
5580
/************************************************************************
5581
 *                  *
5582
 *      Generic interface       *
5583
 *                  *
5584
 ************************************************************************/
5585
5586
#ifdef XSLT_GENERATE_HTML_DOCTYPE
5587
typedef struct xsltHTMLVersion {
5588
    const char *version;
5589
    const char *public;
5590
    const char *system;
5591
} xsltHTMLVersion;
5592
5593
static xsltHTMLVersion xsltHTMLVersions[] = {
5594
    { "5", NULL, "about:legacy-compat" },
5595
    { "4.01frame", "-//W3C//DTD HTML 4.01 Frameset//EN",
5596
      "http://www.w3.org/TR/1999/REC-html401-19991224/frameset.dtd"},
5597
    { "4.01strict", "-//W3C//DTD HTML 4.01//EN",
5598
      "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd"},
5599
    { "4.01trans", "-//W3C//DTD HTML 4.01 Transitional//EN",
5600
      "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"},
5601
    { "4.01", "-//W3C//DTD HTML 4.01 Transitional//EN",
5602
      "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"},
5603
    { "4.0strict", "-//W3C//DTD HTML 4.01//EN",
5604
      "http://www.w3.org/TR/html4/strict.dtd"},
5605
    { "4.0trans", "-//W3C//DTD HTML 4.01 Transitional//EN",
5606
      "http://www.w3.org/TR/html4/loose.dtd"},
5607
    { "4.0frame", "-//W3C//DTD HTML 4.01 Frameset//EN",
5608
      "http://www.w3.org/TR/html4/frameset.dtd"},
5609
    { "4.0", "-//W3C//DTD HTML 4.01 Transitional//EN",
5610
      "http://www.w3.org/TR/html4/loose.dtd"},
5611
    { "3.2", "-//W3C//DTD HTML 3.2//EN", NULL }
5612
};
5613
5614
/**
5615
 * xsltGetHTMLIDs:
5616
 * @version:  the version string
5617
 * @publicID:  used to return the public ID
5618
 * @systemID:  used to return the system ID
5619
 *
5620
 * Returns -1 if not found, 0 otherwise and the system and public
5621
 *         Identifier for this given verion of HTML
5622
 */
5623
static int
5624
xsltGetHTMLIDs(const xmlChar *version, const xmlChar **publicID,
5625
0
              const xmlChar **systemID) {
5626
0
    unsigned int i;
5627
0
    if (version == NULL)
5628
0
  return(-1);
5629
0
    for (i = 0;i < (sizeof(xsltHTMLVersions)/sizeof(xsltHTMLVersions[1]));
5630
0
   i++) {
5631
0
  if (!xmlStrcasecmp(version,
5632
0
               (const xmlChar *) xsltHTMLVersions[i].version)) {
5633
0
      if (publicID != NULL)
5634
0
    *publicID = (const xmlChar *) xsltHTMLVersions[i].public;
5635
0
      if (systemID != NULL)
5636
0
    *systemID = (const xmlChar *) xsltHTMLVersions[i].system;
5637
0
      return(0);
5638
0
  }
5639
0
    }
5640
0
    return(-1);
5641
0
}
5642
#endif
5643
5644
/**
5645
 * xsltApplyStripSpaces:
5646
 * @ctxt:  a XSLT process context
5647
 * @node:  the root of the XML tree
5648
 *
5649
 * Strip the unwanted ignorable spaces from the input tree
5650
 */
5651
void
5652
0
xsltApplyStripSpaces(xsltTransformContextPtr ctxt, xmlNodePtr node) {
5653
0
    xmlNodePtr current;
5654
#ifdef WITH_XSLT_DEBUG_PROCESS
5655
    int nb = 0;
5656
#endif
5657
5658
5659
0
    current = node;
5660
0
    while (current != NULL) {
5661
  /*
5662
   * Cleanup children empty nodes if asked for
5663
   */
5664
0
  if ((IS_XSLT_REAL_NODE(current)) &&
5665
0
      (current->children != NULL) &&
5666
0
      (xsltFindElemSpaceHandling(ctxt, current))) {
5667
0
      xmlNodePtr delete = NULL, cur = current->children;
5668
5669
0
      while (cur != NULL) {
5670
0
    if (IS_BLANK_NODE(cur))
5671
0
        delete = cur;
5672
5673
0
    cur = cur->next;
5674
0
    if (delete != NULL) {
5675
0
        xmlUnlinkNode(delete);
5676
0
        xmlFreeNode(delete);
5677
0
        delete = NULL;
5678
#ifdef WITH_XSLT_DEBUG_PROCESS
5679
        nb++;
5680
#endif
5681
0
    }
5682
0
      }
5683
0
  }
5684
5685
  /*
5686
   * Skip to next node in document order.
5687
   */
5688
0
  if (node->type == XML_ENTITY_REF_NODE) {
5689
      /* process deep in entities */
5690
0
      xsltApplyStripSpaces(ctxt, node->children);
5691
0
  }
5692
0
  if ((current->children != NULL) &&
5693
0
            (current->type != XML_ENTITY_REF_NODE)) {
5694
0
      current = current->children;
5695
0
  } else if (current->next != NULL) {
5696
0
      current = current->next;
5697
0
  } else {
5698
0
      do {
5699
0
    current = current->parent;
5700
0
    if (current == NULL)
5701
0
        break;
5702
0
    if (current == node)
5703
0
        goto done;
5704
0
    if (current->next != NULL) {
5705
0
        current = current->next;
5706
0
        break;
5707
0
    }
5708
0
      } while (current != NULL);
5709
0
  }
5710
0
    }
5711
5712
0
done:
5713
#ifdef WITH_XSLT_DEBUG_PROCESS
5714
    XSLT_TRACE(ctxt,XSLT_TRACE_STRIP_SPACES,xsltGenericDebug(xsltGenericDebugContext,
5715
       "xsltApplyStripSpaces: removed %d ignorable blank node\n", nb));
5716
#endif
5717
0
    return;
5718
0
}
5719
5720
static int
5721
xsltCountKeys(xsltTransformContextPtr ctxt)
5722
0
{
5723
0
    xsltStylesheetPtr style;
5724
0
    xsltKeyDefPtr keyd;
5725
5726
0
    if (ctxt == NULL)
5727
0
  return(-1);
5728
5729
    /*
5730
    * Do we have those nastly templates with a key() in the match pattern?
5731
    */
5732
0
    ctxt->hasTemplKeyPatterns = 0;
5733
0
    style = ctxt->style;
5734
0
    while (style != NULL) {
5735
0
  if (style->keyMatch != NULL) {
5736
0
      ctxt->hasTemplKeyPatterns = 1;
5737
0
      break;
5738
0
  }
5739
0
  style = xsltNextImport(style);
5740
0
    }
5741
    /*
5742
    * Count number of key declarations.
5743
    */
5744
0
    ctxt->nbKeys = 0;
5745
0
    style = ctxt->style;
5746
0
    while (style != NULL) {
5747
0
  keyd = style->keys;
5748
0
  while (keyd) {
5749
0
      ctxt->nbKeys++;
5750
0
      keyd = keyd->next;
5751
0
  }
5752
0
  style = xsltNextImport(style);
5753
0
    }
5754
0
    return(ctxt->nbKeys);
5755
0
}
5756
5757
/**
5758
 * xsltCleanupSourceDoc:
5759
 * @doc:  Document
5760
 *
5761
 * Resets source node flags and ids stored in 'psvi' member.
5762
 */
5763
void
5764
0
xsltCleanupSourceDoc(xmlDocPtr doc) {
5765
0
    xmlNodePtr cur = (xmlNodePtr) doc;
5766
0
    void **psviPtr;
5767
5768
0
    while (1) {
5769
0
        xsltClearSourceNodeFlags(cur, XSLT_SOURCE_NODE_MASK);
5770
0
        psviPtr = xsltGetPSVIPtr(cur);
5771
0
        if (psviPtr)
5772
0
            *psviPtr = NULL;
5773
5774
0
        if (cur->type == XML_ELEMENT_NODE) {
5775
0
            xmlAttrPtr prop = cur->properties;
5776
5777
0
            while (prop) {
5778
0
                prop->atype &= ~(XSLT_SOURCE_NODE_MASK << 27);
5779
0
                prop->psvi = NULL;
5780
0
                prop = prop->next;
5781
0
            }
5782
0
        }
5783
5784
0
        if (cur->children != NULL && cur->type != XML_ENTITY_REF_NODE) {
5785
0
            cur = cur->children;
5786
0
        } else {
5787
0
            if (cur == (xmlNodePtr) doc)
5788
0
                return;
5789
0
            while (cur->next == NULL) {
5790
0
                cur = cur->parent;
5791
0
                if (cur == (xmlNodePtr) doc)
5792
0
                    return;
5793
0
            }
5794
5795
0
            cur = cur->next;
5796
0
        }
5797
0
    }
5798
0
}
5799
5800
/**
5801
 * xsltApplyStylesheetInternal:
5802
 * @style:  a parsed XSLT stylesheet
5803
 * @doc:  a parsed XML document
5804
 * @params:  a NULL terminated array of parameters names/values tuples
5805
 * @output:  the targetted output
5806
 * @profile:  profile FILE * output or NULL
5807
 * @user:  user provided parameter
5808
 *
5809
 * Apply the stylesheet to the document
5810
 * NOTE: This may lead to a non-wellformed output XML wise !
5811
 *
5812
 * Returns the result document or NULL in case of error
5813
 */
5814
static xmlDocPtr
5815
xsltApplyStylesheetInternal(xsltStylesheetPtr style, xmlDocPtr doc,
5816
                            const char **params, const char *output,
5817
                            FILE * profile, xsltTransformContextPtr userCtxt)
5818
0
{
5819
0
    xmlDocPtr res = NULL;
5820
0
    xsltTransformContextPtr ctxt = NULL;
5821
0
    xmlNodePtr root, node;
5822
0
    const xmlChar *method;
5823
0
    const xmlChar *doctypePublic;
5824
0
    const xmlChar *doctypeSystem;
5825
0
    const xmlChar *version;
5826
0
    const xmlChar *encoding;
5827
0
    xsltStackElemPtr variables;
5828
0
    xsltStackElemPtr vptr;
5829
5830
0
    xsltInitGlobals();
5831
5832
0
    if ((style == NULL) || (doc == NULL))
5833
0
        return (NULL);
5834
5835
0
    if (style->internalized == 0) {
5836
#ifdef WITH_XSLT_DEBUG
5837
  xsltGenericDebug(xsltGenericDebugContext,
5838
       "Stylesheet was not fully internalized !\n");
5839
#endif
5840
0
    }
5841
0
    if (doc->intSubset != NULL) {
5842
  /*
5843
   * Avoid hitting the DTD when scanning nodes
5844
   * but keep it linked as doc->intSubset
5845
   */
5846
0
  xmlNodePtr cur = (xmlNodePtr) doc->intSubset;
5847
0
  if (cur->next != NULL)
5848
0
      cur->next->prev = cur->prev;
5849
0
  if (cur->prev != NULL)
5850
0
      cur->prev->next = cur->next;
5851
0
  if (doc->children == cur)
5852
0
      doc->children = cur->next;
5853
0
  if (doc->last == cur)
5854
0
      doc->last = cur->prev;
5855
0
  cur->prev = cur->next = NULL;
5856
0
    }
5857
5858
    /*
5859
     * Check for XPath document order availability
5860
     */
5861
0
    root = xmlDocGetRootElement(doc);
5862
0
    if (root != NULL) {
5863
0
  if (((ptrdiff_t) root->content >= 0) &&
5864
0
            (xslDebugStatus == XSLT_DEBUG_NONE))
5865
0
      xmlXPathOrderDocElems(doc);
5866
0
    }
5867
5868
0
    if (userCtxt != NULL)
5869
0
  ctxt = userCtxt;
5870
0
    else
5871
0
  ctxt = xsltNewTransformContext(style, doc);
5872
5873
0
    if (ctxt == NULL)
5874
0
        return (NULL);
5875
5876
0
    ctxt->initialContextDoc = doc;
5877
0
    ctxt->initialContextNode = (xmlNodePtr) doc;
5878
5879
0
    if (profile != NULL) {
5880
#ifdef WITH_PROFILER
5881
        ctxt->profile = 1;
5882
#else
5883
0
        xsltTransformError(ctxt, NULL, (xmlNodePtr) doc,
5884
0
                "xsltApplyStylesheetInternal: "
5885
0
                "libxslt compiled without profiler\n");
5886
0
        goto error;
5887
0
#endif
5888
0
    }
5889
5890
0
    if (output != NULL)
5891
0
        ctxt->outputFile = output;
5892
0
    else
5893
0
        ctxt->outputFile = NULL;
5894
5895
    /*
5896
     * internalize the modes if needed
5897
     */
5898
0
    if (ctxt->dict != NULL) {
5899
0
        if (ctxt->mode != NULL)
5900
0
      ctxt->mode = xmlDictLookup(ctxt->dict, ctxt->mode, -1);
5901
0
        if (ctxt->modeURI != NULL)
5902
0
      ctxt->modeURI = xmlDictLookup(ctxt->dict, ctxt->modeURI, -1);
5903
0
    }
5904
5905
0
    XSLT_GET_IMPORT_PTR(method, style, method)
5906
0
    XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
5907
0
    XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
5908
0
    XSLT_GET_IMPORT_PTR(version, style, version)
5909
0
    XSLT_GET_IMPORT_PTR(encoding, style, encoding)
5910
5911
0
    if ((method != NULL) &&
5912
0
  (!xmlStrEqual(method, (const xmlChar *) "xml")))
5913
0
    {
5914
0
        if (xmlStrEqual(method, (const xmlChar *) "html")) {
5915
0
            ctxt->type = XSLT_OUTPUT_HTML;
5916
0
            if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
5917
0
                res = htmlNewDoc(doctypeSystem, doctypePublic);
5918
0
      } else {
5919
0
                if (version == NULL) {
5920
0
        xmlDtdPtr dtd;
5921
5922
0
        res = htmlNewDoc(NULL, NULL);
5923
        /*
5924
        * Make sure no DTD node is generated in this case
5925
        */
5926
0
        if (res != NULL) {
5927
0
      dtd = xmlGetIntSubset(res);
5928
0
      if (dtd != NULL) {
5929
0
          xmlUnlinkNode((xmlNodePtr) dtd);
5930
0
          xmlFreeDtd(dtd);
5931
0
      }
5932
0
      res->intSubset = NULL;
5933
0
      res->extSubset = NULL;
5934
0
        }
5935
0
    } else {
5936
5937
0
#ifdef XSLT_GENERATE_HTML_DOCTYPE
5938
0
        xsltGetHTMLIDs(version, &doctypePublic, &doctypeSystem);
5939
0
#endif
5940
0
        res = htmlNewDoc(doctypeSystem, doctypePublic);
5941
0
    }
5942
0
            }
5943
0
            if (res == NULL)
5944
0
                goto error;
5945
0
      res->dict = ctxt->dict;
5946
0
      xmlDictReference(res->dict);
5947
5948
#ifdef WITH_XSLT_DEBUG
5949
      xsltGenericDebug(xsltGenericDebugContext,
5950
    "reusing transformation dict for output\n");
5951
#endif
5952
0
        } else if (xmlStrEqual(method, (const xmlChar *) "xhtml")) {
5953
0
      xsltTransformError(ctxt, NULL, (xmlNodePtr) doc,
5954
0
    "xsltApplyStylesheetInternal: unsupported method xhtml, using html\n");
5955
0
            ctxt->type = XSLT_OUTPUT_HTML;
5956
0
            res = htmlNewDoc(doctypeSystem, doctypePublic);
5957
0
            if (res == NULL)
5958
0
                goto error;
5959
0
      res->dict = ctxt->dict;
5960
0
      xmlDictReference(res->dict);
5961
5962
#ifdef WITH_XSLT_DEBUG
5963
      xsltGenericDebug(xsltGenericDebugContext,
5964
    "reusing transformation dict for output\n");
5965
#endif
5966
0
        } else if (xmlStrEqual(method, (const xmlChar *) "text")) {
5967
0
            ctxt->type = XSLT_OUTPUT_TEXT;
5968
0
            res = xmlNewDoc(style->version);
5969
0
            if (res == NULL)
5970
0
                goto error;
5971
0
      res->dict = ctxt->dict;
5972
0
      xmlDictReference(res->dict);
5973
5974
#ifdef WITH_XSLT_DEBUG
5975
      xsltGenericDebug(xsltGenericDebugContext,
5976
    "reusing transformation dict for output\n");
5977
#endif
5978
0
        } else {
5979
0
      xsltTransformError(ctxt, NULL, (xmlNodePtr) doc,
5980
0
    "xsltApplyStylesheetInternal: unsupported method (%s)\n",
5981
0
    method);
5982
0
            goto error;
5983
0
        }
5984
0
    } else {
5985
0
        ctxt->type = XSLT_OUTPUT_XML;
5986
0
        res = xmlNewDoc(style->version);
5987
0
        if (res == NULL)
5988
0
            goto error;
5989
0
  res->dict = ctxt->dict;
5990
0
  xmlDictReference(ctxt->dict);
5991
#ifdef WITH_XSLT_DEBUG
5992
  xsltGenericDebug(xsltGenericDebugContext,
5993
       "reusing transformation dict for output\n");
5994
#endif
5995
0
    }
5996
0
    res->charset = XML_CHAR_ENCODING_UTF8;
5997
0
    if (encoding != NULL)
5998
0
        res->encoding = xmlStrdup(encoding);
5999
0
    variables = style->variables;
6000
6001
0
    ctxt->node = (xmlNodePtr) doc;
6002
0
    ctxt->output = res;
6003
6004
0
    ctxt->xpathCtxt->contextSize = 1;
6005
0
    ctxt->xpathCtxt->proximityPosition = 1;
6006
0
    ctxt->xpathCtxt->node = NULL; /* TODO: Set the context node here? */
6007
6008
    /*
6009
     * Start the evaluation, evaluate the params, the stylesheets globals
6010
     * and start by processing the top node.
6011
     */
6012
0
    if (xsltNeedElemSpaceHandling(ctxt))
6013
0
  xsltApplyStripSpaces(ctxt, xmlDocGetRootElement(doc));
6014
    /*
6015
    * Evaluate global params and user-provided params.
6016
    */
6017
0
    if (ctxt->globalVars == NULL)
6018
0
  ctxt->globalVars = xmlHashCreate(20);
6019
0
    if (params != NULL) {
6020
0
        xsltEvalUserParams(ctxt, params);
6021
0
    }
6022
6023
    /* need to be called before evaluating global variables */
6024
0
    xsltCountKeys(ctxt);
6025
6026
0
    xsltEvalGlobalVariables(ctxt);
6027
6028
    /* Clean up any unused RVTs. */
6029
0
    xsltReleaseLocalRVTs(ctxt, NULL);
6030
6031
0
    ctxt->insert = (xmlNodePtr) res;
6032
0
    ctxt->varsBase = ctxt->varsNr - 1;
6033
6034
    /*
6035
    * Start processing the source tree -----------------------------------
6036
    */
6037
0
    xsltProcessOneNode(ctxt, ctxt->node, NULL);
6038
    /*
6039
    * Remove all remaining vars from the stack.
6040
    */
6041
0
    xsltLocalVariablePop(ctxt, 0, -2);
6042
0
    xsltShutdownCtxtExts(ctxt);
6043
6044
0
    xsltCleanupTemplates(style); /* TODO: <- style should be read only */
6045
6046
    /*
6047
     * Now cleanup our variables so stylesheet can be re-used
6048
     *
6049
     * TODO: this is not needed anymore global variables are copied
6050
     *       and not evaluated directly anymore, keep this as a check
6051
     */
6052
0
    if (style->variables != variables) {
6053
0
        vptr = style->variables;
6054
0
        while (vptr->next != variables)
6055
0
            vptr = vptr->next;
6056
0
        vptr->next = NULL;
6057
0
        xsltFreeStackElemList(style->variables);
6058
0
        style->variables = variables;
6059
0
    }
6060
0
    vptr = style->variables;
6061
0
    while (vptr != NULL) {
6062
0
        if (vptr->computed) {
6063
0
            if (vptr->value != NULL) {
6064
0
                xmlXPathFreeObject(vptr->value);
6065
0
                vptr->value = NULL;
6066
0
                vptr->computed = 0;
6067
0
            }
6068
0
        }
6069
0
        vptr = vptr->next;
6070
0
    }
6071
#if 0
6072
    /*
6073
     * code disabled by wmb; awaiting kb's review
6074
     * problem is that global variable(s) may contain xpath objects
6075
     * from doc associated with RVT, so can't be freed at this point.
6076
     * xsltFreeTransformContext includes a call to xsltFreeRVTs, so
6077
     * I assume this shouldn't be required at this point.
6078
     */
6079
    /*
6080
    * Free all remaining tree fragments.
6081
    */
6082
    xsltFreeRVTs(ctxt);
6083
#endif
6084
    /*
6085
     * Do some post processing work depending on the generated output
6086
     */
6087
0
    root = xmlDocGetRootElement(res);
6088
0
    if (root != NULL) {
6089
0
        const xmlChar *doctype = NULL;
6090
6091
0
        if ((root->ns != NULL) && (root->ns->prefix != NULL))
6092
0
      doctype = xmlDictQLookup(ctxt->dict, root->ns->prefix, root->name);
6093
0
  if (doctype == NULL)
6094
0
      doctype = root->name;
6095
6096
        /*
6097
         * Apply the default selection of the method
6098
         */
6099
0
        if ((method == NULL) &&
6100
0
            (root->ns == NULL) &&
6101
0
            (!xmlStrcasecmp(root->name, (const xmlChar *) "html"))) {
6102
0
            xmlNodePtr tmp;
6103
6104
0
            tmp = res->children;
6105
0
            while ((tmp != NULL) && (tmp != root)) {
6106
0
                if (tmp->type == XML_ELEMENT_NODE)
6107
0
                    break;
6108
0
                if ((tmp->type == XML_TEXT_NODE) && (!xmlIsBlankNode(tmp)))
6109
0
                    break;
6110
0
    tmp = tmp->next;
6111
0
            }
6112
0
            if (tmp == root) {
6113
0
                ctxt->type = XSLT_OUTPUT_HTML;
6114
    /*
6115
    * REVISIT TODO: XML_HTML_DOCUMENT_NODE is set after the
6116
    *  transformation on the doc, but functions like
6117
    */
6118
0
                res->type = XML_HTML_DOCUMENT_NODE;
6119
0
                if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
6120
0
                    res->intSubset = xmlCreateIntSubset(res, doctype,
6121
0
                                                        doctypePublic,
6122
0
                                                        doctypeSystem);
6123
0
#ifdef XSLT_GENERATE_HTML_DOCTYPE
6124
0
    } else if (version != NULL) {
6125
0
                    xsltGetHTMLIDs(version, &doctypePublic,
6126
0
                                   &doctypeSystem);
6127
0
                    if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
6128
0
                        res->intSubset =
6129
0
                            xmlCreateIntSubset(res, doctype,
6130
0
                                               doctypePublic,
6131
0
                                               doctypeSystem);
6132
0
#endif
6133
0
                }
6134
0
            }
6135
6136
0
        }
6137
0
        if (ctxt->type == XSLT_OUTPUT_XML) {
6138
0
            XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
6139
0
            XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
6140
0
            if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
6141
0
          xmlNodePtr last;
6142
    /* Need a small "hack" here to assure DTD comes before
6143
       possible comment nodes */
6144
0
    node = res->children;
6145
0
    last = res->last;
6146
0
    res->children = NULL;
6147
0
    res->last = NULL;
6148
0
                res->intSubset = xmlCreateIntSubset(res, doctype,
6149
0
                                                    doctypePublic,
6150
0
                                                    doctypeSystem);
6151
0
    if (res->children != NULL) {
6152
0
        res->children->next = node;
6153
0
        node->prev = res->children;
6154
0
        res->last = last;
6155
0
    } else {
6156
0
        res->children = node;
6157
0
        res->last = last;
6158
0
    }
6159
0
      }
6160
0
        }
6161
0
    }
6162
0
    xmlXPathFreeNodeSet(ctxt->nodeList);
6163
6164
#ifdef WITH_PROFILER
6165
    if (profile != NULL) {
6166
        xsltSaveProfiling(ctxt, profile);
6167
    }
6168
#endif
6169
6170
    /*
6171
     * Be pedantic.
6172
     */
6173
0
    if ((ctxt != NULL) && (ctxt->state != XSLT_STATE_OK)) {
6174
0
  xmlFreeDoc(res);
6175
0
  res = NULL;
6176
0
    }
6177
0
    if ((res != NULL) && (ctxt != NULL) && (output != NULL)) {
6178
0
  int ret;
6179
6180
0
  ret = xsltCheckWrite(ctxt->sec, ctxt, (const xmlChar *) output);
6181
0
  if (ret == 0) {
6182
0
      xsltTransformError(ctxt, NULL, NULL,
6183
0
         "xsltApplyStylesheet: forbidden to save to %s\n",
6184
0
             output);
6185
0
  } else if (ret < 0) {
6186
0
      xsltTransformError(ctxt, NULL, NULL,
6187
0
         "xsltApplyStylesheet: saving to %s may not be possible\n",
6188
0
             output);
6189
0
  }
6190
0
    }
6191
6192
#ifdef XSLT_DEBUG_PROFILE_CACHE
6193
    printf("# Cache:\n");
6194
    printf("# Reused tree fragments: %d\n", ctxt->cache->dbgReusedRVTs);
6195
    printf("# Reused variables     : %d\n", ctxt->cache->dbgReusedVars);
6196
#endif
6197
6198
0
    if (ctxt->sourceDocDirty)
6199
0
        xsltCleanupSourceDoc(doc);
6200
6201
0
    if ((ctxt != NULL) && (userCtxt == NULL))
6202
0
  xsltFreeTransformContext(ctxt);
6203
6204
0
    return (res);
6205
6206
0
error:
6207
0
    if (res != NULL)
6208
0
        xmlFreeDoc(res);
6209
6210
#ifdef XSLT_DEBUG_PROFILE_CACHE
6211
    printf("# Cache:\n");
6212
    printf("# Reused tree fragments: %d\n", ctxt->cache->dbgReusedRVTs);
6213
    printf("# Reused variables     : %d\n", ctxt->cache->dbgReusedVars);
6214
#endif
6215
6216
0
    if ((ctxt != NULL) && (userCtxt == NULL))
6217
0
        xsltFreeTransformContext(ctxt);
6218
0
    return (NULL);
6219
0
}
6220
6221
/**
6222
 * xsltApplyStylesheet:
6223
 * @style:  a parsed XSLT stylesheet
6224
 * @doc:  a parsed XML document
6225
 * @params:  a NULL terminated arry of parameters names/values tuples
6226
 *
6227
 * Apply the stylesheet to the document
6228
 * NOTE: This may lead to a non-wellformed output XML wise !
6229
 *
6230
 * Returns the result document or NULL in case of error
6231
 */
6232
xmlDocPtr
6233
xsltApplyStylesheet(xsltStylesheetPtr style, xmlDocPtr doc,
6234
                    const char **params)
6235
0
{
6236
0
    return (xsltApplyStylesheetInternal(style, doc, params, NULL, NULL, NULL));
6237
0
}
6238
6239
/**
6240
 * xsltProfileStylesheet:
6241
 * @style:  a parsed XSLT stylesheet
6242
 * @doc:  a parsed XML document
6243
 * @params:  a NULL terminated arry of parameters names/values tuples
6244
 * @output:  a FILE * for the profiling output
6245
 *
6246
 * Apply the stylesheet to the document and dump the profiling to
6247
 * the given output.
6248
 *
6249
 * Returns the result document or NULL in case of error
6250
 */
6251
xmlDocPtr
6252
xsltProfileStylesheet(xsltStylesheetPtr style, xmlDocPtr doc,
6253
                      const char **params, FILE * output)
6254
0
{
6255
0
    xmlDocPtr res;
6256
6257
0
    res = xsltApplyStylesheetInternal(style, doc, params, NULL, output, NULL);
6258
0
    return (res);
6259
0
}
6260
6261
/**
6262
 * xsltApplyStylesheetUser:
6263
 * @style:  a parsed XSLT stylesheet
6264
 * @doc:  a parsed XML document
6265
 * @params:  a NULL terminated array of parameters names/values tuples
6266
 * @output:  the targetted output
6267
 * @profile:  profile FILE * output or NULL
6268
 * @userCtxt:  user provided transform context
6269
 *
6270
 * Apply the stylesheet to the document and allow the user to provide
6271
 * its own transformation context.
6272
 *
6273
 * Returns the result document or NULL in case of error
6274
 */
6275
xmlDocPtr
6276
xsltApplyStylesheetUser(xsltStylesheetPtr style, xmlDocPtr doc,
6277
                            const char **params, const char *output,
6278
                            FILE * profile, xsltTransformContextPtr userCtxt)
6279
0
{
6280
0
    xmlDocPtr res;
6281
6282
0
    res = xsltApplyStylesheetInternal(style, doc, params, output,
6283
0
                                profile, userCtxt);
6284
0
    return (res);
6285
0
}
6286
6287
/**
6288
 * xsltRunStylesheetUser:
6289
 * @style:  a parsed XSLT stylesheet
6290
 * @doc:  a parsed XML document
6291
 * @params:  a NULL terminated array of parameters names/values tuples
6292
 * @output:  the URL/filename ot the generated resource if available
6293
 * @SAX:  a SAX handler for progressive callback output (not implemented yet)
6294
 * @IObuf:  an output buffer for progressive output (not implemented yet)
6295
 * @profile:  profile FILE * output or NULL
6296
 * @userCtxt:  user provided transform context
6297
 *
6298
 * Apply the stylesheet to the document and generate the output according
6299
 * to @output @SAX and @IObuf. It's an error to specify both @SAX and @IObuf.
6300
 *
6301
 * NOTE: This may lead to a non-wellformed output XML wise !
6302
 * NOTE: This may also result in multiple files being generated
6303
 * NOTE: using IObuf, the result encoding used will be the one used for
6304
 *       creating the output buffer, use the following macro to read it
6305
 *       from the stylesheet
6306
 *       XSLT_GET_IMPORT_PTR(encoding, style, encoding)
6307
 * NOTE: using SAX, any encoding specified in the stylesheet will be lost
6308
 *       since the interface uses only UTF8
6309
 *
6310
 * Returns the number of by written to the main resource or -1 in case of
6311
 *         error.
6312
 */
6313
int
6314
xsltRunStylesheetUser(xsltStylesheetPtr style, xmlDocPtr doc,
6315
                  const char **params, const char *output,
6316
                  xmlSAXHandlerPtr SAX, xmlOutputBufferPtr IObuf,
6317
      FILE * profile, xsltTransformContextPtr userCtxt)
6318
0
{
6319
0
    xmlDocPtr tmp;
6320
0
    int ret;
6321
6322
0
    if ((output == NULL) && (SAX == NULL) && (IObuf == NULL))
6323
0
        return (-1);
6324
0
    if ((SAX != NULL) && (IObuf != NULL))
6325
0
        return (-1);
6326
6327
    /* unsupported yet */
6328
0
    if (SAX != NULL) {
6329
0
        XSLT_TODO   /* xsltRunStylesheet xmlSAXHandlerPtr SAX */
6330
0
  return (-1);
6331
0
    }
6332
6333
0
    tmp = xsltApplyStylesheetInternal(style, doc, params, output, profile,
6334
0
                                userCtxt);
6335
0
    if (tmp == NULL) {
6336
0
  xsltTransformError(NULL, NULL, (xmlNodePtr) doc,
6337
0
                         "xsltRunStylesheet : run failed\n");
6338
0
        return (-1);
6339
0
    }
6340
0
    if (IObuf != NULL) {
6341
        /* TODO: incomplete, IObuf output not progressive */
6342
0
        ret = xsltSaveResultTo(IObuf, tmp, style);
6343
0
    } else {
6344
0
        ret = xsltSaveResultToFilename(output, tmp, style, 0);
6345
0
    }
6346
0
    xmlFreeDoc(tmp);
6347
0
    return (ret);
6348
0
}
6349
6350
/**
6351
 * xsltRunStylesheet:
6352
 * @style:  a parsed XSLT stylesheet
6353
 * @doc:  a parsed XML document
6354
 * @params:  a NULL terminated array of parameters names/values tuples
6355
 * @output:  the URL/filename ot the generated resource if available
6356
 * @SAX:  a SAX handler for progressive callback output (not implemented yet)
6357
 * @IObuf:  an output buffer for progressive output (not implemented yet)
6358
 *
6359
 * Apply the stylesheet to the document and generate the output according
6360
 * to @output @SAX and @IObuf. It's an error to specify both @SAX and @IObuf.
6361
 *
6362
 * NOTE: This may lead to a non-wellformed output XML wise !
6363
 * NOTE: This may also result in multiple files being generated
6364
 * NOTE: using IObuf, the result encoding used will be the one used for
6365
 *       creating the output buffer, use the following macro to read it
6366
 *       from the stylesheet
6367
 *       XSLT_GET_IMPORT_PTR(encoding, style, encoding)
6368
 * NOTE: using SAX, any encoding specified in the stylesheet will be lost
6369
 *       since the interface uses only UTF8
6370
 *
6371
 * Returns the number of bytes written to the main resource or -1 in case of
6372
 *         error.
6373
 */
6374
int
6375
xsltRunStylesheet(xsltStylesheetPtr style, xmlDocPtr doc,
6376
                  const char **params, const char *output,
6377
                  xmlSAXHandlerPtr SAX, xmlOutputBufferPtr IObuf)
6378
0
{
6379
0
    return(xsltRunStylesheetUser(style, doc, params, output, SAX, IObuf,
6380
0
                     NULL, NULL));
6381
0
}
6382
6383
static void
6384
xsltMessageWrapper(xsltTransformContextPtr ctxt, xmlNodePtr node,
6385
0
                   xmlNodePtr inst, xsltElemPreCompPtr comp ATTRIBUTE_UNUSED) {
6386
0
    xsltMessage(ctxt, node, inst);
6387
0
}
6388
6389
/**
6390
 * xsltRegisterAllElement:
6391
 * @ctxt:  the XPath context
6392
 *
6393
 * Registers all default XSLT elements in this context
6394
 */
6395
void
6396
xsltRegisterAllElement(xsltTransformContextPtr ctxt)
6397
0
{
6398
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "apply-templates",
6399
0
                           XSLT_NAMESPACE,
6400
0
         xsltApplyTemplates);
6401
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "apply-imports",
6402
0
                           XSLT_NAMESPACE,
6403
0
         xsltApplyImports);
6404
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "call-template",
6405
0
                           XSLT_NAMESPACE,
6406
0
         xsltCallTemplate);
6407
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "element",
6408
0
                           XSLT_NAMESPACE,
6409
0
         xsltElement);
6410
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "attribute",
6411
0
                           XSLT_NAMESPACE,
6412
0
         xsltAttribute);
6413
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "text",
6414
0
                           XSLT_NAMESPACE,
6415
0
         xsltText);
6416
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "processing-instruction",
6417
0
                           XSLT_NAMESPACE,
6418
0
         xsltProcessingInstruction);
6419
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "comment",
6420
0
                           XSLT_NAMESPACE,
6421
0
         xsltComment);
6422
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "copy",
6423
0
                           XSLT_NAMESPACE,
6424
0
         xsltCopy);
6425
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "value-of",
6426
0
                           XSLT_NAMESPACE,
6427
0
         xsltValueOf);
6428
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "number",
6429
0
                           XSLT_NAMESPACE,
6430
0
         xsltNumber);
6431
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "for-each",
6432
0
                           XSLT_NAMESPACE,
6433
0
         xsltForEach);
6434
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "if",
6435
0
                           XSLT_NAMESPACE,
6436
0
         xsltIf);
6437
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "choose",
6438
0
                           XSLT_NAMESPACE,
6439
0
         xsltChoose);
6440
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "sort",
6441
0
                           XSLT_NAMESPACE,
6442
0
         xsltSort);
6443
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "copy-of",
6444
0
                           XSLT_NAMESPACE,
6445
0
         xsltCopyOf);
6446
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "message",
6447
0
                           XSLT_NAMESPACE,
6448
0
         xsltMessageWrapper);
6449
6450
    /*
6451
     * Those don't have callable entry points but are registered anyway
6452
     */
6453
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "variable",
6454
0
                           XSLT_NAMESPACE,
6455
0
         xsltDebug);
6456
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "param",
6457
0
                           XSLT_NAMESPACE,
6458
0
         xsltDebug);
6459
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "with-param",
6460
0
                           XSLT_NAMESPACE,
6461
0
         xsltDebug);
6462
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "decimal-format",
6463
0
                           XSLT_NAMESPACE,
6464
0
         xsltDebug);
6465
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "when",
6466
0
                           XSLT_NAMESPACE,
6467
0
         xsltDebug);
6468
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "otherwise",
6469
0
                           XSLT_NAMESPACE,
6470
0
         xsltDebug);
6471
0
    xsltRegisterExtElement(ctxt, (const xmlChar *) "fallback",
6472
0
                           XSLT_NAMESPACE,
6473
0
         xsltDebug);
6474
6475
0
}