[DEVENUM]
[reactos.git] / reactos / dll / 3rdparty / libxslt / namespaces.c
1 /*
2 * namespaces.c: Implementation of the XSLT namespaces handling
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 * daniel@veillard.com
10 */
11
12 #include "precomp.h"
13
14 /************************************************************************
15 * *
16 * Module interfaces *
17 * *
18 ************************************************************************/
19
20 #ifdef XSLT_REFACTORED
21 static xsltNsAliasPtr
22 xsltNewNsAlias(xsltCompilerCtxtPtr cctxt)
23 {
24 xsltNsAliasPtr ret;
25
26 if (cctxt == NULL)
27 return(NULL);
28
29 ret = (xsltNsAliasPtr) xmlMalloc(sizeof(xsltNsAlias));
30 if (ret == NULL) {
31 xsltTransformError(NULL, cctxt->style, NULL,
32 "Internal error in xsltNewNsAlias(): Memory allocation failed.\n");
33 cctxt->style->errors++;
34 return(NULL);
35 }
36 memset(ret, 0, sizeof(xsltNsAlias));
37 /*
38 * TODO: Store the item at current stylesheet-level.
39 */
40 ret->next = cctxt->nsAliases;
41 cctxt->nsAliases = ret;
42
43 return(ret);
44 }
45 #endif /* XSLT_REFACTORED */
46 /**
47 * xsltNamespaceAlias:
48 * @style: the XSLT stylesheet
49 * @node: the xsl:namespace-alias node
50 *
51 * Read the stylesheet-prefix and result-prefix attributes, register
52 * them as well as the corresponding namespace.
53 */
54 void
55 xsltNamespaceAlias(xsltStylesheetPtr style, xmlNodePtr node)
56 {
57 xmlChar *resultPrefix = NULL;
58 xmlChar *stylePrefix = NULL;
59 xmlNsPtr literalNs = NULL;
60 xmlNsPtr targetNs = NULL;
61
62 #ifdef XSLT_REFACTORED
63 xsltNsAliasPtr alias;
64
65 if ((style == NULL) || (node == NULL))
66 return;
67
68 /*
69 * SPEC XSLT 1.0:
70 * "If a namespace URI is declared to be an alias for multiple
71 * different namespace URIs, then the declaration with the highest
72 * import precedence is used. It is an error if there is more than
73 * one such declaration. An XSLT processor may signal the error;
74 * if it does not signal the error, it must recover by choosing,
75 * from amongst the declarations with the highest import precedence,
76 * the one that occurs last in the stylesheet."
77 *
78 * SPEC TODO: Check for the errors mentioned above.
79 */
80 /*
81 * NOTE that the XSLT 2.0 also *does* use the NULL namespace if
82 * "#default" is used and there's no default namespace is scope.
83 * I.e., this is *not* an error.
84 * Most XSLT 1.0 implementations work this way.
85 * The XSLT 1.0 spec has nothing to say on the subject.
86 */
87 /*
88 * Attribute "stylesheet-prefix".
89 */
90 stylePrefix = xmlGetNsProp(node, (const xmlChar *)"stylesheet-prefix", NULL);
91 if (stylePrefix == NULL) {
92 xsltTransformError(NULL, style, node,
93 "The attribute 'stylesheet-prefix' is missing.\n");
94 return;
95 }
96 if (xmlStrEqual(stylePrefix, (const xmlChar *)"#default"))
97 literalNs = xmlSearchNs(node->doc, node, NULL);
98 else {
99 literalNs = xmlSearchNs(node->doc, node, stylePrefix);
100 if (literalNs == NULL) {
101 xsltTransformError(NULL, style, node,
102 "Attribute 'stylesheet-prefix': There's no namespace "
103 "declaration in scope for the prefix '%s'.\n",
104 stylePrefix);
105 goto error;
106 }
107 }
108 /*
109 * Attribute "result-prefix".
110 */
111 resultPrefix = xmlGetNsProp(node, (const xmlChar *)"result-prefix", NULL);
112 if (resultPrefix == NULL) {
113 xsltTransformError(NULL, style, node,
114 "The attribute 'result-prefix' is missing.\n");
115 goto error;
116 }
117 if (xmlStrEqual(resultPrefix, (const xmlChar *)"#default"))
118 targetNs = xmlSearchNs(node->doc, node, NULL);
119 else {
120 targetNs = xmlSearchNs(node->doc, node, resultPrefix);
121
122 if (targetNs == NULL) {
123 xsltTransformError(NULL, style, node,
124 "Attribute 'result-prefix': There's no namespace "
125 "declaration in scope for the prefix '%s'.\n",
126 stylePrefix);
127 goto error;
128 }
129 }
130 /*
131 *
132 * Same alias for multiple different target namespace URIs:
133 * TODO: The one with the highest import precedence is used.
134 * Example:
135 * <xsl:namespace-alias stylesheet-prefix="foo"
136 * result-prefix="bar"/>
137 *
138 * <xsl:namespace-alias stylesheet-prefix="foo"
139 * result-prefix="zar"/>
140 *
141 * Same target namespace URI for multiple different aliases:
142 * All alias-definitions will be used.
143 * Example:
144 * <xsl:namespace-alias stylesheet-prefix="bar"
145 * result-prefix="foo"/>
146 *
147 * <xsl:namespace-alias stylesheet-prefix="zar"
148 * result-prefix="foo"/>
149 * Cases using #default:
150 * <xsl:namespace-alias stylesheet-prefix="#default"
151 * result-prefix="#default"/>
152 * TODO: Has this an effect at all?
153 *
154 * <xsl:namespace-alias stylesheet-prefix="foo"
155 * result-prefix="#default"/>
156 * From namespace to no namespace.
157 *
158 * <xsl:namespace-alias stylesheet-prefix="#default"
159 * result-prefix="foo"/>
160 * From no namespace to namespace.
161 */
162
163
164 /*
165 * Store the ns-node in the alias-object.
166 */
167 alias = xsltNewNsAlias(XSLT_CCTXT(style));
168 if (alias == NULL)
169 return;
170 alias->literalNs = literalNs;
171 alias->targetNs = targetNs;
172 XSLT_CCTXT(style)->hasNsAliases = 1;
173
174
175 #else /* XSLT_REFACTORED */
176 const xmlChar *literalNsName;
177 const xmlChar *targetNsName;
178
179
180 if ((style == NULL) || (node == NULL))
181 return;
182
183 stylePrefix = xmlGetNsProp(node, (const xmlChar *)"stylesheet-prefix", NULL);
184 if (stylePrefix == NULL) {
185 xsltTransformError(NULL, style, node,
186 "namespace-alias: stylesheet-prefix attribute missing\n");
187 return;
188 }
189 resultPrefix = xmlGetNsProp(node, (const xmlChar *)"result-prefix", NULL);
190 if (resultPrefix == NULL) {
191 xsltTransformError(NULL, style, node,
192 "namespace-alias: result-prefix attribute missing\n");
193 goto error;
194 }
195
196 if (xmlStrEqual(stylePrefix, (const xmlChar *)"#default")) {
197 literalNs = xmlSearchNs(node->doc, node, NULL);
198 if (literalNs == NULL) {
199 literalNsName = NULL;
200 } else
201 literalNsName = literalNs->href; /* Yes - set for nsAlias table */
202 } else {
203 literalNs = xmlSearchNs(node->doc, node, stylePrefix);
204
205 if ((literalNs == NULL) || (literalNs->href == NULL)) {
206 xsltTransformError(NULL, style, node,
207 "namespace-alias: prefix %s not bound to any namespace\n",
208 stylePrefix);
209 goto error;
210 } else
211 literalNsName = literalNs->href;
212 }
213
214 /*
215 * When "#default" is used for result, if a default namespace has not
216 * been explicitly declared the special value UNDEFINED_DEFAULT_NS is
217 * put into the nsAliases table
218 */
219 if (xmlStrEqual(resultPrefix, (const xmlChar *)"#default")) {
220 targetNs = xmlSearchNs(node->doc, node, NULL);
221 if (targetNs == NULL) {
222 targetNsName = UNDEFINED_DEFAULT_NS;
223 } else
224 targetNsName = targetNs->href;
225 } else {
226 targetNs = xmlSearchNs(node->doc, node, resultPrefix);
227
228 if ((targetNs == NULL) || (targetNs->href == NULL)) {
229 xsltTransformError(NULL, style, node,
230 "namespace-alias: prefix %s not bound to any namespace\n",
231 resultPrefix);
232 goto error;
233 } else
234 targetNsName = targetNs->href;
235 }
236 /*
237 * Special case: if #default is used for
238 * the stylesheet-prefix (literal namespace) and there's no default
239 * namespace in scope, we'll use style->defaultAlias for this.
240 */
241 if (literalNsName == NULL) {
242 if (targetNs != NULL) {
243 /*
244 * BUG TODO: Is it not sufficient to have only 1 field for
245 * this, since subsequently alias declarations will
246 * overwrite this.
247 * Example:
248 * <xsl:namespace-alias result-prefix="foo"
249 * stylesheet-prefix="#default"/>
250 * <xsl:namespace-alias result-prefix="bar"
251 * stylesheet-prefix="#default"/>
252 * The mapping for "foo" won't be visible anymore.
253 */
254 style->defaultAlias = targetNs->href;
255 }
256 } else {
257 if (style->nsAliases == NULL)
258 style->nsAliases = xmlHashCreate(10);
259 if (style->nsAliases == NULL) {
260 xsltTransformError(NULL, style, node,
261 "namespace-alias: cannot create hash table\n");
262 goto error;
263 }
264 xmlHashAddEntry((xmlHashTablePtr) style->nsAliases,
265 literalNsName, (void *) targetNsName);
266 }
267 #endif /* else of XSLT_REFACTORED */
268
269 error:
270 if (stylePrefix != NULL)
271 xmlFree(stylePrefix);
272 if (resultPrefix != NULL)
273 xmlFree(resultPrefix);
274 }
275
276 /**
277 * xsltGetSpecialNamespace:
278 * @ctxt: the transformation context
279 * @invocNode: the invoking node; e.g. a literal result element/attr;
280 * only used for error reports
281 * @nsName: the namespace name (or NULL)
282 * @nsPrefix: the suggested namespace prefix (or NULL)
283 * @target: the result element on which to anchor a namespace
284 *
285 * Find a matching (prefix and ns-name) ns-declaration
286 * for the requested @nsName and @nsPrefix in the result tree.
287 * If none is found then a new ns-declaration will be
288 * added to @resultElem. If, in this case, the given prefix is
289 * already in use, then a ns-declaration with a modified ns-prefix
290 * be we created. Note that this function's priority is to
291 * preserve ns-prefixes; it will only change a prefix if there's
292 * a namespace clash.
293 * If both @nsName and @nsPrefix are NULL, then this will try to
294 * "undeclare" a default namespace by declaring an xmlns="".
295 *
296 * Returns a namespace declaration or NULL.
297 */
298 xmlNsPtr
299 xsltGetSpecialNamespace(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
300 const xmlChar *nsName, const xmlChar *nsPrefix,
301 xmlNodePtr target)
302 {
303 xmlNsPtr ns;
304 int prefixOccupied = 0;
305
306 if ((ctxt == NULL) || (target == NULL) ||
307 (target->type != XML_ELEMENT_NODE))
308 return(NULL);
309
310 /*
311 * NOTE: Namespace exclusion and ns-aliasing is performed at
312 * compilation-time in the refactored code; so this need not be done
313 * here (it was in the old code).
314 * NOTE: @invocNode was named @cur in the old code and was documented to
315 * be an input node; since it was only used to anchor an error report
316 * somewhere, we can safely change this to @invocNode, which now
317 * will be the XSLT instruction (also a literal result element/attribute),
318 * which was responsible for this call.
319 */
320 /*
321 * OPTIMIZE TODO: This all could be optimized by keeping track of
322 * the ns-decls currently in-scope via a specialized context.
323 */
324 if ((nsPrefix == NULL) && ((nsName == NULL) || (nsName[0] == 0))) {
325 /*
326 * NOTE: the "undeclaration" of the default namespace was
327 * part of the logic of the old xsltGetSpecialNamespace() code,
328 * so we'll keep that mechanism.
329 * Related to the old code: bug #302020:
330 */
331 /*
332 * OPTIMIZE TODO: This all could be optimized by keeping track of
333 * the ns-decls currently in-scope via a specialized context.
334 */
335 /*
336 * Search on the result element itself.
337 */
338 if (target->nsDef != NULL) {
339 ns = target->nsDef;
340 do {
341 if (ns->prefix == NULL) {
342 if ((ns->href != NULL) && (ns->href[0] != 0)) {
343 /*
344 * Raise a namespace normalization error.
345 */
346 xsltTransformError(ctxt, NULL, invocNode,
347 "Namespace normalization error: Cannot undeclare "
348 "the default namespace, since the default namespace "
349 "'%s' is already declared on the result element "
350 "'%s'.\n", ns->href, target->name);
351 return(NULL);
352 } else {
353 /*
354 * The default namespace was undeclared on the
355 * result element.
356 */
357 return(NULL);
358 }
359 break;
360 }
361 ns = ns->next;
362 } while (ns != NULL);
363 }
364 if ((target->parent != NULL) &&
365 (target->parent->type == XML_ELEMENT_NODE))
366 {
367 /*
368 * The parent element is in no namespace, so assume
369 * that there is no default namespace in scope.
370 */
371 if (target->parent->ns == NULL)
372 return(NULL);
373
374 ns = xmlSearchNs(target->doc, target->parent,
375 NULL);
376 /*
377 * Fine if there's no default ns is scope, or if the
378 * default ns was undeclared.
379 */
380 if ((ns == NULL) || (ns->href == NULL) || (ns->href[0] == 0))
381 return(NULL);
382
383 /*
384 * Undeclare the default namespace.
385 */
386 xmlNewNs(target, BAD_CAST "", NULL);
387 /* TODO: Check result */
388 return(NULL);
389 }
390 return(NULL);
391 }
392 /*
393 * Handle the XML namespace.
394 * QUESTION: Is this faster than using xmlStrEqual() anyway?
395 */
396 if ((nsPrefix != NULL) &&
397 (nsPrefix[0] == 'x') && (nsPrefix[1] == 'm') &&
398 (nsPrefix[2] == 'l') && (nsPrefix[3] == 0))
399 {
400 return(xmlSearchNs(target->doc, target, nsPrefix));
401 }
402 /*
403 * First: search on the result element itself.
404 */
405 if (target->nsDef != NULL) {
406 ns = target->nsDef;
407 do {
408 if ((ns->prefix == NULL) == (nsPrefix == NULL)) {
409 if (ns->prefix == nsPrefix) {
410 if (xmlStrEqual(ns->href, nsName))
411 return(ns);
412 prefixOccupied = 1;
413 break;
414 } else if (xmlStrEqual(ns->prefix, nsPrefix)) {
415 if (xmlStrEqual(ns->href, nsName))
416 return(ns);
417 prefixOccupied = 1;
418 break;
419 }
420 }
421 ns = ns->next;
422 } while (ns != NULL);
423 }
424 if (prefixOccupied) {
425 /*
426 * If the ns-prefix is occupied by an other ns-decl on the
427 * result element, then this means:
428 * 1) The desired prefix is shadowed
429 * 2) There's no way around changing the prefix
430 *
431 * Try a desperate search for an in-scope ns-decl
432 * with a matching ns-name before we use the last option,
433 * which is to recreate the ns-decl with a modified prefix.
434 */
435 ns = xmlSearchNsByHref(target->doc, target, nsName);
436 if (ns != NULL)
437 return(ns);
438
439 /*
440 * Fallback to changing the prefix.
441 */
442 } else if ((target->parent != NULL) &&
443 (target->parent->type == XML_ELEMENT_NODE))
444 {
445 /*
446 * Try to find a matching ns-decl in the ancestor-axis.
447 *
448 * Check the common case: The parent element of the current
449 * result element is in the same namespace (with an equal ns-prefix).
450 */
451 if ((target->parent->ns != NULL) &&
452 ((target->parent->ns->prefix != NULL) == (nsPrefix != NULL)))
453 {
454 ns = target->parent->ns;
455
456 if (nsPrefix == NULL) {
457 if (xmlStrEqual(ns->href, nsName))
458 return(ns);
459 } else if (xmlStrEqual(ns->prefix, nsPrefix) &&
460 xmlStrEqual(ns->href, nsName))
461 {
462 return(ns);
463 }
464 }
465 /*
466 * Lookup the remaining in-scope namespaces.
467 */
468 ns = xmlSearchNs(target->doc, target->parent, nsPrefix);
469 if (ns != NULL) {
470 if (xmlStrEqual(ns->href, nsName))
471 return(ns);
472 /*
473 * Now check for a nasty case: We need to ensure that the new
474 * ns-decl won't shadow a prefix in-use by an existing attribute.
475 * <foo xmlns:a="urn:test:a">
476 * <bar a:a="val-a">
477 * <xsl:attribute xmlns:a="urn:test:b" name="a:b">
478 * val-b</xsl:attribute>
479 * </bar>
480 * </foo>
481 */
482 if (target->properties) {
483 xmlAttrPtr attr = target->properties;
484 do {
485 if ((attr->ns) &&
486 xmlStrEqual(attr->ns->prefix, nsPrefix))
487 {
488 /*
489 * Bad, this prefix is already in use.
490 * Since we'll change the prefix anyway, try
491 * a search for a matching ns-decl based on the
492 * namespace name.
493 */
494 ns = xmlSearchNsByHref(target->doc, target, nsName);
495 if (ns != NULL)
496 return(ns);
497 goto declare_new_prefix;
498 }
499 attr = attr->next;
500 } while (attr != NULL);
501 }
502 } else {
503 /*
504 * Either no matching ns-prefix was found or the namespace is
505 * shadowed.
506 * Create a new ns-decl on the current result element.
507 *
508 * Hmm, we could also try to reuse an in-scope
509 * namespace with a matching ns-name but a different
510 * ns-prefix.
511 * What has higher priority?
512 * 1) If keeping the prefix: create a new ns-decl.
513 * 2) If reusal: first lookup ns-names; then fallback
514 * to creation of a new ns-decl.
515 * REVISIT: this currently uses case 1) although
516 * the old way was use xmlSearchNsByHref() and to let change
517 * the prefix.
518 */
519 #if 0
520 ns = xmlSearchNsByHref(target->doc, target, nsName);
521 if (ns != NULL)
522 return(ns);
523 #endif
524 }
525 /*
526 * Create the ns-decl on the current result element.
527 */
528 ns = xmlNewNs(target, nsName, nsPrefix);
529 /* TODO: check errors */
530 return(ns);
531 } else {
532 /*
533 * This is either the root of the tree or something weird is going on.
534 */
535 ns = xmlNewNs(target, nsName, nsPrefix);
536 /* TODO: Check result */
537 return(ns);
538 }
539
540 declare_new_prefix:
541 /*
542 * Fallback: we need to generate a new prefix and declare the namespace
543 * on the result element.
544 */
545 {
546 xmlChar pref[30];
547 int counter = 1;
548
549 if (nsPrefix == NULL) {
550 nsPrefix = BAD_CAST "ns";
551 }
552
553 do {
554 snprintf((char *) pref, 30, "%s_%d", nsPrefix, counter++);
555 ns = xmlSearchNs(target->doc, target, BAD_CAST pref);
556 if (counter > 1000) {
557 xsltTransformError(ctxt, NULL, invocNode,
558 "Internal error in xsltAcquireResultInScopeNs(): "
559 "Failed to compute a unique ns-prefix for the "
560 "generated element");
561 return(NULL);
562 }
563 } while (ns != NULL);
564 ns = xmlNewNs(target, nsName, BAD_CAST pref);
565 /* TODO: Check result */
566 return(ns);
567 }
568 return(NULL);
569 }
570
571 /**
572 * xsltGetNamespace:
573 * @ctxt: a transformation context
574 * @cur: the input node
575 * @ns: the namespace
576 * @out: the output node (or its parent)
577 *
578 * Find a matching (prefix and ns-name) ns-declaration
579 * for the requested @ns->prefix and @ns->href in the result tree.
580 * If none is found then a new ns-declaration will be
581 * added to @resultElem. If, in this case, the given prefix is
582 * already in use, then a ns-declaration with a modified ns-prefix
583 * be we created.
584 *
585 * Called by:
586 * - xsltCopyPropList() (*not* anymore)
587 * - xsltShallowCopyElement()
588 * - xsltCopyTreeInternal() (*not* anymore)
589 * - xsltApplySequenceConstructor() (*not* in the refactored code),
590 * - xsltElement() (*not* anymore)
591 *
592 * Returns a namespace declaration or NULL in case of
593 * namespace fixup failures or API or internal errors.
594 */
595 xmlNsPtr
596 xsltGetNamespace(xsltTransformContextPtr ctxt, xmlNodePtr cur, xmlNsPtr ns,
597 xmlNodePtr out)
598 {
599
600 if (ns == NULL)
601 return(NULL);
602
603 #ifdef XSLT_REFACTORED
604 /*
605 * Namespace exclusion and ns-aliasing is performed at
606 * compilation-time in the refactored code.
607 * Additionally, aliasing is not intended for non Literal
608 * Result Elements.
609 */
610 return(xsltGetSpecialNamespace(ctxt, cur, ns->href, ns->prefix, out));
611 #else
612 {
613 xsltStylesheetPtr style;
614 const xmlChar *URI = NULL; /* the replacement URI */
615
616 if ((ctxt == NULL) || (cur == NULL) || (out == NULL))
617 return(NULL);
618
619 style = ctxt->style;
620 while (style != NULL) {
621 if (style->nsAliases != NULL)
622 URI = (const xmlChar *)
623 xmlHashLookup(style->nsAliases, ns->href);
624 if (URI != NULL)
625 break;
626
627 style = xsltNextImport(style);
628 }
629
630
631 if (URI == UNDEFINED_DEFAULT_NS) {
632 return(xsltGetSpecialNamespace(ctxt, cur, NULL, NULL, out));
633 #if 0
634 /*
635 * TODO: Removed, since wrong. If there was no default
636 * namespace in the stylesheet then this must resolve to
637 * the NULL namespace.
638 */
639 xmlNsPtr dflt;
640 dflt = xmlSearchNs(cur->doc, cur, NULL);
641 if (dflt != NULL)
642 URI = dflt->href;
643 else
644 return NULL;
645 #endif
646 } else if (URI == NULL)
647 URI = ns->href;
648
649 return(xsltGetSpecialNamespace(ctxt, cur, URI, ns->prefix, out));
650 }
651 #endif
652 }
653
654 /**
655 * xsltGetPlainNamespace:
656 * @ctxt: a transformation context
657 * @cur: the input node
658 * @ns: the namespace
659 * @out: the result element
660 *
661 * Obsolete.
662 * *Not* called by any Libxslt/Libexslt function.
663 * Exaclty the same as xsltGetNamespace().
664 *
665 * Returns a namespace declaration or NULL in case of
666 * namespace fixup failures or API or internal errors.
667 */
668 xmlNsPtr
669 xsltGetPlainNamespace(xsltTransformContextPtr ctxt, xmlNodePtr cur,
670 xmlNsPtr ns, xmlNodePtr out)
671 {
672 return(xsltGetNamespace(ctxt, cur, ns, out));
673 }
674
675 /**
676 * xsltCopyNamespaceList:
677 * @ctxt: a transformation context
678 * @node: the target node
679 * @cur: the first namespace
680 *
681 * Do a copy of an namespace list. If @node is non-NULL the
682 * new namespaces are added automatically. This handles namespaces
683 * aliases.
684 * This function is intended only for *internal* use at
685 * transformation-time for copying ns-declarations of Literal
686 * Result Elements.
687 *
688 * Called by:
689 * xsltCopyTreeInternal() (transform.c)
690 * xsltShallowCopyElem() (transform.c)
691 *
692 * REVISIT: This function won't be used in the refactored code.
693 *
694 * Returns: a new xmlNsPtr, or NULL in case of error.
695 */
696 xmlNsPtr
697 xsltCopyNamespaceList(xsltTransformContextPtr ctxt, xmlNodePtr node,
698 xmlNsPtr cur) {
699 xmlNsPtr ret = NULL, tmp;
700 xmlNsPtr p = NULL,q;
701
702 if (cur == NULL)
703 return(NULL);
704 if (cur->type != XML_NAMESPACE_DECL)
705 return(NULL);
706
707 /*
708 * One can add namespaces only on element nodes
709 */
710 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
711 node = NULL;
712
713 while (cur != NULL) {
714 if (cur->type != XML_NAMESPACE_DECL)
715 break;
716
717 /*
718 * Avoid duplicating namespace declarations in the tree if
719 * a matching declaration is in scope.
720 */
721 if (node != NULL) {
722 if ((node->ns != NULL) &&
723 (xmlStrEqual(node->ns->prefix, cur->prefix)) &&
724 (xmlStrEqual(node->ns->href, cur->href))) {
725 cur = cur->next;
726 continue;
727 }
728 tmp = xmlSearchNs(node->doc, node, cur->prefix);
729 if ((tmp != NULL) && (xmlStrEqual(tmp->href, cur->href))) {
730 cur = cur->next;
731 continue;
732 }
733 }
734 #ifdef XSLT_REFACTORED
735 /*
736 * Namespace exclusion and ns-aliasing is performed at
737 * compilation-time in the refactored code.
738 */
739 q = xmlNewNs(node, cur->href, cur->prefix);
740 if (p == NULL) {
741 ret = p = q;
742 } else {
743 p->next = q;
744 p = q;
745 }
746 #else
747 /*
748 * TODO: Remove this if the refactored code gets enabled.
749 */
750 if (!xmlStrEqual(cur->href, XSLT_NAMESPACE)) {
751 const xmlChar *URI;
752 /* TODO apply cascading */
753 URI = (const xmlChar *) xmlHashLookup(ctxt->style->nsAliases,
754 cur->href);
755 if (URI == UNDEFINED_DEFAULT_NS)
756 continue;
757 if (URI != NULL) {
758 q = xmlNewNs(node, URI, cur->prefix);
759 } else {
760 q = xmlNewNs(node, cur->href, cur->prefix);
761 }
762 if (p == NULL) {
763 ret = p = q;
764 } else {
765 p->next = q;
766 p = q;
767 }
768 }
769 #endif
770 cur = cur->next;
771 }
772 return(ret);
773 }
774
775 /**
776 * xsltCopyNamespace:
777 * @ctxt: a transformation context
778 * @elem: the target element node
779 * @ns: the namespace node
780 *
781 * Copies a namespace node (declaration). If @elem is not NULL,
782 * then the new namespace will be declared on @elem.
783 *
784 * Returns: a new xmlNsPtr, or NULL in case of an error.
785 */
786 xmlNsPtr
787 xsltCopyNamespace(xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED,
788 xmlNodePtr elem, xmlNsPtr ns)
789 {
790 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
791 return(NULL);
792 /*
793 * One can add namespaces only on element nodes
794 */
795 if ((elem != NULL) && (elem->type != XML_ELEMENT_NODE))
796 return(xmlNewNs(NULL, ns->href, ns->prefix));
797 else
798 return(xmlNewNs(elem, ns->href, ns->prefix));
799 }
800
801
802 /**
803 * xsltFreeNamespaceAliasHashes:
804 * @style: an XSLT stylesheet
805 *
806 * Free up the memory used by namespaces aliases
807 */
808 void
809 xsltFreeNamespaceAliasHashes(xsltStylesheetPtr style) {
810 if (style->nsAliases != NULL)
811 xmlHashFree((xmlHashTablePtr) style->nsAliases, NULL);
812 style->nsAliases = NULL;
813 }