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.
17 #ifdef HAVE_SYS_TYPES_H
18 #include <sys/types.h>
35 #ifndef XSLT_NEED_TRIO
41 #include <libxml/xmlmemory.h>
42 #include <libxml/tree.h>
43 #include <libxml/hash.h>
44 #include <libxml/xmlerror.h>
45 #include <libxml/uri.h>
47 #include "xsltInternals.h"
48 #include "xsltutils.h"
49 #include "namespaces.h"
52 /************************************************************************
56 ************************************************************************/
58 #ifdef XSLT_REFACTORED
60 xsltNewNsAlias(xsltCompilerCtxtPtr cctxt
)
67 ret
= (xsltNsAliasPtr
) xmlMalloc(sizeof(xsltNsAlias
));
69 xsltTransformError(NULL
, cctxt
->style
, NULL
,
70 "Internal error in xsltNewNsAlias(): Memory allocation failed.\n");
71 cctxt
->style
->errors
++;
74 memset(ret
, 0, sizeof(xsltNsAlias
));
76 * TODO: Store the item at current stylesheet-level.
78 ret
->next
= cctxt
->nsAliases
;
79 cctxt
->nsAliases
= ret
;
83 #endif /* XSLT_REFACTORED */
86 * @style: the XSLT stylesheet
87 * @node: the xsl:namespace-alias node
89 * Read the stylesheet-prefix and result-prefix attributes, register
90 * them as well as the corresponding namespace.
93 xsltNamespaceAlias(xsltStylesheetPtr style
, xmlNodePtr node
)
95 xmlChar
*resultPrefix
= NULL
;
96 xmlChar
*stylePrefix
= NULL
;
97 xmlNsPtr literalNs
= NULL
;
98 xmlNsPtr targetNs
= NULL
;
100 #ifdef XSLT_REFACTORED
101 xsltNsAliasPtr alias
;
103 if ((style
== NULL
) || (node
== NULL
))
108 * "If a namespace URI is declared to be an alias for multiple
109 * different namespace URIs, then the declaration with the highest
110 * import precedence is used. It is an error if there is more than
111 * one such declaration. An XSLT processor may signal the error;
112 * if it does not signal the error, it must recover by choosing,
113 * from amongst the declarations with the highest import precedence,
114 * the one that occurs last in the stylesheet."
116 * SPEC TODO: Check for the errors mentioned above.
119 * NOTE that the XSLT 2.0 also *does* use the NULL namespace if
120 * "#default" is used and there's no default namespace is scope.
121 * I.e., this is *not* an error.
122 * Most XSLT 1.0 implementations work this way.
123 * The XSLT 1.0 spec has nothing to say on the subject.
126 * Attribute "stylesheet-prefix".
128 stylePrefix
= xmlGetNsProp(node
, (const xmlChar
*)"stylesheet-prefix", NULL
);
129 if (stylePrefix
== NULL
) {
130 xsltTransformError(NULL
, style
, node
,
131 "The attribute 'stylesheet-prefix' is missing.\n");
134 if (xmlStrEqual(stylePrefix
, (const xmlChar
*)"#default"))
135 literalNs
= xmlSearchNs(node
->doc
, node
, NULL
);
137 literalNs
= xmlSearchNs(node
->doc
, node
, stylePrefix
);
138 if (literalNs
== NULL
) {
139 xsltTransformError(NULL
, style
, node
,
140 "Attribute 'stylesheet-prefix': There's no namespace "
141 "declaration in scope for the prefix '%s'.\n",
147 * Attribute "result-prefix".
149 resultPrefix
= xmlGetNsProp(node
, (const xmlChar
*)"result-prefix", NULL
);
150 if (resultPrefix
== NULL
) {
151 xsltTransformError(NULL
, style
, node
,
152 "The attribute 'result-prefix' is missing.\n");
155 if (xmlStrEqual(resultPrefix
, (const xmlChar
*)"#default"))
156 targetNs
= xmlSearchNs(node
->doc
, node
, NULL
);
158 targetNs
= xmlSearchNs(node
->doc
, node
, resultPrefix
);
160 if (targetNs
== NULL
) {
161 xsltTransformError(NULL
, style
, node
,
162 "Attribute 'result-prefix': There's no namespace "
163 "declaration in scope for the prefix '%s'.\n",
170 * Same alias for multiple different target namespace URIs:
171 * TODO: The one with the highest import precedence is used.
173 * <xsl:namespace-alias stylesheet-prefix="foo"
174 * result-prefix="bar"/>
176 * <xsl:namespace-alias stylesheet-prefix="foo"
177 * result-prefix="zar"/>
179 * Same target namespace URI for multiple different aliases:
180 * All alias-definitions will be used.
182 * <xsl:namespace-alias stylesheet-prefix="bar"
183 * result-prefix="foo"/>
185 * <xsl:namespace-alias stylesheet-prefix="zar"
186 * result-prefix="foo"/>
187 * Cases using #default:
188 * <xsl:namespace-alias stylesheet-prefix="#default"
189 * result-prefix="#default"/>
190 * TODO: Has this an effect at all?
192 * <xsl:namespace-alias stylesheet-prefix="foo"
193 * result-prefix="#default"/>
194 * From namespace to no namespace.
196 * <xsl:namespace-alias stylesheet-prefix="#default"
197 * result-prefix="foo"/>
198 * From no namespace to namespace.
203 * Store the ns-node in the alias-object.
205 alias
= xsltNewNsAlias(XSLT_CCTXT(style
));
208 alias
->literalNs
= literalNs
;
209 alias
->targetNs
= targetNs
;
210 XSLT_CCTXT(style
)->hasNsAliases
= 1;
213 #else /* XSLT_REFACTORED */
214 const xmlChar
*literalNsName
;
215 const xmlChar
*targetNsName
;
218 if ((style
== NULL
) || (node
== NULL
))
221 stylePrefix
= xmlGetNsProp(node
, (const xmlChar
*)"stylesheet-prefix", NULL
);
222 if (stylePrefix
== NULL
) {
223 xsltTransformError(NULL
, style
, node
,
224 "namespace-alias: stylesheet-prefix attribute missing\n");
227 resultPrefix
= xmlGetNsProp(node
, (const xmlChar
*)"result-prefix", NULL
);
228 if (resultPrefix
== NULL
) {
229 xsltTransformError(NULL
, style
, node
,
230 "namespace-alias: result-prefix attribute missing\n");
234 if (xmlStrEqual(stylePrefix
, (const xmlChar
*)"#default")) {
235 literalNs
= xmlSearchNs(node
->doc
, node
, NULL
);
236 if (literalNs
== NULL
) {
237 literalNsName
= NULL
;
239 literalNsName
= literalNs
->href
; /* Yes - set for nsAlias table */
241 literalNs
= xmlSearchNs(node
->doc
, node
, stylePrefix
);
243 if ((literalNs
== NULL
) || (literalNs
->href
== NULL
)) {
244 xsltTransformError(NULL
, style
, node
,
245 "namespace-alias: prefix %s not bound to any namespace\n",
249 literalNsName
= literalNs
->href
;
253 * When "#default" is used for result, if a default namespace has not
254 * been explicitly declared the special value UNDEFINED_DEFAULT_NS is
255 * put into the nsAliases table
257 if (xmlStrEqual(resultPrefix
, (const xmlChar
*)"#default")) {
258 targetNs
= xmlSearchNs(node
->doc
, node
, NULL
);
259 if (targetNs
== NULL
) {
260 targetNsName
= UNDEFINED_DEFAULT_NS
;
262 targetNsName
= targetNs
->href
;
264 targetNs
= xmlSearchNs(node
->doc
, node
, resultPrefix
);
266 if ((targetNs
== NULL
) || (targetNs
->href
== NULL
)) {
267 xsltTransformError(NULL
, style
, node
,
268 "namespace-alias: prefix %s not bound to any namespace\n",
272 targetNsName
= targetNs
->href
;
275 * Special case: if #default is used for
276 * the stylesheet-prefix (literal namespace) and there's no default
277 * namespace in scope, we'll use style->defaultAlias for this.
279 if (literalNsName
== NULL
) {
280 if (targetNs
!= NULL
) {
282 * BUG TODO: Is it not sufficient to have only 1 field for
283 * this, since subsequently alias declarations will
286 * <xsl:namespace-alias result-prefix="foo"
287 * stylesheet-prefix="#default"/>
288 * <xsl:namespace-alias result-prefix="bar"
289 * stylesheet-prefix="#default"/>
290 * The mapping for "foo" won't be visible anymore.
292 style
->defaultAlias
= targetNs
->href
;
295 if (style
->nsAliases
== NULL
)
296 style
->nsAliases
= xmlHashCreate(10);
297 if (style
->nsAliases
== NULL
) {
298 xsltTransformError(NULL
, style
, node
,
299 "namespace-alias: cannot create hash table\n");
302 xmlHashAddEntry((xmlHashTablePtr
) style
->nsAliases
,
303 literalNsName
, (void *) targetNsName
);
305 #endif /* else of XSLT_REFACTORED */
308 if (stylePrefix
!= NULL
)
309 xmlFree(stylePrefix
);
310 if (resultPrefix
!= NULL
)
311 xmlFree(resultPrefix
);
315 * xsltGetSpecialNamespace:
316 * @ctxt: the transformation context
317 * @invocNode: the invoking node; e.g. a literal result element/attr;
318 * only used for error reports
319 * @nsName: the namespace name (or NULL)
320 * @nsPrefix: the suggested namespace prefix (or NULL)
321 * @target: the result element on which to anchor a namespace
323 * Find a matching (prefix and ns-name) ns-declaration
324 * for the requested @nsName and @nsPrefix in the result tree.
325 * If none is found then a new ns-declaration will be
326 * added to @resultElem. If, in this case, the given prefix is
327 * already in use, then a ns-declaration with a modified ns-prefix
328 * be we created. Note that this function's priority is to
329 * preserve ns-prefixes; it will only change a prefix if there's
331 * If both @nsName and @nsPrefix are NULL, then this will try to
332 * "undeclare" a default namespace by declaring an xmlns="".
334 * Returns a namespace declaration or NULL.
337 xsltGetSpecialNamespace(xsltTransformContextPtr ctxt
, xmlNodePtr invocNode
,
338 const xmlChar
*nsName
, const xmlChar
*nsPrefix
,
342 int prefixOccupied
= 0;
344 if ((ctxt
== NULL
) || (target
== NULL
) ||
345 (target
->type
!= XML_ELEMENT_NODE
))
349 * NOTE: Namespace exclusion and ns-aliasing is performed at
350 * compilation-time in the refactored code; so this need not be done
351 * here (it was in the old code).
352 * NOTE: @invocNode was named @cur in the old code and was documented to
353 * be an input node; since it was only used to anchor an error report
354 * somewhere, we can safely change this to @invocNode, which now
355 * will be the XSLT instruction (also a literal result element/attribute),
356 * which was responsible for this call.
359 * OPTIMIZE TODO: This all could be optimized by keeping track of
360 * the ns-decls currently in-scope via a specialized context.
362 if ((nsPrefix
== NULL
) && ((nsName
== NULL
) || (nsName
[0] == 0))) {
364 * NOTE: the "undeclaration" of the default namespace was
365 * part of the logic of the old xsltGetSpecialNamespace() code,
366 * so we'll keep that mechanism.
367 * Related to the old code: bug #302020:
370 * OPTIMIZE TODO: This all could be optimized by keeping track of
371 * the ns-decls currently in-scope via a specialized context.
374 * Search on the result element itself.
376 if (target
->nsDef
!= NULL
) {
379 if (ns
->prefix
== NULL
) {
380 if ((ns
->href
!= NULL
) && (ns
->href
[0] != 0)) {
382 * Raise a namespace normalization error.
384 xsltTransformError(ctxt
, NULL
, invocNode
,
385 "Namespace normalization error: Cannot undeclare "
386 "the default namespace, since the default namespace "
387 "'%s' is already declared on the result element "
388 "'%s'.\n", ns
->href
, target
->name
);
392 * The default namespace was undeclared on the
400 } while (ns
!= NULL
);
402 if ((target
->parent
!= NULL
) &&
403 (target
->parent
->type
== XML_ELEMENT_NODE
))
406 * The parent element is in no namespace, so assume
407 * that there is no default namespace in scope.
409 if (target
->parent
->ns
== NULL
)
412 ns
= xmlSearchNs(target
->doc
, target
->parent
,
415 * Fine if there's no default ns is scope, or if the
416 * default ns was undeclared.
418 if ((ns
== NULL
) || (ns
->href
== NULL
) || (ns
->href
[0] == 0))
422 * Undeclare the default namespace.
424 xmlNewNs(target
, BAD_CAST
"", NULL
);
425 /* TODO: Check result */
431 * Handle the XML namespace.
432 * QUESTION: Is this faster than using xmlStrEqual() anyway?
434 if ((nsPrefix
!= NULL
) &&
435 (nsPrefix
[0] == 'x') && (nsPrefix
[1] == 'm') &&
436 (nsPrefix
[2] == 'l') && (nsPrefix
[3] == 0))
438 return(xmlSearchNs(target
->doc
, target
, nsPrefix
));
441 * First: search on the result element itself.
443 if (target
->nsDef
!= NULL
) {
446 if ((ns
->prefix
== NULL
) == (nsPrefix
== NULL
)) {
447 if (ns
->prefix
== nsPrefix
) {
448 if (xmlStrEqual(ns
->href
, nsName
))
452 } else if (xmlStrEqual(ns
->prefix
, nsPrefix
)) {
453 if (xmlStrEqual(ns
->href
, nsName
))
460 } while (ns
!= NULL
);
462 if (prefixOccupied
) {
464 * If the ns-prefix is occupied by an other ns-decl on the
465 * result element, then this means:
466 * 1) The desired prefix is shadowed
467 * 2) There's no way around changing the prefix
469 * Try a desperate search for an in-scope ns-decl
470 * with a matching ns-name before we use the last option,
471 * which is to recreate the ns-decl with a modified prefix.
473 ns
= xmlSearchNsByHref(target
->doc
, target
, nsName
);
478 * Fallback to changing the prefix.
480 } else if ((target
->parent
!= NULL
) &&
481 (target
->parent
->type
== XML_ELEMENT_NODE
))
484 * Try to find a matching ns-decl in the ancestor-axis.
486 * Check the common case: The parent element of the current
487 * result element is in the same namespace (with an equal ns-prefix).
489 if ((target
->parent
->ns
!= NULL
) &&
490 ((target
->parent
->ns
->prefix
!= NULL
) == (nsPrefix
!= NULL
)))
492 ns
= target
->parent
->ns
;
494 if (nsPrefix
== NULL
) {
495 if (xmlStrEqual(ns
->href
, nsName
))
497 } else if (xmlStrEqual(ns
->prefix
, nsPrefix
) &&
498 xmlStrEqual(ns
->href
, nsName
))
504 * Lookup the remaining in-scope namespaces.
506 ns
= xmlSearchNs(target
->doc
, target
->parent
, nsPrefix
);
508 if (xmlStrEqual(ns
->href
, nsName
))
511 * Now check for a nasty case: We need to ensure that the new
512 * ns-decl won't shadow a prefix in-use by an existing attribute.
513 * <foo xmlns:a="urn:test:a">
515 * <xsl:attribute xmlns:a="urn:test:b" name="a:b">
516 * val-b</xsl:attribute>
520 if (target
->properties
) {
521 xmlAttrPtr attr
= target
->properties
;
524 xmlStrEqual(attr
->ns
->prefix
, nsPrefix
))
527 * Bad, this prefix is already in use.
528 * Since we'll change the prefix anyway, try
529 * a search for a matching ns-decl based on the
532 ns
= xmlSearchNsByHref(target
->doc
, target
, nsName
);
535 goto declare_new_prefix
;
538 } while (attr
!= NULL
);
542 * Either no matching ns-prefix was found or the namespace is
544 * Create a new ns-decl on the current result element.
546 * Hmm, we could also try to reuse an in-scope
547 * namespace with a matching ns-name but a different
549 * What has higher priority?
550 * 1) If keeping the prefix: create a new ns-decl.
551 * 2) If reusal: first lookup ns-names; then fallback
552 * to creation of a new ns-decl.
553 * REVISIT: this currently uses case 1) although
554 * the old way was use xmlSearchNsByHref() and to let change
558 ns
= xmlSearchNsByHref(target
->doc
, target
, nsName
);
564 * Create the ns-decl on the current result element.
566 ns
= xmlNewNs(target
, nsName
, nsPrefix
);
567 /* TODO: check errors */
571 * This is either the root of the tree or something weird is going on.
573 ns
= xmlNewNs(target
, nsName
, nsPrefix
);
574 /* TODO: Check result */
580 * Fallback: we need to generate a new prefix and declare the namespace
581 * on the result element.
587 if (nsPrefix
== NULL
) {
588 nsPrefix
= BAD_CAST
"ns";
592 snprintf((char *) pref
, 30, "%s_%d", nsPrefix
, counter
++);
593 ns
= xmlSearchNs(target
->doc
, target
, BAD_CAST pref
);
594 if (counter
> 1000) {
595 xsltTransformError(ctxt
, NULL
, invocNode
,
596 "Internal error in xsltAcquireResultInScopeNs(): "
597 "Failed to compute a unique ns-prefix for the "
598 "generated element");
601 } while (ns
!= NULL
);
602 ns
= xmlNewNs(target
, nsName
, BAD_CAST pref
);
603 /* TODO: Check result */
611 * @ctxt: a transformation context
612 * @cur: the input node
614 * @out: the output node (or its parent)
616 * Find a matching (prefix and ns-name) ns-declaration
617 * for the requested @ns->prefix and @ns->href in the result tree.
618 * If none is found then a new ns-declaration will be
619 * added to @resultElem. If, in this case, the given prefix is
620 * already in use, then a ns-declaration with a modified ns-prefix
624 * - xsltCopyPropList() (*not* anymore)
625 * - xsltShallowCopyElement()
626 * - xsltCopyTreeInternal() (*not* anymore)
627 * - xsltApplySequenceConstructor() (*not* in the refactored code),
628 * - xsltElement() (*not* anymore)
630 * Returns a namespace declaration or NULL in case of
631 * namespace fixup failures or API or internal errors.
634 xsltGetNamespace(xsltTransformContextPtr ctxt
, xmlNodePtr cur
, xmlNsPtr ns
,
641 #ifdef XSLT_REFACTORED
643 * Namespace exclusion and ns-aliasing is performed at
644 * compilation-time in the refactored code.
645 * Additionally, aliasing is not intended for non Literal
648 return(xsltGetSpecialNamespace(ctxt
, cur
, ns
->href
, ns
->prefix
, out
));
651 xsltStylesheetPtr style
;
652 const xmlChar
*URI
= NULL
; /* the replacement URI */
654 if ((ctxt
== NULL
) || (cur
== NULL
) || (out
== NULL
))
658 while (style
!= NULL
) {
659 if (style
->nsAliases
!= NULL
)
660 URI
= (const xmlChar
*)
661 xmlHashLookup(style
->nsAliases
, ns
->href
);
665 style
= xsltNextImport(style
);
669 if (URI
== UNDEFINED_DEFAULT_NS
) {
670 return(xsltGetSpecialNamespace(ctxt
, cur
, NULL
, NULL
, out
));
673 * TODO: Removed, since wrong. If there was no default
674 * namespace in the stylesheet then this must resolve to
675 * the NULL namespace.
678 dflt
= xmlSearchNs(cur
->doc
, cur
, NULL
);
684 } else if (URI
== NULL
)
687 return(xsltGetSpecialNamespace(ctxt
, cur
, URI
, ns
->prefix
, out
));
693 * xsltGetPlainNamespace:
694 * @ctxt: a transformation context
695 * @cur: the input node
697 * @out: the result element
700 * *Not* called by any Libxslt/Libexslt function.
701 * Exaclty the same as xsltGetNamespace().
703 * Returns a namespace declaration or NULL in case of
704 * namespace fixup failures or API or internal errors.
707 xsltGetPlainNamespace(xsltTransformContextPtr ctxt
, xmlNodePtr cur
,
708 xmlNsPtr ns
, xmlNodePtr out
)
710 return(xsltGetNamespace(ctxt
, cur
, ns
, out
));
714 * xsltCopyNamespaceList:
715 * @ctxt: a transformation context
716 * @node: the target node
717 * @cur: the first namespace
719 * Do a copy of an namespace list. If @node is non-NULL the
720 * new namespaces are added automatically. This handles namespaces
722 * This function is intended only for *internal* use at
723 * transformation-time for copying ns-declarations of Literal
727 * xsltCopyTreeInternal() (transform.c)
728 * xsltShallowCopyElem() (transform.c)
730 * REVISIT: This function won't be used in the refactored code.
732 * Returns: a new xmlNsPtr, or NULL in case of error.
735 xsltCopyNamespaceList(xsltTransformContextPtr ctxt
, xmlNodePtr node
,
737 xmlNsPtr ret
= NULL
, tmp
;
742 if (cur
->type
!= XML_NAMESPACE_DECL
)
746 * One can add namespaces only on element nodes
748 if ((node
!= NULL
) && (node
->type
!= XML_ELEMENT_NODE
))
751 while (cur
!= NULL
) {
752 if (cur
->type
!= XML_NAMESPACE_DECL
)
756 * Avoid duplicating namespace declarations in the tree if
757 * a matching declaration is in scope.
760 if ((node
->ns
!= NULL
) &&
761 (xmlStrEqual(node
->ns
->prefix
, cur
->prefix
)) &&
762 (xmlStrEqual(node
->ns
->href
, cur
->href
))) {
766 tmp
= xmlSearchNs(node
->doc
, node
, cur
->prefix
);
767 if ((tmp
!= NULL
) && (xmlStrEqual(tmp
->href
, cur
->href
))) {
772 #ifdef XSLT_REFACTORED
774 * Namespace exclusion and ns-aliasing is performed at
775 * compilation-time in the refactored code.
777 q
= xmlNewNs(node
, cur
->href
, cur
->prefix
);
786 * TODO: Remove this if the refactored code gets enabled.
788 if (!xmlStrEqual(cur
->href
, XSLT_NAMESPACE
)) {
790 /* TODO apply cascading */
791 URI
= (const xmlChar
*) xmlHashLookup(ctxt
->style
->nsAliases
,
793 if (URI
== UNDEFINED_DEFAULT_NS
)
796 q
= xmlNewNs(node
, URI
, cur
->prefix
);
798 q
= xmlNewNs(node
, cur
->href
, cur
->prefix
);
815 * @ctxt: a transformation context
816 * @elem: the target element node
817 * @ns: the namespace node
819 * Copies a namespace node (declaration). If @elem is not NULL,
820 * then the new namespace will be declared on @elem.
822 * Returns: a new xmlNsPtr, or NULL in case of an error.
825 xsltCopyNamespace(xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED
,
826 xmlNodePtr elem
, xmlNsPtr ns
)
828 if ((ns
== NULL
) || (ns
->type
!= XML_NAMESPACE_DECL
))
831 * One can add namespaces only on element nodes
833 if ((elem
!= NULL
) && (elem
->type
!= XML_ELEMENT_NODE
))
834 return(xmlNewNs(NULL
, ns
->href
, ns
->prefix
));
836 return(xmlNewNs(elem
, ns
->href
, ns
->prefix
));
841 * xsltFreeNamespaceAliasHashes:
842 * @style: an XSLT stylesheet
844 * Free up the memory used by namespaces aliases
847 xsltFreeNamespaceAliasHashes(xsltStylesheetPtr style
) {
848 if (style
->nsAliases
!= NULL
)
849 xmlHashFree((xmlHashTablePtr
) style
->nsAliases
, NULL
);
850 style
->nsAliases
= NULL
;