Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/xslt/libxslt/variables.c
4389 views
1
/*
2
* variables.c: Implementation of the variable storage and lookup
3
*
4
* Reference:
5
* http://www.w3.org/TR/1999/REC-xslt-19991116
6
*
7
* See Copyright for the status of this software.
8
*
9
* [email protected]
10
*/
11
12
#define IN_LIBXSLT
13
#include "libxslt.h"
14
15
#include <string.h>
16
17
#include <libxml/xmlmemory.h>
18
#include <libxml/tree.h>
19
#include <libxml/valid.h>
20
#include <libxml/hash.h>
21
#include <libxml/xmlerror.h>
22
#include <libxml/xpath.h>
23
#include <libxml/xpathInternals.h>
24
#include <libxml/parserInternals.h>
25
#include <libxml/dict.h>
26
#include "xslt.h"
27
#include "xsltInternals.h"
28
#include "xsltutils.h"
29
#include "variables.h"
30
#include "transform.h"
31
#include "imports.h"
32
#include "preproc.h"
33
#include "keys.h"
34
35
#ifdef WITH_XSLT_DEBUG
36
#define WITH_XSLT_DEBUG_VARIABLE
37
#endif
38
39
#ifdef XSLT_REFACTORED
40
const xmlChar *xsltDocFragFake = (const xmlChar *) " fake node libxslt";
41
#endif
42
43
static const xmlChar *xsltComputingGlobalVarMarker =
44
(const xmlChar *) " var/param being computed";
45
46
#define XSLT_VAR_GLOBAL (1<<0)
47
#define XSLT_VAR_IN_SELECT (1<<1)
48
#define XSLT_TCTXT_VARIABLE(c) ((xsltStackElemPtr) (c)->contextVariable)
49
50
/************************************************************************
51
* *
52
* Result Value Tree (Result Tree Fragment) interfaces *
53
* *
54
************************************************************************/
55
/**
56
* xsltCreateRVT:
57
* @ctxt: an XSLT transformation context
58
*
59
* Creates a Result Value Tree
60
* (the XSLT 1.0 term for this is "Result Tree Fragment")
61
*
62
* Returns the result value tree or NULL in case of API or internal errors.
63
*/
64
xmlDocPtr
65
xsltCreateRVT(xsltTransformContextPtr ctxt)
66
{
67
xmlDocPtr container;
68
69
/*
70
* Question: Why is this function public?
71
* Answer: It is called by the EXSLT module.
72
*/
73
if (ctxt == NULL)
74
return(NULL);
75
76
/*
77
* Reuse a RTF from the cache if available.
78
*/
79
if (ctxt->cache->RVT) {
80
container = ctxt->cache->RVT;
81
ctxt->cache->RVT = (xmlDocPtr) container->next;
82
/* clear the internal pointers */
83
container->next = NULL;
84
container->prev = NULL;
85
if (ctxt->cache->nbRVT > 0)
86
ctxt->cache->nbRVT--;
87
#ifdef XSLT_DEBUG_PROFILE_CACHE
88
ctxt->cache->dbgReusedRVTs++;
89
#endif
90
return(container);
91
}
92
93
container = xmlNewDoc(NULL);
94
if (container == NULL)
95
return(NULL);
96
container->dict = ctxt->dict;
97
xmlDictReference(container->dict);
98
XSLT_MARK_RES_TREE_FRAG(container);
99
container->doc = container;
100
container->parent = NULL;
101
return(container);
102
}
103
104
/**
105
* xsltRegisterTmpRVT:
106
* @ctxt: an XSLT transformation context
107
* @RVT: a result value tree (Result Tree Fragment)
108
*
109
* Registers the result value tree (XSLT 1.0 term: Result Tree Fragment)
110
* in the garbage collector.
111
* The fragment will be freed at the exit of the currently
112
* instantiated xsl:template.
113
* Obsolete; this function might produce massive memory overhead,
114
* since the fragment is only freed when the current xsl:template
115
* exits. Use xsltRegisterLocalRVT() instead.
116
*
117
* Returns 0 in case of success and -1 in case of API or internal errors.
118
*/
119
int
120
xsltRegisterTmpRVT(xsltTransformContextPtr ctxt, xmlDocPtr RVT)
121
{
122
if ((ctxt == NULL) || (RVT == NULL))
123
return(-1);
124
125
RVT->prev = NULL;
126
RVT->compression = XSLT_RVT_LOCAL;
127
128
/*
129
* We'll restrict the lifetime of user-created fragments
130
* insinde an xsl:variable and xsl:param to the lifetime of the
131
* var/param itself.
132
*/
133
if (ctxt->contextVariable != NULL) {
134
RVT->next = (xmlNodePtr) XSLT_TCTXT_VARIABLE(ctxt)->fragment;
135
XSLT_TCTXT_VARIABLE(ctxt)->fragment = RVT;
136
return(0);
137
}
138
139
RVT->next = (xmlNodePtr) ctxt->tmpRVT;
140
if (ctxt->tmpRVT != NULL)
141
ctxt->tmpRVT->prev = (xmlNodePtr) RVT;
142
ctxt->tmpRVT = RVT;
143
return(0);
144
}
145
146
/**
147
* xsltRegisterLocalRVT:
148
* @ctxt: an XSLT transformation context
149
* @RVT: a result value tree (Result Tree Fragment; xmlDocPtr)
150
*
151
* Registers a result value tree (XSLT 1.0 term: Result Tree Fragment)
152
* in the RVT garbage collector.
153
* The fragment will be freed when the instruction which created the
154
* fragment exits.
155
*
156
* Returns 0 in case of success and -1 in case of API or internal errors.
157
*/
158
int
159
xsltRegisterLocalRVT(xsltTransformContextPtr ctxt,
160
xmlDocPtr RVT)
161
{
162
if ((ctxt == NULL) || (RVT == NULL))
163
return(-1);
164
165
RVT->prev = NULL;
166
RVT->compression = XSLT_RVT_LOCAL;
167
168
/*
169
* When evaluating "select" expressions of xsl:variable
170
* and xsl:param, we need to bind newly created tree fragments
171
* to the variable itself; otherwise the fragment will be
172
* freed before we leave the scope of a var.
173
*/
174
if ((ctxt->contextVariable != NULL) &&
175
(XSLT_TCTXT_VARIABLE(ctxt)->flags & XSLT_VAR_IN_SELECT))
176
{
177
RVT->next = (xmlNodePtr) XSLT_TCTXT_VARIABLE(ctxt)->fragment;
178
XSLT_TCTXT_VARIABLE(ctxt)->fragment = RVT;
179
return(0);
180
}
181
/*
182
* Store the fragment in the scope of the current instruction.
183
* If not reference by a returning instruction (like EXSLT's function),
184
* then this fragment will be freed, when the instruction exits.
185
*/
186
RVT->next = (xmlNodePtr) ctxt->localRVT;
187
if (ctxt->localRVT != NULL)
188
ctxt->localRVT->prev = (xmlNodePtr) RVT;
189
ctxt->localRVT = RVT;
190
return(0);
191
}
192
193
/**
194
* xsltExtensionInstructionResultFinalize:
195
* @ctxt: an XSLT transformation context
196
*
197
* Finalizes the data (e.g. result tree fragments) created
198
* within a value-returning process (e.g. EXSLT's function).
199
* Tree fragments marked as being returned by a function are
200
* set to normal state, which means that the fragment garbage
201
* collector will free them after the function-calling process exits.
202
*
203
* Returns 0 in case of success and -1 in case of API or internal errors.
204
*
205
* This function is unsupported in newer releases of libxslt.
206
*/
207
int
208
xsltExtensionInstructionResultFinalize(
209
xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED)
210
{
211
xmlGenericError(xmlGenericErrorContext,
212
"xsltExtensionInstructionResultFinalize is unsupported "
213
"in this release of libxslt.\n");
214
return(-1);
215
}
216
217
/**
218
* xsltExtensionInstructionResultRegister:
219
* @ctxt: an XSLT transformation context
220
* @obj: an XPath object to be inspected for result tree fragments
221
*
222
* Marks the result of a value-returning extension instruction
223
* in order to avoid it being garbage collected before the
224
* extension instruction exits.
225
* Note that one still has to additionally register any newly created
226
* tree fragments (via xsltCreateRVT()) with xsltRegisterLocalRVT().
227
*
228
* Returns 0 in case of success and -1 in case of error.
229
*
230
* It isn't necessary to call this function in newer releases of
231
* libxslt.
232
*/
233
int
234
xsltExtensionInstructionResultRegister(
235
xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED,
236
xmlXPathObjectPtr obj ATTRIBUTE_UNUSED)
237
{
238
return(0);
239
}
240
241
/**
242
* xsltFlagRVTs:
243
* @ctxt: an XSLT transformation context
244
* @obj: an XPath object to be inspected for result tree fragments
245
* @val: the flag value
246
*
247
* Updates ownership information of RVTs in @obj according to @val.
248
*
249
* @val = XSLT_RVT_FUNC_RESULT for the result of an extension function, so its
250
* RVTs won't be destroyed after leaving the returning scope.
251
* @val = XSLT_RVT_LOCAL for the result of an extension function to reset
252
* the state of its RVTs after it was returned to a new scope.
253
* @val = XSLT_RVT_GLOBAL for parts of global variables.
254
*
255
* Returns 0 in case of success and -1 in case of error.
256
*/
257
int
258
xsltFlagRVTs(xsltTransformContextPtr ctxt, xmlXPathObjectPtr obj, int val) {
259
int i;
260
xmlNodePtr cur;
261
xmlDocPtr doc;
262
263
if ((ctxt == NULL) || (obj == NULL))
264
return(-1);
265
266
/*
267
* OPTIMIZE TODO: If no local variables/params and no local tree
268
* fragments were created, then we don't need to analyse the XPath
269
* objects for tree fragments.
270
*/
271
272
if ((obj->type != XPATH_NODESET) && (obj->type != XPATH_XSLT_TREE))
273
return(0);
274
if ((obj->nodesetval == NULL) || (obj->nodesetval->nodeNr == 0))
275
return(0);
276
277
for (i = 0; i < obj->nodesetval->nodeNr; i++) {
278
cur = obj->nodesetval->nodeTab[i];
279
if (cur->type == XML_NAMESPACE_DECL) {
280
/*
281
* The XPath module sets the owner element of a ns-node on
282
* the ns->next field.
283
*/
284
if ((((xmlNsPtr) cur)->next != NULL) &&
285
(((xmlNsPtr) cur)->next->type == XML_ELEMENT_NODE))
286
{
287
cur = (xmlNodePtr) ((xmlNsPtr) cur)->next;
288
doc = cur->doc;
289
} else {
290
xsltTransformError(ctxt, NULL, ctxt->inst,
291
"Internal error in xsltFlagRVTs(): "
292
"Cannot retrieve the doc of a namespace node.\n");
293
return(-1);
294
}
295
} else {
296
doc = cur->doc;
297
}
298
if (doc == NULL) {
299
xsltTransformError(ctxt, NULL, ctxt->inst,
300
"Internal error in xsltFlagRVTs(): "
301
"Cannot retrieve the doc of a node.\n");
302
return(-1);
303
}
304
if (doc->name && (doc->name[0] == ' ') &&
305
doc->compression != XSLT_RVT_GLOBAL) {
306
/*
307
* This is a result tree fragment.
308
* We store ownership information in the @compression field.
309
* TODO: How do we know if this is a doc acquired via the
310
* document() function?
311
*/
312
#ifdef WITH_XSLT_DEBUG_VARIABLE
313
XSLT_TRACE(ctxt, XSLT_TRACE_VARIABLES,
314
xsltGenericDebug(xsltGenericDebugContext,
315
"Flagging RVT %p: %d -> %d\n",
316
(void *) doc, doc->compression, val));
317
#endif
318
319
if (val == XSLT_RVT_LOCAL) {
320
if (doc->compression == XSLT_RVT_FUNC_RESULT)
321
doc->compression = XSLT_RVT_LOCAL;
322
} else if (val == XSLT_RVT_GLOBAL) {
323
if (doc->compression != XSLT_RVT_LOCAL) {
324
xmlGenericError(xmlGenericErrorContext,
325
"xsltFlagRVTs: Invalid transition %d => GLOBAL\n",
326
doc->compression);
327
doc->compression = XSLT_RVT_GLOBAL;
328
return(-1);
329
}
330
331
/* Will be registered as persistant in xsltReleaseLocalRVTs. */
332
doc->compression = XSLT_RVT_GLOBAL;
333
} else if (val == XSLT_RVT_FUNC_RESULT) {
334
doc->compression = val;
335
}
336
}
337
}
338
339
return(0);
340
}
341
342
/**
343
* xsltReleaseRVT:
344
* @ctxt: an XSLT transformation context
345
* @RVT: a result value tree (Result Tree Fragment)
346
*
347
* Either frees the RVT (which is an xmlDoc) or stores
348
* it in the context's cache for later reuse.
349
*/
350
void
351
xsltReleaseRVT(xsltTransformContextPtr ctxt, xmlDocPtr RVT)
352
{
353
if (RVT == NULL)
354
return;
355
356
if (ctxt && (ctxt->cache->nbRVT < 40)) {
357
/*
358
* Store the Result Tree Fragment.
359
* Free the document info.
360
*/
361
if (RVT->_private != NULL) {
362
xsltFreeDocumentKeys((xsltDocumentPtr) RVT->_private);
363
xmlFree(RVT->_private);
364
RVT->_private = NULL;
365
}
366
/*
367
* Clear the document tree.
368
*/
369
if (RVT->children != NULL) {
370
xmlFreeNodeList(RVT->children);
371
RVT->children = NULL;
372
RVT->last = NULL;
373
}
374
if (RVT->ids != NULL) {
375
xmlFreeIDTable((xmlIDTablePtr) RVT->ids);
376
RVT->ids = NULL;
377
}
378
379
/*
380
* Reset the ownership information.
381
*/
382
RVT->compression = 0;
383
384
RVT->next = (xmlNodePtr) ctxt->cache->RVT;
385
ctxt->cache->RVT = RVT;
386
387
ctxt->cache->nbRVT++;
388
389
#ifdef XSLT_DEBUG_PROFILE_CACHE
390
ctxt->cache->dbgCachedRVTs++;
391
#endif
392
return;
393
}
394
/*
395
* Free it.
396
*/
397
if (RVT->_private != NULL) {
398
xsltFreeDocumentKeys((xsltDocumentPtr) RVT->_private);
399
xmlFree(RVT->_private);
400
}
401
xmlFreeDoc(RVT);
402
}
403
404
/**
405
* xsltRegisterPersistRVT:
406
* @ctxt: an XSLT transformation context
407
* @RVT: a result value tree (Result Tree Fragment)
408
*
409
* Register the result value tree (XSLT 1.0 term: Result Tree Fragment)
410
* in the fragment garbage collector.
411
* The fragment will be freed when the transformation context is
412
* freed.
413
*
414
* Returns 0 in case of success and -1 in case of error.
415
*/
416
int
417
xsltRegisterPersistRVT(xsltTransformContextPtr ctxt, xmlDocPtr RVT)
418
{
419
if ((ctxt == NULL) || (RVT == NULL)) return(-1);
420
421
RVT->compression = XSLT_RVT_GLOBAL;
422
RVT->prev = NULL;
423
RVT->next = (xmlNodePtr) ctxt->persistRVT;
424
if (ctxt->persistRVT != NULL)
425
ctxt->persistRVT->prev = (xmlNodePtr) RVT;
426
ctxt->persistRVT = RVT;
427
return(0);
428
}
429
430
/**
431
* xsltFreeRVTs:
432
* @ctxt: an XSLT transformation context
433
*
434
* Frees all registered result value trees (Result Tree Fragments)
435
* of the transformation. Internal function; should not be called
436
* by user-code.
437
*/
438
void
439
xsltFreeRVTs(xsltTransformContextPtr ctxt)
440
{
441
xmlDocPtr cur, next;
442
443
if (ctxt == NULL)
444
return;
445
/*
446
* Local fragments.
447
*/
448
cur = ctxt->localRVT;
449
while (cur != NULL) {
450
next = (xmlDocPtr) cur->next;
451
if (cur->_private != NULL) {
452
xsltFreeDocumentKeys(cur->_private);
453
xmlFree(cur->_private);
454
}
455
xmlFreeDoc(cur);
456
cur = next;
457
}
458
ctxt->localRVT = NULL;
459
/*
460
* User-created per-template fragments.
461
*/
462
cur = ctxt->tmpRVT;
463
while (cur != NULL) {
464
next = (xmlDocPtr) cur->next;
465
if (cur->_private != NULL) {
466
xsltFreeDocumentKeys(cur->_private);
467
xmlFree(cur->_private);
468
}
469
xmlFreeDoc(cur);
470
cur = next;
471
}
472
ctxt->tmpRVT = NULL;
473
/*
474
* Global fragments.
475
*/
476
cur = ctxt->persistRVT;
477
while (cur != NULL) {
478
next = (xmlDocPtr) cur->next;
479
if (cur->_private != NULL) {
480
xsltFreeDocumentKeys(cur->_private);
481
xmlFree(cur->_private);
482
}
483
xmlFreeDoc(cur);
484
cur = next;
485
}
486
ctxt->persistRVT = NULL;
487
}
488
489
/************************************************************************
490
* *
491
* Module interfaces *
492
* *
493
************************************************************************/
494
495
/**
496
* xsltNewStackElem:
497
*
498
* Create a new XSLT ParserContext
499
*
500
* Returns the newly allocated xsltParserStackElem or NULL in case of error
501
*/
502
static xsltStackElemPtr
503
xsltNewStackElem(xsltTransformContextPtr ctxt)
504
{
505
xsltStackElemPtr ret;
506
/*
507
* Reuse a stack item from the cache if available.
508
*/
509
if (ctxt && ctxt->cache->stackItems) {
510
ret = ctxt->cache->stackItems;
511
ctxt->cache->stackItems = ret->next;
512
ret->next = NULL;
513
ctxt->cache->nbStackItems--;
514
#ifdef XSLT_DEBUG_PROFILE_CACHE
515
ctxt->cache->dbgReusedVars++;
516
#endif
517
return(ret);
518
}
519
ret = (xsltStackElemPtr) xmlMalloc(sizeof(xsltStackElem));
520
if (ret == NULL) {
521
xsltTransformError(NULL, NULL, NULL,
522
"xsltNewStackElem : malloc failed\n");
523
return(NULL);
524
}
525
memset(ret, 0, sizeof(xsltStackElem));
526
ret->context = ctxt;
527
return(ret);
528
}
529
530
/**
531
* xsltCopyStackElem:
532
* @elem: an XSLT stack element
533
*
534
* Makes a copy of the stack element
535
*
536
* Returns the copy of NULL
537
*/
538
static xsltStackElemPtr
539
xsltCopyStackElem(xsltStackElemPtr elem) {
540
xsltStackElemPtr cur;
541
542
cur = (xsltStackElemPtr) xmlMalloc(sizeof(xsltStackElem));
543
if (cur == NULL) {
544
xsltTransformError(NULL, NULL, NULL,
545
"xsltCopyStackElem : malloc failed\n");
546
return(NULL);
547
}
548
memset(cur, 0, sizeof(xsltStackElem));
549
cur->context = elem->context;
550
cur->name = elem->name;
551
cur->nameURI = elem->nameURI;
552
cur->select = elem->select;
553
cur->tree = elem->tree;
554
cur->comp = elem->comp;
555
return(cur);
556
}
557
558
/**
559
* xsltFreeStackElem:
560
* @elem: an XSLT stack element
561
*
562
* Free up the memory allocated by @elem
563
*/
564
static void
565
xsltFreeStackElem(xsltStackElemPtr elem) {
566
if (elem == NULL)
567
return;
568
if (elem->value != NULL)
569
xmlXPathFreeObject(elem->value);
570
/*
571
* Release the list of temporary Result Tree Fragments.
572
*/
573
if (elem->context) {
574
xmlDocPtr cur;
575
576
while (elem->fragment != NULL) {
577
cur = elem->fragment;
578
elem->fragment = (xmlDocPtr) cur->next;
579
580
if (cur->compression == XSLT_RVT_LOCAL) {
581
xsltReleaseRVT(elem->context, cur);
582
} else if (cur->compression == XSLT_RVT_FUNC_RESULT) {
583
xsltRegisterLocalRVT(elem->context, cur);
584
cur->compression = XSLT_RVT_FUNC_RESULT;
585
} else {
586
xmlGenericError(xmlGenericErrorContext,
587
"xsltFreeStackElem: Unexpected RVT flag %d\n",
588
cur->compression);
589
}
590
}
591
}
592
/*
593
* Cache or free the variable structure.
594
*/
595
if (elem->context && (elem->context->cache->nbStackItems < 50)) {
596
/*
597
* Store the item in the cache.
598
*/
599
xsltTransformContextPtr ctxt = elem->context;
600
memset(elem, 0, sizeof(xsltStackElem));
601
elem->context = ctxt;
602
elem->next = ctxt->cache->stackItems;
603
ctxt->cache->stackItems = elem;
604
ctxt->cache->nbStackItems++;
605
#ifdef XSLT_DEBUG_PROFILE_CACHE
606
ctxt->cache->dbgCachedVars++;
607
#endif
608
return;
609
}
610
xmlFree(elem);
611
}
612
613
static void
614
xsltFreeStackElemEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
615
xsltFreeStackElem((xsltStackElemPtr) payload);
616
}
617
618
619
/**
620
* xsltFreeStackElemList:
621
* @elem: an XSLT stack element
622
*
623
* Free up the memory allocated by @elem
624
*/
625
void
626
xsltFreeStackElemList(xsltStackElemPtr elem) {
627
xsltStackElemPtr next;
628
629
while (elem != NULL) {
630
next = elem->next;
631
xsltFreeStackElem(elem);
632
elem = next;
633
}
634
}
635
636
/**
637
* xsltStackLookup:
638
* @ctxt: an XSLT transformation context
639
* @name: the local part of the name
640
* @nameURI: the URI part of the name
641
*
642
* Locate an element in the stack based on its name.
643
*/
644
static xsltStackElemPtr
645
xsltStackLookup(xsltTransformContextPtr ctxt, const xmlChar *name,
646
const xmlChar *nameURI) {
647
int i;
648
xsltStackElemPtr cur;
649
650
if ((ctxt == NULL) || (name == NULL) || (ctxt->varsNr == 0))
651
return(NULL);
652
653
/*
654
* Do the lookup from the top of the stack, but
655
* don't use params being computed in a call-param
656
* First lookup expects the variable name and URI to
657
* come from the disctionnary and hence pointer comparison.
658
*/
659
for (i = ctxt->varsNr; i > ctxt->varsBase; i--) {
660
cur = ctxt->varsTab[i-1];
661
while (cur != NULL) {
662
if ((cur->name == name) && (cur->nameURI == nameURI)) {
663
return(cur);
664
}
665
cur = cur->next;
666
}
667
}
668
669
/*
670
* Redo the lookup with interned string compares
671
* to avoid string compares.
672
*/
673
name = xmlDictLookup(ctxt->dict, name, -1);
674
if (nameURI != NULL)
675
nameURI = xmlDictLookup(ctxt->dict, nameURI, -1);
676
677
for (i = ctxt->varsNr; i > ctxt->varsBase; i--) {
678
cur = ctxt->varsTab[i-1];
679
while (cur != NULL) {
680
if ((cur->name == name) && (cur->nameURI == nameURI)) {
681
return(cur);
682
}
683
cur = cur->next;
684
}
685
}
686
687
return(NULL);
688
}
689
690
#ifdef XSLT_REFACTORED
691
#else
692
693
/**
694
* xsltCheckStackElem:
695
* @ctxt: xn XSLT transformation context
696
* @name: the variable name
697
* @nameURI: the variable namespace URI
698
*
699
* Checks whether a variable or param is already defined.
700
*
701
* URGENT TODO: Checks for redefinition of vars/params should be
702
* done only at compilation time.
703
*
704
* Returns 1 if variable is present, 2 if param is present, 3 if this
705
* is an inherited param, 0 if not found, -1 in case of failure.
706
*/
707
static int
708
xsltCheckStackElem(xsltTransformContextPtr ctxt, const xmlChar *name,
709
const xmlChar *nameURI) {
710
xsltStackElemPtr cur;
711
712
if ((ctxt == NULL) || (name == NULL))
713
return(-1);
714
715
cur = xsltStackLookup(ctxt, name, nameURI);
716
if (cur == NULL)
717
return(0);
718
if (cur->comp != NULL) {
719
if (cur->comp->type == XSLT_FUNC_WITHPARAM)
720
return(3);
721
else if (cur->comp->type == XSLT_FUNC_PARAM)
722
return(2);
723
}
724
725
return(1);
726
}
727
728
#endif /* XSLT_REFACTORED */
729
730
/**
731
* xsltAddStackElem:
732
* @ctxt: xn XSLT transformation context
733
* @elem: a stack element
734
*
735
* Push an element (or list) onto the stack.
736
* In case of a list, each member will be pushed into
737
* a seperate slot; i.e. there's always 1 stack entry for
738
* 1 stack element.
739
*
740
* Returns 0 in case of success, -1 in case of failure.
741
*/
742
static int
743
xsltAddStackElem(xsltTransformContextPtr ctxt, xsltStackElemPtr elem)
744
{
745
if ((ctxt == NULL) || (elem == NULL))
746
return(-1);
747
748
do {
749
if (ctxt->varsNr >= ctxt->varsMax) {
750
xsltStackElemPtr *tmp;
751
int newMax = ctxt->varsMax == 0 ? 10 : 2 * ctxt->varsMax;
752
753
tmp = (xsltStackElemPtr *) xmlRealloc(ctxt->varsTab,
754
newMax * sizeof(*tmp));
755
if (tmp == NULL) {
756
xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
757
return (-1);
758
}
759
ctxt->varsTab = tmp;
760
ctxt->varsMax = newMax;
761
}
762
ctxt->varsTab[ctxt->varsNr++] = elem;
763
ctxt->vars = elem;
764
765
elem = elem->next;
766
} while (elem != NULL);
767
768
return(0);
769
}
770
771
/**
772
* xsltAddStackElemList:
773
* @ctxt: xn XSLT transformation context
774
* @elems: a stack element list
775
*
776
* Push an element list onto the stack.
777
*
778
* Returns 0 in case of success, -1 in case of failure.
779
*/
780
int
781
xsltAddStackElemList(xsltTransformContextPtr ctxt, xsltStackElemPtr elems)
782
{
783
return(xsltAddStackElem(ctxt, elems));
784
}
785
786
/************************************************************************
787
* *
788
* Module interfaces *
789
* *
790
************************************************************************/
791
792
/**
793
* xsltEvalVariable:
794
* @ctxt: the XSLT transformation context
795
* @variable: the variable or parameter item
796
* @comp: the compiled XSLT instruction
797
*
798
* Evaluate a variable value.
799
*
800
* Returns the XPath Object value or NULL in case of error
801
*/
802
static xmlXPathObjectPtr
803
xsltEvalVariable(xsltTransformContextPtr ctxt, xsltStackElemPtr variable,
804
xsltStylePreCompPtr castedComp)
805
{
806
#ifdef XSLT_REFACTORED
807
xsltStyleItemVariablePtr comp =
808
(xsltStyleItemVariablePtr) castedComp;
809
#else
810
xsltStylePreCompPtr comp = castedComp;
811
#endif
812
xmlXPathObjectPtr result = NULL;
813
xmlNodePtr oldInst;
814
815
if ((ctxt == NULL) || (variable == NULL))
816
return(NULL);
817
818
/*
819
* A variable or parameter are evaluated on demand; thus the
820
* context (of XSLT and XPath) need to be temporarily adjusted and
821
* restored on exit.
822
*/
823
oldInst = ctxt->inst;
824
825
#ifdef WITH_XSLT_DEBUG_VARIABLE
826
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
827
"Evaluating variable '%s'\n", variable->name));
828
#endif
829
if (variable->select != NULL) {
830
xmlXPathCompExprPtr xpExpr = NULL;
831
xmlDocPtr oldXPDoc;
832
xmlNodePtr oldXPContextNode;
833
int oldXPProximityPosition, oldXPContextSize, oldXPNsNr;
834
xmlNsPtr *oldXPNamespaces;
835
xmlXPathContextPtr xpctxt = ctxt->xpathCtxt;
836
xsltStackElemPtr oldVar = ctxt->contextVariable;
837
838
if ((comp != NULL) && (comp->comp != NULL)) {
839
xpExpr = comp->comp;
840
} else {
841
xpExpr = xmlXPathCtxtCompile(ctxt->xpathCtxt, variable->select);
842
}
843
if (xpExpr == NULL)
844
return(NULL);
845
/*
846
* Save context states.
847
*/
848
oldXPDoc = xpctxt->doc;
849
oldXPContextNode = xpctxt->node;
850
oldXPProximityPosition = xpctxt->proximityPosition;
851
oldXPContextSize = xpctxt->contextSize;
852
oldXPNamespaces = xpctxt->namespaces;
853
oldXPNsNr = xpctxt->nsNr;
854
855
xpctxt->node = ctxt->node;
856
/*
857
* OPTIMIZE TODO: Lame try to set the context doc.
858
* Get rid of this somehow in xpath.c.
859
*/
860
if ((ctxt->node->type != XML_NAMESPACE_DECL) &&
861
ctxt->node->doc)
862
xpctxt->doc = ctxt->node->doc;
863
/*
864
* BUG TODO: The proximity position and the context size will
865
* potentially be wrong.
866
* Example:
867
* <xsl:template select="foo">
868
* <xsl:variable name="pos" select="position()"/>
869
* <xsl:for-each select="bar">
870
* <xsl:value-of select="$pos"/>
871
* </xsl:for-each>
872
* </xsl:template>
873
* Here the proximity position and context size are changed
874
* to the context of <xsl:for-each select="bar">, but
875
* the variable needs to be evaluated in the context of
876
* <xsl:template select="foo">.
877
*/
878
if (comp != NULL) {
879
880
#ifdef XSLT_REFACTORED
881
if (comp->inScopeNs != NULL) {
882
xpctxt->namespaces = comp->inScopeNs->list;
883
xpctxt->nsNr = comp->inScopeNs->xpathNumber;
884
} else {
885
xpctxt->namespaces = NULL;
886
xpctxt->nsNr = 0;
887
}
888
#else
889
xpctxt->namespaces = comp->nsList;
890
xpctxt->nsNr = comp->nsNr;
891
#endif
892
} else {
893
xpctxt->namespaces = NULL;
894
xpctxt->nsNr = 0;
895
}
896
897
/*
898
* We need to mark that we are "selecting" a var's value;
899
* if any tree fragments are created inside the expression,
900
* then those need to be stored inside the variable; otherwise
901
* we'll eventually free still referenced fragments, before
902
* we leave the scope of the variable.
903
*/
904
ctxt->contextVariable = variable;
905
variable->flags |= XSLT_VAR_IN_SELECT;
906
907
result = xmlXPathCompiledEval(xpExpr, xpctxt);
908
909
variable->flags ^= XSLT_VAR_IN_SELECT;
910
/*
911
* Restore Context states.
912
*/
913
ctxt->contextVariable = oldVar;
914
915
xpctxt->doc = oldXPDoc;
916
xpctxt->node = oldXPContextNode;
917
xpctxt->contextSize = oldXPContextSize;
918
xpctxt->proximityPosition = oldXPProximityPosition;
919
xpctxt->namespaces = oldXPNamespaces;
920
xpctxt->nsNr = oldXPNsNr;
921
922
if ((comp == NULL) || (comp->comp == NULL))
923
xmlXPathFreeCompExpr(xpExpr);
924
if (result == NULL) {
925
xsltTransformError(ctxt, NULL,
926
(comp != NULL) ? comp->inst : NULL,
927
"Failed to evaluate the expression of variable '%s'.\n",
928
variable->name);
929
ctxt->state = XSLT_STATE_STOPPED;
930
931
#ifdef WITH_XSLT_DEBUG_VARIABLE
932
#ifdef LIBXML_DEBUG_ENABLED
933
} else {
934
if ((xsltGenericDebugContext == stdout) ||
935
(xsltGenericDebugContext == stderr))
936
xmlXPathDebugDumpObject((FILE *)xsltGenericDebugContext,
937
result, 0);
938
#endif
939
#endif
940
}
941
} else {
942
if (variable->tree == NULL) {
943
result = xmlXPathNewCString("");
944
} else {
945
if (variable->tree) {
946
xmlDocPtr container;
947
xmlNodePtr oldInsert;
948
xmlDocPtr oldOutput;
949
const xmlChar *oldLastText;
950
int oldLastTextSize, oldLastTextUse;
951
xsltStackElemPtr oldVar = ctxt->contextVariable;
952
953
/*
954
* Generate a result tree fragment.
955
*/
956
container = xsltCreateRVT(ctxt);
957
if (container == NULL)
958
goto error;
959
/*
960
* NOTE: Local Result Tree Fragments of params/variables
961
* are not registered globally anymore; the life-time
962
* is not directly dependant of the param/variable itself.
963
*
964
* OLD: xsltRegisterTmpRVT(ctxt, container);
965
*/
966
/*
967
* Attach the Result Tree Fragment to the variable;
968
* when the variable is freed, it will also free
969
* the Result Tree Fragment.
970
*/
971
variable->fragment = container;
972
container->compression = XSLT_RVT_LOCAL;
973
974
oldOutput = ctxt->output;
975
oldInsert = ctxt->insert;
976
oldLastText = ctxt->lasttext;
977
oldLastTextSize = ctxt->lasttsize;
978
oldLastTextUse = ctxt->lasttuse;
979
980
ctxt->output = container;
981
ctxt->insert = (xmlNodePtr) container;
982
ctxt->contextVariable = variable;
983
/*
984
* Process the sequence constructor (variable->tree).
985
* The resulting tree will be held by @container.
986
*/
987
xsltApplyOneTemplate(ctxt, ctxt->node, variable->tree,
988
NULL, NULL);
989
990
ctxt->contextVariable = oldVar;
991
ctxt->insert = oldInsert;
992
ctxt->output = oldOutput;
993
ctxt->lasttext = oldLastText;
994
ctxt->lasttsize = oldLastTextSize;
995
ctxt->lasttuse = oldLastTextUse;
996
997
result = xmlXPathNewValueTree((xmlNodePtr) container);
998
}
999
if (result == NULL) {
1000
result = xmlXPathNewCString("");
1001
} else {
1002
/*
1003
* This stops older libxml2 versions from freeing the nodes
1004
* in the tree.
1005
*/
1006
result->boolval = 0;
1007
}
1008
#ifdef WITH_XSLT_DEBUG_VARIABLE
1009
#ifdef LIBXML_DEBUG_ENABLED
1010
1011
if ((xsltGenericDebugContext == stdout) ||
1012
(xsltGenericDebugContext == stderr))
1013
xmlXPathDebugDumpObject((FILE *)xsltGenericDebugContext,
1014
result, 0);
1015
#endif
1016
#endif
1017
}
1018
}
1019
1020
error:
1021
ctxt->inst = oldInst;
1022
return(result);
1023
}
1024
1025
/**
1026
* xsltEvalGlobalVariable:
1027
* @elem: the variable or parameter
1028
* @ctxt: the XSLT transformation context
1029
*
1030
* Evaluates a the value of a global xsl:variable or
1031
* xsl:param declaration.
1032
*
1033
* Returns the XPath Object value or NULL in case of error
1034
*/
1035
static xmlXPathObjectPtr
1036
xsltEvalGlobalVariable(xsltStackElemPtr elem, xsltTransformContextPtr ctxt)
1037
{
1038
xmlXPathObjectPtr result = NULL;
1039
xmlNodePtr oldInst;
1040
const xmlChar* oldVarName;
1041
1042
#ifdef XSLT_REFACTORED
1043
xsltStyleBasicItemVariablePtr comp;
1044
#else
1045
xsltStylePreCompPtr comp;
1046
#endif
1047
1048
if ((ctxt == NULL) || (elem == NULL))
1049
return(NULL);
1050
if (elem->computed)
1051
return(elem->value);
1052
1053
1054
#ifdef WITH_XSLT_DEBUG_VARIABLE
1055
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
1056
"Evaluating global variable %s\n", elem->name));
1057
#endif
1058
1059
#ifdef WITH_DEBUGGER
1060
if ((ctxt->debugStatus != XSLT_DEBUG_NONE) &&
1061
elem->comp && elem->comp->inst)
1062
xslHandleDebugger(elem->comp->inst, NULL, NULL, ctxt);
1063
#endif
1064
1065
oldInst = ctxt->inst;
1066
#ifdef XSLT_REFACTORED
1067
comp = (xsltStyleBasicItemVariablePtr) elem->comp;
1068
#else
1069
comp = elem->comp;
1070
#endif
1071
oldVarName = elem->name;
1072
elem->name = xsltComputingGlobalVarMarker;
1073
/*
1074
* OPTIMIZE TODO: We should consider instantiating global vars/params
1075
* on-demand. The vars/params don't need to be evaluated if never
1076
* called; and in the case of global params, if values for such params
1077
* are provided by the user.
1078
*/
1079
if (elem->select != NULL) {
1080
xmlXPathCompExprPtr xpExpr = NULL;
1081
xmlDocPtr oldXPDoc;
1082
xmlNodePtr oldXPContextNode;
1083
int oldXPProximityPosition, oldXPContextSize, oldXPNsNr;
1084
xmlNsPtr *oldXPNamespaces;
1085
xmlXPathContextPtr xpctxt = ctxt->xpathCtxt;
1086
1087
if ((comp != NULL) && (comp->comp != NULL)) {
1088
xpExpr = comp->comp;
1089
} else {
1090
xpExpr = xmlXPathCtxtCompile(ctxt->xpathCtxt, elem->select);
1091
}
1092
if (xpExpr == NULL)
1093
goto error;
1094
1095
1096
if (comp != NULL)
1097
ctxt->inst = comp->inst;
1098
else
1099
ctxt->inst = NULL;
1100
/*
1101
* SPEC XSLT 1.0:
1102
* "At top-level, the expression or template specifying the
1103
* variable value is evaluated with the same context as that used
1104
* to process the root node of the source document: the current
1105
* node is the root node of the source document and the current
1106
* node list is a list containing just the root node of the source
1107
* document."
1108
*/
1109
/*
1110
* Save context states.
1111
*/
1112
oldXPDoc = xpctxt->doc;
1113
oldXPContextNode = xpctxt->node;
1114
oldXPProximityPosition = xpctxt->proximityPosition;
1115
oldXPContextSize = xpctxt->contextSize;
1116
oldXPNamespaces = xpctxt->namespaces;
1117
oldXPNsNr = xpctxt->nsNr;
1118
1119
xpctxt->node = ctxt->initialContextNode;
1120
xpctxt->doc = ctxt->initialContextDoc;
1121
xpctxt->contextSize = 1;
1122
xpctxt->proximityPosition = 1;
1123
1124
if (comp != NULL) {
1125
1126
#ifdef XSLT_REFACTORED
1127
if (comp->inScopeNs != NULL) {
1128
xpctxt->namespaces = comp->inScopeNs->list;
1129
xpctxt->nsNr = comp->inScopeNs->xpathNumber;
1130
} else {
1131
xpctxt->namespaces = NULL;
1132
xpctxt->nsNr = 0;
1133
}
1134
#else
1135
xpctxt->namespaces = comp->nsList;
1136
xpctxt->nsNr = comp->nsNr;
1137
#endif
1138
} else {
1139
xpctxt->namespaces = NULL;
1140
xpctxt->nsNr = 0;
1141
}
1142
1143
result = xmlXPathCompiledEval(xpExpr, xpctxt);
1144
1145
/*
1146
* Restore Context states.
1147
*/
1148
xpctxt->doc = oldXPDoc;
1149
xpctxt->node = oldXPContextNode;
1150
xpctxt->contextSize = oldXPContextSize;
1151
xpctxt->proximityPosition = oldXPProximityPosition;
1152
xpctxt->namespaces = oldXPNamespaces;
1153
xpctxt->nsNr = oldXPNsNr;
1154
1155
if ((comp == NULL) || (comp->comp == NULL))
1156
xmlXPathFreeCompExpr(xpExpr);
1157
if (result == NULL) {
1158
if (comp == NULL)
1159
xsltTransformError(ctxt, NULL, NULL,
1160
"Evaluating global variable %s failed\n", elem->name);
1161
else
1162
xsltTransformError(ctxt, NULL, comp->inst,
1163
"Evaluating global variable %s failed\n", elem->name);
1164
ctxt->state = XSLT_STATE_STOPPED;
1165
goto error;
1166
}
1167
1168
/*
1169
* Mark all RVTs that are referenced from result as part
1170
* of this variable so they won't be freed too early.
1171
*/
1172
xsltFlagRVTs(ctxt, result, XSLT_RVT_GLOBAL);
1173
1174
#ifdef WITH_XSLT_DEBUG_VARIABLE
1175
#ifdef LIBXML_DEBUG_ENABLED
1176
if ((xsltGenericDebugContext == stdout) ||
1177
(xsltGenericDebugContext == stderr))
1178
xmlXPathDebugDumpObject((FILE *)xsltGenericDebugContext,
1179
result, 0);
1180
#endif
1181
#endif
1182
} else {
1183
if (elem->tree == NULL) {
1184
result = xmlXPathNewCString("");
1185
} else {
1186
xmlDocPtr container;
1187
xmlNodePtr oldInsert;
1188
xmlDocPtr oldOutput, oldXPDoc;
1189
/*
1190
* Generate a result tree fragment.
1191
*/
1192
container = xsltCreateRVT(ctxt);
1193
if (container == NULL)
1194
goto error;
1195
/*
1196
* Let the lifetime of the tree fragment be handled by
1197
* the Libxslt's garbage collector.
1198
*/
1199
xsltRegisterPersistRVT(ctxt, container);
1200
1201
oldOutput = ctxt->output;
1202
oldInsert = ctxt->insert;
1203
1204
oldXPDoc = ctxt->xpathCtxt->doc;
1205
1206
ctxt->output = container;
1207
ctxt->insert = (xmlNodePtr) container;
1208
1209
ctxt->xpathCtxt->doc = ctxt->initialContextDoc;
1210
/*
1211
* Process the sequence constructor.
1212
*/
1213
xsltApplyOneTemplate(ctxt, ctxt->node, elem->tree, NULL, NULL);
1214
1215
ctxt->xpathCtxt->doc = oldXPDoc;
1216
1217
ctxt->insert = oldInsert;
1218
ctxt->output = oldOutput;
1219
1220
result = xmlXPathNewValueTree((xmlNodePtr) container);
1221
if (result == NULL) {
1222
result = xmlXPathNewCString("");
1223
} else {
1224
/*
1225
* This stops older libxml2 versions from freeing the nodes
1226
* in the tree.
1227
*/
1228
result->boolval = 0;
1229
}
1230
#ifdef WITH_XSLT_DEBUG_VARIABLE
1231
#ifdef LIBXML_DEBUG_ENABLED
1232
if ((xsltGenericDebugContext == stdout) ||
1233
(xsltGenericDebugContext == stderr))
1234
xmlXPathDebugDumpObject((FILE *)xsltGenericDebugContext,
1235
result, 0);
1236
#endif
1237
#endif
1238
}
1239
}
1240
1241
error:
1242
elem->name = oldVarName;
1243
ctxt->inst = oldInst;
1244
if (result != NULL) {
1245
elem->value = result;
1246
elem->computed = 1;
1247
}
1248
return(result);
1249
}
1250
1251
/**
1252
* xsltEvalGlobalVariables:
1253
* @ctxt: the XSLT transformation context
1254
*
1255
* Evaluates all global variables and parameters of a stylesheet.
1256
* For internal use only. This is called at start of a transformation.
1257
*
1258
* Returns 0 in case of success, -1 in case of error
1259
*/
1260
int
1261
xsltEvalGlobalVariables(xsltTransformContextPtr ctxt) {
1262
xsltStackElemPtr elem;
1263
xsltStackElemPtr head = NULL;
1264
xsltStylesheetPtr style;
1265
1266
if ((ctxt == NULL) || (ctxt->document == NULL))
1267
return(-1);
1268
1269
#ifdef WITH_XSLT_DEBUG_VARIABLE
1270
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
1271
"Registering global variables\n"));
1272
#endif
1273
/*
1274
* Walk the list from the stylesheets and populate the hash table
1275
*/
1276
style = ctxt->style;
1277
while (style != NULL) {
1278
elem = style->variables;
1279
1280
#ifdef WITH_XSLT_DEBUG_VARIABLE
1281
if ((style->doc != NULL) && (style->doc->URL != NULL)) {
1282
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
1283
"Registering global variables from %s\n",
1284
style->doc->URL));
1285
}
1286
#endif
1287
1288
while (elem != NULL) {
1289
xsltStackElemPtr def;
1290
1291
/*
1292
* Global variables are stored in the variables pool.
1293
*/
1294
def = (xsltStackElemPtr)
1295
xmlHashLookup2(ctxt->globalVars,
1296
elem->name, elem->nameURI);
1297
if (def == NULL) {
1298
1299
def = xsltCopyStackElem(elem);
1300
if (xmlHashAddEntry2(ctxt->globalVars,
1301
elem->name, elem->nameURI, def) < 0) {
1302
xmlGenericError(xmlGenericErrorContext,
1303
"hash update failed\n");
1304
xsltFreeStackElem(def);
1305
return(-1);
1306
}
1307
def->next = head;
1308
head = def;
1309
} else if ((elem->comp != NULL) &&
1310
(elem->comp->type == XSLT_FUNC_VARIABLE)) {
1311
/*
1312
* Redefinition of variables from a different stylesheet
1313
* should not generate a message.
1314
*/
1315
if ((elem->comp->inst != NULL) &&
1316
(def->comp != NULL) && (def->comp->inst != NULL) &&
1317
(elem->comp->inst->doc == def->comp->inst->doc))
1318
{
1319
xsltTransformError(ctxt, style, elem->comp->inst,
1320
"Global variable %s already defined\n", elem->name);
1321
if (style != NULL) style->errors++;
1322
}
1323
}
1324
elem = elem->next;
1325
}
1326
1327
style = xsltNextImport(style);
1328
}
1329
1330
/*
1331
* This part does the actual evaluation. Note that scanning the hash
1332
* table would result in a non-deterministic order, leading to
1333
* non-deterministic generated IDs.
1334
*/
1335
elem = head;
1336
while (elem != NULL) {
1337
xsltStackElemPtr next;
1338
1339
xsltEvalGlobalVariable(elem, ctxt);
1340
next = elem->next;
1341
elem->next = NULL;
1342
elem = next;
1343
}
1344
1345
return(0);
1346
}
1347
1348
/**
1349
* xsltRegisterGlobalVariable:
1350
* @style: the XSLT transformation context
1351
* @name: the variable name
1352
* @ns_uri: the variable namespace URI
1353
* @sel: the expression which need to be evaluated to generate a value
1354
* @tree: the subtree if sel is NULL
1355
* @comp: the precompiled value
1356
* @value: the string value if available
1357
*
1358
* Register a new variable value. If @value is NULL it unregisters
1359
* the variable
1360
*
1361
* Returns 0 in case of success, -1 in case of error
1362
*/
1363
static int
1364
xsltRegisterGlobalVariable(xsltStylesheetPtr style, const xmlChar *name,
1365
const xmlChar *ns_uri, const xmlChar *sel,
1366
xmlNodePtr tree, xsltStylePreCompPtr comp,
1367
const xmlChar *value) {
1368
xsltStackElemPtr elem, tmp;
1369
if (style == NULL)
1370
return(-1);
1371
if (name == NULL)
1372
return(-1);
1373
if (comp == NULL)
1374
return(-1);
1375
1376
#ifdef WITH_XSLT_DEBUG_VARIABLE
1377
if (comp->type == XSLT_FUNC_PARAM)
1378
xsltGenericDebug(xsltGenericDebugContext,
1379
"Defining global param %s\n", name);
1380
else
1381
xsltGenericDebug(xsltGenericDebugContext,
1382
"Defining global variable %s\n", name);
1383
#endif
1384
1385
elem = xsltNewStackElem(NULL);
1386
if (elem == NULL)
1387
return(-1);
1388
elem->comp = comp;
1389
elem->name = xmlDictLookup(style->dict, name, -1);
1390
elem->select = xmlDictLookup(style->dict, sel, -1);
1391
if (ns_uri)
1392
elem->nameURI = xmlDictLookup(style->dict, ns_uri, -1);
1393
elem->tree = tree;
1394
tmp = style->variables;
1395
while (tmp != NULL) {
1396
if ((elem->comp->type == XSLT_FUNC_VARIABLE) &&
1397
(tmp->comp->type == XSLT_FUNC_VARIABLE) &&
1398
(xmlStrEqual(elem->name, tmp->name)) &&
1399
((elem->nameURI == tmp->nameURI) ||
1400
(xmlStrEqual(elem->nameURI, tmp->nameURI))))
1401
{
1402
xsltTransformError(NULL, style, comp->inst,
1403
"redefinition of global variable %s\n", elem->name);
1404
style->errors++;
1405
}
1406
tmp = tmp->next;
1407
}
1408
elem->next = style->variables;
1409
style->variables = elem;
1410
if (value != NULL) {
1411
elem->computed = 1;
1412
elem->value = xmlXPathNewString(value);
1413
}
1414
return(0);
1415
}
1416
1417
/**
1418
* xsltProcessUserParamInternal
1419
*
1420
* @ctxt: the XSLT transformation context
1421
* @name: a null terminated parameter name
1422
* @value: a null terminated value (may be an XPath expression)
1423
* @eval: 0 to treat the value literally, else evaluate as XPath expression
1424
*
1425
* If @eval is 0 then @value is treated literally and is stored in the global
1426
* parameter/variable table without any change.
1427
*
1428
* Uf @eval is 1 then @value is treated as an XPath expression and is
1429
* evaluated. In this case, if you want to pass a string which will be
1430
* interpreted literally then it must be enclosed in single or double quotes.
1431
* If the string contains single quotes (double quotes) then it cannot be
1432
* enclosed single quotes (double quotes). If the string which you want to
1433
* be treated literally contains both single and double quotes (e.g. Meet
1434
* at Joe's for "Twelfth Night" at 7 o'clock) then there is no suitable
1435
* quoting character. You cannot use &apos; or &quot; inside the string
1436
* because the replacement of character entities with their equivalents is
1437
* done at a different stage of processing. The solution is to call
1438
* xsltQuoteUserParams or xsltQuoteOneUserParam.
1439
*
1440
* This needs to be done on parsed stylesheets before starting to apply
1441
* transformations. Normally this will be called (directly or indirectly)
1442
* only from xsltEvalUserParams, xsltEvalOneUserParam, xsltQuoteUserParams,
1443
* or xsltQuoteOneUserParam.
1444
*
1445
* Returns 0 in case of success, -1 in case of error
1446
*/
1447
1448
static
1449
int
1450
xsltProcessUserParamInternal(xsltTransformContextPtr ctxt,
1451
const xmlChar * name,
1452
const xmlChar * value,
1453
int eval) {
1454
1455
xsltStylesheetPtr style;
1456
const xmlChar *prefix;
1457
const xmlChar *href;
1458
xmlXPathCompExprPtr xpExpr;
1459
xmlXPathObjectPtr result;
1460
1461
xsltStackElemPtr elem;
1462
int res;
1463
void *res_ptr;
1464
1465
if (ctxt == NULL)
1466
return(-1);
1467
if (name == NULL)
1468
return(0);
1469
if (value == NULL)
1470
return(0);
1471
1472
style = ctxt->style;
1473
1474
#ifdef WITH_XSLT_DEBUG_VARIABLE
1475
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
1476
"Evaluating user parameter %s=%s\n", name, value));
1477
#endif
1478
1479
/*
1480
* Name lookup
1481
*/
1482
href = NULL;
1483
1484
if (name[0] == '{') {
1485
int len = 0;
1486
1487
while ((name[len] != 0) && (name[len] != '}')) len++;
1488
if (name[len] == 0) {
1489
xsltTransformError(ctxt, style, NULL,
1490
"user param : malformed parameter name : %s\n", name);
1491
} else {
1492
href = xmlDictLookup(ctxt->dict, &name[1], len-1);
1493
name = xmlDictLookup(ctxt->dict, &name[len + 1], -1);
1494
}
1495
}
1496
else {
1497
name = xsltSplitQName(ctxt->dict, name, &prefix);
1498
if (prefix != NULL) {
1499
xmlNsPtr ns;
1500
1501
ns = xmlSearchNs(style->doc, xmlDocGetRootElement(style->doc),
1502
prefix);
1503
if (ns == NULL) {
1504
xsltTransformError(ctxt, style, NULL,
1505
"user param : no namespace bound to prefix %s\n", prefix);
1506
href = NULL;
1507
} else {
1508
href = ns->href;
1509
}
1510
}
1511
}
1512
1513
if (name == NULL)
1514
return (-1);
1515
1516
res_ptr = xmlHashLookup2(ctxt->globalVars, name, href);
1517
if (res_ptr != 0) {
1518
xsltTransformError(ctxt, style, NULL,
1519
"Global parameter %s already defined\n", name);
1520
}
1521
if (ctxt->globalVars == NULL)
1522
ctxt->globalVars = xmlHashCreate(20);
1523
1524
/*
1525
* do not overwrite variables with parameters from the command line
1526
*/
1527
while (style != NULL) {
1528
elem = ctxt->style->variables;
1529
while (elem != NULL) {
1530
if ((elem->comp != NULL) &&
1531
(elem->comp->type == XSLT_FUNC_VARIABLE) &&
1532
(xmlStrEqual(elem->name, name)) &&
1533
(xmlStrEqual(elem->nameURI, href))) {
1534
return(0);
1535
}
1536
elem = elem->next;
1537
}
1538
style = xsltNextImport(style);
1539
}
1540
style = ctxt->style;
1541
elem = NULL;
1542
1543
/*
1544
* Do the evaluation if @eval is non-zero.
1545
*/
1546
1547
result = NULL;
1548
if (eval != 0) {
1549
xpExpr = xmlXPathCtxtCompile(ctxt->xpathCtxt, value);
1550
if (xpExpr != NULL) {
1551
xmlDocPtr oldXPDoc;
1552
xmlNodePtr oldXPContextNode;
1553
int oldXPProximityPosition, oldXPContextSize, oldXPNsNr;
1554
xmlNsPtr *oldXPNamespaces;
1555
xmlXPathContextPtr xpctxt = ctxt->xpathCtxt;
1556
1557
/*
1558
* Save context states.
1559
*/
1560
oldXPDoc = xpctxt->doc;
1561
oldXPContextNode = xpctxt->node;
1562
oldXPProximityPosition = xpctxt->proximityPosition;
1563
oldXPContextSize = xpctxt->contextSize;
1564
oldXPNamespaces = xpctxt->namespaces;
1565
oldXPNsNr = xpctxt->nsNr;
1566
1567
/*
1568
* SPEC XSLT 1.0:
1569
* "At top-level, the expression or template specifying the
1570
* variable value is evaluated with the same context as that used
1571
* to process the root node of the source document: the current
1572
* node is the root node of the source document and the current
1573
* node list is a list containing just the root node of the source
1574
* document."
1575
*/
1576
xpctxt->doc = ctxt->initialContextDoc;
1577
xpctxt->node = ctxt->initialContextNode;
1578
xpctxt->contextSize = 1;
1579
xpctxt->proximityPosition = 1;
1580
/*
1581
* There is really no in scope namespace for parameters on the
1582
* command line.
1583
*/
1584
xpctxt->namespaces = NULL;
1585
xpctxt->nsNr = 0;
1586
1587
result = xmlXPathCompiledEval(xpExpr, xpctxt);
1588
1589
/*
1590
* Restore Context states.
1591
*/
1592
xpctxt->doc = oldXPDoc;
1593
xpctxt->node = oldXPContextNode;
1594
xpctxt->contextSize = oldXPContextSize;
1595
xpctxt->proximityPosition = oldXPProximityPosition;
1596
xpctxt->namespaces = oldXPNamespaces;
1597
xpctxt->nsNr = oldXPNsNr;
1598
1599
xmlXPathFreeCompExpr(xpExpr);
1600
}
1601
if (result == NULL) {
1602
xsltTransformError(ctxt, style, NULL,
1603
"Evaluating user parameter %s failed\n", name);
1604
ctxt->state = XSLT_STATE_STOPPED;
1605
return(-1);
1606
}
1607
}
1608
1609
/*
1610
* If @eval is 0 then @value is to be taken literally and result is NULL
1611
*
1612
* If @eval is not 0, then @value is an XPath expression and has been
1613
* successfully evaluated and result contains the resulting value and
1614
* is not NULL.
1615
*
1616
* Now create an xsltStackElemPtr for insertion into the context's
1617
* global variable/parameter hash table.
1618
*/
1619
1620
#ifdef WITH_XSLT_DEBUG_VARIABLE
1621
#ifdef LIBXML_DEBUG_ENABLED
1622
if ((xsltGenericDebugContext == stdout) ||
1623
(xsltGenericDebugContext == stderr))
1624
xmlXPathDebugDumpObject((FILE *)xsltGenericDebugContext,
1625
result, 0);
1626
#endif
1627
#endif
1628
1629
elem = xsltNewStackElem(NULL);
1630
if (elem != NULL) {
1631
elem->name = name;
1632
elem->select = xmlDictLookup(ctxt->dict, value, -1);
1633
if (href != NULL)
1634
elem->nameURI = xmlDictLookup(ctxt->dict, href, -1);
1635
elem->tree = NULL;
1636
elem->computed = 1;
1637
if (eval == 0) {
1638
elem->value = xmlXPathNewString(value);
1639
}
1640
else {
1641
elem->value = result;
1642
}
1643
}
1644
1645
/*
1646
* Global parameters are stored in the XPath context variables pool.
1647
*/
1648
1649
res = xmlHashAddEntry2(ctxt->globalVars, name, href, elem);
1650
if (res != 0) {
1651
xsltFreeStackElem(elem);
1652
xsltTransformError(ctxt, style, NULL,
1653
"Global parameter %s already defined\n", name);
1654
}
1655
return(0);
1656
}
1657
1658
/**
1659
* xsltEvalUserParams:
1660
*
1661
* @ctxt: the XSLT transformation context
1662
* @params: a NULL terminated array of parameters name/value tuples
1663
*
1664
* Evaluate the global variables of a stylesheet. This needs to be
1665
* done on parsed stylesheets before starting to apply transformations.
1666
* Each of the parameters is evaluated as an XPath expression and stored
1667
* in the global variables/parameter hash table. If you want your
1668
* parameter used literally, use xsltQuoteUserParams.
1669
*
1670
* Returns 0 in case of success, -1 in case of error
1671
*/
1672
1673
int
1674
xsltEvalUserParams(xsltTransformContextPtr ctxt, const char **params) {
1675
size_t indx = 0;
1676
const xmlChar *name;
1677
const xmlChar *value;
1678
1679
if (params == NULL)
1680
return(0);
1681
while (params[indx] != NULL) {
1682
name = (const xmlChar *) params[indx++];
1683
value = (const xmlChar *) params[indx++];
1684
if (xsltEvalOneUserParam(ctxt, name, value) != 0)
1685
return(-1);
1686
}
1687
return 0;
1688
}
1689
1690
/**
1691
* xsltQuoteUserParams:
1692
*
1693
* @ctxt: the XSLT transformation context
1694
* @params: a NULL terminated arry of parameters names/values tuples
1695
*
1696
* Similar to xsltEvalUserParams, but the values are treated literally and
1697
* are * *not* evaluated as XPath expressions. This should be done on parsed
1698
* stylesheets before starting to apply transformations.
1699
*
1700
* Returns 0 in case of success, -1 in case of error.
1701
*/
1702
1703
int
1704
xsltQuoteUserParams(xsltTransformContextPtr ctxt, const char **params) {
1705
size_t indx = 0;
1706
const xmlChar *name;
1707
const xmlChar *value;
1708
1709
if (params == NULL)
1710
return(0);
1711
while (params[indx] != NULL) {
1712
name = (const xmlChar *) params[indx++];
1713
value = (const xmlChar *) params[indx++];
1714
if (xsltQuoteOneUserParam(ctxt, name, value) != 0)
1715
return(-1);
1716
}
1717
return 0;
1718
}
1719
1720
/**
1721
* xsltEvalOneUserParam:
1722
* @ctxt: the XSLT transformation context
1723
* @name: a null terminated string giving the name of the parameter
1724
* @value: a null terminated string giving the XPath expression to be evaluated
1725
*
1726
* This is normally called from xsltEvalUserParams to process a single
1727
* parameter from a list of parameters. The @value is evaluated as an
1728
* XPath expression and the result is stored in the context's global
1729
* variable/parameter hash table.
1730
*
1731
* To have a parameter treated literally (not as an XPath expression)
1732
* use xsltQuoteUserParams (or xsltQuoteOneUserParam). For more
1733
* details see description of xsltProcessOneUserParamInternal.
1734
*
1735
* Returns 0 in case of success, -1 in case of error.
1736
*/
1737
1738
int
1739
xsltEvalOneUserParam(xsltTransformContextPtr ctxt,
1740
const xmlChar * name,
1741
const xmlChar * value) {
1742
return xsltProcessUserParamInternal(ctxt, name, value,
1743
1 /* xpath eval ? */);
1744
}
1745
1746
/**
1747
* xsltQuoteOneUserParam:
1748
* @ctxt: the XSLT transformation context
1749
* @name: a null terminated string giving the name of the parameter
1750
* @value: a null terminated string giving the parameter value
1751
*
1752
* This is normally called from xsltQuoteUserParams to process a single
1753
* parameter from a list of parameters. The @value is stored in the
1754
* context's global variable/parameter hash table.
1755
*
1756
* Returns 0 in case of success, -1 in case of error.
1757
*/
1758
1759
int
1760
xsltQuoteOneUserParam(xsltTransformContextPtr ctxt,
1761
const xmlChar * name,
1762
const xmlChar * value) {
1763
return xsltProcessUserParamInternal(ctxt, name, value,
1764
0 /* xpath eval ? */);
1765
}
1766
1767
/**
1768
* xsltBuildVariable:
1769
* @ctxt: the XSLT transformation context
1770
* @comp: the precompiled form
1771
* @tree: the tree if select is NULL
1772
*
1773
* Computes a new variable value.
1774
*
1775
* Returns the xsltStackElemPtr or NULL in case of error
1776
*/
1777
static xsltStackElemPtr
1778
xsltBuildVariable(xsltTransformContextPtr ctxt,
1779
xsltStylePreCompPtr castedComp,
1780
xmlNodePtr tree)
1781
{
1782
#ifdef XSLT_REFACTORED
1783
xsltStyleBasicItemVariablePtr comp =
1784
(xsltStyleBasicItemVariablePtr) castedComp;
1785
#else
1786
xsltStylePreCompPtr comp = castedComp;
1787
#endif
1788
xsltStackElemPtr elem;
1789
1790
#ifdef WITH_XSLT_DEBUG_VARIABLE
1791
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
1792
"Building variable %s", comp->name));
1793
if (comp->select != NULL)
1794
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
1795
" select %s", comp->select));
1796
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, "\n"));
1797
#endif
1798
1799
elem = xsltNewStackElem(ctxt);
1800
if (elem == NULL)
1801
return(NULL);
1802
elem->comp = (xsltStylePreCompPtr) comp;
1803
elem->name = comp->name;
1804
elem->select = comp->select;
1805
elem->nameURI = comp->ns;
1806
elem->tree = tree;
1807
elem->value = xsltEvalVariable(ctxt, elem,
1808
(xsltStylePreCompPtr) comp);
1809
elem->computed = 1;
1810
return(elem);
1811
}
1812
1813
/**
1814
* xsltRegisterVariable:
1815
* @ctxt: the XSLT transformation context
1816
* @comp: the compiled XSLT-variable (or param) instruction
1817
* @tree: the tree if select is NULL
1818
* @isParam: indicates if this is a parameter
1819
*
1820
* Computes and registers a new variable.
1821
*
1822
* Returns 0 in case of success, -1 in case of error
1823
*/
1824
static int
1825
xsltRegisterVariable(xsltTransformContextPtr ctxt,
1826
xsltStylePreCompPtr castedComp,
1827
xmlNodePtr tree, int isParam)
1828
{
1829
#ifdef XSLT_REFACTORED
1830
xsltStyleBasicItemVariablePtr comp =
1831
(xsltStyleBasicItemVariablePtr) castedComp;
1832
#else
1833
xsltStylePreCompPtr comp = castedComp;
1834
int present;
1835
#endif
1836
xsltStackElemPtr variable;
1837
1838
#ifdef XSLT_REFACTORED
1839
/*
1840
* REFACTORED NOTE: Redefinitions of vars/params are checked
1841
* at compilation time in the refactored code.
1842
* xsl:with-param parameters are checked in xsltApplyXSLTTemplate().
1843
*/
1844
#else
1845
present = xsltCheckStackElem(ctxt, comp->name, comp->ns);
1846
if (isParam == 0) {
1847
if ((present != 0) && (present != 3)) {
1848
/* TODO: report QName. */
1849
xsltTransformError(ctxt, NULL, comp->inst,
1850
"XSLT-variable: Redefinition of variable '%s'.\n", comp->name);
1851
return(0);
1852
}
1853
} else if (present != 0) {
1854
if ((present == 1) || (present == 2)) {
1855
/* TODO: report QName. */
1856
xsltTransformError(ctxt, NULL, comp->inst,
1857
"XSLT-param: Redefinition of parameter '%s'.\n", comp->name);
1858
return(0);
1859
}
1860
#ifdef WITH_XSLT_DEBUG_VARIABLE
1861
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
1862
"param %s defined by caller\n", comp->name));
1863
#endif
1864
return(0);
1865
}
1866
#endif /* else of XSLT_REFACTORED */
1867
1868
variable = xsltBuildVariable(ctxt, (xsltStylePreCompPtr) comp, tree);
1869
if (xsltAddStackElem(ctxt, variable) < 0) {
1870
xsltFreeStackElem(variable);
1871
return(-1);
1872
}
1873
return(0);
1874
}
1875
1876
/**
1877
* xsltGlobalVariableLookup:
1878
* @ctxt: the XSLT transformation context
1879
* @name: the variable name
1880
* @ns_uri: the variable namespace URI
1881
*
1882
* Search in the Variable array of the context for the given
1883
* variable value.
1884
*
1885
* Returns the value or NULL if not found
1886
*/
1887
static xmlXPathObjectPtr
1888
xsltGlobalVariableLookup(xsltTransformContextPtr ctxt, const xmlChar *name,
1889
const xmlChar *ns_uri) {
1890
xsltStackElemPtr elem;
1891
xmlXPathObjectPtr ret = NULL;
1892
1893
/*
1894
* Lookup the global variables in XPath global variable hash table
1895
*/
1896
if ((ctxt->xpathCtxt == NULL) || (ctxt->globalVars == NULL))
1897
return(NULL);
1898
elem = (xsltStackElemPtr)
1899
xmlHashLookup2(ctxt->globalVars, name, ns_uri);
1900
if (elem == NULL) {
1901
#ifdef WITH_XSLT_DEBUG_VARIABLE
1902
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
1903
"global variable not found %s\n", name));
1904
#endif
1905
return(NULL);
1906
}
1907
/*
1908
* URGENT TODO: Move the detection of recursive definitions
1909
* to compile-time.
1910
*/
1911
if (elem->computed == 0) {
1912
if (elem->name == xsltComputingGlobalVarMarker) {
1913
xsltTransformError(ctxt, NULL, elem->comp->inst,
1914
"Recursive definition of %s\n", name);
1915
return(NULL);
1916
}
1917
ret = xsltEvalGlobalVariable(elem, ctxt);
1918
} else
1919
ret = elem->value;
1920
return(xmlXPathObjectCopy(ret));
1921
}
1922
1923
/**
1924
* xsltVariableLookup:
1925
* @ctxt: the XSLT transformation context
1926
* @name: the variable name
1927
* @ns_uri: the variable namespace URI
1928
*
1929
* Search in the Variable array of the context for the given
1930
* variable value.
1931
*
1932
* Returns the value or NULL if not found
1933
*/
1934
xmlXPathObjectPtr
1935
xsltVariableLookup(xsltTransformContextPtr ctxt, const xmlChar *name,
1936
const xmlChar *ns_uri) {
1937
xsltStackElemPtr elem;
1938
1939
if (ctxt == NULL)
1940
return(NULL);
1941
1942
elem = xsltStackLookup(ctxt, name, ns_uri);
1943
if (elem == NULL) {
1944
return(xsltGlobalVariableLookup(ctxt, name, ns_uri));
1945
}
1946
if (elem->computed == 0) {
1947
#ifdef WITH_XSLT_DEBUG_VARIABLE
1948
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
1949
"uncomputed variable %s\n", name));
1950
#endif
1951
elem->value = xsltEvalVariable(ctxt, elem, NULL);
1952
elem->computed = 1;
1953
}
1954
if (elem->value != NULL)
1955
return(xmlXPathObjectCopy(elem->value));
1956
#ifdef WITH_XSLT_DEBUG_VARIABLE
1957
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
1958
"variable not found %s\n", name));
1959
#endif
1960
return(NULL);
1961
}
1962
1963
/**
1964
* xsltParseStylesheetCallerParam:
1965
* @ctxt: the XSLT transformation context
1966
* @inst: the xsl:with-param instruction element
1967
*
1968
* Processes an xsl:with-param instruction at transformation time.
1969
* The value is computed, but not recorded.
1970
* NOTE that this is also called with an *xsl:param* element
1971
* from exsltFuncFunctionFunction().
1972
*
1973
* Returns the new xsltStackElemPtr or NULL
1974
*/
1975
1976
xsltStackElemPtr
1977
xsltParseStylesheetCallerParam(xsltTransformContextPtr ctxt, xmlNodePtr inst)
1978
{
1979
#ifdef XSLT_REFACTORED
1980
xsltStyleBasicItemVariablePtr comp;
1981
#else
1982
xsltStylePreCompPtr comp;
1983
#endif
1984
xmlNodePtr tree = NULL; /* The first child node of the instruction or
1985
the instruction itself. */
1986
xsltStackElemPtr param = NULL;
1987
1988
if ((ctxt == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
1989
return(NULL);
1990
1991
#ifdef XSLT_REFACTORED
1992
comp = (xsltStyleBasicItemVariablePtr) inst->psvi;
1993
#else
1994
comp = (xsltStylePreCompPtr) inst->psvi;
1995
#endif
1996
1997
if (comp == NULL) {
1998
xsltTransformError(ctxt, NULL, inst,
1999
"Internal error in xsltParseStylesheetCallerParam(): "
2000
"The XSLT 'with-param' instruction was not compiled.\n");
2001
return(NULL);
2002
}
2003
if (comp->name == NULL) {
2004
xsltTransformError(ctxt, NULL, inst,
2005
"Internal error in xsltParseStylesheetCallerParam(): "
2006
"XSLT 'with-param': The attribute 'name' was not compiled.\n");
2007
return(NULL);
2008
}
2009
2010
#ifdef WITH_XSLT_DEBUG_VARIABLE
2011
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
2012
"Handling xsl:with-param %s\n", comp->name));
2013
#endif
2014
2015
if (comp->select == NULL) {
2016
tree = inst->children;
2017
} else {
2018
#ifdef WITH_XSLT_DEBUG_VARIABLE
2019
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
2020
" select %s\n", comp->select));
2021
#endif
2022
tree = inst;
2023
}
2024
2025
param = xsltBuildVariable(ctxt, (xsltStylePreCompPtr) comp, tree);
2026
2027
return(param);
2028
}
2029
2030
/**
2031
* xsltParseGlobalVariable:
2032
* @style: the XSLT stylesheet
2033
* @cur: the "variable" element
2034
*
2035
* Parses a global XSLT 'variable' declaration at compilation time
2036
* and registers it
2037
*/
2038
void
2039
xsltParseGlobalVariable(xsltStylesheetPtr style, xmlNodePtr cur)
2040
{
2041
#ifdef XSLT_REFACTORED
2042
xsltStyleItemVariablePtr comp;
2043
#else
2044
xsltStylePreCompPtr comp;
2045
#endif
2046
2047
if ((cur == NULL) || (style == NULL) || (cur->type != XML_ELEMENT_NODE))
2048
return;
2049
2050
#ifdef XSLT_REFACTORED
2051
/*
2052
* Note that xsltStylePreCompute() will be called from
2053
* xslt.c only.
2054
*/
2055
comp = (xsltStyleItemVariablePtr) cur->psvi;
2056
#else
2057
xsltStylePreCompute(style, cur);
2058
comp = (xsltStylePreCompPtr) cur->psvi;
2059
#endif
2060
if (comp == NULL) {
2061
xsltTransformError(NULL, style, cur,
2062
"xsl:variable : compilation failed\n");
2063
return;
2064
}
2065
2066
if (comp->name == NULL) {
2067
xsltTransformError(NULL, style, cur,
2068
"xsl:variable : missing name attribute\n");
2069
return;
2070
}
2071
2072
/*
2073
* Parse the content (a sequence constructor) of xsl:variable.
2074
*/
2075
if (cur->children != NULL) {
2076
#ifdef XSLT_REFACTORED
2077
xsltParseSequenceConstructor(XSLT_CCTXT(style), cur->children);
2078
#else
2079
xsltParseTemplateContent(style, cur);
2080
#endif
2081
}
2082
#ifdef WITH_XSLT_DEBUG_VARIABLE
2083
xsltGenericDebug(xsltGenericDebugContext,
2084
"Registering global variable %s\n", comp->name);
2085
#endif
2086
2087
xsltRegisterGlobalVariable(style, comp->name, comp->ns,
2088
comp->select, cur->children, (xsltStylePreCompPtr) comp,
2089
NULL);
2090
}
2091
2092
/**
2093
* xsltParseGlobalParam:
2094
* @style: the XSLT stylesheet
2095
* @cur: the "param" element
2096
*
2097
* parse an XSLT transformation param declaration and record
2098
* its value.
2099
*/
2100
2101
void
2102
xsltParseGlobalParam(xsltStylesheetPtr style, xmlNodePtr cur) {
2103
#ifdef XSLT_REFACTORED
2104
xsltStyleItemParamPtr comp;
2105
#else
2106
xsltStylePreCompPtr comp;
2107
#endif
2108
2109
if ((cur == NULL) || (style == NULL) || (cur->type != XML_ELEMENT_NODE))
2110
return;
2111
2112
#ifdef XSLT_REFACTORED
2113
/*
2114
* Note that xsltStylePreCompute() will be called from
2115
* xslt.c only.
2116
*/
2117
comp = (xsltStyleItemParamPtr) cur->psvi;
2118
#else
2119
xsltStylePreCompute(style, cur);
2120
comp = (xsltStylePreCompPtr) cur->psvi;
2121
#endif
2122
if (comp == NULL) {
2123
xsltTransformError(NULL, style, cur,
2124
"xsl:param : compilation failed\n");
2125
return;
2126
}
2127
2128
if (comp->name == NULL) {
2129
xsltTransformError(NULL, style, cur,
2130
"xsl:param : missing name attribute\n");
2131
return;
2132
}
2133
2134
/*
2135
* Parse the content (a sequence constructor) of xsl:param.
2136
*/
2137
if (cur->children != NULL) {
2138
#ifdef XSLT_REFACTORED
2139
xsltParseSequenceConstructor(XSLT_CCTXT(style), cur->children);
2140
#else
2141
xsltParseTemplateContent(style, cur);
2142
#endif
2143
}
2144
2145
#ifdef WITH_XSLT_DEBUG_VARIABLE
2146
xsltGenericDebug(xsltGenericDebugContext,
2147
"Registering global param %s\n", comp->name);
2148
#endif
2149
2150
xsltRegisterGlobalVariable(style, comp->name, comp->ns,
2151
comp->select, cur->children, (xsltStylePreCompPtr) comp,
2152
NULL);
2153
}
2154
2155
/**
2156
* xsltParseStylesheetVariable:
2157
* @ctxt: the XSLT transformation context
2158
* @inst: the xsl:variable instruction element
2159
*
2160
* Registers a local XSLT 'variable' instruction at transformation time
2161
* and evaluates its value.
2162
*/
2163
void
2164
xsltParseStylesheetVariable(xsltTransformContextPtr ctxt, xmlNodePtr inst)
2165
{
2166
#ifdef XSLT_REFACTORED
2167
xsltStyleItemVariablePtr comp;
2168
#else
2169
xsltStylePreCompPtr comp;
2170
#endif
2171
2172
if ((inst == NULL) || (ctxt == NULL) || (inst->type != XML_ELEMENT_NODE))
2173
return;
2174
2175
comp = inst->psvi;
2176
if (comp == NULL) {
2177
xsltTransformError(ctxt, NULL, inst,
2178
"Internal error in xsltParseStylesheetVariable(): "
2179
"The XSLT 'variable' instruction was not compiled.\n");
2180
return;
2181
}
2182
if (comp->name == NULL) {
2183
xsltTransformError(ctxt, NULL, inst,
2184
"Internal error in xsltParseStylesheetVariable(): "
2185
"The attribute 'name' was not compiled.\n");
2186
return;
2187
}
2188
2189
#ifdef WITH_XSLT_DEBUG_VARIABLE
2190
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
2191
"Registering variable '%s'\n", comp->name));
2192
#endif
2193
2194
xsltRegisterVariable(ctxt, (xsltStylePreCompPtr) comp, inst->children, 0);
2195
}
2196
2197
/**
2198
* xsltParseStylesheetParam:
2199
* @ctxt: the XSLT transformation context
2200
* @cur: the XSLT 'param' element
2201
*
2202
* Registers a local XSLT 'param' declaration at transformation time and
2203
* evaluates its value.
2204
*/
2205
void
2206
xsltParseStylesheetParam(xsltTransformContextPtr ctxt, xmlNodePtr cur)
2207
{
2208
#ifdef XSLT_REFACTORED
2209
xsltStyleItemParamPtr comp;
2210
#else
2211
xsltStylePreCompPtr comp;
2212
#endif
2213
2214
if ((cur == NULL) || (ctxt == NULL) || (cur->type != XML_ELEMENT_NODE))
2215
return;
2216
2217
comp = cur->psvi;
2218
if ((comp == NULL) || (comp->name == NULL)) {
2219
xsltTransformError(ctxt, NULL, cur,
2220
"Internal error in xsltParseStylesheetParam(): "
2221
"The XSLT 'param' declaration was not compiled correctly.\n");
2222
return;
2223
}
2224
2225
#ifdef WITH_XSLT_DEBUG_VARIABLE
2226
XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
2227
"Registering param %s\n", comp->name));
2228
#endif
2229
2230
xsltRegisterVariable(ctxt, (xsltStylePreCompPtr) comp, cur->children, 1);
2231
}
2232
2233
/**
2234
* xsltFreeGlobalVariables:
2235
* @ctxt: the XSLT transformation context
2236
*
2237
* Free up the data associated to the global variables
2238
* its value.
2239
*/
2240
2241
void
2242
xsltFreeGlobalVariables(xsltTransformContextPtr ctxt) {
2243
xmlHashFree(ctxt->globalVars, xsltFreeStackElemEntry);
2244
}
2245
2246
/**
2247
* xsltXPathVariableLookup:
2248
* @ctxt: a void * but the the XSLT transformation context actually
2249
* @name: the variable name
2250
* @ns_uri: the variable namespace URI
2251
*
2252
* This is the entry point when a varibale is needed by the XPath
2253
* interpretor.
2254
*
2255
* Returns the value or NULL if not found
2256
*/
2257
xmlXPathObjectPtr
2258
xsltXPathVariableLookup(void *ctxt, const xmlChar *name,
2259
const xmlChar *ns_uri) {
2260
xsltTransformContextPtr tctxt;
2261
xmlXPathObjectPtr valueObj = NULL;
2262
2263
if ((ctxt == NULL) || (name == NULL))
2264
return(NULL);
2265
2266
#ifdef WITH_XSLT_DEBUG_VARIABLE
2267
XSLT_TRACE(((xsltTransformContextPtr)ctxt),XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
2268
"Lookup variable '%s'\n", name));
2269
#endif
2270
2271
tctxt = (xsltTransformContextPtr) ctxt;
2272
/*
2273
* Local variables/params ---------------------------------------------
2274
*
2275
* Do the lookup from the top of the stack, but
2276
* don't use params being computed in a call-param
2277
* First lookup expects the variable name and URI to
2278
* come from the disctionnary and hence pointer comparison.
2279
*/
2280
if (tctxt->varsNr != 0) {
2281
int i;
2282
xsltStackElemPtr variable = NULL, cur;
2283
2284
for (i = tctxt->varsNr; i > tctxt->varsBase; i--) {
2285
cur = tctxt->varsTab[i-1];
2286
if ((cur->name == name) && (cur->nameURI == ns_uri)) {
2287
variable = cur;
2288
goto local_variable_found;
2289
}
2290
cur = cur->next;
2291
}
2292
/*
2293
* Redo the lookup with interned strings to avoid string comparison.
2294
*
2295
* OPTIMIZE TODO: The problem here is, that if we request a
2296
* global variable, then this will be also executed.
2297
*/
2298
{
2299
const xmlChar *tmpName = name, *tmpNsName = ns_uri;
2300
2301
name = xmlDictLookup(tctxt->dict, name, -1);
2302
if (ns_uri)
2303
ns_uri = xmlDictLookup(tctxt->dict, ns_uri, -1);
2304
if ((tmpName != name) || (tmpNsName != ns_uri)) {
2305
for (i = tctxt->varsNr; i > tctxt->varsBase; i--) {
2306
cur = tctxt->varsTab[i-1];
2307
if ((cur->name == name) && (cur->nameURI == ns_uri)) {
2308
variable = cur;
2309
goto local_variable_found;
2310
}
2311
}
2312
}
2313
}
2314
2315
local_variable_found:
2316
2317
if (variable) {
2318
if (variable->computed == 0) {
2319
2320
#ifdef WITH_XSLT_DEBUG_VARIABLE
2321
XSLT_TRACE(tctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
2322
"uncomputed variable '%s'\n", name));
2323
#endif
2324
variable->value = xsltEvalVariable(tctxt, variable, NULL);
2325
variable->computed = 1;
2326
}
2327
if (variable->value != NULL) {
2328
valueObj = xmlXPathObjectCopy(variable->value);
2329
}
2330
return(valueObj);
2331
}
2332
}
2333
/*
2334
* Global variables/params --------------------------------------------
2335
*/
2336
if (tctxt->globalVars) {
2337
valueObj = xsltGlobalVariableLookup(tctxt, name, ns_uri);
2338
}
2339
2340
if (valueObj == NULL) {
2341
2342
#ifdef WITH_XSLT_DEBUG_VARIABLE
2343
XSLT_TRACE(tctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
2344
"variable not found '%s'\n", name));
2345
#endif
2346
2347
if (ns_uri) {
2348
xsltTransformError(tctxt, NULL, tctxt->inst,
2349
"Variable '{%s}%s' has not been declared.\n", ns_uri, name);
2350
} else {
2351
xsltTransformError(tctxt, NULL, tctxt->inst,
2352
"Variable '%s' has not been declared.\n", name);
2353
}
2354
} else {
2355
2356
#ifdef WITH_XSLT_DEBUG_VARIABLE
2357
XSLT_TRACE(tctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext,
2358
"found variable '%s'\n", name));
2359
#endif
2360
}
2361
2362
return(valueObj);
2363
}
2364
2365