2 * attributes.c: Implementation of the XSLT attributes 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>
36 #include <libxml/xmlmemory.h>
37 #include <libxml/tree.h>
38 #include <libxml/hash.h>
39 #include <libxml/xmlerror.h>
40 #include <libxml/uri.h>
41 #include <libxml/parserInternals.h>
43 #include "xsltInternals.h"
44 #include "xsltutils.h"
45 #include "attributes.h"
46 #include "namespaces.h"
47 #include "templates.h"
49 #include "transform.h"
52 #define WITH_XSLT_DEBUG_ATTRIBUTES
53 #ifdef WITH_XSLT_DEBUG
54 #define WITH_XSLT_DEBUG_ATTRIBUTES
64 #define IS_BLANK(c) (((c) == 0x20) || ((c) == 0x09) || ((c) == 0xA) || \
67 #define IS_BLANK_NODE(n) \
68 (((n)->type == XML_TEXT_NODE) && (xsltIsBlank((n)->content)))
70 #define ATTRSET_UNRESOLVED 0
71 #define ATTRSET_RESOLVING 1
72 #define ATTRSET_RESOLVED 2
76 * The in-memory structure corresponding to an XSLT Attribute in
81 typedef struct _xsltAttrElem xsltAttrElem;
82 typedef xsltAttrElem *xsltAttrElemPtr;
83 struct _xsltAttrElem {
84 struct _xsltAttrElem *next;/* chained list */
85 xmlNodePtr attr; /* the xsl:attribute definition */
88 typedef struct _xsltUseAttrSet xsltUseAttrSet;
89 typedef xsltUseAttrSet *xsltUseAttrSetPtr;
90 struct _xsltUseAttrSet {
91 struct _xsltUseAttrSet *next; /* chained list */
92 const xmlChar *ncname;
96 typedef struct _xsltAttrSet xsltAttrSet;
97 typedef xsltAttrSet *xsltAttrSetPtr;
100 xsltAttrElemPtr attrs; /* list head */
101 xsltUseAttrSetPtr useAttrSets; /* list head */
104 typedef struct _xsltAttrSetContext xsltAttrSetContext;
105 typedef xsltAttrSetContext *xsltAttrSetContextPtr;
106 struct _xsltAttrSetContext {
107 xsltStylesheetPtr topStyle;
108 xsltStylesheetPtr style;
112 xsltResolveAttrSet(xsltAttrSetPtr set, xsltStylesheetPtr topStyle,
113 xsltStylesheetPtr style, const xmlChar *name,
114 const xmlChar *ns, int depth);
116 /************************************************************************
118 * XSLT Attribute handling *
120 ************************************************************************/
124 * @attr: the new xsl:attribute node
126 * Create a new XSLT AttrElem
128 * Returns the newly allocated xsltAttrElemPtr or NULL in case of error
130 static xsltAttrElemPtr
131 xsltNewAttrElem(xmlNodePtr attr) {
134 cur = (xsltAttrElemPtr) xmlMalloc(sizeof(xsltAttrElem));
136 xsltGenericError(xsltGenericErrorContext,
137 "xsltNewAttrElem : malloc failed\n");
140 memset(cur, 0, sizeof(xsltAttrElem));
147 * @attr: an XSLT AttrElem
149 * Free up the memory allocated by @attr
152 xsltFreeAttrElem(xsltAttrElemPtr attr) {
157 * xsltFreeAttrElemList:
158 * @list: an XSLT AttrElem list
160 * Free up the memory allocated by @list
163 xsltFreeAttrElemList(xsltAttrElemPtr list) {
164 xsltAttrElemPtr next;
166 while (list != NULL) {
168 xsltFreeAttrElem(list);
174 * xsltAddAttrElemList:
175 * @list: an XSLT AttrElem list
176 * @attr: the new xsl:attribute node
178 * Add the new attribute to the list.
180 * Returns the new list pointer
182 static xsltAttrElemPtr
183 xsltAddAttrElemList(xsltAttrElemPtr list, xmlNodePtr attr) {
184 xsltAttrElemPtr next, cur;
189 return(xsltNewAttrElem(attr));
191 while (cur != NULL) {
194 cur->next = xsltNewAttrElem(attr);
204 * @ncname: local name
207 * Create a new XSLT UseAttrSet
209 * Returns the newly allocated xsltUseAttrSetPtr or NULL in case of error.
211 static xsltUseAttrSetPtr
212 xsltNewUseAttrSet(const xmlChar *ncname, const xmlChar *ns) {
213 xsltUseAttrSetPtr cur;
215 cur = (xsltUseAttrSetPtr) xmlMalloc(sizeof(xsltUseAttrSet));
217 xsltGenericError(xsltGenericErrorContext,
218 "xsltNewUseAttrSet : malloc failed\n");
221 memset(cur, 0, sizeof(xsltUseAttrSet));
222 cur->ncname = ncname;
228 * xsltFreeUseAttrSet:
229 * @use: an XSLT UseAttrSet
231 * Free up the memory allocated by @use
234 xsltFreeUseAttrSet(xsltUseAttrSetPtr use) {
239 * xsltFreeUseAttrSetList:
240 * @list: an XSLT UseAttrSet list
242 * Free up the memory allocated by @list
245 xsltFreeUseAttrSetList(xsltUseAttrSetPtr list) {
246 xsltUseAttrSetPtr next;
248 while (list != NULL) {
250 xsltFreeUseAttrSet(list);
256 * xsltAddUseAttrSetList:
257 * @list: a xsltUseAttrSet list
258 * @ncname: local name
261 * Add the use-attribute-set name to the list.
263 * Returns the new list pointer.
265 static xsltUseAttrSetPtr
266 xsltAddUseAttrSetList(xsltUseAttrSetPtr list, const xmlChar *ncname,
268 xsltUseAttrSetPtr next, cur;
273 return(xsltNewUseAttrSet(ncname, ns));
275 while (cur != NULL) {
276 if ((cur->ncname == ncname) && (cur->ns == ns))
280 cur->next = xsltNewUseAttrSet(ncname, ns);
291 * Create a new attribute set.
293 * Returns the newly allocated xsltAttrSetPtr or NULL in case of error.
295 static xsltAttrSetPtr
299 cur = (xsltAttrSetPtr) xmlMalloc(sizeof(xsltAttrSet));
301 xsltGenericError(xsltGenericErrorContext,
302 "xsltNewAttrSet : malloc failed\n");
305 memset(cur, 0, sizeof(xsltAttrSet));
311 * @set: an attribute set
313 * Free memory allocated by @set
316 xsltFreeAttrSet(xsltAttrSetPtr set) {
320 xsltFreeAttrElemList(set->attrs);
321 xsltFreeUseAttrSetList(set->useAttrSets);
327 * @set: an attribute set
328 * @other: another attribute set
330 * Add all the attributes from @other to @set,
331 * but drop redefinition of existing values.
334 xsltMergeAttrSets(xsltAttrSetPtr set, xsltAttrSetPtr other) {
336 xsltAttrElemPtr old = other->attrs;
339 while (old != NULL) {
341 * Check that the attribute is not yet in the list
345 while (cur != NULL) {
346 xsltStylePreCompPtr curComp = cur->attr->psvi;
347 xsltStylePreCompPtr oldComp = old->attr->psvi;
349 if ((curComp->name == oldComp->name) &&
350 (curComp->ns == oldComp->ns)) {
354 if (cur->next == NULL)
361 set->attrs = xsltNewAttrElem(old->attr);
363 cur->next = xsltNewAttrElem(old->attr);
371 /************************************************************************
373 * Module interfaces *
375 ************************************************************************/
378 * xsltParseStylesheetAttributeSet:
379 * @style: the XSLT stylesheet
380 * @cur: the "attribute-set" element
382 * parse an XSLT stylesheet attribute-set element
386 xsltParseStylesheetAttributeSet(xsltStylesheetPtr style, xmlNodePtr cur) {
387 const xmlChar *ncname;
388 const xmlChar *prefix;
389 const xmlChar *nsUri = NULL;
394 if ((cur == NULL) || (style == NULL) || (cur->type != XML_ELEMENT_NODE))
397 value = xmlGetNsProp(cur, (const xmlChar *)"name", NULL);
398 if ((value == NULL) || (*value == 0)) {
399 xsltGenericError(xsltGenericErrorContext,
400 "xsl:attribute-set : name is missing\n");
406 if (xmlValidateQName(value, 0)) {
407 xsltTransformError(NULL, style, cur,
408 "xsl:attribute-set : The name '%s' is not a valid QName.\n",
415 ncname = xsltSplitQName(style->dict, value, &prefix);
418 if (prefix != NULL) {
419 xmlNsPtr ns = xmlSearchNs(style->doc, cur, prefix);
421 xsltTransformError(NULL, style, cur,
422 "xsl:attribute-set : No namespace found for QName '%s:%s'\n",
430 if (style->attributeSets == NULL) {
431 #ifdef WITH_XSLT_DEBUG_ATTRIBUTES
432 xsltGenericDebug(xsltGenericDebugContext,
433 "creating attribute set table\n");
435 style->attributeSets = xmlHashCreate(10);
437 if (style->attributeSets == NULL)
440 set = xmlHashLookup2(style->attributeSets, ncname, nsUri);
442 set = xsltNewAttrSet();
445 xmlHashAddEntry2(style->attributeSets, ncname, nsUri, set);
449 * Parse the content. Only xsl:attribute elements are allowed.
451 child = cur->children;
452 while (child != NULL) {
454 * Report invalid nodes.
456 if ((child->type != XML_ELEMENT_NODE) ||
457 (child->ns == NULL) ||
458 (! IS_XSLT_ELEM(child)))
460 if (child->type == XML_ELEMENT_NODE)
461 xsltTransformError(NULL, style, child,
462 "xsl:attribute-set : unexpected child %s\n",
465 xsltTransformError(NULL, style, child,
466 "xsl:attribute-set : child of unexpected type\n");
467 } else if (!IS_XSLT_NAME(child, "attribute")) {
468 xsltTransformError(NULL, style, child,
469 "xsl:attribute-set : unexpected child xsl:%s\n",
472 #ifdef WITH_XSLT_DEBUG_ATTRIBUTES
473 xsltGenericDebug(xsltGenericDebugContext,
474 "add attribute to list %s\n", ncname);
476 xsltStylePreCompute(style, child);
477 if (child->children != NULL) {
478 #ifdef XSLT_REFACTORED
479 xsltParseSequenceConstructor(XSLT_CCTXT(style),
482 xsltParseTemplateContent(style, child);
485 if (child->psvi == NULL) {
486 xsltTransformError(NULL, style, child,
487 "xsl:attribute-set : internal error, attribute %s not "
488 "compiled\n", child->name);
491 set->attrs = xsltAddAttrElemList(set->attrs, child);
499 * Process attribute "use-attribute-sets".
501 value = xmlGetNsProp(cur, BAD_CAST "use-attribute-sets", NULL);
503 const xmlChar *curval, *endval;
505 while (*curval != 0) {
506 while (IS_BLANK(*curval)) curval++;
510 while ((*endval != 0) && (!IS_BLANK(*endval))) endval++;
511 curval = xmlDictLookup(style->dict, curval, endval - curval);
513 const xmlChar *ncname2 = NULL;
514 const xmlChar *prefix2 = NULL;
515 const xmlChar *nsUri2 = NULL;
517 #ifdef WITH_XSLT_DEBUG_ATTRIBUTES
518 xsltGenericDebug(xsltGenericDebugContext,
519 "xsl:attribute-set : %s adds use %s\n", ncname, curval);
522 if (xmlValidateQName(curval, 0)) {
523 xsltTransformError(NULL, style, cur,
524 "xsl:attribute-set : The name '%s' in "
525 "use-attribute-sets is not a valid QName.\n", curval);
531 ncname2 = xsltSplitQName(style->dict, curval, &prefix2);
532 if (prefix2 != NULL) {
533 xmlNsPtr ns2 = xmlSearchNs(style->doc, cur, prefix2);
535 xsltTransformError(NULL, style, cur,
536 "xsl:attribute-set : No namespace found for QName "
537 "'%s:%s' in use-attribute-sets\n",
545 set->useAttrSets = xsltAddUseAttrSetList(set->useAttrSets,
554 #ifdef WITH_XSLT_DEBUG_ATTRIBUTES
555 xsltGenericDebug(xsltGenericDebugContext,
556 "updated attribute list %s\n", ncname);
561 * xsltResolveUseAttrSets:
562 * @set: the attribute set
563 * @asctx: the context for attribute set resolution
564 * @depth: recursion depth
566 * Process "use-attribute-sets".
569 xsltResolveUseAttrSets(xsltAttrSetPtr set, xsltStylesheetPtr topStyle,
571 xsltStylesheetPtr cur;
572 xsltAttrSetPtr other;
573 xsltUseAttrSetPtr use = set->useAttrSets;
574 xsltUseAttrSetPtr next;
576 while (use != NULL) {
578 * Iterate top stylesheet and all imports.
581 while (cur != NULL) {
582 if (cur->attributeSets) {
583 other = xmlHashLookup2(cur->attributeSets, use->ncname,
586 xsltResolveAttrSet(other, topStyle, cur, use->ncname,
588 xsltMergeAttrSets(set, other);
592 cur = xsltNextImport(cur);
596 /* Free useAttrSets early. */
597 xsltFreeUseAttrSet(use);
601 set->useAttrSets = NULL;
605 * xsltResolveAttrSet:
606 * @set: the attribute set
607 * @asctx: the context for attribute set resolution
608 * @name: the local name of the attirbute set
609 * @ns: the namespace of the attribute set
610 * @depth: recursion depth
612 * resolve the references in an attribute set.
615 xsltResolveAttrSet(xsltAttrSetPtr set, xsltStylesheetPtr topStyle,
616 xsltStylesheetPtr style, const xmlChar *name,
617 const xmlChar *ns, int depth) {
618 xsltStylesheetPtr cur;
619 xsltAttrSetPtr other;
621 if (set->state == ATTRSET_RESOLVED)
623 if (set->state == ATTRSET_RESOLVING) {
624 xsltTransformError(NULL, topStyle, NULL,
625 "xsl:attribute-set : use-attribute-sets recursion detected"
628 set->state = ATTRSET_RESOLVED;
632 xsltTransformError(NULL, topStyle, NULL,
633 "xsl:attribute-set : use-attribute-sets maximum recursion "
634 "depth exceeded on %s\n", name);
639 set->state = ATTRSET_RESOLVING;
641 xsltResolveUseAttrSets(set, topStyle, depth);
643 /* Merge imported sets. */
644 cur = xsltNextImport(style);
645 while (cur != NULL) {
646 if (cur->attributeSets != NULL) {
647 other = xmlHashLookup2(cur->attributeSets, name, ns);
650 #ifdef WITH_XSLT_DEBUG_ATTRIBUTES
651 xsltGenericDebug(xsltGenericDebugContext,
652 "xsl:attribute-set : merging import for %s\n", name);
654 xsltResolveUseAttrSets(other, topStyle, depth);
655 xsltMergeAttrSets(set, other);
656 xmlHashRemoveEntry2(cur->attributeSets, name, ns, NULL);
657 xsltFreeAttrSet(other);
661 cur = xsltNextImport(cur);
664 set->state = ATTRSET_RESOLVED;
668 * xsltResolveSASCallback:
669 * @set: the attribute set
670 * @asctx: the context for attribute set resolution
671 * @name: the local name of the attirbute set
672 * @ns: the namespace of the attribute set
674 * resolve the references in an attribute set.
677 xsltResolveSASCallback(xsltAttrSetPtr set, xsltAttrSetContextPtr asctx,
678 const xmlChar *name, const xmlChar *ns,
679 ATTRIBUTE_UNUSED const xmlChar *ignored) {
680 xsltStylesheetPtr topStyle = asctx->topStyle;
681 xsltStylesheetPtr style = asctx->style;
683 xsltResolveAttrSet(set, topStyle, style, name, ns, 1);
685 /* Move attribute sets to top stylesheet. */
686 if (style != topStyle) {
688 * This imported stylesheet won't be visited anymore. Don't bother
689 * removing the hash entry.
691 if (xmlHashAddEntry2(topStyle->attributeSets, name, ns, set) < 0) {
692 xsltGenericError(xsltGenericErrorContext,
693 "xsl:attribute-set : internal error, can't move imported "
694 " attribute set %s\n", name);
700 * xsltResolveStylesheetAttributeSet:
701 * @style: the XSLT stylesheet
703 * resolve the references between attribute sets.
706 xsltResolveStylesheetAttributeSet(xsltStylesheetPtr style) {
707 xsltStylesheetPtr cur;
708 xsltAttrSetContext asctx;
710 #ifdef WITH_XSLT_DEBUG_ATTRIBUTES
711 xsltGenericDebug(xsltGenericDebugContext,
712 "Resolving attribute sets references\n");
714 asctx.topStyle = style;
716 while (cur != NULL) {
717 if (cur->attributeSets != NULL) {
718 if (style->attributeSets == NULL) {
719 #ifdef WITH_XSLT_DEBUG_ATTRIBUTES
720 xsltGenericDebug(xsltGenericDebugContext,
721 "creating attribute set table\n");
723 style->attributeSets = xmlHashCreate(10);
726 xmlHashScanFull(cur->attributeSets,
727 (xmlHashScannerFull) xsltResolveSASCallback, &asctx);
731 * the attribute lists have either been migrated to style
732 * or freed directly in xsltResolveSASCallback()
734 xmlHashFree(cur->attributeSets, NULL);
735 cur->attributeSets = NULL;
738 cur = xsltNextImport(cur);
744 * @ctxt: a XSLT process context
745 * @contextNode: the current node in the source tree
746 * @inst: the xsl:attribute element
747 * @castedComp: precomputed information
749 * Process the xslt attribute node on the source node
752 xsltAttribute(xsltTransformContextPtr ctxt,
753 xmlNodePtr contextNode,
755 xsltStylePreCompPtr castedComp)
757 #ifdef XSLT_REFACTORED
758 xsltStyleItemAttributePtr comp =
759 (xsltStyleItemAttributePtr) castedComp;
761 xsltStylePreCompPtr comp = castedComp;
763 xmlNodePtr targetElem;
764 xmlChar *prop = NULL;
765 const xmlChar *name = NULL, *prefix = NULL, *nsName = NULL;
766 xmlChar *value = NULL;
770 if ((ctxt == NULL) || (contextNode == NULL) || (inst == NULL) ||
771 (inst->type != XML_ELEMENT_NODE) )
775 * A comp->has_name == 0 indicates that we need to skip this instruction,
776 * since it was evaluated to be invalid already during compilation.
781 * BIG NOTE: This previously used xsltGetSpecialNamespace() and
782 * xsltGetNamespace(), but since both are not appropriate, we
783 * will process namespace lookup here to avoid adding yet another
784 * ns-lookup function to namespaces.c.
787 * SPEC XSLT 1.0: Error cases:
788 * - Creating nodes other than text nodes during the instantiation of
789 * the content of the xsl:attribute element; implementations may
790 * either signal the error or ignore the offending nodes."
794 xsltTransformError(ctxt, NULL, inst,
795 "Internal error in xsltAttribute(): "
796 "The XSLT 'attribute' instruction was not compiled.\n");
800 * TODO: Shouldn't ctxt->insert == NULL be treated as an internal error?
801 * So report an internal error?
803 if (ctxt->insert == NULL)
807 * "Adding an attribute to a node that is not an element;
808 * implementations may either signal the error or ignore the attribute."
810 * TODO: I think we should signal such errors in the future, and maybe
811 * provide an option to ignore such errors.
813 targetElem = ctxt->insert;
814 if (targetElem->type != XML_ELEMENT_NODE)
819 * "Adding an attribute to an element after children have been added
820 * to it; implementations may either signal the error or ignore the
823 * TODO: We should decide whether not to report such errors or
824 * to ignore them; note that we *ignore* if the parent is not an
825 * element, but here we report an error.
827 if (targetElem->children != NULL) {
829 * NOTE: Ah! This seems to be intended to support streamed
830 * result generation!.
832 xsltTransformError(ctxt, NULL, inst,
833 "xsl:attribute: Cannot add attributes to an "
834 "element if children have been already added "
835 "to the element.\n");
845 if (ctxt->debugStatus != XSLT_DEBUG_NONE)
846 xslHandleDebugger(inst, contextNode, NULL, ctxt);
849 if (comp->name == NULL) {
850 /* TODO: fix attr acquisition wrt to the XSLT namespace */
851 prop = xsltEvalAttrValueTemplate(ctxt, inst,
852 (const xmlChar *) "name", XSLT_NAMESPACE);
854 xsltTransformError(ctxt, NULL, inst,
855 "xsl:attribute: The attribute 'name' is missing.\n");
858 if (xmlValidateQName(prop, 0)) {
859 xsltTransformError(ctxt, NULL, inst,
860 "xsl:attribute: The effective name '%s' is not a "
861 "valid QName.\n", prop);
862 /* we fall through to catch any further errors, if possible */
866 * Reject a name of "xmlns".
868 if (xmlStrEqual(prop, BAD_CAST "xmlns")) {
869 xsltTransformError(ctxt, NULL, inst,
870 "xsl:attribute: The effective name 'xmlns' is not allowed.\n");
875 name = xsltSplitQName(ctxt->dict, prop, &prefix);
879 * The "name" value was static.
881 #ifdef XSLT_REFACTORED
882 prefix = comp->nsPrefix;
885 name = xsltSplitQName(ctxt->dict, comp->name, &prefix);
890 * Process namespace semantics
891 * ---------------------------
893 * Evaluate the namespace name.
897 * The "namespace" attribute was existent.
899 if (comp->ns != NULL) {
901 * No AVT; just plain text for the namespace name.
903 if (comp->ns[0] != 0)
910 /* TODO: check attr acquisition wrt to the XSLT namespace */
911 tmpNsName = xsltEvalAttrValueTemplate(ctxt, inst,
912 (const xmlChar *) "namespace", XSLT_NAMESPACE);
914 * This fixes bug #302020: The AVT might also evaluate to the
915 * empty string; this means that the empty string also indicates
918 * "If the string is empty, then the expanded-name of the
919 * attribute has a null namespace URI."
921 if ((tmpNsName != NULL) && (tmpNsName[0] != 0))
922 nsName = xmlDictLookup(ctxt->dict, BAD_CAST tmpNsName, -1);
926 if (xmlStrEqual(nsName, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
927 xsltTransformError(ctxt, NULL, inst,
928 "xsl:attribute: Namespace http://www.w3.org/2000/xmlns/ "
932 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
933 prefix = BAD_CAST "xml";
934 } else if (xmlStrEqual(prefix, BAD_CAST "xml")) {
937 } else if (prefix != NULL) {
940 * "If the namespace attribute is not present, then the QName is
941 * expanded into an expanded-name using the namespace declarations
942 * in effect for the xsl:attribute element, *not* including any
943 * default namespace declaration."
945 ns = xmlSearchNs(inst->doc, inst, prefix);
948 * Note that this is treated as an error now (checked with
949 * Saxon, Xalan-J and MSXML).
951 xsltTransformError(ctxt, NULL, inst,
952 "xsl:attribute: The QName '%s:%s' has no "
953 "namespace binding in scope in the stylesheet; "
954 "this is an error, since the namespace was not "
955 "specified by the instruction itself.\n", prefix, name);
961 * Find/create a matching ns-decl in the result tree.
968 * OPTIMIZE TODO: How do we know if we are adding to a
969 * fragment or to the result tree?
971 * If we are adding to a result tree fragment (i.e., not to the
972 * actual result tree), we'll don't bother searching for the
973 * ns-decl, but just store it in the dummy-doc of the result
976 if (nsName != NULL) {
978 * TODO: Get the doc of @targetElem.
980 ns = xsltTreeAcquireStoredNs(some doc, nsName, prefix);
985 if (nsName != NULL) {
987 * Something about ns-prefixes:
989 * "XSLT processors may make use of the prefix of the QName specified
990 * in the name attribute when selecting the prefix used for outputting
991 * the created attribute as XML; however, they are not required to do
992 * so and, if the prefix is xmlns, they must not do so"
995 * xsl:attribute can produce a scenario where the prefix is NULL,
996 * so generate a prefix.
998 if ((prefix == NULL) || xmlStrEqual(prefix, BAD_CAST "xmlns")) {
999 xmlChar *pref = xmlStrdup(BAD_CAST "ns_1");
1001 ns = xsltGetSpecialNamespace(ctxt, inst, nsName, pref, targetElem);
1005 ns = xsltGetSpecialNamespace(ctxt, inst, nsName, prefix,
1009 xsltTransformError(ctxt, NULL, inst,
1010 "Namespace fixup error: Failed to acquire an in-scope "
1011 "namespace binding for the generated attribute '{%s}%s'.\n",
1017 * Construction of the value
1018 * -------------------------
1020 if (inst->children == NULL) {
1023 * TODO: Do we need to put the empty string in ?
1025 attr = xmlSetNsProp(ctxt->insert, ns, name, (const xmlChar *) "");
1026 } else if ((inst->children->next == NULL) &&
1027 ((inst->children->type == XML_TEXT_NODE) ||
1028 (inst->children->type == XML_CDATA_SECTION_NODE)))
1033 * xmlSetNsProp() will take care of duplicates.
1035 attr = xmlSetNsProp(ctxt->insert, ns, name, NULL);
1036 if (attr == NULL) /* TODO: report error ? */
1039 * This was taken over from xsltCopyText() (transform.c).
1041 if (ctxt->internalized &&
1042 (ctxt->insert->doc != NULL) &&
1043 (ctxt->insert->doc->dict == ctxt->dict))
1045 copyTxt = xmlNewText(NULL);
1046 if (copyTxt == NULL) /* TODO: report error */
1049 * This is a safe scenario where we don't need to lookup
1052 copyTxt->content = inst->children->content;
1054 * Copy "disable-output-escaping" information.
1055 * TODO: Does this have any effect for attribute values
1058 if (inst->children->name == xmlStringTextNoenc)
1059 copyTxt->name = xmlStringTextNoenc;
1064 copyTxt = xmlNewText(inst->children->content);
1065 if (copyTxt == NULL) /* TODO: report error */
1068 attr->children = attr->last = copyTxt;
1069 copyTxt->parent = (xmlNodePtr) attr;
1070 copyTxt->doc = attr->doc;
1072 * Copy "disable-output-escaping" information.
1073 * TODO: Does this have any effect for attribute values
1076 if (inst->children->name == xmlStringTextNoenc)
1077 copyTxt->name = xmlStringTextNoenc;
1080 * since we create the attribute without content IDness must be
1081 * asserted as a second step
1083 if ((copyTxt->content != NULL) &&
1084 (xmlIsID(attr->doc, attr->parent, attr)))
1085 xmlAddID(NULL, attr->doc, copyTxt->content, attr);
1088 * The sequence constructor might be complex, so instantiate it.
1090 value = xsltEvalTemplateString(ctxt, contextNode, inst);
1091 if (value != NULL) {
1092 attr = xmlSetNsProp(ctxt->insert, ns, name, value);
1096 * TODO: Do we have to add the empty string to the attr?
1097 * TODO: Does a value of NULL indicate an
1098 * error in xsltEvalTemplateString() ?
1100 attr = xmlSetNsProp(ctxt->insert, ns, name,
1101 (const xmlChar *) "");
1110 * xsltApplyAttributeSet:
1111 * @ctxt: the XSLT stylesheet
1112 * @node: the node in the source tree.
1113 * @inst: the attribute node "xsl:use-attribute-sets"
1114 * @attrSets: the list of QNames of the attribute-sets to be applied
1116 * Apply the xsl:use-attribute-sets.
1117 * If @attrSets is NULL, then @inst will be used to exctract this
1119 * If both, @attrSets and @inst, are NULL, then this will do nothing.
1122 xsltApplyAttributeSet(xsltTransformContextPtr ctxt, xmlNodePtr node,
1124 const xmlChar *attrSets)
1126 const xmlChar *ncname = NULL;
1127 const xmlChar *prefix = NULL;
1128 const xmlChar *curstr, *endstr;
1130 xsltStylesheetPtr style;
1132 if (attrSets == NULL) {
1137 * Extract the value from @inst.
1139 if (inst->type == XML_ATTRIBUTE_NODE) {
1140 if ( ((xmlAttrPtr) inst)->children != NULL)
1141 attrSets = ((xmlAttrPtr) inst)->children->content;
1144 if (attrSets == NULL) {
1146 * TODO: Return an error?
1153 * Parse/apply the list of QNames.
1156 while (*curstr != 0) {
1157 while (IS_BLANK(*curstr))
1162 while ((*endstr != 0) && (!IS_BLANK(*endstr)))
1164 curstr = xmlDictLookup(ctxt->dict, curstr, endstr - curstr);
1167 const xmlChar *nsUri = NULL;
1169 #ifdef WITH_XSLT_DEBUG_ATTRIBUTES
1170 xsltGenericDebug(xsltGenericDebugContext,
1171 "apply attribute set %s\n", curstr);
1174 if (xmlValidateQName(curstr, 0)) {
1175 xsltTransformError(ctxt, NULL, inst,
1176 "The name '%s' in use-attribute-sets is not a valid "
1177 "QName.\n", curstr);
1181 ncname = xsltSplitQName(ctxt->dict, curstr, &prefix);
1182 if (prefix != NULL) {
1183 ns = xmlSearchNs(inst->doc, inst, prefix);
1185 xsltTransformError(ctxt, NULL, inst,
1186 "use-attribute-set : No namespace found for QName "
1187 "'%s:%s'\n", prefix, ncname);
1193 style = ctxt->style;
1195 #ifdef WITH_DEBUGGER
1196 if ((style != NULL) &&
1197 (style->attributeSets != NULL) &&
1198 (ctxt->debugStatus != XSLT_DEBUG_NONE))
1200 set = xmlHashLookup2(style->attributeSets, ncname, nsUri);
1201 if ((set != NULL) && (set->attrs != NULL) &&
1202 (set->attrs->attr != NULL))
1203 xslHandleDebugger(set->attrs->attr->parent, node, NULL,
1208 * Lookup the referenced attribute-set. All attribute sets were
1209 * moved to the top stylesheet so there's no need to iterate
1210 * imported stylesheets
1212 set = xmlHashLookup2(style->attributeSets, ncname, nsUri);
1214 xsltAttrElemPtr cur = set->attrs;
1215 while (cur != NULL) {
1216 if (cur->attr != NULL) {
1217 xsltAttribute(ctxt, node, cur->attr,
1229 * xsltFreeAttributeSetsHashes:
1230 * @style: an XSLT stylesheet
1232 * Free up the memory used by attribute sets
1235 xsltFreeAttributeSetsHashes(xsltStylesheetPtr style) {
1236 if (style->attributeSets != NULL)
1237 xmlHashFree((xmlHashTablePtr) style->attributeSets,
1238 (xmlHashDeallocator) xsltFreeAttrSet);
1239 style->attributeSets = NULL;