2 * namespaces.c: Implementation of the XSLT namespaces handling
5 * http://www.w3.org/TR/1999/REC-xslt-19991116
7 * See Copyright for the status of this software.
14 /************************************************************************
18 ************************************************************************/
20 #ifdef XSLT_REFACTORED
22 xsltNewNsAlias(xsltCompilerCtxtPtr cctxt
)
29 ret
= (xsltNsAliasPtr
) xmlMalloc(sizeof(xsltNsAlias
));
31 xsltTransformError(NULL
, cctxt
->style
, NULL
,
32 "Internal error in xsltNewNsAlias(): Memory allocation failed.\n");
33 cctxt
->style
->errors
++;
36 memset(ret
, 0, sizeof(xsltNsAlias
));
38 * TODO: Store the item at current stylesheet-level.
40 ret
->next
= cctxt
->nsAliases
;
41 cctxt
->nsAliases
= ret
;
45 #endif /* XSLT_REFACTORED */
48 * @style: the XSLT stylesheet
49 * @node: the xsl:namespace-alias node
51 * Read the stylesheet-prefix and result-prefix attributes, register
52 * them as well as the corresponding namespace.
55 xsltNamespaceAlias(xsltStylesheetPtr style
, xmlNodePtr node
)
57 xmlChar
*resultPrefix
= NULL
;
58 xmlChar
*stylePrefix
= NULL
;
59 xmlNsPtr literalNs
= NULL
;
60 xmlNsPtr targetNs
= NULL
;
62 #ifdef XSLT_REFACTORED
65 if ((style
== NULL
) || (node
== NULL
))
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."
78 * SPEC TODO: Check for the errors mentioned above.
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.
88 * Attribute "stylesheet-prefix".
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");
96 if (xmlStrEqual(stylePrefix
, (const xmlChar
*)"#default"))
97 literalNs
= xmlSearchNs(node
->doc
, node
, NULL
);
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",
109 * Attribute "result-prefix".
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");
117 if (xmlStrEqual(resultPrefix
, (const xmlChar
*)"#default"))
118 targetNs
= xmlSearchNs(node
->doc
, node
, NULL
);
120 targetNs
= xmlSearchNs(node
->doc
, node
, resultPrefix
);
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",
132 * Same alias for multiple different target namespace URIs:
133 * TODO: The one with the highest import precedence is used.
135 * <xsl:namespace-alias stylesheet-prefix="foo"
136 * result-prefix="bar"/>
138 * <xsl:namespace-alias stylesheet-prefix="foo"
139 * result-prefix="zar"/>
141 * Same target namespace URI for multiple different aliases:
142 * All alias-definitions will be used.
144 * <xsl:namespace-alias stylesheet-prefix="bar"
145 * result-prefix="foo"/>
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?
154 * <xsl:namespace-alias stylesheet-prefix="foo"
155 * result-prefix="#default"/>
156 * From namespace to no namespace.
158 * <xsl:namespace-alias stylesheet-prefix="#default"
159 * result-prefix="foo"/>
160 * From no namespace to namespace.
165 * Store the ns-node in the alias-object.
167 alias
= xsltNewNsAlias(XSLT_CCTXT(style
));
170 alias
->literalNs
= literalNs
;
171 alias
->targetNs
= targetNs
;
172 XSLT_CCTXT(style
)->hasNsAliases
= 1;
175 #else /* XSLT_REFACTORED */
176 const xmlChar
*literalNsName
;
177 const xmlChar
*targetNsName
;
180 if ((style
== NULL
) || (node
== NULL
))
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");
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");
196 if (xmlStrEqual(stylePrefix
, (const xmlChar
*)"#default")) {
197 literalNs
= xmlSearchNs(node
->doc
, node
, NULL
);
198 if (literalNs
== NULL
) {
199 literalNsName
= NULL
;
201 literalNsName
= literalNs
->href
; /* Yes - set for nsAlias table */
203 literalNs
= xmlSearchNs(node
->doc
, node
, stylePrefix
);
205 if ((literalNs
== NULL
) || (literalNs
->href
== NULL
)) {
206 xsltTransformError(NULL
, style
, node
,
207 "namespace-alias: prefix %s not bound to any namespace\n",
211 literalNsName
= literalNs
->href
;
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
219 if (xmlStrEqual(resultPrefix
, (const xmlChar
*)"#default")) {
220 targetNs
= xmlSearchNs(node
->doc
, node
, NULL
);
221 if (targetNs
== NULL
) {
222 targetNsName
= UNDEFINED_DEFAULT_NS
;
224 targetNsName
= targetNs
->href
;
226 targetNs
= xmlSearchNs(node
->doc
, node
, resultPrefix
);
228 if ((targetNs
== NULL
) || (targetNs
->href
== NULL
)) {
229 xsltTransformError(NULL
, style
, node
,
230 "namespace-alias: prefix %s not bound to any namespace\n",
234 targetNsName
= targetNs
->href
;
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.
241 if (literalNsName
== NULL
) {
242 if (targetNs
!= NULL
) {
244 * BUG TODO: Is it not sufficient to have only 1 field for
245 * this, since subsequently alias declarations will
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.
254 style
->defaultAlias
= targetNs
->href
;
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");
264 xmlHashAddEntry((xmlHashTablePtr
) style
->nsAliases
,
265 literalNsName
, (void *) targetNsName
);
267 #endif /* else of XSLT_REFACTORED */
270 if (stylePrefix
!= NULL
)
271 xmlFree(stylePrefix
);
272 if (resultPrefix
!= NULL
)
273 xmlFree(resultPrefix
);
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
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
293 * If both @nsName and @nsPrefix are NULL, then this will try to
294 * "undeclare" a default namespace by declaring an xmlns="".
296 * Returns a namespace declaration or NULL.
299 xsltGetSpecialNamespace(xsltTransformContextPtr ctxt
, xmlNodePtr invocNode
,
300 const xmlChar
*nsName
, const xmlChar
*nsPrefix
,
304 int prefixOccupied
= 0;
306 if ((ctxt
== NULL
) || (target
== NULL
) ||
307 (target
->type
!= XML_ELEMENT_NODE
))
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.
321 * OPTIMIZE TODO: This all could be optimized by keeping track of
322 * the ns-decls currently in-scope via a specialized context.
324 if ((nsPrefix
== NULL
) && ((nsName
== NULL
) || (nsName
[0] == 0))) {
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:
332 * OPTIMIZE TODO: This all could be optimized by keeping track of
333 * the ns-decls currently in-scope via a specialized context.
336 * Search on the result element itself.
338 if (target
->nsDef
!= NULL
) {
341 if (ns
->prefix
== NULL
) {
342 if ((ns
->href
!= NULL
) && (ns
->href
[0] != 0)) {
344 * Raise a namespace normalization error.
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
);
354 * The default namespace was undeclared on the
362 } while (ns
!= NULL
);
364 if ((target
->parent
!= NULL
) &&
365 (target
->parent
->type
== XML_ELEMENT_NODE
))
368 * The parent element is in no namespace, so assume
369 * that there is no default namespace in scope.
371 if (target
->parent
->ns
== NULL
)
374 ns
= xmlSearchNs(target
->doc
, target
->parent
,
377 * Fine if there's no default ns is scope, or if the
378 * default ns was undeclared.
380 if ((ns
== NULL
) || (ns
->href
== NULL
) || (ns
->href
[0] == 0))
384 * Undeclare the default namespace.
386 xmlNewNs(target
, BAD_CAST
"", NULL
);
387 /* TODO: Check result */
393 * Handle the XML namespace.
394 * QUESTION: Is this faster than using xmlStrEqual() anyway?
396 if ((nsPrefix
!= NULL
) &&
397 (nsPrefix
[0] == 'x') && (nsPrefix
[1] == 'm') &&
398 (nsPrefix
[2] == 'l') && (nsPrefix
[3] == 0))
400 return(xmlSearchNs(target
->doc
, target
, nsPrefix
));
403 * First: search on the result element itself.
405 if (target
->nsDef
!= NULL
) {
408 if ((ns
->prefix
== NULL
) == (nsPrefix
== NULL
)) {
409 if (ns
->prefix
== nsPrefix
) {
410 if (xmlStrEqual(ns
->href
, nsName
))
414 } else if (xmlStrEqual(ns
->prefix
, nsPrefix
)) {
415 if (xmlStrEqual(ns
->href
, nsName
))
422 } while (ns
!= NULL
);
424 if (prefixOccupied
) {
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
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.
435 ns
= xmlSearchNsByHref(target
->doc
, target
, nsName
);
440 * Fallback to changing the prefix.
442 } else if ((target
->parent
!= NULL
) &&
443 (target
->parent
->type
== XML_ELEMENT_NODE
))
446 * Try to find a matching ns-decl in the ancestor-axis.
448 * Check the common case: The parent element of the current
449 * result element is in the same namespace (with an equal ns-prefix).
451 if ((target
->parent
->ns
!= NULL
) &&
452 ((target
->parent
->ns
->prefix
!= NULL
) == (nsPrefix
!= NULL
)))
454 ns
= target
->parent
->ns
;
456 if (nsPrefix
== NULL
) {
457 if (xmlStrEqual(ns
->href
, nsName
))
459 } else if (xmlStrEqual(ns
->prefix
, nsPrefix
) &&
460 xmlStrEqual(ns
->href
, nsName
))
466 * Lookup the remaining in-scope namespaces.
468 ns
= xmlSearchNs(target
->doc
, target
->parent
, nsPrefix
);
470 if (xmlStrEqual(ns
->href
, nsName
))
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">
477 * <xsl:attribute xmlns:a="urn:test:b" name="a:b">
478 * val-b</xsl:attribute>
482 if (target
->properties
) {
483 xmlAttrPtr attr
= target
->properties
;
486 xmlStrEqual(attr
->ns
->prefix
, nsPrefix
))
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
494 ns
= xmlSearchNsByHref(target
->doc
, target
, nsName
);
497 goto declare_new_prefix
;
500 } while (attr
!= NULL
);
504 * Either no matching ns-prefix was found or the namespace is
506 * Create a new ns-decl on the current result element.
508 * Hmm, we could also try to reuse an in-scope
509 * namespace with a matching ns-name but a different
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
520 ns
= xmlSearchNsByHref(target
->doc
, target
, nsName
);
526 * Create the ns-decl on the current result element.
528 ns
= xmlNewNs(target
, nsName
, nsPrefix
);
529 /* TODO: check errors */
533 * This is either the root of the tree or something weird is going on.
535 ns
= xmlNewNs(target
, nsName
, nsPrefix
);
536 /* TODO: Check result */
542 * Fallback: we need to generate a new prefix and declare the namespace
543 * on the result element.
549 if (nsPrefix
== NULL
) {
550 nsPrefix
= BAD_CAST
"ns";
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");
563 } while (ns
!= NULL
);
564 ns
= xmlNewNs(target
, nsName
, BAD_CAST pref
);
565 /* TODO: Check result */
573 * @ctxt: a transformation context
574 * @cur: the input node
576 * @out: the output node (or its parent)
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
586 * - xsltCopyPropList() (*not* anymore)
587 * - xsltShallowCopyElement()
588 * - xsltCopyTreeInternal() (*not* anymore)
589 * - xsltApplySequenceConstructor() (*not* in the refactored code),
590 * - xsltElement() (*not* anymore)
592 * Returns a namespace declaration or NULL in case of
593 * namespace fixup failures or API or internal errors.
596 xsltGetNamespace(xsltTransformContextPtr ctxt
, xmlNodePtr cur
, xmlNsPtr ns
,
603 #ifdef XSLT_REFACTORED
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
610 return(xsltGetSpecialNamespace(ctxt
, cur
, ns
->href
, ns
->prefix
, out
));
613 xsltStylesheetPtr style
;
614 const xmlChar
*URI
= NULL
; /* the replacement URI */
616 if ((ctxt
== NULL
) || (cur
== NULL
) || (out
== NULL
))
620 while (style
!= NULL
) {
621 if (style
->nsAliases
!= NULL
)
622 URI
= (const xmlChar
*)
623 xmlHashLookup(style
->nsAliases
, ns
->href
);
627 style
= xsltNextImport(style
);
631 if (URI
== UNDEFINED_DEFAULT_NS
) {
632 return(xsltGetSpecialNamespace(ctxt
, cur
, NULL
, NULL
, out
));
635 * TODO: Removed, since wrong. If there was no default
636 * namespace in the stylesheet then this must resolve to
637 * the NULL namespace.
640 dflt
= xmlSearchNs(cur
->doc
, cur
, NULL
);
646 } else if (URI
== NULL
)
649 return(xsltGetSpecialNamespace(ctxt
, cur
, URI
, ns
->prefix
, out
));
655 * xsltGetPlainNamespace:
656 * @ctxt: a transformation context
657 * @cur: the input node
659 * @out: the result element
662 * *Not* called by any Libxslt/Libexslt function.
663 * Exaclty the same as xsltGetNamespace().
665 * Returns a namespace declaration or NULL in case of
666 * namespace fixup failures or API or internal errors.
669 xsltGetPlainNamespace(xsltTransformContextPtr ctxt
, xmlNodePtr cur
,
670 xmlNsPtr ns
, xmlNodePtr out
)
672 return(xsltGetNamespace(ctxt
, cur
, ns
, out
));
676 * xsltCopyNamespaceList:
677 * @ctxt: a transformation context
678 * @node: the target node
679 * @cur: the first namespace
681 * Do a copy of an namespace list. If @node is non-NULL the
682 * new namespaces are added automatically. This handles namespaces
684 * This function is intended only for *internal* use at
685 * transformation-time for copying ns-declarations of Literal
689 * xsltCopyTreeInternal() (transform.c)
690 * xsltShallowCopyElem() (transform.c)
692 * REVISIT: This function won't be used in the refactored code.
694 * Returns: a new xmlNsPtr, or NULL in case of error.
697 xsltCopyNamespaceList(xsltTransformContextPtr ctxt
, xmlNodePtr node
,
699 xmlNsPtr ret
= NULL
, tmp
;
704 if (cur
->type
!= XML_NAMESPACE_DECL
)
708 * One can add namespaces only on element nodes
710 if ((node
!= NULL
) && (node
->type
!= XML_ELEMENT_NODE
))
713 while (cur
!= NULL
) {
714 if (cur
->type
!= XML_NAMESPACE_DECL
)
718 * Avoid duplicating namespace declarations in the tree if
719 * a matching declaration is in scope.
722 if ((node
->ns
!= NULL
) &&
723 (xmlStrEqual(node
->ns
->prefix
, cur
->prefix
)) &&
724 (xmlStrEqual(node
->ns
->href
, cur
->href
))) {
728 tmp
= xmlSearchNs(node
->doc
, node
, cur
->prefix
);
729 if ((tmp
!= NULL
) && (xmlStrEqual(tmp
->href
, cur
->href
))) {
734 #ifdef XSLT_REFACTORED
736 * Namespace exclusion and ns-aliasing is performed at
737 * compilation-time in the refactored code.
739 q
= xmlNewNs(node
, cur
->href
, cur
->prefix
);
748 * TODO: Remove this if the refactored code gets enabled.
750 if (!xmlStrEqual(cur
->href
, XSLT_NAMESPACE
)) {
752 /* TODO apply cascading */
753 URI
= (const xmlChar
*) xmlHashLookup(ctxt
->style
->nsAliases
,
755 if (URI
== UNDEFINED_DEFAULT_NS
)
758 q
= xmlNewNs(node
, URI
, cur
->prefix
);
760 q
= xmlNewNs(node
, cur
->href
, cur
->prefix
);
777 * @ctxt: a transformation context
778 * @elem: the target element node
779 * @ns: the namespace node
781 * Copies a namespace node (declaration). If @elem is not NULL,
782 * then the new namespace will be declared on @elem.
784 * Returns: a new xmlNsPtr, or NULL in case of an error.
787 xsltCopyNamespace(xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED
,
788 xmlNodePtr elem
, xmlNsPtr ns
)
790 if ((ns
== NULL
) || (ns
->type
!= XML_NAMESPACE_DECL
))
793 * One can add namespaces only on element nodes
795 if ((elem
!= NULL
) && (elem
->type
!= XML_ELEMENT_NODE
))
796 return(xmlNewNs(NULL
, ns
->href
, ns
->prefix
));
798 return(xmlNewNs(elem
, ns
->href
, ns
->prefix
));
803 * xsltFreeNamespaceAliasHashes:
804 * @style: an XSLT stylesheet
806 * Free up the memory used by namespaces aliases
809 xsltFreeNamespaceAliasHashes(xsltStylesheetPtr style
) {
810 if (style
->nsAliases
!= NULL
)
811 xmlHashFree((xmlHashTablePtr
) style
->nsAliases
, NULL
);
812 style
->nsAliases
= NULL
;