2 * tree.c : implementation of access function for an XML tree.
5 * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
7 * See Copyright for the status of this software.
16 #include <string.h> /* for memset() only ! */
28 #include <libxml/xmlmemory.h>
29 #include <libxml/tree.h>
30 #include <libxml/parser.h>
31 #include <libxml/uri.h>
32 #include <libxml/entities.h>
33 #include <libxml/valid.h>
34 #include <libxml/xmlerror.h>
35 #include <libxml/parserInternals.h>
36 #include <libxml/globals.h>
37 #ifdef LIBXML_HTML_ENABLED
38 #include <libxml/HTMLtree.h>
40 #ifdef LIBXML_DEBUG_ENABLED
41 #include <libxml/debugXML.h>
44 int __xmlRegisterCallbacks = 0;
46 /************************************************************************
48 * Forward declarations *
50 ************************************************************************/
53 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
55 static xmlChar* xmlGetPropNodeValueInternal(xmlAttrPtr prop);
57 /************************************************************************
59 * Tree memory error handler *
61 ************************************************************************/
64 * @extra: extra informations
66 * Handle an out of memory condition
69 xmlTreeErrMemory(const char *extra)
71 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
76 * @code: the error number
77 * @extra: extra informations
79 * Handle an out of memory condition
82 xmlTreeErr(int code, xmlNodePtr node, const char *extra)
84 const char *msg = NULL;
87 case XML_TREE_INVALID_HEX:
88 msg = "invalid hexadecimal character value\n";
90 case XML_TREE_INVALID_DEC:
91 msg = "invalid decimal character value\n";
93 case XML_TREE_UNTERMINATED_ENTITY:
94 msg = "unterminated entity reference %15s\n";
96 case XML_TREE_NOT_UTF8:
97 msg = "string is not in UTF-8\n";
100 msg = "unexpected error number\n";
102 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
105 /************************************************************************
107 * A few static variables and macros *
109 ************************************************************************/
110 /* #undef xmlStringText */
111 const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
112 /* #undef xmlStringTextNoenc */
113 const xmlChar xmlStringTextNoenc[] =
114 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
115 /* #undef xmlStringComment */
116 const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
118 static int xmlCompressMode = 0;
119 static int xmlCheckDTD = 1;
121 #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
122 xmlNodePtr ulccur = (n)->children; \
123 if (ulccur == NULL) { \
126 while (ulccur->next != NULL) { \
127 ulccur->parent = (n); \
128 ulccur = ulccur->next; \
130 ulccur->parent = (n); \
131 (n)->last = ulccur; \
134 #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
135 (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
137 /* #define DEBUG_BUFFER */
138 /* #define DEBUG_TREE */
140 /************************************************************************
142 * Functions to move to entities.c once the *
143 * API freeze is smoothen and they can be made public. *
145 ************************************************************************/
146 #include <libxml/hash.h>
148 #ifdef LIBXML_TREE_ENABLED
150 * xmlGetEntityFromDtd:
151 * @dtd: A pointer to the DTD to search
152 * @name: The entity name
154 * Do an entity lookup in the DTD entity hash table and
155 * return the corresponding entity, if found.
157 * Returns A pointer to the entity structure or NULL if not found.
160 xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
161 xmlEntitiesTablePtr table;
163 if((dtd != NULL) && (dtd->entities != NULL)) {
164 table = (xmlEntitiesTablePtr) dtd->entities;
165 return((xmlEntityPtr) xmlHashLookup(table, name));
166 /* return(xmlGetEntityFromTable(table, name)); */
171 * xmlGetParameterEntityFromDtd:
172 * @dtd: A pointer to the DTD to search
173 * @name: The entity name
175 * Do an entity lookup in the DTD pararmeter entity hash table and
176 * return the corresponding entity, if found.
178 * Returns A pointer to the entity structure or NULL if not found.
181 xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
182 xmlEntitiesTablePtr table;
184 if ((dtd != NULL) && (dtd->pentities != NULL)) {
185 table = (xmlEntitiesTablePtr) dtd->pentities;
186 return((xmlEntityPtr) xmlHashLookup(table, name));
187 /* return(xmlGetEntityFromTable(table, name)); */
191 #endif /* LIBXML_TREE_ENABLED */
193 /************************************************************************
195 * QName handling helper *
197 ************************************************************************/
202 * @prefix: the prefix
203 * @memory: preallocated memory
204 * @len: preallocated memory length
206 * Builds the QName @prefix:@ncname in @memory if there is enough space
207 * and prefix is not NULL nor empty, otherwise allocate a new string.
208 * If prefix is NULL or empty it returns ncname.
210 * Returns the new string which must be freed by the caller if different from
211 * @memory and @ncname or NULL in case of error
214 xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
215 xmlChar *memory, int len) {
219 if (ncname == NULL) return(NULL);
220 if (prefix == NULL) return((xmlChar *) ncname);
222 lenn = strlen((char *) ncname);
223 lenp = strlen((char *) prefix);
225 if ((memory == NULL) || (len < lenn + lenp + 2)) {
226 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
228 xmlTreeErrMemory("building QName");
234 memcpy(&ret[0], prefix, lenp);
236 memcpy(&ret[lenp + 1], ncname, lenn);
237 ret[lenn + lenp + 1] = 0;
243 * @name: the full QName
244 * @prefix: a xmlChar **
246 * parse an XML qualified name string
248 * [NS 5] QName ::= (Prefix ':')? LocalPart
250 * [NS 6] Prefix ::= NCName
252 * [NS 7] LocalPart ::= NCName
254 * Returns NULL if not a QName, otherwise the local part, and prefix
255 * is updated to get the Prefix if any.
259 xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
263 if (prefix == NULL) return(NULL);
265 if (name == NULL) return(NULL);
267 #ifndef XML_XML_NAMESPACE
268 /* xml: prefix is not really a namespace */
269 if ((name[0] == 'x') && (name[1] == 'm') &&
270 (name[2] == 'l') && (name[3] == ':'))
274 /* nasty but valid */
279 * we are not trying to validate but just to cut, and yes it will
280 * work even if this is as set of UTF-8 encoded chars
282 while ((name[len] != 0) && (name[len] != ':'))
288 *prefix = xmlStrndup(name, len);
289 if (*prefix == NULL) {
290 xmlTreeErrMemory("QName split");
293 ret = xmlStrdup(&name[len + 1]);
295 xmlTreeErrMemory("QName split");
296 if (*prefix != NULL) {
308 * @name: the full QName
311 * parse an XML qualified name string,i
313 * returns NULL if it is not a Qualified Name, otherwise, update len
314 * with the lenght in byte of the prefix and return a pointer
315 * to the start of the name without the prefix
319 xmlSplitQName3(const xmlChar *name, int *len) {
322 if (name == NULL) return(NULL);
323 if (len == NULL) return(NULL);
325 /* nasty but valid */
330 * we are not trying to validate but just to cut, and yes it will
331 * work even if this is as set of UTF-8 encoded chars
333 while ((name[l] != 0) && (name[l] != ':'))
344 /************************************************************************
346 * Check Name, NCName and QName strings *
348 ************************************************************************/
350 #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
352 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_DEBUG_ENABLED) || defined (LIBXML_HTML_ENABLED) || defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED)
355 * @value: the value to check
356 * @space: allow spaces in front and end of the string
358 * Check that a value conforms to the lexical space of NCName
360 * Returns 0 if this validates, a positive error code number otherwise
361 * and -1 in case of internal or API error.
364 xmlValidateNCName(const xmlChar *value, int space) {
365 const xmlChar *cur = value;
372 * First quick algorithm for ASCII range
375 while (IS_BLANK_CH(*cur)) cur++;
376 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
381 while (((*cur >= 'a') && (*cur <= 'z')) ||
382 ((*cur >= 'A') && (*cur <= 'Z')) ||
383 ((*cur >= '0') && (*cur <= '9')) ||
384 (*cur == '_') || (*cur == '-') || (*cur == '.'))
387 while (IS_BLANK_CH(*cur)) cur++;
393 * Second check for chars outside the ASCII range
396 c = CUR_SCHAR(cur, l);
398 while (IS_BLANK(c)) {
400 c = CUR_SCHAR(cur, l);
403 if ((!IS_LETTER(c)) && (c != '_'))
406 c = CUR_SCHAR(cur, l);
407 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
408 (c == '-') || (c == '_') || IS_COMBINING(c) ||
411 c = CUR_SCHAR(cur, l);
414 while (IS_BLANK(c)) {
416 c = CUR_SCHAR(cur, l);
426 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
429 * @value: the value to check
430 * @space: allow spaces in front and end of the string
432 * Check that a value conforms to the lexical space of QName
434 * Returns 0 if this validates, a positive error code number otherwise
435 * and -1 in case of internal or API error.
438 xmlValidateQName(const xmlChar *value, int space) {
439 const xmlChar *cur = value;
445 * First quick algorithm for ASCII range
448 while (IS_BLANK_CH(*cur)) cur++;
449 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
454 while (((*cur >= 'a') && (*cur <= 'z')) ||
455 ((*cur >= 'A') && (*cur <= 'Z')) ||
456 ((*cur >= '0') && (*cur <= '9')) ||
457 (*cur == '_') || (*cur == '-') || (*cur == '.'))
461 if (((*cur >= 'a') && (*cur <= 'z')) ||
462 ((*cur >= 'A') && (*cur <= 'Z')) ||
467 while (((*cur >= 'a') && (*cur <= 'z')) ||
468 ((*cur >= 'A') && (*cur <= 'Z')) ||
469 ((*cur >= '0') && (*cur <= '9')) ||
470 (*cur == '_') || (*cur == '-') || (*cur == '.'))
474 while (IS_BLANK_CH(*cur)) cur++;
480 * Second check for chars outside the ASCII range
483 c = CUR_SCHAR(cur, l);
485 while (IS_BLANK(c)) {
487 c = CUR_SCHAR(cur, l);
490 if ((!IS_LETTER(c)) && (c != '_'))
493 c = CUR_SCHAR(cur, l);
494 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
495 (c == '-') || (c == '_') || IS_COMBINING(c) ||
498 c = CUR_SCHAR(cur, l);
502 c = CUR_SCHAR(cur, l);
503 if ((!IS_LETTER(c)) && (c != '_'))
506 c = CUR_SCHAR(cur, l);
507 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
508 (c == '-') || (c == '_') || IS_COMBINING(c) ||
511 c = CUR_SCHAR(cur, l);
515 while (IS_BLANK(c)) {
517 c = CUR_SCHAR(cur, l);
527 * @value: the value to check
528 * @space: allow spaces in front and end of the string
530 * Check that a value conforms to the lexical space of Name
532 * Returns 0 if this validates, a positive error code number otherwise
533 * and -1 in case of internal or API error.
536 xmlValidateName(const xmlChar *value, int space) {
537 const xmlChar *cur = value;
543 * First quick algorithm for ASCII range
546 while (IS_BLANK_CH(*cur)) cur++;
547 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
548 (*cur == '_') || (*cur == ':'))
552 while (((*cur >= 'a') && (*cur <= 'z')) ||
553 ((*cur >= 'A') && (*cur <= 'Z')) ||
554 ((*cur >= '0') && (*cur <= '9')) ||
555 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
558 while (IS_BLANK_CH(*cur)) cur++;
564 * Second check for chars outside the ASCII range
567 c = CUR_SCHAR(cur, l);
569 while (IS_BLANK(c)) {
571 c = CUR_SCHAR(cur, l);
574 if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
577 c = CUR_SCHAR(cur, l);
578 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
579 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
581 c = CUR_SCHAR(cur, l);
584 while (IS_BLANK(c)) {
586 c = CUR_SCHAR(cur, l);
595 * xmlValidateNMToken:
596 * @value: the value to check
597 * @space: allow spaces in front and end of the string
599 * Check that a value conforms to the lexical space of NMToken
601 * Returns 0 if this validates, a positive error code number otherwise
602 * and -1 in case of internal or API error.
605 xmlValidateNMToken(const xmlChar *value, int space) {
606 const xmlChar *cur = value;
612 * First quick algorithm for ASCII range
615 while (IS_BLANK_CH(*cur)) cur++;
616 if (((*cur >= 'a') && (*cur <= 'z')) ||
617 ((*cur >= 'A') && (*cur <= 'Z')) ||
618 ((*cur >= '0') && (*cur <= '9')) ||
619 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
623 while (((*cur >= 'a') && (*cur <= 'z')) ||
624 ((*cur >= 'A') && (*cur <= 'Z')) ||
625 ((*cur >= '0') && (*cur <= '9')) ||
626 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
629 while (IS_BLANK_CH(*cur)) cur++;
635 * Second check for chars outside the ASCII range
638 c = CUR_SCHAR(cur, l);
640 while (IS_BLANK(c)) {
642 c = CUR_SCHAR(cur, l);
645 if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
646 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
649 c = CUR_SCHAR(cur, l);
650 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
651 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
653 c = CUR_SCHAR(cur, l);
656 while (IS_BLANK(c)) {
658 c = CUR_SCHAR(cur, l);
665 #endif /* LIBXML_TREE_ENABLED */
667 /************************************************************************
669 * Allocation and deallocation of basic structures *
671 ************************************************************************/
674 * xmlSetBufferAllocationScheme:
675 * @scheme: allocation method to use
677 * Set the buffer allocation method. Types are
678 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
679 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
680 * improves performance
683 xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
684 if ((scheme == XML_BUFFER_ALLOC_EXACT) ||
685 (scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
686 (scheme == XML_BUFFER_ALLOC_HYBRID))
687 xmlBufferAllocScheme = scheme;
691 * xmlGetBufferAllocationScheme:
694 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
695 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
696 * improves performance
697 * XML_BUFFER_ALLOC_HYBRID - use exact sizes on small strings to keep memory usage tight
698 * in normal usage, and doubleit on large strings to avoid
699 * pathological performance.
701 * Returns the current allocation scheme
703 xmlBufferAllocationScheme
704 xmlGetBufferAllocationScheme(void) {
705 return(xmlBufferAllocScheme);
710 * @node: the element carrying the namespace
711 * @href: the URI associated
712 * @prefix: the prefix for the namespace
714 * Creation of a new Namespace. This function will refuse to create
715 * a namespace with a similar prefix than an existing one present on this
717 * We use href==NULL in the case of an element creation where the namespace
719 * Returns a new namespace pointer or NULL
722 xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
725 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
728 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
729 /* xml namespace is predefined, no need to add it */
730 if (xmlStrEqual(href, XML_XML_NAMESPACE))
734 * Problem, this is an attempt to bind xml prefix to a wrong
735 * namespace, which breaks
736 * Namespace constraint: Reserved Prefixes and Namespace Names
737 * from XML namespace. But documents authors may not care in
738 * their context so let's proceed.
743 * Allocate a new Namespace and fill the fields.
745 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
747 xmlTreeErrMemory("building namespace");
750 memset(cur, 0, sizeof(xmlNs));
751 cur->type = XML_LOCAL_NAMESPACE;
754 cur->href = xmlStrdup(href);
756 cur->prefix = xmlStrdup(prefix);
759 * Add it at the end to preserve parsing order ...
760 * and checks for existing use of the prefix
763 if (node->nsDef == NULL) {
766 xmlNsPtr prev = node->nsDef;
768 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
769 (xmlStrEqual(prev->prefix, cur->prefix))) {
773 while (prev->next != NULL) {
775 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
776 (xmlStrEqual(prev->prefix, cur->prefix))) {
789 * @node: a node in the document
790 * @ns: a namespace pointer
792 * Associate a namespace to a node, a posteriori.
795 xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
798 xmlGenericError(xmlGenericErrorContext,
799 "xmlSetNs: node == NULL\n");
808 * @cur: the namespace pointer
810 * Free up the structures associated to a namespace
813 xmlFreeNs(xmlNsPtr cur) {
816 xmlGenericError(xmlGenericErrorContext,
817 "xmlFreeNs : ns == NULL\n");
821 if (cur->href != NULL) xmlFree((char *) cur->href);
822 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
828 * @cur: the first namespace pointer
830 * Free up all the structures associated to the chained namespaces.
833 xmlFreeNsList(xmlNsPtr cur) {
837 xmlGenericError(xmlGenericErrorContext,
838 "xmlFreeNsList : ns == NULL\n");
842 while (cur != NULL) {
851 * @doc: the document pointer
852 * @name: the DTD name
853 * @ExternalID: the external ID
854 * @SystemID: the system ID
856 * Creation of a new DTD for the external subset. To create an
857 * internal subset, use xmlCreateIntSubset().
859 * Returns a pointer to the new DTD structure
862 xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
863 const xmlChar *ExternalID, const xmlChar *SystemID) {
866 if ((doc != NULL) && (doc->extSubset != NULL)) {
868 xmlGenericError(xmlGenericErrorContext,
869 "xmlNewDtd(%s): document %s already have a DTD %s\n",
870 /* !!! */ (char *) name, doc->name,
871 /* !!! */ (char *)doc->extSubset->name);
877 * Allocate a new DTD and fill the fields.
879 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
881 xmlTreeErrMemory("building DTD");
884 memset(cur, 0 , sizeof(xmlDtd));
885 cur->type = XML_DTD_NODE;
888 cur->name = xmlStrdup(name);
889 if (ExternalID != NULL)
890 cur->ExternalID = xmlStrdup(ExternalID);
891 if (SystemID != NULL)
892 cur->SystemID = xmlStrdup(SystemID);
894 doc->extSubset = cur;
897 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
898 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
904 * @doc: the document pointer
906 * Get the internal subset of a document
907 * Returns a pointer to the DTD structure or NULL if not found
911 xmlGetIntSubset(xmlDocPtr doc) {
917 while (cur != NULL) {
918 if (cur->type == XML_DTD_NODE)
919 return((xmlDtdPtr) cur);
922 return((xmlDtdPtr) doc->intSubset);
926 * xmlCreateIntSubset:
927 * @doc: the document pointer
928 * @name: the DTD name
929 * @ExternalID: the external (PUBLIC) ID
930 * @SystemID: the system ID
932 * Create the internal subset of a document
933 * Returns a pointer to the new DTD structure
936 xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
937 const xmlChar *ExternalID, const xmlChar *SystemID) {
940 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
942 xmlGenericError(xmlGenericErrorContext,
944 "xmlCreateIntSubset(): document %s already have an internal subset\n",
951 * Allocate a new DTD and fill the fields.
953 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
955 xmlTreeErrMemory("building internal subset");
958 memset(cur, 0, sizeof(xmlDtd));
959 cur->type = XML_DTD_NODE;
962 cur->name = xmlStrdup(name);
963 if (cur->name == NULL) {
964 xmlTreeErrMemory("building internal subset");
969 if (ExternalID != NULL) {
970 cur->ExternalID = xmlStrdup(ExternalID);
971 if (cur->ExternalID == NULL) {
972 xmlTreeErrMemory("building internal subset");
973 if (cur->name != NULL)
974 xmlFree((char *)cur->name);
979 if (SystemID != NULL) {
980 cur->SystemID = xmlStrdup(SystemID);
981 if (cur->SystemID == NULL) {
982 xmlTreeErrMemory("building internal subset");
983 if (cur->name != NULL)
984 xmlFree((char *)cur->name);
985 if (cur->ExternalID != NULL)
986 xmlFree((char *)cur->ExternalID);
992 doc->intSubset = cur;
995 if (doc->children == NULL) {
996 doc->children = (xmlNodePtr) cur;
997 doc->last = (xmlNodePtr) cur;
999 if (doc->type == XML_HTML_DOCUMENT_NODE) {
1002 prev = doc->children;
1003 prev->prev = (xmlNodePtr) cur;
1005 doc->children = (xmlNodePtr) cur;
1009 next = doc->children;
1010 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
1013 cur->prev = doc->last;
1014 cur->prev->next = (xmlNodePtr) cur;
1016 doc->last = (xmlNodePtr) cur;
1019 cur->prev = next->prev;
1020 if (cur->prev == NULL)
1021 doc->children = (xmlNodePtr) cur;
1023 cur->prev->next = (xmlNodePtr) cur;
1024 next->prev = (xmlNodePtr) cur;
1030 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1031 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1039 * Free a string if it is not owned by the "dict" dictionnary in the
1042 #define DICT_FREE(str) \
1043 if ((str) && ((!dict) || \
1044 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1045 xmlFree((char *)(str));
1052 * Copy a string using a "dict" dictionnary in the current scope,
1055 #define DICT_COPY(str, cpy) \
1058 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1059 cpy = (xmlChar *) (str); \
1061 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1063 cpy = xmlStrdup((const xmlChar *)(str)); }
1069 * Copy a string using a "dict" dictionnary in the current scope,
1072 #define DICT_CONST_COPY(str, cpy) \
1075 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1076 cpy = (const xmlChar *) (str); \
1078 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1080 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1085 * @cur: the DTD structure to free up
1087 * Free a DTD structure.
1090 xmlFreeDtd(xmlDtdPtr cur) {
1091 xmlDictPtr dict = NULL;
1096 if (cur->doc != NULL) dict = cur->doc->dict;
1098 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1099 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1101 if (cur->children != NULL) {
1102 xmlNodePtr next, c = cur->children;
1105 * Cleanup all nodes which are not part of the specific lists
1106 * of notations, elements, attributes and entities.
1110 if ((c->type != XML_NOTATION_NODE) &&
1111 (c->type != XML_ELEMENT_DECL) &&
1112 (c->type != XML_ATTRIBUTE_DECL) &&
1113 (c->type != XML_ENTITY_DECL)) {
1120 DICT_FREE(cur->name)
1121 DICT_FREE(cur->SystemID)
1122 DICT_FREE(cur->ExternalID)
1124 if (cur->notations != NULL)
1125 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
1127 if (cur->elements != NULL)
1128 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1129 if (cur->attributes != NULL)
1130 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1131 if (cur->entities != NULL)
1132 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1133 if (cur->pentities != NULL)
1134 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1141 * @version: xmlChar string giving the version of XML "1.0"
1143 * Creates a new XML document
1145 * Returns a new document
1148 xmlNewDoc(const xmlChar *version) {
1151 if (version == NULL)
1152 version = (const xmlChar *) "1.0";
1155 * Allocate a new document and fill the fields.
1157 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1159 xmlTreeErrMemory("building doc");
1162 memset(cur, 0, sizeof(xmlDoc));
1163 cur->type = XML_DOCUMENT_NODE;
1165 cur->version = xmlStrdup(version);
1166 if (cur->version == NULL) {
1167 xmlTreeErrMemory("building doc");
1171 cur->standalone = -1;
1172 cur->compression = -1; /* not initialized */
1174 cur->parseFlags = 0;
1175 cur->properties = XML_DOC_USERBUILT;
1177 * The in memory encoding is always UTF8
1178 * This field will never change and would
1179 * be obsolete if not for binary compatibility.
1181 cur->charset = XML_CHAR_ENCODING_UTF8;
1183 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1184 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1190 * @cur: pointer to the document
1192 * Free up all the structures used by a document, tree included.
1195 xmlFreeDoc(xmlDocPtr cur) {
1196 xmlDtdPtr extSubset, intSubset;
1197 xmlDictPtr dict = NULL;
1201 xmlGenericError(xmlGenericErrorContext,
1202 "xmlFreeDoc : document == NULL\n");
1206 #ifdef LIBXML_DEBUG_RUNTIME
1207 #ifdef LIBXML_DEBUG_ENABLED
1208 xmlDebugCheckDocument(stderr, cur);
1212 if (cur != NULL) dict = cur->dict;
1214 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1215 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1218 * Do this before freeing the children list to avoid ID lookups
1220 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1222 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1224 extSubset = cur->extSubset;
1225 intSubset = cur->intSubset;
1226 if (intSubset == extSubset)
1228 if (extSubset != NULL) {
1229 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
1230 cur->extSubset = NULL;
1231 xmlFreeDtd(extSubset);
1233 if (intSubset != NULL) {
1234 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
1235 cur->intSubset = NULL;
1236 xmlFreeDtd(intSubset);
1239 if (cur->children != NULL) xmlFreeNodeList(cur->children);
1240 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
1242 DICT_FREE(cur->version)
1243 DICT_FREE(cur->name)
1244 DICT_FREE(cur->encoding)
1247 if (dict) xmlDictFree(dict);
1251 * xmlStringLenGetNodeList:
1252 * @doc: the document
1253 * @value: the value of the text
1254 * @len: the length of the string value
1256 * Parse the value string and build the node list associated. Should
1257 * produce a flat tree with only TEXTs and ENTITY_REFs.
1258 * Returns a pointer to the first child
1261 xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
1262 xmlNodePtr ret = NULL, last = NULL;
1265 const xmlChar *cur = value, *end = cur + len;
1268 xmlBufferPtr buffer;
1270 if (value == NULL) return(NULL);
1272 buffer = xmlBufferCreateSize(0);
1273 if (buffer == NULL) return(NULL);
1274 xmlBufferSetAllocationScheme(buffer, XML_BUFFER_ALLOC_HYBRID);
1277 while ((cur < end) && (*cur != 0)) {
1278 if (cur[0] == '&') {
1283 * Save the current text.
1286 if (xmlBufferAdd(buffer, q, cur - q))
1290 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1296 while (tmp != ';') { /* Non input consuming loop */
1297 if ((tmp >= '0') && (tmp <= '9'))
1298 charval = charval * 16 + (tmp - '0');
1299 else if ((tmp >= 'a') && (tmp <= 'f'))
1300 charval = charval * 16 + (tmp - 'a') + 10;
1301 else if ((tmp >= 'A') && (tmp <= 'F'))
1302 charval = charval * 16 + (tmp - 'A') + 10;
1304 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1318 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1324 while (tmp != ';') { /* Non input consuming loops */
1325 if ((tmp >= '0') && (tmp <= '9'))
1326 charval = charval * 10 + (tmp - '0');
1328 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1344 * Read the entity string
1348 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1349 if ((cur >= end) || (*cur == 0)) {
1350 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1356 * Predefined entities don't generate nodes
1358 val = xmlStrndup(q, cur - q);
1359 ent = xmlGetDocEntity(doc, val);
1360 if ((ent != NULL) &&
1361 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1363 if (xmlBufferCat(buffer, ent->content))
1368 * Flush buffer so far
1371 node = xmlNewDocText(doc, NULL);
1373 if (val != NULL) xmlFree(val);
1376 node->content = xmlBufferDetach(buffer);
1381 last = xmlAddNextSibling(last, node);
1386 * Create a new REFERENCE_REF node
1388 node = xmlNewReference(doc, val);
1390 if (val != NULL) xmlFree(val);
1393 else if ((ent != NULL) && (ent->children == NULL)) {
1396 ent->children = xmlStringGetNodeList(doc,
1397 (const xmlChar*)node->content);
1399 temp = ent->children;
1401 temp->parent = (xmlNodePtr)ent;
1409 last = xmlAddNextSibling(last, node);
1421 l = xmlCopyCharMultiByte(buf, charval);
1424 if (xmlBufferCat(buffer, buf))
1434 * Handle the last piece of text.
1436 if (xmlBufferAdd(buffer, q, cur - q))
1441 node = xmlNewDocText(doc, NULL);
1442 if (node == NULL) goto out;
1443 node->content = xmlBufferDetach(buffer);
1448 last = xmlAddNextSibling(last, node);
1450 } else if (ret == NULL) {
1451 ret = xmlNewDocText(doc, BAD_CAST "");
1455 xmlBufferFree(buffer);
1460 * xmlStringGetNodeList:
1461 * @doc: the document
1462 * @value: the value of the attribute
1464 * Parse the value string and build the node list associated. Should
1465 * produce a flat tree with only TEXTs and ENTITY_REFs.
1466 * Returns a pointer to the first child
1469 xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
1470 xmlNodePtr ret = NULL, last = NULL;
1473 const xmlChar *cur = value;
1476 xmlBufferPtr buffer;
1478 if (value == NULL) return(NULL);
1480 buffer = xmlBufferCreateSize(0);
1481 if (buffer == NULL) return(NULL);
1482 xmlBufferSetAllocationScheme(buffer, XML_BUFFER_ALLOC_HYBRID);
1486 if (cur[0] == '&') {
1491 * Save the current text.
1494 if (xmlBufferAdd(buffer, q, cur - q))
1498 if ((cur[1] == '#') && (cur[2] == 'x')) {
1501 while (tmp != ';') { /* Non input consuming loop */
1502 if ((tmp >= '0') && (tmp <= '9'))
1503 charval = charval * 16 + (tmp - '0');
1504 else if ((tmp >= 'a') && (tmp <= 'f'))
1505 charval = charval * 16 + (tmp - 'a') + 10;
1506 else if ((tmp >= 'A') && (tmp <= 'F'))
1507 charval = charval * 16 + (tmp - 'A') + 10;
1509 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1520 } else if (cur[1] == '#') {
1523 while (tmp != ';') { /* Non input consuming loops */
1524 if ((tmp >= '0') && (tmp <= '9'))
1525 charval = charval * 10 + (tmp - '0');
1527 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1540 * Read the entity string
1544 while ((*cur != 0) && (*cur != ';')) cur++;
1546 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1547 (xmlNodePtr) doc, (const char *) q);
1552 * Predefined entities don't generate nodes
1554 val = xmlStrndup(q, cur - q);
1555 ent = xmlGetDocEntity(doc, val);
1556 if ((ent != NULL) &&
1557 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1559 if (xmlBufferCat(buffer, ent->content))
1564 * Flush buffer so far
1567 node = xmlNewDocText(doc, NULL);
1568 node->content = xmlBufferDetach(buffer);
1573 last = xmlAddNextSibling(last, node);
1578 * Create a new REFERENCE_REF node
1580 node = xmlNewReference(doc, val);
1582 if (val != NULL) xmlFree(val);
1585 else if ((ent != NULL) && (ent->children == NULL)) {
1588 ent->children = xmlStringGetNodeList(doc,
1589 (const xmlChar*)node->content);
1591 temp = ent->children;
1593 temp->parent = (xmlNodePtr)ent;
1600 last = xmlAddNextSibling(last, node);
1612 len = xmlCopyCharMultiByte(buf, charval);
1615 if (xmlBufferCat(buffer, buf))
1622 if ((cur != q) || (ret == NULL)) {
1624 * Handle the last piece of text.
1626 xmlBufferAdd(buffer, q, cur - q);
1630 node = xmlNewDocText(doc, NULL);
1631 node->content = xmlBufferDetach(buffer);
1636 last = xmlAddNextSibling(last, node);
1641 xmlBufferFree(buffer);
1646 * xmlNodeListGetString:
1647 * @doc: the document
1648 * @list: a Node list
1649 * @inLine: should we replace entity contents or show their external form
1651 * Build the string equivalent to the text contained in the Node list
1652 * made of TEXTs and ENTITY_REFs
1654 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1657 xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1659 xmlNodePtr node = list;
1660 xmlChar *ret = NULL;
1666 while (node != NULL) {
1667 if ((node->type == XML_TEXT_NODE) ||
1668 (node->type == XML_CDATA_SECTION_NODE)) {
1670 ret = xmlStrcat(ret, node->content);
1674 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1675 if (buffer != NULL) {
1676 ret = xmlStrcat(ret, buffer);
1680 } else if (node->type == XML_ENTITY_REF_NODE) {
1682 ent = xmlGetDocEntity(doc, node->name);
1686 /* an entity content can be any "well balanced chunk",
1687 * i.e. the result of the content [43] production:
1688 * http://www.w3.org/TR/REC-xml#NT-content.
1689 * So it can contain text, CDATA section or nested
1690 * entity reference nodes (among others).
1691 * -> we recursive call xmlNodeListGetString()
1692 * which handles these types */
1693 buffer = xmlNodeListGetString(doc, ent->children, 1);
1694 if (buffer != NULL) {
1695 ret = xmlStrcat(ret, buffer);
1699 ret = xmlStrcat(ret, node->content);
1706 ret = xmlStrncat(ret, buf, 1);
1707 ret = xmlStrcat(ret, node->name);
1710 ret = xmlStrncat(ret, buf, 1);
1715 xmlGenericError(xmlGenericErrorContext,
1716 "xmlGetNodeListString : invalid node type %d\n",
1725 #ifdef LIBXML_TREE_ENABLED
1727 * xmlNodeListGetRawString:
1728 * @doc: the document
1729 * @list: a Node list
1730 * @inLine: should we replace entity contents or show their external form
1732 * Builds the string equivalent to the text contained in the Node list
1733 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1734 * this function doesn't do any character encoding handling.
1736 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1739 xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1741 xmlNodePtr node = list;
1742 xmlChar *ret = NULL;
1748 while (node != NULL) {
1749 if ((node->type == XML_TEXT_NODE) ||
1750 (node->type == XML_CDATA_SECTION_NODE)) {
1752 ret = xmlStrcat(ret, node->content);
1756 buffer = xmlEncodeSpecialChars(doc, node->content);
1757 if (buffer != NULL) {
1758 ret = xmlStrcat(ret, buffer);
1762 } else if (node->type == XML_ENTITY_REF_NODE) {
1764 ent = xmlGetDocEntity(doc, node->name);
1768 /* an entity content can be any "well balanced chunk",
1769 * i.e. the result of the content [43] production:
1770 * http://www.w3.org/TR/REC-xml#NT-content.
1771 * So it can contain text, CDATA section or nested
1772 * entity reference nodes (among others).
1773 * -> we recursive call xmlNodeListGetRawString()
1774 * which handles these types */
1776 xmlNodeListGetRawString(doc, ent->children, 1);
1777 if (buffer != NULL) {
1778 ret = xmlStrcat(ret, buffer);
1782 ret = xmlStrcat(ret, node->content);
1789 ret = xmlStrncat(ret, buf, 1);
1790 ret = xmlStrcat(ret, node->name);
1793 ret = xmlStrncat(ret, buf, 1);
1798 xmlGenericError(xmlGenericErrorContext,
1799 "xmlGetNodeListString : invalid node type %d\n",
1807 #endif /* LIBXML_TREE_ENABLED */
1810 xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1811 const xmlChar * name, const xmlChar * value,
1815 xmlDocPtr doc = NULL;
1817 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
1818 if ((eatname == 1) &&
1819 ((node->doc == NULL) ||
1820 (!(xmlDictOwns(node->doc->dict, name)))))
1821 xmlFree((xmlChar *) name);
1826 * Allocate a new property and fill the fields.
1828 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1830 if ((eatname == 1) &&
1831 ((node == NULL) || (node->doc == NULL) ||
1832 (!(xmlDictOwns(node->doc->dict, name)))))
1833 xmlFree((xmlChar *) name);
1834 xmlTreeErrMemory("building attribute");
1837 memset(cur, 0, sizeof(xmlAttr));
1838 cur->type = XML_ATTRIBUTE_NODE;
1848 if ((doc != NULL) && (doc->dict != NULL))
1849 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1851 cur->name = xmlStrdup(name);
1855 if (value != NULL) {
1858 if(!xmlCheckUTF8(value)) {
1859 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc,
1862 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1864 cur->children = xmlNewDocText(doc, value);
1866 tmp = cur->children;
1867 while (tmp != NULL) {
1868 tmp->parent = (xmlNodePtr) cur;
1869 if (tmp->next == NULL)
1876 * Add it at the end to preserve parsing order ...
1879 if (node->properties == NULL) {
1880 node->properties = cur;
1882 xmlAttrPtr prev = node->properties;
1884 while (prev->next != NULL)
1891 if ((value != NULL) && (node != NULL) &&
1892 (xmlIsID(node->doc, node, cur) == 1))
1893 xmlAddID(NULL, node->doc, value, cur);
1895 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1896 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1900 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1901 defined(LIBXML_SCHEMAS_ENABLED)
1904 * @node: the holding node
1905 * @name: the name of the attribute
1906 * @value: the value of the attribute
1908 * Create a new property carried by a node.
1909 * Returns a pointer to the attribute
1912 xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1916 xmlGenericError(xmlGenericErrorContext,
1917 "xmlNewProp : name == NULL\n");
1922 return xmlNewPropInternal(node, NULL, name, value, 0);
1924 #endif /* LIBXML_TREE_ENABLED */
1928 * @node: the holding node
1929 * @ns: the namespace
1930 * @name: the name of the attribute
1931 * @value: the value of the attribute
1933 * Create a new property tagged with a namespace and carried by a node.
1934 * Returns a pointer to the attribute
1937 xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1938 const xmlChar *value) {
1942 xmlGenericError(xmlGenericErrorContext,
1943 "xmlNewNsProp : name == NULL\n");
1948 return xmlNewPropInternal(node, ns, name, value, 0);
1952 * xmlNewNsPropEatName:
1953 * @node: the holding node
1954 * @ns: the namespace
1955 * @name: the name of the attribute
1956 * @value: the value of the attribute
1958 * Create a new property tagged with a namespace and carried by a node.
1959 * Returns a pointer to the attribute
1962 xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1963 const xmlChar *value) {
1967 xmlGenericError(xmlGenericErrorContext,
1968 "xmlNewNsPropEatName : name == NULL\n");
1973 return xmlNewPropInternal(node, ns, name, value, 1);
1978 * @doc: the document
1979 * @name: the name of the attribute
1980 * @value: the value of the attribute
1982 * Create a new property carried by a document.
1983 * Returns a pointer to the attribute
1986 xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1991 xmlGenericError(xmlGenericErrorContext,
1992 "xmlNewDocProp : name == NULL\n");
1998 * Allocate a new property and fill the fields.
2000 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
2002 xmlTreeErrMemory("building attribute");
2005 memset(cur, 0, sizeof(xmlAttr));
2006 cur->type = XML_ATTRIBUTE_NODE;
2008 if ((doc != NULL) && (doc->dict != NULL))
2009 cur->name = xmlDictLookup(doc->dict, name, -1);
2011 cur->name = xmlStrdup(name);
2013 if (value != NULL) {
2016 cur->children = xmlStringGetNodeList(doc, value);
2019 tmp = cur->children;
2020 while (tmp != NULL) {
2021 tmp->parent = (xmlNodePtr) cur;
2022 if (tmp->next == NULL)
2028 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2029 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2035 * @cur: the first property in the list
2037 * Free a property and all its siblings, all the children are freed too.
2040 xmlFreePropList(xmlAttrPtr cur) {
2042 if (cur == NULL) return;
2043 while (cur != NULL) {
2052 * @cur: an attribute
2054 * Free one attribute, all the content is freed too
2057 xmlFreeProp(xmlAttrPtr cur) {
2058 xmlDictPtr dict = NULL;
2059 if (cur == NULL) return;
2061 if (cur->doc != NULL) dict = cur->doc->dict;
2063 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
2064 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2066 /* Check for ID removal -> leading to invalid references ! */
2067 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2068 xmlRemoveID(cur->doc, cur);
2070 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2071 DICT_FREE(cur->name)
2077 * @cur: an attribute
2079 * Unlink and free one attribute, all the content is freed too
2080 * Note this doesn't work for namespace definition attributes
2082 * Returns 0 if success and -1 in case of error.
2085 xmlRemoveProp(xmlAttrPtr cur) {
2089 xmlGenericError(xmlGenericErrorContext,
2090 "xmlRemoveProp : cur == NULL\n");
2094 if (cur->parent == NULL) {
2096 xmlGenericError(xmlGenericErrorContext,
2097 "xmlRemoveProp : cur->parent == NULL\n");
2101 tmp = cur->parent->properties;
2103 cur->parent->properties = cur->next;
2104 if (cur->next != NULL)
2105 cur->next->prev = NULL;
2109 while (tmp != NULL) {
2110 if (tmp->next == cur) {
2111 tmp->next = cur->next;
2112 if (tmp->next != NULL)
2113 tmp->next->prev = tmp;
2120 xmlGenericError(xmlGenericErrorContext,
2121 "xmlRemoveProp : attribute not owned by its node\n");
2128 * @doc: the target document
2129 * @name: the processing instruction name
2130 * @content: the PI content
2132 * Creation of a processing instruction element.
2133 * Returns a pointer to the new node object.
2136 xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
2141 xmlGenericError(xmlGenericErrorContext,
2142 "xmlNewPI : name == NULL\n");
2148 * Allocate a new node and fill the fields.
2150 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2152 xmlTreeErrMemory("building PI");
2155 memset(cur, 0, sizeof(xmlNode));
2156 cur->type = XML_PI_NODE;
2158 if ((doc != NULL) && (doc->dict != NULL))
2159 cur->name = xmlDictLookup(doc->dict, name, -1);
2161 cur->name = xmlStrdup(name);
2162 if (content != NULL) {
2163 cur->content = xmlStrdup(content);
2167 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2168 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2174 * @name: the processing instruction name
2175 * @content: the PI content
2177 * Creation of a processing instruction element.
2178 * Use xmlDocNewPI preferably to get string interning
2180 * Returns a pointer to the new node object.
2183 xmlNewPI(const xmlChar *name, const xmlChar *content) {
2184 return(xmlNewDocPI(NULL, name, content));
2189 * @ns: namespace if any
2190 * @name: the node name
2192 * Creation of a new node element. @ns is optional (NULL).
2194 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2198 xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2203 xmlGenericError(xmlGenericErrorContext,
2204 "xmlNewNode : name == NULL\n");
2210 * Allocate a new node and fill the fields.
2212 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2214 xmlTreeErrMemory("building node");
2217 memset(cur, 0, sizeof(xmlNode));
2218 cur->type = XML_ELEMENT_NODE;
2220 cur->name = xmlStrdup(name);
2223 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2224 xmlRegisterNodeDefaultValue(cur);
2229 * xmlNewNodeEatName:
2230 * @ns: namespace if any
2231 * @name: the node name
2233 * Creation of a new node element. @ns is optional (NULL).
2235 * Returns a pointer to the new node object, with pointer @name as
2236 * new node's name. Use xmlNewNode() if a copy of @name string is
2237 * is needed as new node's name.
2240 xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2245 xmlGenericError(xmlGenericErrorContext,
2246 "xmlNewNode : name == NULL\n");
2252 * Allocate a new node and fill the fields.
2254 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2256 xmlTreeErrMemory("building node");
2257 /* we can't check here that name comes from the doc dictionnary */
2260 memset(cur, 0, sizeof(xmlNode));
2261 cur->type = XML_ELEMENT_NODE;
2266 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2267 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2273 * @doc: the document
2274 * @ns: namespace if any
2275 * @name: the node name
2276 * @content: the XML text content if any
2278 * Creation of a new node element within a document. @ns and @content
2279 * are optional (NULL).
2280 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2281 * references, but XML special chars need to be escaped first by using
2282 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2283 * need entities support.
2285 * Returns a pointer to the new node object.
2288 xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2289 const xmlChar *name, const xmlChar *content) {
2292 if ((doc != NULL) && (doc->dict != NULL))
2293 cur = xmlNewNodeEatName(ns, (xmlChar *)
2294 xmlDictLookup(doc->dict, name, -1));
2296 cur = xmlNewNode(ns, name);
2299 if (content != NULL) {
2300 cur->children = xmlStringGetNodeList(doc, content);
2301 UPDATE_LAST_CHILD_AND_PARENT(cur)
2309 * xmlNewDocNodeEatName:
2310 * @doc: the document
2311 * @ns: namespace if any
2312 * @name: the node name
2313 * @content: the XML text content if any
2315 * Creation of a new node element within a document. @ns and @content
2316 * are optional (NULL).
2317 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2318 * references, but XML special chars need to be escaped first by using
2319 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2320 * need entities support.
2322 * Returns a pointer to the new node object.
2325 xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2326 xmlChar *name, const xmlChar *content) {
2329 cur = xmlNewNodeEatName(ns, name);
2332 if (content != NULL) {
2333 cur->children = xmlStringGetNodeList(doc, content);
2334 UPDATE_LAST_CHILD_AND_PARENT(cur)
2337 /* if name don't come from the doc dictionnary free it here */
2338 if ((name != NULL) && (doc != NULL) &&
2339 (!(xmlDictOwns(doc->dict, name))))
2345 #ifdef LIBXML_TREE_ENABLED
2348 * @doc: the document
2349 * @ns: namespace if any
2350 * @name: the node name
2351 * @content: the text content if any
2353 * Creation of a new node element within a document. @ns and @content
2354 * are optional (NULL).
2356 * Returns a pointer to the new node object.
2359 xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2360 const xmlChar *name, const xmlChar *content) {
2363 cur = xmlNewDocNode(doc, ns, name, NULL);
2366 if (content != NULL) {
2367 cur->children = xmlNewDocText(doc, content);
2368 UPDATE_LAST_CHILD_AND_PARENT(cur)
2375 * xmlNewDocFragment:
2376 * @doc: the document owning the fragment
2378 * Creation of a new Fragment node.
2379 * Returns a pointer to the new node object.
2382 xmlNewDocFragment(xmlDocPtr doc) {
2386 * Allocate a new DocumentFragment node and fill the fields.
2388 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2390 xmlTreeErrMemory("building fragment");
2393 memset(cur, 0, sizeof(xmlNode));
2394 cur->type = XML_DOCUMENT_FRAG_NODE;
2398 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2399 xmlRegisterNodeDefaultValue(cur);
2402 #endif /* LIBXML_TREE_ENABLED */
2406 * @content: the text content
2408 * Creation of a new text node.
2409 * Returns a pointer to the new node object.
2412 xmlNewText(const xmlChar *content) {
2416 * Allocate a new node and fill the fields.
2418 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2420 xmlTreeErrMemory("building text");
2423 memset(cur, 0, sizeof(xmlNode));
2424 cur->type = XML_TEXT_NODE;
2426 cur->name = xmlStringText;
2427 if (content != NULL) {
2428 cur->content = xmlStrdup(content);
2431 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2432 xmlRegisterNodeDefaultValue(cur);
2436 #ifdef LIBXML_TREE_ENABLED
2439 * @parent: the parent node
2440 * @ns: a namespace if any
2441 * @name: the name of the child
2442 * @content: the text content of the child if any.
2444 * Creation of a new child element, added at the end of @parent children list.
2445 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2446 * created element inherits the namespace of @parent. If @content is non NULL,
2447 * a child TEXT node will be created containing the string @content.
2448 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2449 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
2450 * reserved XML chars that might appear in @content, such as the ampersand,
2451 * greater-than or less-than signs, are automatically replaced by their XML
2452 * escaped entity representations.
2454 * Returns a pointer to the new node object.
2457 xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2458 const xmlChar *name, const xmlChar *content) {
2459 xmlNodePtr cur, prev;
2461 if (parent == NULL) {
2463 xmlGenericError(xmlGenericErrorContext,
2464 "xmlNewTextChild : parent == NULL\n");
2471 xmlGenericError(xmlGenericErrorContext,
2472 "xmlNewTextChild : name == NULL\n");
2478 * Allocate a new node
2480 if (parent->type == XML_ELEMENT_NODE) {
2482 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2484 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2485 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2486 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2488 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2490 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2491 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2492 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2496 if (cur == NULL) return(NULL);
2499 * add the new element at the end of the children list.
2501 cur->type = XML_ELEMENT_NODE;
2502 cur->parent = parent;
2503 cur->doc = parent->doc;
2504 if (parent->children == NULL) {
2505 parent->children = cur;
2508 prev = parent->last;
2516 #endif /* LIBXML_TREE_ENABLED */
2520 * @doc: the document
2521 * @name: the char ref string, starting with # or "&# ... ;"
2523 * Creation of a new character reference node.
2524 * Returns a pointer to the new node object.
2527 xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2534 * Allocate a new node and fill the fields.
2536 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2538 xmlTreeErrMemory("building character reference");
2541 memset(cur, 0, sizeof(xmlNode));
2542 cur->type = XML_ENTITY_REF_NODE;
2545 if (name[0] == '&') {
2548 len = xmlStrlen(name);
2549 if (name[len - 1] == ';')
2550 cur->name = xmlStrndup(name, len - 1);
2552 cur->name = xmlStrndup(name, len);
2554 cur->name = xmlStrdup(name);
2556 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2557 xmlRegisterNodeDefaultValue(cur);
2563 * @doc: the document
2564 * @name: the reference name, or the reference string with & and ;
2566 * Creation of a new reference node.
2567 * Returns a pointer to the new node object.
2570 xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
2578 * Allocate a new node and fill the fields.
2580 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2582 xmlTreeErrMemory("building reference");
2585 memset(cur, 0, sizeof(xmlNode));
2586 cur->type = XML_ENTITY_REF_NODE;
2589 if (name[0] == '&') {
2592 len = xmlStrlen(name);
2593 if (name[len - 1] == ';')
2594 cur->name = xmlStrndup(name, len - 1);
2596 cur->name = xmlStrndup(name, len);
2598 cur->name = xmlStrdup(name);
2600 ent = xmlGetDocEntity(doc, cur->name);
2602 cur->content = ent->content;
2604 * The parent pointer in entity is a DTD pointer and thus is NOT
2605 * updated. Not sure if this is 100% correct.
2608 cur->children = (xmlNodePtr) ent;
2609 cur->last = (xmlNodePtr) ent;
2612 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2613 xmlRegisterNodeDefaultValue(cur);
2619 * @doc: the document
2620 * @content: the text content
2622 * Creation of a new text node within a document.
2623 * Returns a pointer to the new node object.
2626 xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
2629 cur = xmlNewText(content);
2630 if (cur != NULL) cur->doc = doc;
2636 * @content: the text content
2637 * @len: the text len.
2639 * Creation of a new text node with an extra parameter for the content's length
2640 * Returns a pointer to the new node object.
2643 xmlNewTextLen(const xmlChar *content, int len) {
2647 * Allocate a new node and fill the fields.
2649 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2651 xmlTreeErrMemory("building text");
2654 memset(cur, 0, sizeof(xmlNode));
2655 cur->type = XML_TEXT_NODE;
2657 cur->name = xmlStringText;
2658 if (content != NULL) {
2659 cur->content = xmlStrndup(content, len);
2662 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2663 xmlRegisterNodeDefaultValue(cur);
2669 * @doc: the document
2670 * @content: the text content
2671 * @len: the text len.
2673 * Creation of a new text node with an extra content length parameter. The
2674 * text node pertain to a given document.
2675 * Returns a pointer to the new node object.
2678 xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2681 cur = xmlNewTextLen(content, len);
2682 if (cur != NULL) cur->doc = doc;
2688 * @content: the comment content
2690 * Creation of a new node containing a comment.
2691 * Returns a pointer to the new node object.
2694 xmlNewComment(const xmlChar *content) {
2698 * Allocate a new node and fill the fields.
2700 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2702 xmlTreeErrMemory("building comment");
2705 memset(cur, 0, sizeof(xmlNode));
2706 cur->type = XML_COMMENT_NODE;
2708 cur->name = xmlStringComment;
2709 if (content != NULL) {
2710 cur->content = xmlStrdup(content);
2713 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2714 xmlRegisterNodeDefaultValue(cur);
2720 * @doc: the document
2721 * @content: the CDATA block content content
2722 * @len: the length of the block
2724 * Creation of a new node containing a CDATA block.
2725 * Returns a pointer to the new node object.
2728 xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2732 * Allocate a new node and fill the fields.
2734 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2736 xmlTreeErrMemory("building CDATA");
2739 memset(cur, 0, sizeof(xmlNode));
2740 cur->type = XML_CDATA_SECTION_NODE;
2743 if (content != NULL) {
2744 cur->content = xmlStrndup(content, len);
2747 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2748 xmlRegisterNodeDefaultValue(cur);
2754 * @doc: the document
2755 * @content: the comment content
2757 * Creation of a new node containing a comment within a document.
2758 * Returns a pointer to the new node object.
2761 xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2764 cur = xmlNewComment(content);
2765 if (cur != NULL) cur->doc = doc;
2771 * @tree: the top element
2772 * @doc: the document
2774 * update all nodes under the tree to point to the right document
2777 xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
2782 if (tree->doc != doc) {
2783 if(tree->type == XML_ELEMENT_NODE) {
2784 prop = tree->properties;
2785 while (prop != NULL) {
2787 xmlSetListDoc(prop->children, doc);
2791 if (tree->children != NULL)
2792 xmlSetListDoc(tree->children, doc);
2799 * @list: the first element
2800 * @doc: the document
2802 * update all nodes in the list to point to the right document
2805 xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2811 while (cur != NULL) {
2812 if (cur->doc != doc)
2813 xmlSetTreeDoc(cur, doc);
2818 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
2821 * @parent: the parent node
2822 * @ns: a namespace if any
2823 * @name: the name of the child
2824 * @content: the XML content of the child if any.
2826 * Creation of a new child element, added at the end of @parent children list.
2827 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2828 * created element inherits the namespace of @parent. If @content is non NULL,
2829 * a child list containing the TEXTs and ENTITY_REFs node will be created.
2830 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2831 * references. XML special chars must be escaped first by using
2832 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
2834 * Returns a pointer to the new node object.
2837 xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2838 const xmlChar *name, const xmlChar *content) {
2839 xmlNodePtr cur, prev;
2841 if (parent == NULL) {
2843 xmlGenericError(xmlGenericErrorContext,
2844 "xmlNewChild : parent == NULL\n");
2851 xmlGenericError(xmlGenericErrorContext,
2852 "xmlNewChild : name == NULL\n");
2858 * Allocate a new node
2860 if (parent->type == XML_ELEMENT_NODE) {
2862 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2864 cur = xmlNewDocNode(parent->doc, ns, name, content);
2865 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2866 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2868 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2870 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
2871 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2872 cur = xmlNewDocNode( parent->doc, ns, name, content);
2876 if (cur == NULL) return(NULL);
2879 * add the new element at the end of the children list.
2881 cur->type = XML_ELEMENT_NODE;
2882 cur->parent = parent;
2883 cur->doc = parent->doc;
2884 if (parent->children == NULL) {
2885 parent->children = cur;
2888 prev = parent->last;
2896 #endif /* LIBXML_TREE_ENABLED */
2899 * xmlAddPropSibling:
2900 * @prev: the attribute to which @prop is added after
2901 * @cur: the base attribute passed to calling function
2902 * @prop: the new attribute
2904 * Add a new attribute after @prev using @cur as base attribute.
2905 * When inserting before @cur, @prev is passed as @cur->prev.
2906 * When inserting after @cur, @prev is passed as @cur.
2907 * If an existing attribute is found it is detroyed prior to adding @prop.
2909 * Returns the attribute being inserted or NULL in case of error.
2912 xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2915 if (cur->type != XML_ATTRIBUTE_NODE)
2918 /* check if an attribute with the same name exists */
2919 if (prop->ns == NULL)
2920 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2922 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2924 if (prop->doc != cur->doc) {
2925 xmlSetTreeDoc(prop, cur->doc);
2927 prop->parent = cur->parent;
2930 prop->next = prev->next;
2933 prop->next->prev = prop;
2938 if (prop->prev == NULL && prop->parent != NULL)
2939 prop->parent->properties = (xmlAttrPtr) prop;
2940 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2941 /* different instance, destroy it (attributes must be unique) */
2942 xmlRemoveProp((xmlAttrPtr) attr);
2948 * xmlAddNextSibling:
2949 * @cur: the child node
2950 * @elem: the new node
2952 * Add a new node @elem as the next sibling of @cur
2953 * If the new node was already inserted in a document it is
2954 * first unlinked from its existing context.
2955 * As a result of text merging @elem may be freed.
2956 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2957 * If there is an attribute with equal name, it is first destroyed.
2959 * Returns the new node or NULL in case of error.
2962 xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2965 xmlGenericError(xmlGenericErrorContext,
2966 "xmlAddNextSibling : cur == NULL\n");
2972 xmlGenericError(xmlGenericErrorContext,
2973 "xmlAddNextSibling : elem == NULL\n");
2980 xmlGenericError(xmlGenericErrorContext,
2981 "xmlAddNextSibling : cur == elem\n");
2986 xmlUnlinkNode(elem);
2988 if (elem->type == XML_TEXT_NODE) {
2989 if (cur->type == XML_TEXT_NODE) {
2990 xmlNodeAddContent(cur, elem->content);
2994 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
2995 (cur->name == cur->next->name)) {
2998 tmp = xmlStrdup(elem->content);
2999 tmp = xmlStrcat(tmp, cur->next->content);
3000 xmlNodeSetContent(cur->next, tmp);
3005 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3006 return xmlAddPropSibling(cur, cur, elem);
3009 if (elem->doc != cur->doc) {
3010 xmlSetTreeDoc(elem, cur->doc);
3012 elem->parent = cur->parent;
3014 elem->next = cur->next;
3016 if (elem->next != NULL)
3017 elem->next->prev = elem;
3018 if ((elem->parent != NULL) && (elem->parent->last == cur))
3019 elem->parent->last = elem;
3023 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
3024 defined(LIBXML_SCHEMAS_ENABLED)
3026 * xmlAddPrevSibling:
3027 * @cur: the child node
3028 * @elem: the new node
3030 * Add a new node @elem as the previous sibling of @cur
3031 * merging adjacent TEXT nodes (@elem may be freed)
3032 * If the new node was already inserted in a document it is
3033 * first unlinked from its existing context.
3034 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3035 * If there is an attribute with equal name, it is first destroyed.
3037 * Returns the new node or NULL in case of error.
3040 xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
3043 xmlGenericError(xmlGenericErrorContext,
3044 "xmlAddPrevSibling : cur == NULL\n");
3050 xmlGenericError(xmlGenericErrorContext,
3051 "xmlAddPrevSibling : elem == NULL\n");
3058 xmlGenericError(xmlGenericErrorContext,
3059 "xmlAddPrevSibling : cur == elem\n");
3064 xmlUnlinkNode(elem);
3066 if (elem->type == XML_TEXT_NODE) {
3067 if (cur->type == XML_TEXT_NODE) {
3070 tmp = xmlStrdup(elem->content);
3071 tmp = xmlStrcat(tmp, cur->content);
3072 xmlNodeSetContent(cur, tmp);
3077 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3078 (cur->name == cur->prev->name)) {
3079 xmlNodeAddContent(cur->prev, elem->content);
3083 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3084 return xmlAddPropSibling(cur->prev, cur, elem);
3087 if (elem->doc != cur->doc) {
3088 xmlSetTreeDoc(elem, cur->doc);
3090 elem->parent = cur->parent;
3092 elem->prev = cur->prev;
3094 if (elem->prev != NULL)
3095 elem->prev->next = elem;
3096 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
3097 elem->parent->children = elem;
3101 #endif /* LIBXML_TREE_ENABLED */
3105 * @cur: the child node
3106 * @elem: the new node
3108 * Add a new element @elem to the list of siblings of @cur
3109 * merging adjacent TEXT nodes (@elem may be freed)
3110 * If the new element was already inserted in a document it is
3111 * first unlinked from its existing context.
3113 * Returns the new element or NULL in case of error.
3116 xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3121 xmlGenericError(xmlGenericErrorContext,
3122 "xmlAddSibling : cur == NULL\n");
3129 xmlGenericError(xmlGenericErrorContext,
3130 "xmlAddSibling : elem == NULL\n");
3137 xmlGenericError(xmlGenericErrorContext,
3138 "xmlAddSibling : cur == elem\n");
3144 * Constant time is we can rely on the ->parent->last to find
3147 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
3148 (cur->parent->children != NULL) &&
3149 (cur->parent->last != NULL) &&
3150 (cur->parent->last->next == NULL)) {
3151 cur = cur->parent->last;
3153 while (cur->next != NULL) cur = cur->next;
3156 xmlUnlinkNode(elem);
3158 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3159 (cur->name == elem->name)) {
3160 xmlNodeAddContent(cur, elem->content);
3163 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3164 return xmlAddPropSibling(cur, cur, elem);
3167 if (elem->doc != cur->doc) {
3168 xmlSetTreeDoc(elem, cur->doc);
3170 parent = cur->parent;
3173 elem->parent = parent;
3176 parent->last = elem;
3183 * @parent: the parent node
3184 * @cur: the first node in the list
3186 * Add a list of node at the end of the child list of the parent
3187 * merging adjacent TEXT nodes (@cur may be freed)
3189 * Returns the last child or NULL in case of error.
3192 xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3195 if (parent == NULL) {
3197 xmlGenericError(xmlGenericErrorContext,
3198 "xmlAddChildList : parent == NULL\n");
3205 xmlGenericError(xmlGenericErrorContext,
3206 "xmlAddChildList : child == NULL\n");
3211 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3212 (cur->doc != parent->doc)) {
3214 xmlGenericError(xmlGenericErrorContext,
3215 "Elements moved to a different document\n");
3220 * add the first element at the end of the children list.
3223 if (parent->children == NULL) {
3224 parent->children = cur;
3227 * If cur and parent->last both are TEXT nodes, then merge them.
3229 if ((cur->type == XML_TEXT_NODE) &&
3230 (parent->last->type == XML_TEXT_NODE) &&
3231 (cur->name == parent->last->name)) {
3232 xmlNodeAddContent(parent->last, cur->content);
3234 * if it's the only child, nothing more to be done.
3236 if (cur->next == NULL) {
3238 return(parent->last);
3244 prev = parent->last;
3248 while (cur->next != NULL) {
3249 cur->parent = parent;
3250 if (cur->doc != parent->doc) {
3251 xmlSetTreeDoc(cur, parent->doc);
3255 cur->parent = parent;
3256 /* the parent may not be linked to a doc ! */
3257 if (cur->doc != parent->doc) {
3258 xmlSetTreeDoc(cur, parent->doc);
3267 * @parent: the parent node
3268 * @cur: the child node
3270 * Add a new node to @parent, at the end of the child (or property) list
3271 * merging adjacent TEXT nodes (in which case @cur is freed)
3272 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3273 * If there is an attribute with equal name, it is first destroyed.
3275 * Returns the child or NULL in case of error.
3278 xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3281 if (parent == NULL) {
3283 xmlGenericError(xmlGenericErrorContext,
3284 "xmlAddChild : parent == NULL\n");
3291 xmlGenericError(xmlGenericErrorContext,
3292 "xmlAddChild : child == NULL\n");
3297 if (parent == cur) {
3299 xmlGenericError(xmlGenericErrorContext,
3300 "xmlAddChild : parent == cur\n");
3305 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
3306 * cur is then freed.
3308 if (cur->type == XML_TEXT_NODE) {
3309 if ((parent->type == XML_TEXT_NODE) &&
3310 (parent->content != NULL) &&
3311 (parent->name == cur->name)) {
3312 xmlNodeAddContent(parent, cur->content);
3316 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
3317 (parent->last->name == cur->name) &&
3318 (parent->last != cur)) {
3319 xmlNodeAddContent(parent->last, cur->content);
3321 return(parent->last);
3326 * add the new element at the end of the children list.
3329 cur->parent = parent;
3330 if (cur->doc != parent->doc) {
3331 xmlSetTreeDoc(cur, parent->doc);
3333 /* this check prevents a loop on tree-traversions if a developer
3334 * tries to add a node to its parent multiple times
3342 if ((parent->type == XML_TEXT_NODE) &&
3343 (parent->content != NULL) &&
3345 xmlNodeAddContent(parent, cur->content);
3349 if (cur->type == XML_ATTRIBUTE_NODE) {
3350 if (parent->type != XML_ELEMENT_NODE)
3352 if (parent->properties != NULL) {
3353 /* check if an attribute with the same name exists */
3354 xmlAttrPtr lastattr;
3356 if (cur->ns == NULL)
3357 lastattr = xmlHasNsProp(parent, cur->name, NULL);
3359 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
3360 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
3361 /* different instance, destroy it (attributes must be unique) */
3362 xmlUnlinkNode((xmlNodePtr) lastattr);
3363 xmlFreeProp(lastattr);
3365 if (lastattr == (xmlAttrPtr) cur)
3369 if (parent->properties == NULL) {
3370 parent->properties = (xmlAttrPtr) cur;
3373 xmlAttrPtr lastattr = parent->properties;
3374 while (lastattr->next != NULL) {
3375 lastattr = lastattr->next;
3377 lastattr->next = (xmlAttrPtr) cur;
3378 ((xmlAttrPtr) cur)->prev = lastattr;
3381 if (parent->children == NULL) {
3382 parent->children = cur;
3385 prev = parent->last;
3396 * @parent: the parent node
3398 * Search the last child of a node.
3399 * Returns the last child or NULL if none.
3402 xmlGetLastChild(xmlNodePtr parent) {
3403 if (parent == NULL) {
3405 xmlGenericError(xmlGenericErrorContext,
3406 "xmlGetLastChild : parent == NULL\n");
3410 return(parent->last);
3413 #ifdef LIBXML_TREE_ENABLED
3415 * 5 interfaces from DOM ElementTraversal
3419 * xmlChildElementCount:
3420 * @parent: the parent node
3422 * Finds the current number of child nodes of that element which are
3424 * Note the handling of entities references is different than in
3425 * the W3C DOM element traversal spec since we don't have back reference
3426 * from entities content to entities references.
3428 * Returns the count of element child or 0 if not available
3431 xmlChildElementCount(xmlNodePtr parent) {
3432 unsigned long ret = 0;
3433 xmlNodePtr cur = NULL;
3437 switch (parent->type) {
3438 case XML_ELEMENT_NODE:
3439 case XML_ENTITY_NODE:
3440 case XML_DOCUMENT_NODE:
3441 case XML_HTML_DOCUMENT_NODE:
3442 cur = parent->children;
3447 while (cur != NULL) {
3448 if (cur->type == XML_ELEMENT_NODE)
3456 * xmlFirstElementChild:
3457 * @parent: the parent node
3459 * Finds the first child node of that element which is a Element node
3460 * Note the handling of entities references is different than in
3461 * the W3C DOM element traversal spec since we don't have back reference
3462 * from entities content to entities references.
3464 * Returns the first element child or NULL if not available
3467 xmlFirstElementChild(xmlNodePtr parent) {
3468 xmlNodePtr cur = NULL;
3472 switch (parent->type) {
3473 case XML_ELEMENT_NODE:
3474 case XML_ENTITY_NODE:
3475 case XML_DOCUMENT_NODE:
3476 case XML_HTML_DOCUMENT_NODE:
3477 cur = parent->children;
3482 while (cur != NULL) {
3483 if (cur->type == XML_ELEMENT_NODE)
3491 * xmlLastElementChild:
3492 * @parent: the parent node
3494 * Finds the last child node of that element which is a Element node
3495 * Note the handling of entities references is different than in
3496 * the W3C DOM element traversal spec since we don't have back reference
3497 * from entities content to entities references.
3499 * Returns the last element child or NULL if not available
3502 xmlLastElementChild(xmlNodePtr parent) {
3503 xmlNodePtr cur = NULL;
3507 switch (parent->type) {
3508 case XML_ELEMENT_NODE:
3509 case XML_ENTITY_NODE:
3510 case XML_DOCUMENT_NODE:
3511 case XML_HTML_DOCUMENT_NODE:
3517 while (cur != NULL) {
3518 if (cur->type == XML_ELEMENT_NODE)
3526 * xmlPreviousElementSibling:
3527 * @node: the current node
3529 * Finds the first closest previous sibling of the node which is an
3531 * Note the handling of entities references is different than in
3532 * the W3C DOM element traversal spec since we don't have back reference
3533 * from entities content to entities references.
3535 * Returns the previous element sibling or NULL if not available
3538 xmlPreviousElementSibling(xmlNodePtr node) {
3541 switch (node->type) {
3542 case XML_ELEMENT_NODE:
3544 case XML_CDATA_SECTION_NODE:
3545 case XML_ENTITY_REF_NODE:
3546 case XML_ENTITY_NODE:
3548 case XML_COMMENT_NODE:
3549 case XML_XINCLUDE_START:
3550 case XML_XINCLUDE_END:
3556 while (node != NULL) {
3557 if (node->type == XML_ELEMENT_NODE)
3565 * xmlNextElementSibling:
3566 * @node: the current node
3568 * Finds the first closest next sibling of the node which is an
3570 * Note the handling of entities references is different than in
3571 * the W3C DOM element traversal spec since we don't have back reference
3572 * from entities content to entities references.
3574 * Returns the next element sibling or NULL if not available
3577 xmlNextElementSibling(xmlNodePtr node) {
3580 switch (node->type) {
3581 case XML_ELEMENT_NODE:
3583 case XML_CDATA_SECTION_NODE:
3584 case XML_ENTITY_REF_NODE:
3585 case XML_ENTITY_NODE:
3587 case XML_COMMENT_NODE:
3589 case XML_XINCLUDE_START:
3590 case XML_XINCLUDE_END:
3596 while (node != NULL) {
3597 if (node->type == XML_ELEMENT_NODE)
3604 #endif /* LIBXML_TREE_ENABLED */
3608 * @cur: the first node in the list
3610 * Free a node and all its siblings, this is a recursive behaviour, all
3611 * the children are freed too.
3614 xmlFreeNodeList(xmlNodePtr cur) {
3616 xmlDictPtr dict = NULL;
3618 if (cur == NULL) return;
3619 if (cur->type == XML_NAMESPACE_DECL) {
3620 xmlFreeNsList((xmlNsPtr) cur);
3623 if ((cur->type == XML_DOCUMENT_NODE) ||
3624 #ifdef LIBXML_DOCB_ENABLED
3625 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
3627 (cur->type == XML_HTML_DOCUMENT_NODE)) {
3628 xmlFreeDoc((xmlDocPtr) cur);
3631 if (cur->doc != NULL) dict = cur->doc->dict;
3632 while (cur != NULL) {
3634 if (cur->type != XML_DTD_NODE) {
3636 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3637 xmlDeregisterNodeDefaultValue(cur);
3639 if ((cur->children != NULL) &&
3640 (cur->type != XML_ENTITY_REF_NODE))
3641 xmlFreeNodeList(cur->children);
3642 if (((cur->type == XML_ELEMENT_NODE) ||
3643 (cur->type == XML_XINCLUDE_START) ||
3644 (cur->type == XML_XINCLUDE_END)) &&
3645 (cur->properties != NULL))
3646 xmlFreePropList(cur->properties);
3647 if ((cur->type != XML_ELEMENT_NODE) &&
3648 (cur->type != XML_XINCLUDE_START) &&
3649 (cur->type != XML_XINCLUDE_END) &&
3650 (cur->type != XML_ENTITY_REF_NODE) &&
3651 (cur->content != (xmlChar *) &(cur->properties))) {
3652 DICT_FREE(cur->content)
3654 if (((cur->type == XML_ELEMENT_NODE) ||
3655 (cur->type == XML_XINCLUDE_START) ||
3656 (cur->type == XML_XINCLUDE_END)) &&
3657 (cur->nsDef != NULL))
3658 xmlFreeNsList(cur->nsDef);
3661 * When a node is a text node or a comment, it uses a global static
3662 * variable for the name of the node.
3663 * Otherwise the node name might come from the document's
3666 if ((cur->name != NULL) &&
3667 (cur->type != XML_TEXT_NODE) &&
3668 (cur->type != XML_COMMENT_NODE))
3669 DICT_FREE(cur->name)
3680 * Free a node, this is a recursive behaviour, all the children are freed too.
3681 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3684 xmlFreeNode(xmlNodePtr cur) {
3685 xmlDictPtr dict = NULL;
3687 if (cur == NULL) return;
3689 /* use xmlFreeDtd for DTD nodes */
3690 if (cur->type == XML_DTD_NODE) {
3691 xmlFreeDtd((xmlDtdPtr) cur);
3694 if (cur->type == XML_NAMESPACE_DECL) {
3695 xmlFreeNs((xmlNsPtr) cur);
3698 if (cur->type == XML_ATTRIBUTE_NODE) {
3699 xmlFreeProp((xmlAttrPtr) cur);
3703 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3704 xmlDeregisterNodeDefaultValue(cur);
3706 if (cur->doc != NULL) dict = cur->doc->dict;
3708 if (cur->type == XML_ENTITY_DECL) {
3709 xmlEntityPtr ent = (xmlEntityPtr) cur;
3710 DICT_FREE(ent->SystemID);
3711 DICT_FREE(ent->ExternalID);
3713 if ((cur->children != NULL) &&
3714 (cur->type != XML_ENTITY_REF_NODE))
3715 xmlFreeNodeList(cur->children);
3716 if (((cur->type == XML_ELEMENT_NODE) ||
3717 (cur->type == XML_XINCLUDE_START) ||
3718 (cur->type == XML_XINCLUDE_END)) &&
3719 (cur->properties != NULL))
3720 xmlFreePropList(cur->properties);
3721 if ((cur->type != XML_ELEMENT_NODE) &&
3722 (cur->content != NULL) &&
3723 (cur->type != XML_ENTITY_REF_NODE) &&
3724 (cur->type != XML_XINCLUDE_END) &&
3725 (cur->type != XML_XINCLUDE_START) &&
3726 (cur->content != (xmlChar *) &(cur->properties))) {
3727 DICT_FREE(cur->content)
3731 * When a node is a text node or a comment, it uses a global static
3732 * variable for the name of the node.
3733 * Otherwise the node name might come from the document's dictionnary
3735 if ((cur->name != NULL) &&
3736 (cur->type != XML_TEXT_NODE) &&
3737 (cur->type != XML_COMMENT_NODE))
3738 DICT_FREE(cur->name)
3740 if (((cur->type == XML_ELEMENT_NODE) ||
3741 (cur->type == XML_XINCLUDE_START) ||
3742 (cur->type == XML_XINCLUDE_END)) &&
3743 (cur->nsDef != NULL))
3744 xmlFreeNsList(cur->nsDef);
3752 * Unlink a node from it's current context, the node is not freed
3753 * If one need to free the node, use xmlFreeNode() routine after the
3754 * unlink to discard it.
3757 xmlUnlinkNode(xmlNodePtr cur) {
3760 xmlGenericError(xmlGenericErrorContext,
3761 "xmlUnlinkNode : node == NULL\n");
3765 if (cur->type == XML_DTD_NODE) {
3769 if (doc->intSubset == (xmlDtdPtr) cur)
3770 doc->intSubset = NULL;
3771 if (doc->extSubset == (xmlDtdPtr) cur)
3772 doc->extSubset = NULL;
3775 if (cur->type == XML_ENTITY_DECL) {
3779 if (doc->intSubset != NULL) {
3780 if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur)
3781 xmlHashRemoveEntry(doc->intSubset->entities, cur->name,
3783 if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur)
3784 xmlHashRemoveEntry(doc->intSubset->pentities, cur->name,
3787 if (doc->extSubset != NULL) {
3788 if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur)
3789 xmlHashRemoveEntry(doc->extSubset->entities, cur->name,
3791 if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur)
3792 xmlHashRemoveEntry(doc->extSubset->pentities, cur->name,
3797 if (cur->parent != NULL) {
3799 parent = cur->parent;
3800 if (cur->type == XML_ATTRIBUTE_NODE) {
3801 if (parent->properties == (xmlAttrPtr) cur)
3802 parent->properties = ((xmlAttrPtr) cur)->next;
3804 if (parent->children == cur)
3805 parent->children = cur->next;
3806 if (parent->last == cur)
3807 parent->last = cur->prev;
3811 if (cur->next != NULL)
3812 cur->next->prev = cur->prev;
3813 if (cur->prev != NULL)
3814 cur->prev->next = cur->next;
3815 cur->next = cur->prev = NULL;
3818 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
3821 * @old: the old node
3824 * Unlink the old node from its current context, prune the new one
3825 * at the same place. If @cur was already inserted in a document it is
3826 * first unlinked from its existing context.
3828 * Returns the @old node
3831 xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
3832 if (old == cur) return(NULL);
3833 if ((old == NULL) || (old->parent == NULL)) {
3835 xmlGenericError(xmlGenericErrorContext,
3836 "xmlReplaceNode : old == NULL or without parent\n");
3847 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3849 xmlGenericError(xmlGenericErrorContext,
3850 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3854 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3856 xmlGenericError(xmlGenericErrorContext,
3857 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3862 xmlSetTreeDoc(cur, old->doc);
3863 cur->parent = old->parent;
3864 cur->next = old->next;
3865 if (cur->next != NULL)
3866 cur->next->prev = cur;
3867 cur->prev = old->prev;
3868 if (cur->prev != NULL)
3869 cur->prev->next = cur;
3870 if (cur->parent != NULL) {
3871 if (cur->type == XML_ATTRIBUTE_NODE) {
3872 if (cur->parent->properties == (xmlAttrPtr)old)
3873 cur->parent->properties = ((xmlAttrPtr) cur);
3875 if (cur->parent->children == old)
3876 cur->parent->children = cur;
3877 if (cur->parent->last == old)
3878 cur->parent->last = cur;
3881 old->next = old->prev = NULL;
3885 #endif /* LIBXML_TREE_ENABLED */
3887 /************************************************************************
3891 ************************************************************************/
3895 * @cur: the namespace
3897 * Do a copy of the namespace.
3899 * Returns: a new #xmlNsPtr, or NULL in case of error.
3902 xmlCopyNamespace(xmlNsPtr cur) {
3905 if (cur == NULL) return(NULL);
3906 switch (cur->type) {
3907 case XML_LOCAL_NAMESPACE:
3908 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3912 xmlGenericError(xmlGenericErrorContext,
3913 "xmlCopyNamespace: invalid type %d\n", cur->type);
3921 * xmlCopyNamespaceList:
3922 * @cur: the first namespace
3924 * Do a copy of an namespace list.
3926 * Returns: a new #xmlNsPtr, or NULL in case of error.
3929 xmlCopyNamespaceList(xmlNsPtr cur) {
3930 xmlNsPtr ret = NULL;
3931 xmlNsPtr p = NULL,q;
3933 while (cur != NULL) {
3934 q = xmlCopyNamespace(cur);
3947 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
3950 xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
3953 if (cur == NULL) return(NULL);
3955 ret = xmlNewDocProp(target->doc, cur->name, NULL);
3956 else if (doc != NULL)
3957 ret = xmlNewDocProp(doc, cur->name, NULL);
3958 else if (cur->parent != NULL)
3959 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3960 else if (cur->children != NULL)
3961 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3963 ret = xmlNewDocProp(NULL, cur->name, NULL);
3964 if (ret == NULL) return(NULL);
3965 ret->parent = target;
3967 if ((cur->ns != NULL) && (target != NULL)) {
3970 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3973 * Humm, we are copying an element whose namespace is defined
3974 * out of the new tree scope. Search it in the original tree
3975 * and add it at the top of the new tree
3977 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
3979 xmlNodePtr root = target;
3980 xmlNodePtr pred = NULL;
3982 while (root->parent != NULL) {
3984 root = root->parent;
3986 if (root == (xmlNodePtr) target->doc) {
3987 /* correct possibly cycling above the document elt */
3990 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3994 * we have to find something appropriate here since
3995 * we cant be sure, that the namespce we found is identified
3998 if (xmlStrEqual(ns->href, cur->ns->href)) {
3999 /* this is the nice case */
4003 * we are in trouble: we need a new reconcilied namespace.
4006 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
4013 if (cur->children != NULL) {
4016 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
4018 tmp = ret->children;
4019 while (tmp != NULL) {
4020 /* tmp->parent = (xmlNodePtr)ret; */
4021 if (tmp->next == NULL)
4029 if ((target!= NULL) && (cur!= NULL) &&
4030 (target->doc != NULL) && (cur->doc != NULL) &&
4031 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
4032 if (xmlIsID(cur->doc, cur->parent, cur)) {
4035 id = xmlNodeListGetString(cur->doc, cur->children, 1);
4037 xmlAddID(NULL, target->doc, id, ret);
4047 * @target: the element where the attribute will be grafted
4048 * @cur: the attribute
4050 * Do a copy of the attribute.
4052 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4055 xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
4056 return xmlCopyPropInternal(NULL, target, cur);
4061 * @target: the element where the attributes will be grafted
4062 * @cur: the first attribute
4064 * Do a copy of an attribute list.
4066 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4069 xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
4070 xmlAttrPtr ret = NULL;
4071 xmlAttrPtr p = NULL,q;
4073 while (cur != NULL) {
4074 q = xmlCopyProp(target, cur);
4090 * NOTE about the CopyNode operations !
4092 * They are split into external and internal parts for one
4093 * tricky reason: namespaces. Doing a direct copy of a node
4094 * say RPM:Copyright without changing the namespace pointer to
4095 * something else can produce stale links. One way to do it is
4096 * to keep a reference counter but this doesn't work as soon
4097 * as one move the element or the subtree out of the scope of
4098 * the existing namespace. The actual solution seems to add
4099 * a copy of the namespace at the top of the copied tree if
4100 * not available in the subtree.
4101 * Hence two functions, the public front-end call the inner ones
4102 * The argument "recursive" normally indicates a recursive copy
4103 * of the node with values 0 (no) and 1 (yes). For XInclude,
4104 * however, we allow a value of 2 to indicate copy properties and
4105 * namespace info, but don't recurse on children.
4109 xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
4113 if (node == NULL) return(NULL);
4114 switch (node->type) {
4116 case XML_CDATA_SECTION_NODE:
4117 case XML_ELEMENT_NODE:
4118 case XML_DOCUMENT_FRAG_NODE:
4119 case XML_ENTITY_REF_NODE:
4120 case XML_ENTITY_NODE:
4122 case XML_COMMENT_NODE:
4123 case XML_XINCLUDE_START:
4124 case XML_XINCLUDE_END:
4126 case XML_ATTRIBUTE_NODE:
4127 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
4128 case XML_NAMESPACE_DECL:
4129 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
4131 case XML_DOCUMENT_NODE:
4132 case XML_HTML_DOCUMENT_NODE:
4133 #ifdef LIBXML_DOCB_ENABLED
4134 case XML_DOCB_DOCUMENT_NODE:
4136 #ifdef LIBXML_TREE_ENABLED
4137 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
4138 #endif /* LIBXML_TREE_ENABLED */
4139 case XML_DOCUMENT_TYPE_NODE:
4140 case XML_NOTATION_NODE:
4142 case XML_ELEMENT_DECL:
4143 case XML_ATTRIBUTE_DECL:
4144 case XML_ENTITY_DECL:
4149 * Allocate a new node and fill the fields.
4151 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4153 xmlTreeErrMemory("copying node");
4156 memset(ret, 0, sizeof(xmlNode));
4157 ret->type = node->type;
4160 ret->parent = parent;
4161 if (node->name == xmlStringText)
4162 ret->name = xmlStringText;
4163 else if (node->name == xmlStringTextNoenc)
4164 ret->name = xmlStringTextNoenc;
4165 else if (node->name == xmlStringComment)
4166 ret->name = xmlStringComment;
4167 else if (node->name != NULL) {
4168 if ((doc != NULL) && (doc->dict != NULL))
4169 ret->name = xmlDictLookup(doc->dict, node->name, -1);
4171 ret->name = xmlStrdup(node->name);
4173 if ((node->type != XML_ELEMENT_NODE) &&
4174 (node->content != NULL) &&
4175 (node->type != XML_ENTITY_REF_NODE) &&
4176 (node->type != XML_XINCLUDE_END) &&
4177 (node->type != XML_XINCLUDE_START)) {
4178 ret->content = xmlStrdup(node->content);
4180 if (node->type == XML_ELEMENT_NODE)
4181 ret->line = node->line;
4183 if (parent != NULL) {
4187 * this is a tricky part for the node register thing:
4188 * in case ret does get coalesced in xmlAddChild
4189 * the deregister-node callback is called; so we register ret now already
4191 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
4192 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4194 tmp = xmlAddChild(parent, ret);
4195 /* node could have coalesced */
4202 if (((node->type == XML_ELEMENT_NODE) ||
4203 (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
4204 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
4206 if (node->ns != NULL) {
4209 ns = xmlSearchNs(doc, ret, node->ns->prefix);
4212 * Humm, we are copying an element whose namespace is defined
4213 * out of the new tree scope. Search it in the original tree
4214 * and add it at the top of the new tree
4216 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
4218 xmlNodePtr root = ret;
4220 while (root->parent != NULL) root = root->parent;
4221 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4223 ret->ns = xmlNewReconciliedNs(doc, ret, node->ns);
4227 * reference the existing namespace definition in our own tree.
4232 if (((node->type == XML_ELEMENT_NODE) ||
4233 (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
4234 ret->properties = xmlCopyPropList(ret, node->properties);
4235 if (node->type == XML_ENTITY_REF_NODE) {
4236 if ((doc == NULL) || (node->doc != doc)) {
4238 * The copied node will go into a separate document, so
4239 * to avoid dangling references to the ENTITY_DECL node
4240 * we cannot keep the reference. Try to find it in the
4243 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
4245 ret->children = node->children;
4247 ret->last = ret->children;
4248 } else if ((node->children != NULL) && (extended != 2)) {
4249 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
4250 UPDATE_LAST_CHILD_AND_PARENT(ret)
4254 /* if parent != NULL we already registered the node above */
4255 if ((parent == NULL) &&
4256 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
4257 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4262 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
4263 xmlNodePtr ret = NULL;
4264 xmlNodePtr p = NULL,q;
4266 while (node != NULL) {
4267 #ifdef LIBXML_TREE_ENABLED
4268 if (node->type == XML_DTD_NODE ) {
4273 if (doc->intSubset == NULL) {
4274 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
4277 doc->intSubset = (xmlDtdPtr) q;
4278 xmlAddChild(parent, q);
4280 q = (xmlNodePtr) doc->intSubset;
4281 xmlAddChild(parent, q);
4284 #endif /* LIBXML_TREE_ENABLED */
4285 q = xmlStaticCopyNode(node, doc, parent, 1);
4289 } else if (p != q) {
4290 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
4303 * @extended: if 1 do a recursive copy (properties, namespaces and children
4305 * if 2 copy properties and namespaces (when applicable)
4307 * Do a copy of the node.
4309 * Returns: a new #xmlNodePtr, or NULL in case of error.
4312 xmlCopyNode(const xmlNodePtr node, int extended) {
4315 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
4322 * @doc: the document
4323 * @extended: if 1 do a recursive copy (properties, namespaces and children
4325 * if 2 copy properties and namespaces (when applicable)
4327 * Do a copy of the node to a given document.
4329 * Returns: a new #xmlNodePtr, or NULL in case of error.
4332 xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) {
4335 ret = xmlStaticCopyNode(node, doc, NULL, extended);
4340 * xmlDocCopyNodeList:
4341 * @doc: the target document
4342 * @node: the first node in the list.
4344 * Do a recursive copy of the node list.
4346 * Returns: a new #xmlNodePtr, or NULL in case of error.
4348 xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) {
4349 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4355 * @node: the first node in the list.
4357 * Do a recursive copy of the node list.
4358 * Use xmlDocCopyNodeList() if possible to ensure string interning.
4360 * Returns: a new #xmlNodePtr, or NULL in case of error.
4362 xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
4363 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4367 #if defined(LIBXML_TREE_ENABLED)
4372 * Do a copy of the dtd.
4374 * Returns: a new #xmlDtdPtr, or NULL in case of error.
4377 xmlCopyDtd(xmlDtdPtr dtd) {
4379 xmlNodePtr cur, p = NULL, q;
4381 if (dtd == NULL) return(NULL);
4382 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4383 if (ret == NULL) return(NULL);
4384 if (dtd->entities != NULL)
4385 ret->entities = (void *) xmlCopyEntitiesTable(
4386 (xmlEntitiesTablePtr) dtd->entities);
4387 if (dtd->notations != NULL)
4388 ret->notations = (void *) xmlCopyNotationTable(
4389 (xmlNotationTablePtr) dtd->notations);
4390 if (dtd->elements != NULL)
4391 ret->elements = (void *) xmlCopyElementTable(
4392 (xmlElementTablePtr) dtd->elements);
4393 if (dtd->attributes != NULL)
4394 ret->attributes = (void *) xmlCopyAttributeTable(
4395 (xmlAttributeTablePtr) dtd->attributes);
4396 if (dtd->pentities != NULL)
4397 ret->pentities = (void *) xmlCopyEntitiesTable(
4398 (xmlEntitiesTablePtr) dtd->pentities);
4400 cur = dtd->children;
4401 while (cur != NULL) {
4404 if (cur->type == XML_ENTITY_DECL) {
4405 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4406 switch (tmp->etype) {
4407 case XML_INTERNAL_GENERAL_ENTITY:
4408 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4409 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4410 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4412 case XML_INTERNAL_PARAMETER_ENTITY:
4413 case XML_EXTERNAL_PARAMETER_ENTITY:
4415 xmlGetParameterEntityFromDtd(ret, tmp->name);
4417 case XML_INTERNAL_PREDEFINED_ENTITY:
4420 } else if (cur->type == XML_ELEMENT_DECL) {
4421 xmlElementPtr tmp = (xmlElementPtr) cur;
4423 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4424 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4425 xmlAttributePtr tmp = (xmlAttributePtr) cur;
4427 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4428 } else if (cur->type == XML_COMMENT_NODE) {
4429 q = xmlCopyNode(cur, 0);
4443 q->parent = (xmlNodePtr) ret;
4454 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
4457 * @doc: the document
4458 * @recursive: if not zero do a recursive copy.
4460 * Do a copy of the document info. If recursive, the content tree will
4461 * be copied too as well as DTD, namespaces and entities.
4463 * Returns: a new #xmlDocPtr, or NULL in case of error.
4466 xmlCopyDoc(xmlDocPtr doc, int recursive) {
4469 if (doc == NULL) return(NULL);
4470 ret = xmlNewDoc(doc->version);
4471 if (ret == NULL) return(NULL);
4472 if (doc->name != NULL)
4473 ret->name = xmlMemStrdup(doc->name);
4474 if (doc->encoding != NULL)
4475 ret->encoding = xmlStrdup(doc->encoding);
4476 if (doc->URL != NULL)
4477 ret->URL = xmlStrdup(doc->URL);
4478 ret->charset = doc->charset;
4479 ret->compression = doc->compression;
4480 ret->standalone = doc->standalone;
4481 if (!recursive) return(ret);
4484 ret->children = NULL;
4485 #ifdef LIBXML_TREE_ENABLED
4486 if (doc->intSubset != NULL) {
4487 ret->intSubset = xmlCopyDtd(doc->intSubset);
4488 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
4489 ret->intSubset->parent = ret;
4492 if (doc->oldNs != NULL)
4493 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4494 if (doc->children != NULL) {
4497 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4500 tmp = ret->children;
4501 while (tmp != NULL) {
4502 if (tmp->next == NULL)
4509 #endif /* LIBXML_TREE_ENABLED */
4511 /************************************************************************
4513 * Content access functions *
4515 ************************************************************************/
4521 * Get line number of @node. This requires activation of this option
4522 * before invoking the parser by calling xmlLineNumbersDefault(1)
4524 * Returns the line number if successful, -1 otherwise
4527 xmlGetLineNo(xmlNodePtr node)
4533 if ((node->type == XML_ELEMENT_NODE) ||
4534 (node->type == XML_TEXT_NODE) ||
4535 (node->type == XML_COMMENT_NODE) ||
4536 (node->type == XML_PI_NODE))
4537 result = (long) node->line;
4538 else if ((node->prev != NULL) &&
4539 ((node->prev->type == XML_ELEMENT_NODE) ||
4540 (node->prev->type == XML_TEXT_NODE) ||
4541 (node->prev->type == XML_COMMENT_NODE) ||
4542 (node->prev->type == XML_PI_NODE)))
4543 result = xmlGetLineNo(node->prev);
4544 else if ((node->parent != NULL) &&
4545 (node->parent->type == XML_ELEMENT_NODE))
4546 result = xmlGetLineNo(node->parent);
4551 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
4556 * Build a structure based Path for the given node
4558 * Returns the new path or NULL in case of error. The caller must free
4559 * the returned string
4562 xmlGetNodePath(xmlNodePtr node)
4564 xmlNodePtr cur, tmp, next;
4565 xmlChar *buffer = NULL, *temp;
4571 int occur = 0, generic;
4577 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4578 if (buffer == NULL) {
4579 xmlTreeErrMemory("getting node path");
4582 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4584 xmlTreeErrMemory("getting node path");
4595 if ((cur->type == XML_DOCUMENT_NODE) ||
4596 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4597 if (buffer[0] == '/')
4601 } else if (cur->type == XML_ELEMENT_NODE) {
4604 name = (const char *) cur->name;
4606 if (cur->ns->prefix != NULL) {
4607 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4608 (char *)cur->ns->prefix, (char *)cur->name);
4609 nametemp[sizeof(nametemp) - 1] = 0;
4613 * We cannot express named elements in the default
4614 * namespace, so use "*".
4623 * Thumbler index computation
4624 * TODO: the ocurence test seems bogus for namespaced names
4627 while (tmp != NULL) {
4628 if ((tmp->type == XML_ELEMENT_NODE) &&
4630 (xmlStrEqual(cur->name, tmp->name) &&
4631 ((tmp->ns == cur->ns) ||
4632 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4633 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4639 while (tmp != NULL && occur == 0) {
4640 if ((tmp->type == XML_ELEMENT_NODE) &&
4642 (xmlStrEqual(cur->name, tmp->name) &&
4643 ((tmp->ns == cur->ns) ||
4644 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4645 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4653 } else if (cur->type == XML_COMMENT_NODE) {
4659 * Thumbler index computation
4662 while (tmp != NULL) {
4663 if (tmp->type == XML_COMMENT_NODE)
4669 while (tmp != NULL && occur == 0) {
4670 if (tmp->type == XML_COMMENT_NODE)
4678 } else if ((cur->type == XML_TEXT_NODE) ||
4679 (cur->type == XML_CDATA_SECTION_NODE)) {
4685 * Thumbler index computation
4688 while (tmp != NULL) {
4689 if ((tmp->type == XML_TEXT_NODE) ||
4690 (tmp->type == XML_CDATA_SECTION_NODE))
4695 * Evaluate if this is the only text- or CDATA-section-node;
4696 * if yes, then we'll get "text()", otherwise "text()[1]".
4700 while (tmp != NULL) {
4701 if ((tmp->type == XML_TEXT_NODE) ||
4702 (tmp->type == XML_CDATA_SECTION_NODE))
4711 } else if (cur->type == XML_PI_NODE) {
4713 snprintf(nametemp, sizeof(nametemp) - 1,
4714 "processing-instruction('%s')", (char *)cur->name);
4715 nametemp[sizeof(nametemp) - 1] = 0;
4721 * Thumbler index computation
4724 while (tmp != NULL) {
4725 if ((tmp->type == XML_PI_NODE) &&
4726 (xmlStrEqual(cur->name, tmp->name)))
4732 while (tmp != NULL && occur == 0) {
4733 if ((tmp->type == XML_PI_NODE) &&
4734 (xmlStrEqual(cur->name, tmp->name)))
4743 } else if (cur->type == XML_ATTRIBUTE_NODE) {
4745 name = (const char *) (((xmlAttrPtr) cur)->name);
4747 if (cur->ns->prefix != NULL)
4748 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4749 (char *)cur->ns->prefix, (char *)cur->name);
4751 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4753 nametemp[sizeof(nametemp) - 1] = 0;
4756 next = ((xmlAttrPtr) cur)->parent;
4762 * Make sure there is enough room
4764 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4766 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4767 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4769 xmlTreeErrMemory("getting node path");
4775 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4777 xmlTreeErrMemory("getting node path");
4785 snprintf((char *) buf, buf_len, "%s%s%s",
4786 sep, name, (char *) buffer);
4788 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
4789 sep, name, occur, (char *) buffer);
4790 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
4792 } while (cur != NULL);
4796 #endif /* LIBXML_TREE_ENABLED */
4799 * xmlDocGetRootElement:
4800 * @doc: the document
4802 * Get the root element of the document (doc->children is a list
4803 * containing possibly comments, PIs, etc ...).
4805 * Returns the #xmlNodePtr for the root or NULL
4808 xmlDocGetRootElement(xmlDocPtr doc) {
4811 if (doc == NULL) return(NULL);
4812 ret = doc->children;
4813 while (ret != NULL) {
4814 if (ret->type == XML_ELEMENT_NODE)
4821 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
4823 * xmlDocSetRootElement:
4824 * @doc: the document
4825 * @root: the new document root element, if root is NULL no action is taken,
4826 * to remove a node from a document use xmlUnlinkNode(root) instead.
4828 * Set the root element of the document (doc->children is a list
4829 * containing possibly comments, PIs, etc ...).
4831 * Returns the old root element if any was found, NULL if root was NULL
4834 xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4835 xmlNodePtr old = NULL;
4837 if (doc == NULL) return(NULL);
4840 xmlUnlinkNode(root);
4841 xmlSetTreeDoc(root, doc);
4842 root->parent = (xmlNodePtr) doc;
4843 old = doc->children;
4844 while (old != NULL) {
4845 if (old->type == XML_ELEMENT_NODE)
4850 if (doc->children == NULL) {
4851 doc->children = root;
4854 xmlAddSibling(doc->children, root);
4857 xmlReplaceNode(old, root);
4863 #if defined(LIBXML_TREE_ENABLED)
4866 * @cur: the node being changed
4867 * @lang: the language description
4869 * Set the language of a node, i.e. the values of the xml:lang
4873 xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
4876 if (cur == NULL) return;
4879 case XML_CDATA_SECTION_NODE:
4880 case XML_COMMENT_NODE:
4881 case XML_DOCUMENT_NODE:
4882 case XML_DOCUMENT_TYPE_NODE:
4883 case XML_DOCUMENT_FRAG_NODE:
4884 case XML_NOTATION_NODE:
4885 case XML_HTML_DOCUMENT_NODE:
4887 case XML_ELEMENT_DECL:
4888 case XML_ATTRIBUTE_DECL:
4889 case XML_ENTITY_DECL:
4891 case XML_ENTITY_REF_NODE:
4892 case XML_ENTITY_NODE:
4893 case XML_NAMESPACE_DECL:
4894 #ifdef LIBXML_DOCB_ENABLED
4895 case XML_DOCB_DOCUMENT_NODE:
4897 case XML_XINCLUDE_START:
4898 case XML_XINCLUDE_END:
4900 case XML_ELEMENT_NODE:
4901 case XML_ATTRIBUTE_NODE:
4904 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4907 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
4909 #endif /* LIBXML_TREE_ENABLED */
4913 * @cur: the node being checked
4915 * Searches the language of a node, i.e. the values of the xml:lang
4916 * attribute or the one carried by the nearest ancestor.
4918 * Returns a pointer to the lang value, or NULL if not found
4919 * It's up to the caller to free the memory with xmlFree().
4922 xmlNodeGetLang(xmlNodePtr cur) {
4925 while (cur != NULL) {
4926 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
4935 #ifdef LIBXML_TREE_ENABLED
4937 * xmlNodeSetSpacePreserve:
4938 * @cur: the node being changed
4939 * @val: the xml:space value ("0": default, 1: "preserve")
4941 * Set (or reset) the space preserving behaviour of a node, i.e. the
4942 * value of the xml:space attribute.
4945 xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
4948 if (cur == NULL) return;
4951 case XML_CDATA_SECTION_NODE:
4952 case XML_COMMENT_NODE:
4953 case XML_DOCUMENT_NODE:
4954 case XML_DOCUMENT_TYPE_NODE:
4955 case XML_DOCUMENT_FRAG_NODE:
4956 case XML_NOTATION_NODE:
4957 case XML_HTML_DOCUMENT_NODE:
4959 case XML_ELEMENT_DECL:
4960 case XML_ATTRIBUTE_DECL:
4961 case XML_ENTITY_DECL:
4963 case XML_ENTITY_REF_NODE:
4964 case XML_ENTITY_NODE:
4965 case XML_NAMESPACE_DECL:
4966 case XML_XINCLUDE_START:
4967 case XML_XINCLUDE_END:
4968 #ifdef LIBXML_DOCB_ENABLED
4969 case XML_DOCB_DOCUMENT_NODE:
4972 case XML_ELEMENT_NODE:
4973 case XML_ATTRIBUTE_NODE:
4976 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4981 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
4984 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
4988 #endif /* LIBXML_TREE_ENABLED */
4991 * xmlNodeGetSpacePreserve:
4992 * @cur: the node being checked
4994 * Searches the space preserving behaviour of a node, i.e. the values
4995 * of the xml:space attribute or the one carried by the nearest
4998 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
5001 xmlNodeGetSpacePreserve(xmlNodePtr cur) {
5004 while (cur != NULL) {
5005 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
5006 if (space != NULL) {
5007 if (xmlStrEqual(space, BAD_CAST "preserve")) {
5011 if (xmlStrEqual(space, BAD_CAST "default")) {
5022 #ifdef LIBXML_TREE_ENABLED
5025 * @cur: the node being changed
5026 * @name: the new tag name
5028 * Set (or reset) the name of a node.
5031 xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
5035 if (cur == NULL) return;
5036 if (name == NULL) return;
5039 case XML_CDATA_SECTION_NODE:
5040 case XML_COMMENT_NODE:
5041 case XML_DOCUMENT_TYPE_NODE:
5042 case XML_DOCUMENT_FRAG_NODE:
5043 case XML_NOTATION_NODE:
5044 case XML_HTML_DOCUMENT_NODE:
5045 case XML_NAMESPACE_DECL:
5046 case XML_XINCLUDE_START:
5047 case XML_XINCLUDE_END:
5048 #ifdef LIBXML_DOCB_ENABLED
5049 case XML_DOCB_DOCUMENT_NODE:
5052 case XML_ELEMENT_NODE:
5053 case XML_ATTRIBUTE_NODE:
5055 case XML_ENTITY_REF_NODE:
5056 case XML_ENTITY_NODE:
5058 case XML_DOCUMENT_NODE:
5059 case XML_ELEMENT_DECL:
5060 case XML_ATTRIBUTE_DECL:
5061 case XML_ENTITY_DECL:
5070 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
5071 xmlFree((xmlChar *) cur->name);
5072 cur->name = xmlDictLookup(dict, name, -1);
5074 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
5075 cur->name = xmlStrdup(name);
5080 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
5083 * @cur: the node being changed
5084 * @uri: the new base URI
5086 * Set (or reset) the base URI of a node, i.e. the value of the
5087 * xml:base attribute.
5090 xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
5094 if (cur == NULL) return;
5097 case XML_CDATA_SECTION_NODE:
5098 case XML_COMMENT_NODE:
5099 case XML_DOCUMENT_TYPE_NODE:
5100 case XML_DOCUMENT_FRAG_NODE:
5101 case XML_NOTATION_NODE:
5103 case XML_ELEMENT_DECL:
5104 case XML_ATTRIBUTE_DECL:
5105 case XML_ENTITY_DECL:
5107 case XML_ENTITY_REF_NODE:
5108 case XML_ENTITY_NODE:
5109 case XML_NAMESPACE_DECL:
5110 case XML_XINCLUDE_START:
5111 case XML_XINCLUDE_END:
5113 case XML_ELEMENT_NODE:
5114 case XML_ATTRIBUTE_NODE:
5116 case XML_DOCUMENT_NODE:
5117 #ifdef LIBXML_DOCB_ENABLED
5118 case XML_DOCB_DOCUMENT_NODE:
5120 case XML_HTML_DOCUMENT_NODE: {
5121 xmlDocPtr doc = (xmlDocPtr) cur;
5123 if (doc->URL != NULL)
5124 xmlFree((xmlChar *) doc->URL);
5128 doc->URL = xmlPathToURI(uri);
5133 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5136 fixed = xmlPathToURI(uri);
5137 if (fixed != NULL) {
5138 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
5141 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
5144 #endif /* LIBXML_TREE_ENABLED */
5148 * @doc: the document the node pertains to
5149 * @cur: the node being checked
5151 * Searches for the BASE URL. The code should work on both XML
5152 * and HTML document even if base mechanisms are completely different.
5153 * It returns the base as defined in RFC 2396 sections
5154 * 5.1.1. Base URI within Document Content
5156 * 5.1.2. Base URI from the Encapsulating Entity
5157 * However it does not return the document base (5.1.3), use
5158 * doc->URL in this case
5160 * Returns a pointer to the base URL, or NULL if not found
5161 * It's up to the caller to free the memory with xmlFree().
5164 xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
5165 xmlChar *oldbase = NULL;
5166 xmlChar *base, *newbase;
5168 if ((cur == NULL) && (doc == NULL))
5170 if (doc == NULL) doc = cur->doc;
5171 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
5172 cur = doc->children;
5173 while ((cur != NULL) && (cur->name != NULL)) {
5174 if (cur->type != XML_ELEMENT_NODE) {
5178 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5179 cur = cur->children;
5182 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5183 cur = cur->children;
5186 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5187 return(xmlGetProp(cur, BAD_CAST "href"));
5193 while (cur != NULL) {
5194 if (cur->type == XML_ENTITY_DECL) {
5195 xmlEntityPtr ent = (xmlEntityPtr) cur;
5196 return(xmlStrdup(ent->URI));
5198 if (cur->type == XML_ELEMENT_NODE) {
5199 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
5201 if (oldbase != NULL) {
5202 newbase = xmlBuildURI(oldbase, base);
5203 if (newbase != NULL) {
5215 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
5216 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
5217 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
5223 if ((doc != NULL) && (doc->URL != NULL)) {
5224 if (oldbase == NULL)
5225 return(xmlStrdup(doc->URL));
5226 newbase = xmlBuildURI(oldbase, doc->URL);
5234 * xmlNodeBufGetContent:
5236 * @cur: the node being read
5238 * Read the value of a node @cur, this can be either the text carried
5239 * directly by this node if it's a TEXT node or the aggregate string
5240 * of the values carried by this node child's (TEXT and ENTITY_REF).
5241 * Entity references are substituted.
5242 * Fills up the buffer @buffer with this value
5244 * Returns 0 in case of success and -1 in case of error.
5247 xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
5249 if ((cur == NULL) || (buffer == NULL)) return(-1);
5250 switch (cur->type) {
5251 case XML_CDATA_SECTION_NODE:
5253 xmlBufferCat(buffer, cur->content);
5255 case XML_DOCUMENT_FRAG_NODE:
5256 case XML_ELEMENT_NODE:{
5257 xmlNodePtr tmp = cur;
5259 while (tmp != NULL) {
5260 switch (tmp->type) {
5261 case XML_CDATA_SECTION_NODE:
5263 if (tmp->content != NULL)
5264 xmlBufferCat(buffer, tmp->content);
5266 case XML_ENTITY_REF_NODE:
5267 xmlNodeBufGetContent(buffer, tmp);
5275 if (tmp->children != NULL) {
5276 if (tmp->children->type != XML_ENTITY_DECL) {
5277 tmp = tmp->children;
5284 if (tmp->next != NULL) {
5297 if (tmp->next != NULL) {
5301 } while (tmp != NULL);
5305 case XML_ATTRIBUTE_NODE:{
5306 xmlAttrPtr attr = (xmlAttrPtr) cur;
5307 xmlNodePtr tmp = attr->children;
5309 while (tmp != NULL) {
5310 if (tmp->type == XML_TEXT_NODE)
5311 xmlBufferCat(buffer, tmp->content);
5313 xmlNodeBufGetContent(buffer, tmp);
5318 case XML_COMMENT_NODE:
5320 xmlBufferCat(buffer, cur->content);
5322 case XML_ENTITY_REF_NODE:{
5326 /* lookup entity declaration */
5327 ent = xmlGetDocEntity(cur->doc, cur->name);
5331 /* an entity content can be any "well balanced chunk",
5332 * i.e. the result of the content [43] production:
5333 * http://www.w3.org/TR/REC-xml#NT-content
5334 * -> we iterate through child nodes and recursive call
5335 * xmlNodeGetContent() which handles all possible node types */
5336 tmp = ent->children;
5338 xmlNodeBufGetContent(buffer, tmp);
5343 case XML_ENTITY_NODE:
5344 case XML_DOCUMENT_TYPE_NODE:
5345 case XML_NOTATION_NODE:
5347 case XML_XINCLUDE_START:
5348 case XML_XINCLUDE_END:
5350 case XML_DOCUMENT_NODE:
5351 #ifdef LIBXML_DOCB_ENABLED
5352 case XML_DOCB_DOCUMENT_NODE:
5354 case XML_HTML_DOCUMENT_NODE:
5355 cur = cur->children;
5356 while (cur!= NULL) {
5357 if ((cur->type == XML_ELEMENT_NODE) ||
5358 (cur->type == XML_TEXT_NODE) ||
5359 (cur->type == XML_CDATA_SECTION_NODE)) {
5360 xmlNodeBufGetContent(buffer, cur);
5365 case XML_NAMESPACE_DECL:
5366 xmlBufferCat(buffer, ((xmlNsPtr) cur)->href);
5368 case XML_ELEMENT_DECL:
5369 case XML_ATTRIBUTE_DECL:
5370 case XML_ENTITY_DECL:
5376 * xmlNodeGetContent:
5377 * @cur: the node being read
5379 * Read the value of a node, this can be either the text carried
5380 * directly by this node if it's a TEXT node or the aggregate string
5381 * of the values carried by this node child's (TEXT and ENTITY_REF).
5382 * Entity references are substituted.
5383 * Returns a new #xmlChar * or NULL if no content is available.
5384 * It's up to the caller to free the memory with xmlFree().
5387 xmlNodeGetContent(xmlNodePtr cur)
5391 switch (cur->type) {
5392 case XML_DOCUMENT_FRAG_NODE:
5393 case XML_ELEMENT_NODE:{
5394 xmlBufferPtr buffer;
5397 buffer = xmlBufferCreateSize(64);
5400 xmlNodeBufGetContent(buffer, cur);
5401 ret = buffer->content;
5402 buffer->content = NULL;
5403 xmlBufferFree(buffer);
5406 case XML_ATTRIBUTE_NODE:
5407 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
5408 case XML_COMMENT_NODE:
5410 if (cur->content != NULL)
5411 return (xmlStrdup(cur->content));
5413 case XML_ENTITY_REF_NODE:{
5415 xmlBufferPtr buffer;
5418 /* lookup entity declaration */
5419 ent = xmlGetDocEntity(cur->doc, cur->name);
5423 buffer = xmlBufferCreate();
5427 xmlNodeBufGetContent(buffer, cur);
5429 ret = buffer->content;
5430 buffer->content = NULL;
5431 xmlBufferFree(buffer);
5434 case XML_ENTITY_NODE:
5435 case XML_DOCUMENT_TYPE_NODE:
5436 case XML_NOTATION_NODE:
5438 case XML_XINCLUDE_START:
5439 case XML_XINCLUDE_END:
5441 case XML_DOCUMENT_NODE:
5442 #ifdef LIBXML_DOCB_ENABLED
5443 case XML_DOCB_DOCUMENT_NODE:
5445 case XML_HTML_DOCUMENT_NODE: {
5446 xmlBufferPtr buffer;
5449 buffer = xmlBufferCreate();
5453 xmlNodeBufGetContent(buffer, (xmlNodePtr) cur);
5455 ret = buffer->content;
5456 buffer->content = NULL;
5457 xmlBufferFree(buffer);
5460 case XML_NAMESPACE_DECL: {
5463 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5466 case XML_ELEMENT_DECL:
5469 case XML_ATTRIBUTE_DECL:
5472 case XML_ENTITY_DECL:
5475 case XML_CDATA_SECTION_NODE:
5477 if (cur->content != NULL)
5478 return (xmlStrdup(cur->content));
5485 * xmlNodeSetContent:
5486 * @cur: the node being modified
5487 * @content: the new value of the content
5489 * Replace the content of a node.
5490 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5491 * references, but XML special chars need to be escaped first by using
5492 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5495 xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5498 xmlGenericError(xmlGenericErrorContext,
5499 "xmlNodeSetContent : node == NULL\n");
5503 switch (cur->type) {
5504 case XML_DOCUMENT_FRAG_NODE:
5505 case XML_ELEMENT_NODE:
5506 case XML_ATTRIBUTE_NODE:
5507 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5508 cur->children = xmlStringGetNodeList(cur->doc, content);
5509 UPDATE_LAST_CHILD_AND_PARENT(cur)
5512 case XML_CDATA_SECTION_NODE:
5513 case XML_ENTITY_REF_NODE:
5514 case XML_ENTITY_NODE:
5516 case XML_COMMENT_NODE:
5517 if ((cur->content != NULL) &&
5518 (cur->content != (xmlChar *) &(cur->properties))) {
5519 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5520 (xmlDictOwns(cur->doc->dict, cur->content))))
5521 xmlFree(cur->content);
5523 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5524 cur->last = cur->children = NULL;
5525 if (content != NULL) {
5526 cur->content = xmlStrdup(content);
5528 cur->content = NULL;
5529 cur->properties = NULL;
5532 case XML_DOCUMENT_NODE:
5533 case XML_HTML_DOCUMENT_NODE:
5534 case XML_DOCUMENT_TYPE_NODE:
5535 case XML_XINCLUDE_START:
5536 case XML_XINCLUDE_END:
5537 #ifdef LIBXML_DOCB_ENABLED
5538 case XML_DOCB_DOCUMENT_NODE:
5541 case XML_NOTATION_NODE:
5545 case XML_NAMESPACE_DECL:
5547 case XML_ELEMENT_DECL:
5550 case XML_ATTRIBUTE_DECL:
5553 case XML_ENTITY_DECL:
5559 #ifdef LIBXML_TREE_ENABLED
5561 * xmlNodeSetContentLen:
5562 * @cur: the node being modified
5563 * @content: the new value of the content
5564 * @len: the size of @content
5566 * Replace the content of a node.
5567 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5568 * references, but XML special chars need to be escaped first by using
5569 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5572 xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5575 xmlGenericError(xmlGenericErrorContext,
5576 "xmlNodeSetContentLen : node == NULL\n");
5580 switch (cur->type) {
5581 case XML_DOCUMENT_FRAG_NODE:
5582 case XML_ELEMENT_NODE:
5583 case XML_ATTRIBUTE_NODE:
5584 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5585 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5586 UPDATE_LAST_CHILD_AND_PARENT(cur)
5589 case XML_CDATA_SECTION_NODE:
5590 case XML_ENTITY_REF_NODE:
5591 case XML_ENTITY_NODE:
5593 case XML_COMMENT_NODE:
5594 case XML_NOTATION_NODE:
5595 if ((cur->content != NULL) &&
5596 (cur->content != (xmlChar *) &(cur->properties))) {
5597 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5598 (xmlDictOwns(cur->doc->dict, cur->content))))
5599 xmlFree(cur->content);
5601 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5602 cur->children = cur->last = NULL;
5603 if (content != NULL) {
5604 cur->content = xmlStrndup(content, len);
5606 cur->content = NULL;
5607 cur->properties = NULL;
5610 case XML_DOCUMENT_NODE:
5612 case XML_HTML_DOCUMENT_NODE:
5613 case XML_DOCUMENT_TYPE_NODE:
5614 case XML_NAMESPACE_DECL:
5615 case XML_XINCLUDE_START:
5616 case XML_XINCLUDE_END:
5617 #ifdef LIBXML_DOCB_ENABLED
5618 case XML_DOCB_DOCUMENT_NODE:
5621 case XML_ELEMENT_DECL:
5624 case XML_ATTRIBUTE_DECL:
5627 case XML_ENTITY_DECL:
5632 #endif /* LIBXML_TREE_ENABLED */
5635 * xmlNodeAddContentLen:
5636 * @cur: the node being modified
5637 * @content: extra content
5638 * @len: the size of @content
5640 * Append the extra substring to the node content.
5641 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5642 * raw text, so unescaped XML special chars are allowed, entity
5643 * references are not supported.
5646 xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5649 xmlGenericError(xmlGenericErrorContext,
5650 "xmlNodeAddContentLen : node == NULL\n");
5654 if (len <= 0) return;
5655 switch (cur->type) {
5656 case XML_DOCUMENT_FRAG_NODE:
5657 case XML_ELEMENT_NODE: {
5658 xmlNodePtr last, newNode, tmp;
5661 newNode = xmlNewTextLen(content, len);
5662 if (newNode != NULL) {
5663 tmp = xmlAddChild(cur, newNode);
5666 if ((last != NULL) && (last->next == newNode)) {
5667 xmlTextMerge(last, newNode);
5672 case XML_ATTRIBUTE_NODE:
5675 case XML_CDATA_SECTION_NODE:
5676 case XML_ENTITY_REF_NODE:
5677 case XML_ENTITY_NODE:
5679 case XML_COMMENT_NODE:
5680 case XML_NOTATION_NODE:
5681 if (content != NULL) {
5682 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5683 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5684 xmlDictOwns(cur->doc->dict, cur->content))) {
5685 cur->content = xmlStrncatNew(cur->content, content, len);
5686 cur->properties = NULL;
5690 cur->content = xmlStrncat(cur->content, content, len);
5692 case XML_DOCUMENT_NODE:
5694 case XML_HTML_DOCUMENT_NODE:
5695 case XML_DOCUMENT_TYPE_NODE:
5696 case XML_NAMESPACE_DECL:
5697 case XML_XINCLUDE_START:
5698 case XML_XINCLUDE_END:
5699 #ifdef LIBXML_DOCB_ENABLED
5700 case XML_DOCB_DOCUMENT_NODE:
5703 case XML_ELEMENT_DECL:
5704 case XML_ATTRIBUTE_DECL:
5705 case XML_ENTITY_DECL:
5711 * xmlNodeAddContent:
5712 * @cur: the node being modified
5713 * @content: extra content
5715 * Append the extra substring to the node content.
5716 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5717 * raw text, so unescaped XML special chars are allowed, entity
5718 * references are not supported.
5721 xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5726 xmlGenericError(xmlGenericErrorContext,
5727 "xmlNodeAddContent : node == NULL\n");
5731 if (content == NULL) return;
5732 len = xmlStrlen(content);
5733 xmlNodeAddContentLen(cur, content, len);
5738 * @first: the first text node
5739 * @second: the second text node being merged
5741 * Merge two text nodes into one
5742 * Returns the first text node augmented
5745 xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5746 if (first == NULL) return(second);
5747 if (second == NULL) return(first);
5748 if (first->type != XML_TEXT_NODE) return(first);
5749 if (second->type != XML_TEXT_NODE) return(first);
5750 if (second->name != first->name)
5752 xmlNodeAddContent(first, second->content);
5753 xmlUnlinkNode(second);
5754 xmlFreeNode(second);
5758 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
5761 * @doc: the document
5762 * @node: the current node
5764 * Search all the namespace applying to a given element.
5765 * Returns an NULL terminated array of all the #xmlNsPtr found
5766 * that need to be freed by the caller or NULL if no
5767 * namespace if defined
5770 xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5773 xmlNsPtr *ret = NULL;
5778 while (node != NULL) {
5779 if (node->type == XML_ELEMENT_NODE) {
5781 while (cur != NULL) {
5784 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5787 xmlTreeErrMemory("getting namespace list");
5792 for (i = 0; i < nbns; i++) {
5793 if ((cur->prefix == ret[i]->prefix) ||
5794 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5798 if (nbns >= maxns) {
5800 ret = (xmlNsPtr *) xmlRealloc(ret,
5805 xmlTreeErrMemory("getting namespace list");
5816 node = node->parent;
5820 #endif /* LIBXML_TREE_ENABLED */
5823 * xmlTreeEnsureXMLDecl:
5826 * Ensures that there is an XML namespace declaration on the doc.
5828 * Returns the XML ns-struct or NULL on API and internal errors.
5831 xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5835 if (doc->oldNs != NULL)
5836 return (doc->oldNs);
5839 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5842 "allocating the XML namespace");
5845 memset(ns, 0, sizeof(xmlNs));
5846 ns->type = XML_LOCAL_NAMESPACE;
5847 ns->href = xmlStrdup(XML_XML_NAMESPACE);
5848 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5856 * @doc: the document
5857 * @node: the current node
5858 * @nameSpace: the namespace prefix
5860 * Search a Ns registered under a given name space for a document.
5861 * recurse on the parents until it finds the defined namespace
5862 * or return NULL otherwise.
5863 * @nameSpace can be NULL, this is a search for the default namespace.
5864 * We don't allow to cross entities boundaries. If you don't declare
5865 * the namespace within those you will be in troubles !!! A warning
5866 * is generated to cover this case.
5868 * Returns the namespace pointer or NULL.
5871 xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
5874 xmlNodePtr orig = node;
5876 if (node == NULL) return(NULL);
5877 if ((nameSpace != NULL) &&
5878 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
5879 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5881 * The XML-1.0 namespace is normally held on the root
5882 * element. In this case exceptionally create it on the
5885 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5887 xmlTreeErrMemory("searching namespace");
5890 memset(cur, 0, sizeof(xmlNs));
5891 cur->type = XML_LOCAL_NAMESPACE;
5892 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5893 cur->prefix = xmlStrdup((const xmlChar *)"xml");
5894 cur->next = node->nsDef;
5904 * Return the XML namespace declaration held by the doc.
5906 if (doc->oldNs == NULL)
5907 return(xmlTreeEnsureXMLDecl(doc));
5911 while (node != NULL) {
5912 if ((node->type == XML_ENTITY_REF_NODE) ||
5913 (node->type == XML_ENTITY_NODE) ||
5914 (node->type == XML_ENTITY_DECL))
5916 if (node->type == XML_ELEMENT_NODE) {
5918 while (cur != NULL) {
5919 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5920 (cur->href != NULL))
5922 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5923 (cur->href != NULL) &&
5924 (xmlStrEqual(cur->prefix, nameSpace)))
5931 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5932 (cur->href != NULL))
5934 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5935 (cur->href != NULL) &&
5936 (xmlStrEqual(cur->prefix, nameSpace)))
5941 node = node->parent;
5948 * @doc: the document
5949 * @node: the current node
5950 * @ancestor: the ancestor carrying the namespace
5951 * @prefix: the namespace prefix
5953 * Verify that the given namespace held on @ancestor is still in scope
5956 * Returns 1 if true, 0 if false and -1 in case of error.
5959 xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5960 xmlNodePtr ancestor, const xmlChar * prefix)
5964 while ((node != NULL) && (node != ancestor)) {
5965 if ((node->type == XML_ENTITY_REF_NODE) ||
5966 (node->type == XML_ENTITY_NODE) ||
5967 (node->type == XML_ENTITY_DECL))
5969 if (node->type == XML_ELEMENT_NODE) {
5971 while (tst != NULL) {
5972 if ((tst->prefix == NULL)
5973 && (prefix == NULL))
5975 if ((tst->prefix != NULL)
5977 && (xmlStrEqual(tst->prefix, prefix)))
5982 node = node->parent;
5984 if (node != ancestor)
5990 * xmlSearchNsByHref:
5991 * @doc: the document
5992 * @node: the current node
5993 * @href: the namespace value
5995 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5996 * the defined namespace or return NULL otherwise.
5997 * Returns the namespace pointer or NULL.
6000 xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
6003 xmlNodePtr orig = node;
6006 if ((node == NULL) || (href == NULL))
6008 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
6010 * Only the document can hold the XML spec namespace.
6012 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6014 * The XML-1.0 namespace is normally held on the root
6015 * element. In this case exceptionally create it on the
6018 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6020 xmlTreeErrMemory("searching namespace");
6023 memset(cur, 0, sizeof(xmlNs));
6024 cur->type = XML_LOCAL_NAMESPACE;
6025 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6026 cur->prefix = xmlStrdup((const xmlChar *) "xml");
6027 cur->next = node->nsDef;
6037 * Return the XML namespace declaration held by the doc.
6039 if (doc->oldNs == NULL)
6040 return(xmlTreeEnsureXMLDecl(doc));
6044 is_attr = (node->type == XML_ATTRIBUTE_NODE);
6045 while (node != NULL) {
6046 if ((node->type == XML_ENTITY_REF_NODE) ||
6047 (node->type == XML_ENTITY_NODE) ||
6048 (node->type == XML_ENTITY_DECL))
6050 if (node->type == XML_ELEMENT_NODE) {
6052 while (cur != NULL) {
6053 if ((cur->href != NULL) && (href != NULL) &&
6054 (xmlStrEqual(cur->href, href))) {
6055 if (((!is_attr) || (cur->prefix != NULL)) &&
6056 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6064 if ((cur->href != NULL) && (href != NULL) &&
6065 (xmlStrEqual(cur->href, href))) {
6066 if (((!is_attr) || (cur->prefix != NULL)) &&
6067 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6073 node = node->parent;
6079 * xmlNewReconciliedNs:
6080 * @doc: the document
6081 * @tree: a node expected to hold the new namespace
6082 * @ns: the original namespace
6084 * This function tries to locate a namespace definition in a tree
6085 * ancestors, or create a new namespace definition node similar to
6086 * @ns trying to reuse the same prefix. However if the given prefix is
6087 * null (default namespace) or reused within the subtree defined by
6088 * @tree or on one of its ancestors then a new prefix is generated.
6089 * Returns the (new) namespace definition or NULL in case of error
6092 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
6099 xmlGenericError(xmlGenericErrorContext,
6100 "xmlNewReconciliedNs : tree == NULL\n");
6104 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
6106 xmlGenericError(xmlGenericErrorContext,
6107 "xmlNewReconciliedNs : ns == NULL\n");
6112 * Search an existing namespace definition inherited.
6114 def = xmlSearchNsByHref(doc, tree, ns->href);
6119 * Find a close prefix which is not already in use.
6120 * Let's strip namespace prefixes longer than 20 chars !
6122 if (ns->prefix == NULL)
6123 snprintf((char *) prefix, sizeof(prefix), "default");
6125 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
6127 def = xmlSearchNs(doc, tree, prefix);
6128 while (def != NULL) {
6129 if (counter > 1000) return(NULL);
6130 if (ns->prefix == NULL)
6131 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
6133 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
6134 (char *)ns->prefix, counter++);
6135 def = xmlSearchNs(doc, tree, prefix);
6139 * OK, now we are ready to create a new one.
6141 def = xmlNewNs(tree, ns->href, prefix);
6145 #ifdef LIBXML_TREE_ENABLED
6147 * xmlReconciliateNs:
6148 * @doc: the document
6149 * @tree: a node defining the subtree to reconciliate
6151 * This function checks that all the namespaces declared within the given
6152 * tree are properly declared. This is needed for example after Copy or Cut
6153 * and then paste operations. The subtree may still hold pointers to
6154 * namespace declarations outside the subtree or invalid/masked. As much
6155 * as possible the function try to reuse the existing namespaces found in
6156 * the new environment. If not possible the new namespaces are redeclared
6157 * on @tree at the top of the given subtree.
6158 * Returns the number of namespace declarations created or -1 in case of error.
6161 xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
6162 xmlNsPtr *oldNs = NULL;
6163 xmlNsPtr *newNs = NULL;
6168 xmlNodePtr node = tree;
6172 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6173 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
6174 if (node->doc != doc) return(-1);
6175 while (node != NULL) {
6177 * Reconciliate the node namespace
6179 if (node->ns != NULL) {
6181 * initialize the cache if needed
6183 if (sizeCache == 0) {
6185 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6187 if (oldNs == NULL) {
6188 xmlTreeErrMemory("fixing namespaces");
6191 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6193 if (newNs == NULL) {
6194 xmlTreeErrMemory("fixing namespaces");
6199 for (i = 0;i < nbCache;i++) {
6200 if (oldNs[i] == node->ns) {
6201 node->ns = newNs[i];
6207 * OK we need to recreate a new namespace definition
6209 n = xmlNewReconciliedNs(doc, tree, node->ns);
6210 if (n != NULL) { /* :-( what if else ??? */
6212 * check if we need to grow the cache buffers.
6214 if (sizeCache <= nbCache) {
6216 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
6218 if (oldNs == NULL) {
6219 xmlTreeErrMemory("fixing namespaces");
6223 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
6225 if (newNs == NULL) {
6226 xmlTreeErrMemory("fixing namespaces");
6232 oldNs[nbCache++] = node->ns;
6238 * now check for namespace hold by attributes on the node.
6240 if (node->type == XML_ELEMENT_NODE) {
6241 attr = node->properties;
6242 while (attr != NULL) {
6243 if (attr->ns != NULL) {
6245 * initialize the cache if needed
6247 if (sizeCache == 0) {
6249 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6251 if (oldNs == NULL) {
6252 xmlTreeErrMemory("fixing namespaces");
6255 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6257 if (newNs == NULL) {
6258 xmlTreeErrMemory("fixing namespaces");
6263 for (i = 0;i < nbCache;i++) {
6264 if (oldNs[i] == attr->ns) {
6265 attr->ns = newNs[i];
6271 * OK we need to recreate a new namespace definition
6273 n = xmlNewReconciliedNs(doc, tree, attr->ns);
6274 if (n != NULL) { /* :-( what if else ??? */
6276 * check if we need to grow the cache buffers.
6278 if (sizeCache <= nbCache) {
6280 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
6281 sizeCache * sizeof(xmlNsPtr));
6282 if (oldNs == NULL) {
6283 xmlTreeErrMemory("fixing namespaces");
6287 newNs = (xmlNsPtr *) xmlRealloc(newNs,
6288 sizeCache * sizeof(xmlNsPtr));
6289 if (newNs == NULL) {
6290 xmlTreeErrMemory("fixing namespaces");
6296 oldNs[nbCache++] = attr->ns;
6306 * Browse the full subtree, deep first
6308 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
6310 node = node->children;
6311 } else if ((node != tree) && (node->next != NULL)) {
6314 } else if (node != tree) {
6315 /* go up to parents->next if needed */
6316 while (node != tree) {
6317 if (node->parent != NULL)
6318 node = node->parent;
6319 if ((node != tree) && (node->next != NULL)) {
6323 if (node->parent == NULL) {
6328 /* exit condition */
6340 #endif /* LIBXML_TREE_ENABLED */
6343 xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name,
6344 const xmlChar *nsName, int useDTD)
6348 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6351 if (node->properties != NULL) {
6352 prop = node->properties;
6353 if (nsName == NULL) {
6355 * We want the attr to be in no namespace.
6358 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6362 } while (prop != NULL);
6365 * We want the attr to be in the specified namespace.
6368 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6369 ((prop->ns->href == nsName) ||
6370 xmlStrEqual(prop->ns->href, nsName)))
6375 } while (prop != NULL);
6379 #ifdef LIBXML_TREE_ENABLED
6383 * Check if there is a default/fixed attribute declaration in
6384 * the internal or external subset.
6386 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6387 xmlDocPtr doc = node->doc;
6388 xmlAttributePtr attrDecl = NULL;
6389 xmlChar *elemQName, *tmpstr = NULL;
6392 * We need the QName of the element for the DTD-lookup.
6394 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6395 tmpstr = xmlStrdup(node->ns->prefix);
6396 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6397 tmpstr = xmlStrcat(tmpstr, node->name);
6402 elemQName = (xmlChar *) node->name;
6403 if (nsName == NULL) {
6405 * The common and nice case: Attr in no namespace.
6407 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6408 elemQName, name, NULL);
6409 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6410 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6411 elemQName, name, NULL);
6414 xmlNsPtr *nsList, *cur;
6417 * The ugly case: Search using the prefixes of in-scope
6418 * ns-decls corresponding to @nsName.
6420 nsList = xmlGetNsList(node->doc, node);
6421 if (nsList == NULL) {
6427 while (*cur != NULL) {
6428 if (xmlStrEqual((*cur)->href, nsName)) {
6429 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6430 name, (*cur)->prefix);
6433 if (doc->extSubset != NULL) {
6434 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6435 name, (*cur)->prefix);
6447 * Only default/fixed attrs are relevant.
6449 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6450 return((xmlAttrPtr) attrDecl);
6452 #endif /* LIBXML_TREE_ENABLED */
6457 xmlGetPropNodeValueInternal(xmlAttrPtr prop)
6461 if (prop->type == XML_ATTRIBUTE_NODE) {
6463 * Note that we return at least the empty string.
6464 * TODO: Do we really always want that?
6466 if (prop->children != NULL) {
6467 if ((prop->children->next == NULL) &&
6468 ((prop->children->type == XML_TEXT_NODE) ||
6469 (prop->children->type == XML_CDATA_SECTION_NODE)))
6472 * Optimization for the common case: only 1 text node.
6474 return(xmlStrdup(prop->children->content));
6478 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6483 return(xmlStrdup((xmlChar *)""));
6484 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6485 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6493 * @name: the attribute name
6495 * Search an attribute associated to a node
6496 * This function also looks in DTD attribute declaration for #FIXED or
6497 * default declaration values unless DTD use has been turned off.
6499 * Returns the attribute or the attribute declaration or NULL if
6500 * neither was found.
6503 xmlHasProp(xmlNodePtr node, const xmlChar *name) {
6507 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6510 * Check on the properties attached to the node
6512 prop = node->properties;
6513 while (prop != NULL) {
6514 if (xmlStrEqual(prop->name, name)) {
6519 if (!xmlCheckDTD) return(NULL);
6522 * Check if there is a default declaration in the internal
6523 * or external subsets
6527 xmlAttributePtr attrDecl;
6528 if (doc->intSubset != NULL) {
6529 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6530 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6531 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6532 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6533 /* return attribute declaration only if a default value is given
6534 (that includes #FIXED declarations) */
6535 return((xmlAttrPtr) attrDecl);
6544 * @name: the attribute name
6545 * @nameSpace: the URI of the namespace
6547 * Search for an attribute associated to a node
6548 * This attribute has to be anchored in the namespace specified.
6549 * This does the entity substitution.
6550 * This function looks in DTD attribute declaration for #FIXED or
6551 * default declaration values unless DTD use has been turned off.
6552 * Note that a namespace of NULL indicates to use the default namespace.
6554 * Returns the attribute or the attribute declaration or NULL
6555 * if neither was found.
6558 xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
6560 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
6566 * @name: the attribute name
6568 * Search and get the value of an attribute associated to a node
6569 * This does the entity substitution.
6570 * This function looks in DTD attribute declaration for #FIXED or
6571 * default declaration values unless DTD use has been turned off.
6572 * NOTE: this function acts independently of namespaces associated
6573 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6574 * for namespace aware processing.
6576 * Returns the attribute value or NULL if not found.
6577 * It's up to the caller to free the memory with xmlFree().
6580 xmlGetProp(xmlNodePtr node, const xmlChar *name) {
6583 prop = xmlHasProp(node, name);
6586 return(xmlGetPropNodeValueInternal(prop));
6592 * @name: the attribute name
6594 * Search and get the value of an attribute associated to a node
6595 * This does the entity substitution.
6596 * This function looks in DTD attribute declaration for #FIXED or
6597 * default declaration values unless DTD use has been turned off.
6598 * This function is similar to xmlGetProp except it will accept only
6599 * an attribute in no namespace.
6601 * Returns the attribute value or NULL if not found.
6602 * It's up to the caller to free the memory with xmlFree().
6605 xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
6608 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6611 return(xmlGetPropNodeValueInternal(prop));
6617 * @name: the attribute name
6618 * @nameSpace: the URI of the namespace
6620 * Search and get the value of an attribute associated to a node
6621 * This attribute has to be anchored in the namespace specified.
6622 * This does the entity substitution.
6623 * This function looks in DTD attribute declaration for #FIXED or
6624 * default declaration values unless DTD use has been turned off.
6626 * Returns the attribute value or NULL if not found.
6627 * It's up to the caller to free the memory with xmlFree().
6630 xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
6633 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6636 return(xmlGetPropNodeValueInternal(prop));
6639 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6643 * @name: the attribute name
6645 * Remove an attribute carried by a node.
6646 * This handles only attributes in no namespace.
6647 * Returns 0 if successful, -1 if not found
6650 xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
6653 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6656 xmlUnlinkNode((xmlNodePtr) prop);
6664 * @ns: the namespace definition
6665 * @name: the attribute name
6667 * Remove an attribute carried by a node.
6668 * Returns 0 if successful, -1 if not found
6671 xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
6674 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6677 xmlUnlinkNode((xmlNodePtr) prop);
6683 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
6687 * @name: the attribute name (a QName)
6688 * @value: the attribute value
6690 * Set (or reset) an attribute carried by a node.
6691 * If @name has a prefix, then the corresponding
6692 * namespace-binding will be used, if in scope; it is an
6693 * error it there's no such ns-binding for the prefix in
6695 * Returns the attribute pointer.
6699 xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
6701 const xmlChar *nqname;
6703 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
6709 nqname = xmlSplitQName3(name, &len);
6710 if (nqname != NULL) {
6712 xmlChar *prefix = xmlStrndup(name, len);
6713 ns = xmlSearchNs(node->doc, node, prefix);
6717 return(xmlSetNsProp(node, ns, nqname, value));
6719 return(xmlSetNsProp(node, NULL, name, value));
6725 * @ns: the namespace definition
6726 * @name: the attribute name
6727 * @value: the attribute value
6729 * Set (or reset) an attribute carried by a node.
6730 * The ns structure must be in scope, this is not checked
6732 * Returns the attribute pointer.
6735 xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6736 const xmlChar *value)
6740 if (ns && (ns->href == NULL))
6742 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6745 * Modify the attribute's value.
6747 if (prop->atype == XML_ATTRIBUTE_ID) {
6748 xmlRemoveID(node->doc, prop);
6749 prop->atype = XML_ATTRIBUTE_ID;
6751 if (prop->children != NULL)
6752 xmlFreeNodeList(prop->children);
6753 prop->children = NULL;
6756 if (value != NULL) {
6759 if(!xmlCheckUTF8(value)) {
6760 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc,
6762 if (node->doc != NULL)
6763 node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
6765 prop->children = xmlNewDocText(node->doc, value);
6767 tmp = prop->children;
6768 while (tmp != NULL) {
6769 tmp->parent = (xmlNodePtr) prop;
6770 if (tmp->next == NULL)
6775 if (prop->atype == XML_ATTRIBUTE_ID)
6776 xmlAddID(NULL, node->doc, value, prop);
6780 * No equal attr found; create a new one.
6782 return(xmlNewPropInternal(node, ns, name, value, 0));
6785 #endif /* LIBXML_TREE_ENABLED */
6791 * Is this node a Text node ?
6792 * Returns 1 yes, 0 no
6795 xmlNodeIsText(xmlNodePtr node) {
6796 if (node == NULL) return(0);
6798 if (node->type == XML_TEXT_NODE) return(1);
6806 * Checks whether this node is an empty or whitespace only
6807 * (and possibly ignorable) text-node.
6809 * Returns 1 yes, 0 no
6812 xmlIsBlankNode(xmlNodePtr node) {
6814 if (node == NULL) return(0);
6816 if ((node->type != XML_TEXT_NODE) &&
6817 (node->type != XML_CDATA_SECTION_NODE))
6819 if (node->content == NULL) return(1);
6820 cur = node->content;
6822 if (!IS_BLANK_CH(*cur)) return(0);
6832 * @content: the content
6833 * @len: @content length
6835 * Concat the given string at the end of the existing node content
6837 * Returns -1 in case of error, 0 otherwise
6841 xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
6842 if (node == NULL) return(-1);
6844 if ((node->type != XML_TEXT_NODE) &&
6845 (node->type != XML_CDATA_SECTION_NODE) &&
6846 (node->type != XML_COMMENT_NODE) &&
6847 (node->type != XML_PI_NODE)) {
6849 xmlGenericError(xmlGenericErrorContext,
6850 "xmlTextConcat: node is not text nor CDATA\n");
6854 /* need to check if content is currently in the dictionary */
6855 if ((node->content == (xmlChar *) &(node->properties)) ||
6856 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6857 xmlDictOwns(node->doc->dict, node->content))) {
6858 node->content = xmlStrncatNew(node->content, content, len);
6860 node->content = xmlStrncat(node->content, content, len);
6862 node->properties = NULL;
6863 if (node->content == NULL)
6868 /************************************************************************
6870 * Output : to a FILE or in memory *
6872 ************************************************************************/
6877 * routine to create an XML buffer.
6878 * returns the new structure.
6881 xmlBufferCreate(void) {
6884 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6886 xmlTreeErrMemory("creating buffer");
6890 ret->size = xmlDefaultBufferSize;
6891 ret->alloc = xmlBufferAllocScheme;
6892 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
6893 if (ret->content == NULL) {
6894 xmlTreeErrMemory("creating buffer");
6898 ret->content[0] = 0;
6899 ret->contentIO = NULL;
6904 * xmlBufferCreateSize:
6905 * @size: initial size of buffer
6907 * routine to create an XML buffer.
6908 * returns the new structure.
6911 xmlBufferCreateSize(size_t size) {
6914 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6916 xmlTreeErrMemory("creating buffer");
6920 ret->alloc = xmlBufferAllocScheme;
6921 ret->size = (size ? size+2 : 0); /* +1 for ending null */
6923 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
6924 if (ret->content == NULL) {
6925 xmlTreeErrMemory("creating buffer");
6929 ret->content[0] = 0;
6931 ret->content = NULL;
6932 ret->contentIO = NULL;
6940 * Remove the string contained in a buffer and gie it back to the
6941 * caller. The buffer is reset to an empty content.
6942 * This doesn't work with immutable buffers as they can't be reset.
6944 * Returns the previous string contained by the buffer.
6947 xmlBufferDetach(xmlBufferPtr buf) {
6952 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
6956 buf->content = NULL;
6965 * xmlBufferCreateStatic:
6966 * @mem: the memory area
6967 * @size: the size in byte
6969 * routine to create an XML buffer from an immutable memory area.
6970 * The area won't be modified nor copied, and is expected to be
6971 * present until the end of the buffer lifetime.
6973 * returns the new structure.
6976 xmlBufferCreateStatic(void *mem, size_t size) {
6979 if ((mem == NULL) || (size == 0))
6982 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6984 xmlTreeErrMemory("creating buffer");
6989 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
6990 ret->content = (xmlChar *) mem;
6995 * xmlBufferSetAllocationScheme:
6996 * @buf: the buffer to tune
6997 * @scheme: allocation scheme to use
6999 * Sets the allocation scheme for this buffer
7002 xmlBufferSetAllocationScheme(xmlBufferPtr buf,
7003 xmlBufferAllocationScheme scheme) {
7006 xmlGenericError(xmlGenericErrorContext,
7007 "xmlBufferSetAllocationScheme: buf == NULL\n");
7011 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7012 (buf->alloc == XML_BUFFER_ALLOC_IO)) return;
7013 if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
7014 (scheme == XML_BUFFER_ALLOC_EXACT) ||
7015 (scheme == XML_BUFFER_ALLOC_HYBRID) ||
7016 (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
7017 buf->alloc = scheme;
7022 * @buf: the buffer to free
7024 * Frees an XML buffer. It frees both the content and the structure which
7028 xmlBufferFree(xmlBufferPtr buf) {
7031 xmlGenericError(xmlGenericErrorContext,
7032 "xmlBufferFree: buf == NULL\n");
7037 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7038 (buf->contentIO != NULL)) {
7039 xmlFree(buf->contentIO);
7040 } else if ((buf->content != NULL) &&
7041 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
7042 xmlFree(buf->content);
7054 xmlBufferEmpty(xmlBufferPtr buf) {
7055 if (buf == NULL) return;
7056 if (buf->content == NULL) return;
7058 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
7059 buf->content = BAD_CAST "";
7060 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7061 (buf->contentIO != NULL)) {
7062 size_t start_buf = buf->content - buf->contentIO;
7064 buf->size += start_buf;
7065 buf->content = buf->contentIO;
7066 buf->content[0] = 0;
7068 buf->content[0] = 0;
7074 * @buf: the buffer to dump
7075 * @len: the number of xmlChar to remove
7077 * Remove the beginning of an XML buffer.
7079 * Returns the number of #xmlChar removed, or -1 in case of failure.
7082 xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
7083 if (buf == NULL) return(-1);
7084 if (len == 0) return(0);
7085 if (len > buf->use) return(-1);
7088 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7089 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
7091 * we just move the content pointer, but also make sure
7092 * the perceived buffer size has shrinked accordingly
7094 buf->content += len;
7098 * sometimes though it maybe be better to really shrink
7101 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7102 size_t start_buf = buf->content - buf->contentIO;
7103 if (start_buf >= buf->size) {
7104 memmove(buf->contentIO, &buf->content[0], buf->use);
7105 buf->content = buf->contentIO;
7106 buf->content[buf->use] = 0;
7107 buf->size += start_buf;
7111 memmove(buf->content, &buf->content[len], buf->use);
7112 buf->content[buf->use] = 0;
7120 * @len: the minimum free size to allocate
7122 * Grow the available space of an XML buffer.
7124 * Returns the new available space or -1 in case of error
7127 xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
7131 if (buf == NULL) return(-1);
7133 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7134 if (len + buf->use < buf->size) return(0);
7137 * Windows has a BIG problem on realloc timing, so we try to double
7138 * the buffer size (if that's enough) (bug 146697)
7139 * Apparently BSD too, and it's probably best for linux too
7140 * On an embedded system this may be something to change
7143 if (buf->size > len)
7144 size = buf->size * 2;
7146 size = buf->use + len + 100;
7148 size = buf->use + len + 100;
7151 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7152 size_t start_buf = buf->content - buf->contentIO;
7154 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
7155 if (newbuf == NULL) {
7156 xmlTreeErrMemory("growing buffer");
7159 buf->contentIO = newbuf;
7160 buf->content = newbuf + start_buf;
7162 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
7163 if (newbuf == NULL) {
7164 xmlTreeErrMemory("growing buffer");
7167 buf->content = newbuf;
7170 return(buf->size - buf->use);
7175 * @file: the file output
7176 * @buf: the buffer to dump
7178 * Dumps an XML buffer to a FILE *.
7179 * Returns the number of #xmlChar written
7182 xmlBufferDump(FILE *file, xmlBufferPtr buf) {
7187 xmlGenericError(xmlGenericErrorContext,
7188 "xmlBufferDump: buf == NULL\n");
7192 if (buf->content == NULL) {
7194 xmlGenericError(xmlGenericErrorContext,
7195 "xmlBufferDump: buf->content == NULL\n");
7201 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
7209 * Function to extract the content of a buffer
7211 * Returns the internal content
7215 xmlBufferContent(const xmlBufferPtr buf)
7220 return buf->content;
7227 * Function to get the length of a buffer
7229 * Returns the length of data in the internal content
7233 xmlBufferLength(const xmlBufferPtr buf)
7243 * @buf: the buffer to resize
7244 * @size: the desired size
7246 * Resize a buffer to accommodate minimum size of @size.
7248 * Returns 0 in case of problems, 1 otherwise
7251 xmlBufferResize(xmlBufferPtr buf, unsigned int size)
7253 unsigned int newSize;
7254 xmlChar* rebuf = NULL;
7260 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7262 /* Don't resize if we don't have to */
7263 if (size < buf->size)
7266 /* figure out new size */
7267 switch (buf->alloc){
7268 case XML_BUFFER_ALLOC_IO:
7269 case XML_BUFFER_ALLOC_DOUBLEIT:
7270 /*take care of empty case*/
7271 newSize = (buf->size ? buf->size*2 : size + 10);
7272 while (size > newSize) {
7273 if (newSize > UINT_MAX / 2) {
7274 xmlTreeErrMemory("growing buffer");
7280 case XML_BUFFER_ALLOC_EXACT:
7283 case XML_BUFFER_ALLOC_HYBRID:
7284 if (buf->use < BASE_BUFFER_SIZE)
7287 newSize = buf->size * 2;
7288 while (size > newSize) {
7289 if (newSize > UINT_MAX / 2) {
7290 xmlTreeErrMemory("growing buffer");
7303 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7304 start_buf = buf->content - buf->contentIO;
7306 if (start_buf > newSize) {
7307 /* move data back to start */
7308 memmove(buf->contentIO, buf->content, buf->use);
7309 buf->content = buf->contentIO;
7310 buf->content[buf->use] = 0;
7311 buf->size += start_buf;
7313 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
7314 if (rebuf == NULL) {
7315 xmlTreeErrMemory("growing buffer");
7318 buf->contentIO = rebuf;
7319 buf->content = rebuf + start_buf;
7322 if (buf->content == NULL) {
7323 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7324 } else if (buf->size - buf->use < 100) {
7325 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
7328 * if we are reallocating a buffer far from being full, it's
7329 * better to make a new allocation and copy only the used range
7330 * and free the old one.
7332 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7333 if (rebuf != NULL) {
7334 memcpy(rebuf, buf->content, buf->use);
7335 xmlFree(buf->content);
7336 rebuf[buf->use] = 0;
7339 if (rebuf == NULL) {
7340 xmlTreeErrMemory("growing buffer");
7343 buf->content = rebuf;
7345 buf->size = newSize;
7352 * @buf: the buffer to dump
7353 * @str: the #xmlChar string
7354 * @len: the number of #xmlChar to add
7356 * Add a string range to an XML buffer. if len == -1, the length of
7357 * str is recomputed.
7359 * Returns 0 successful, a positive error code number otherwise
7360 * and -1 in case of internal or API error.
7363 xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
7364 unsigned int needSize;
7366 if ((str == NULL) || (buf == NULL)) {
7369 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7372 xmlGenericError(xmlGenericErrorContext,
7373 "xmlBufferAdd: len < 0\n");
7377 if (len == 0) return 0;
7380 len = xmlStrlen(str);
7382 if (len < 0) return -1;
7383 if (len == 0) return 0;
7385 needSize = buf->use + len + 2;
7386 if (needSize > buf->size){
7387 if (!xmlBufferResize(buf, needSize)){
7388 xmlTreeErrMemory("growing buffer");
7389 return XML_ERR_NO_MEMORY;
7393 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
7395 buf->content[buf->use] = 0;
7402 * @str: the #xmlChar string
7403 * @len: the number of #xmlChar to add
7405 * Add a string range to the beginning of an XML buffer.
7406 * if len == -1, the length of @str is recomputed.
7408 * Returns 0 successful, a positive error code number otherwise
7409 * and -1 in case of internal or API error.
7412 xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7413 unsigned int needSize;
7417 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7420 xmlGenericError(xmlGenericErrorContext,
7421 "xmlBufferAddHead: str == NULL\n");
7427 xmlGenericError(xmlGenericErrorContext,
7428 "xmlBufferAddHead: len < 0\n");
7432 if (len == 0) return 0;
7435 len = xmlStrlen(str);
7437 if (len <= 0) return -1;
7439 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7440 size_t start_buf = buf->content - buf->contentIO;
7442 if (start_buf > (unsigned int) len) {
7444 * We can add it in the space previously shrinked
7446 buf->content -= len;
7447 memmove(&buf->content[0], str, len);
7453 needSize = buf->use + len + 2;
7454 if (needSize > buf->size){
7455 if (!xmlBufferResize(buf, needSize)){
7456 xmlTreeErrMemory("growing buffer");
7457 return XML_ERR_NO_MEMORY;
7461 memmove(&buf->content[len], &buf->content[0], buf->use);
7462 memmove(&buf->content[0], str, len);
7464 buf->content[buf->use] = 0;
7470 * @buf: the buffer to add to
7471 * @str: the #xmlChar string
7473 * Append a zero terminated string to an XML buffer.
7475 * Returns 0 successful, a positive error code number otherwise
7476 * and -1 in case of internal or API error.
7479 xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
7482 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7483 if (str == NULL) return -1;
7484 return xmlBufferAdd(buf, str, -1);
7489 * @buf: the buffer to dump
7490 * @str: the C char string
7492 * Append a zero terminated C string to an XML buffer.
7494 * Returns 0 successful, a positive error code number otherwise
7495 * and -1 in case of internal or API error.
7498 xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7503 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7506 xmlGenericError(xmlGenericErrorContext,
7507 "xmlBufferCCat: str == NULL\n");
7511 for (cur = str;*cur != 0;cur++) {
7512 if (buf->use + 10 >= buf->size) {
7513 if (!xmlBufferResize(buf, buf->use+10)){
7514 xmlTreeErrMemory("growing buffer");
7515 return XML_ERR_NO_MEMORY;
7518 buf->content[buf->use++] = *cur;
7520 buf->content[buf->use] = 0;
7525 * xmlBufferWriteCHAR:
7526 * @buf: the XML buffer
7527 * @string: the string to add
7529 * routine which manages and grows an output buffer. This one adds
7530 * xmlChars at the end of the buffer.
7533 xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
7536 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7537 xmlBufferCat(buf, string);
7541 * xmlBufferWriteChar:
7542 * @buf: the XML buffer output
7543 * @string: the string to add
7545 * routine which manage and grows an output buffer. This one add
7546 * C chars at the end of the array.
7549 xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
7552 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7553 xmlBufferCCat(buf, string);
7558 * xmlBufferWriteQuotedString:
7559 * @buf: the XML buffer output
7560 * @string: the string to add
7562 * routine which manage and grows an output buffer. This one writes
7563 * a quoted or double quoted #xmlChar string, checking first if it holds
7564 * quote or double-quotes internally
7567 xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
7568 const xmlChar *cur, *base;
7571 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7572 if (xmlStrchr(string, '\"')) {
7573 if (xmlStrchr(string, '\'')) {
7575 xmlGenericError(xmlGenericErrorContext,
7576 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7578 xmlBufferCCat(buf, "\"");
7579 base = cur = string;
7583 xmlBufferAdd(buf, base, cur - base);
7584 xmlBufferAdd(buf, BAD_CAST """, 6);
7593 xmlBufferAdd(buf, base, cur - base);
7594 xmlBufferCCat(buf, "\"");
7597 xmlBufferCCat(buf, "\'");
7598 xmlBufferCat(buf, string);
7599 xmlBufferCCat(buf, "\'");
7602 xmlBufferCCat(buf, "\"");
7603 xmlBufferCat(buf, string);
7604 xmlBufferCCat(buf, "\"");
7610 * xmlGetDocCompressMode:
7611 * @doc: the document
7613 * get the compression ratio for a document, ZLIB based
7614 * Returns 0 (uncompressed) to 9 (max compression)
7617 xmlGetDocCompressMode (xmlDocPtr doc) {
7618 if (doc == NULL) return(-1);
7619 return(doc->compression);
7623 * xmlSetDocCompressMode:
7624 * @doc: the document
7625 * @mode: the compression ratio
7627 * set the compression ratio for a document, ZLIB based
7628 * Correct values: 0 (uncompressed) to 9 (max compression)
7631 xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7632 if (doc == NULL) return;
7633 if (mode < 0) doc->compression = 0;
7634 else if (mode > 9) doc->compression = 9;
7635 else doc->compression = mode;
7639 * xmlGetCompressMode:
7641 * get the default compression mode used, ZLIB based.
7642 * Returns 0 (uncompressed) to 9 (max compression)
7645 xmlGetCompressMode(void)
7647 return (xmlCompressMode);
7651 * xmlSetCompressMode:
7652 * @mode: the compression ratio
7654 * set the default compression mode used, ZLIB based
7655 * Correct values: 0 (uncompressed) to 9 (max compression)
7658 xmlSetCompressMode(int mode) {
7659 if (mode < 0) xmlCompressMode = 0;
7660 else if (mode > 9) xmlCompressMode = 9;
7661 else xmlCompressMode = mode;
7664 #define XML_TREE_NSMAP_PARENT -1
7665 #define XML_TREE_NSMAP_XML -2
7666 #define XML_TREE_NSMAP_DOC -3
7667 #define XML_TREE_NSMAP_CUSTOM -4
7669 typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7670 struct xmlNsMapItem {
7671 xmlNsMapItemPtr next;
7672 xmlNsMapItemPtr prev;
7673 xmlNsPtr oldNs; /* old ns decl reference */
7674 xmlNsPtr newNs; /* new ns decl reference */
7675 int shadowDepth; /* Shadowed at this depth */
7678 * >= 0 == @node's ns-decls
7679 * -1 == @parent's ns-decls
7680 * -2 == the doc->oldNs XML ns-decl
7681 * -3 == the doc->oldNs storage ns-decls
7682 * -4 == ns-decls provided via custom ns-handling
7687 typedef struct xmlNsMap *xmlNsMapPtr;
7689 xmlNsMapItemPtr first;
7690 xmlNsMapItemPtr last;
7691 xmlNsMapItemPtr pool;
7694 #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7695 #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7696 #define XML_NSMAP_POP(m, i) \
7698 (m)->last = (i)->prev; \
7699 if ((m)->last == NULL) \
7700 (m)->first = NULL; \
7702 (m)->last->next = NULL; \
7703 (i)->next = (m)->pool; \
7707 * xmlDOMWrapNsMapFree:
7713 xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7715 xmlNsMapItemPtr cur, tmp;
7720 while (cur != NULL) {
7726 while (cur != NULL) {
7735 * xmlDOMWrapNsMapAddItem:
7737 * @oldNs: the old ns-struct
7738 * @newNs: the new ns-struct
7739 * @depth: depth and ns-kind information
7741 * Adds an ns-mapping item.
7743 static xmlNsMapItemPtr
7744 xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
7745 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
7747 xmlNsMapItemPtr ret;
7752 if ((position != -1) && (position != 0))
7758 * Create the ns-map.
7760 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7762 xmlTreeErrMemory("allocating namespace map");
7765 memset(map, 0, sizeof(struct xmlNsMap));
7769 if (map->pool != NULL) {
7771 * Reuse an item from the pool.
7774 map->pool = ret->next;
7775 memset(ret, 0, sizeof(struct xmlNsMapItem));
7778 * Create a new item.
7780 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7782 xmlTreeErrMemory("allocating namespace map item");
7785 memset(ret, 0, sizeof(struct xmlNsMapItem));
7788 if (map->first == NULL) {
7794 } else if (position == -1) {
7798 ret->prev = map->last;
7799 map->last->next = ret;
7801 } else if (position == 0) {
7803 * Set on first position.
7805 map->first->prev = ret;
7806 ret->next = map->first;
7813 ret->shadowDepth = -1;
7819 * xmlDOMWrapStoreNs:
7821 * @nsName: the namespace name
7822 * @prefix: the prefix
7824 * Creates or reuses an xmlNs struct on doc->oldNs with
7825 * the given prefix and namespace name.
7827 * Returns the aquired ns struct or NULL in case of an API
7828 * or internal error.
7831 xmlDOMWrapStoreNs(xmlDocPtr doc,
7832 const xmlChar *nsName,
7833 const xmlChar *prefix)
7839 ns = xmlTreeEnsureXMLDecl(doc);
7842 if (ns->next != NULL) {
7845 while (ns != NULL) {
7846 if (((ns->prefix == prefix) ||
7847 xmlStrEqual(ns->prefix, prefix)) &&
7848 xmlStrEqual(ns->href, nsName)) {
7851 if (ns->next == NULL)
7858 ns->next = xmlNewNs(NULL, nsName, prefix);
7865 * xmlDOMWrapNewCtxt:
7867 * Allocates and initializes a new DOM-wrapper context.
7869 * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror.
7872 xmlDOMWrapNewCtxt(void)
7874 xmlDOMWrapCtxtPtr ret;
7876 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
7878 xmlTreeErrMemory("allocating DOM-wrapper context");
7881 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
7886 * xmlDOMWrapFreeCtxt:
7887 * @ctxt: the DOM-wrapper context
7889 * Frees the DOM-wrapper context.
7892 xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
7896 if (ctxt->namespaceMap != NULL)
7897 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
7899 * TODO: Store the namespace map in the context.
7905 * xmlTreeLookupNsListByPrefix:
7906 * @nsList: a list of ns-structs
7907 * @prefix: the searched prefix
7909 * Searches for a ns-decl with the given prefix in @nsList.
7911 * Returns the ns-decl if found, NULL if not found and on
7915 xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
7923 if ((prefix == ns->prefix) ||
7924 xmlStrEqual(prefix, ns->prefix)) {
7928 } while (ns != NULL);
7935 * xmlDOMWrapNSNormGatherInScopeNs:
7936 * @map: the namespace map
7937 * @node: the node to start with
7939 * Puts in-scope namespaces into the ns-map.
7941 * Returns 0 on success, -1 on API or internal errors.
7944 xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
7952 if ((map == NULL) || (*map != NULL))
7955 * Get in-scope ns-decls of @parent.
7958 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
7959 if (cur->type == XML_ELEMENT_NODE) {
7960 if (cur->nsDef != NULL) {
7964 if (XML_NSMAP_NOTEMPTY(*map)) {
7966 * Skip shadowed prefixes.
7968 XML_NSMAP_FOREACH(*map, mi) {
7969 if ((ns->prefix == mi->newNs->prefix) ||
7970 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
7979 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
7980 ns, XML_TREE_NSMAP_PARENT);
7984 mi->shadowDepth = 0;
7986 } while (ns != NULL);
7995 * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
7996 * otherwise copy it, when it was in the source-dict.
7998 #define XML_TREE_ADOPT_STR(str) \
7999 if (adoptStr && (str != NULL)) { \
8000 if (destDoc->dict) { \
8001 const xmlChar *old = str; \
8002 str = xmlDictLookup(destDoc->dict, str, -1); \
8003 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
8004 (!xmlDictOwns(sourceDoc->dict, old))) \
8005 xmlFree((char *)old); \
8006 } else if ((sourceDoc) && (sourceDoc->dict) && \
8007 xmlDictOwns(sourceDoc->dict, str)) { \
8008 str = BAD_CAST xmlStrdup(str); \
8013 * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
8014 * put it in dest-dict or copy it.
8016 #define XML_TREE_ADOPT_STR_2(str) \
8017 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
8018 (sourceDoc->dict != NULL) && \
8019 xmlDictOwns(sourceDoc->dict, cur->content)) { \
8020 if (destDoc->dict) \
8021 cur->content = (xmlChar *) \
8022 xmlDictLookup(destDoc->dict, cur->content, -1); \
8024 cur->content = xmlStrdup(BAD_CAST cur->content); \
8028 * xmlDOMWrapNSNormAddNsMapItem2:
8030 * For internal use. Adds a ns-decl mapping.
8032 * Returns 0 on success, -1 on internal errors.
8035 xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
8036 xmlNsPtr oldNs, xmlNsPtr newNs)
8038 if (*list == NULL) {
8039 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
8040 if (*list == NULL) {
8041 xmlTreeErrMemory("alloc ns map item");
8046 } else if ((*number) >= (*size)) {
8048 *list = (xmlNsPtr *) xmlRealloc(*list,
8049 (*size) * 2 * sizeof(xmlNsPtr));
8050 if (*list == NULL) {
8051 xmlTreeErrMemory("realloc ns map item");
8055 (*list)[2 * (*number)] = oldNs;
8056 (*list)[2 * (*number) +1] = newNs;
8062 * xmlDOMWrapRemoveNode:
8063 * @ctxt: a DOM wrapper context
8065 * @node: the node to be removed.
8066 * @options: set of options, unused at the moment
8068 * Unlinks the given node from its owner.
8069 * This will substitute ns-references to node->nsDef for
8070 * ns-references to doc->oldNs, thus ensuring the removed
8071 * branch to be autark wrt ns-references.
8073 * NOTE: This function was not intensively tested.
8075 * Returns 0 on success, 1 if the node is not supported,
8076 * -1 on API and internal errors.
8079 xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
8080 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
8082 xmlNsPtr *list = NULL;
8083 int sizeList, nbList, i, j;
8086 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
8089 /* TODO: 0 or -1 ? */
8090 if (node->parent == NULL)
8093 switch (node->type) {
8095 case XML_CDATA_SECTION_NODE:
8096 case XML_ENTITY_REF_NODE:
8098 case XML_COMMENT_NODE:
8099 xmlUnlinkNode(node);
8101 case XML_ELEMENT_NODE:
8102 case XML_ATTRIBUTE_NODE:
8107 xmlUnlinkNode(node);
8109 * Save out-of-scope ns-references in doc->oldNs.
8112 switch (node->type) {
8113 case XML_ELEMENT_NODE:
8114 if ((ctxt == NULL) && (node->nsDef != NULL)) {
8117 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8118 &nbList, ns, ns) == -1)
8119 goto internal_error;
8121 } while (ns != NULL);
8123 /* No break on purpose. */
8124 case XML_ATTRIBUTE_NODE:
8125 if (node->ns != NULL) {
8130 for (i = 0, j = 0; i < nbList; i++, j += 2) {
8131 if (node->ns == list[j]) {
8132 node->ns = list[++j];
8144 * Add to doc's oldNs.
8146 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
8149 goto internal_error;
8155 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8156 &nbList, node->ns, ns) == -1)
8157 goto internal_error;
8161 if ((node->type == XML_ELEMENT_NODE) &&
8162 (node->properties != NULL)) {
8163 node = (xmlNodePtr) node->properties;
8171 if ((node->type == XML_ELEMENT_NODE) &&
8172 (node->children != NULL)) {
8173 node = node->children;
8179 if (node->next != NULL)
8182 node = node->parent;
8185 } while (node != NULL);
8198 * xmlSearchNsByNamespaceStrict:
8199 * @doc: the document
8200 * @node: the start node
8201 * @nsName: the searched namespace name
8202 * @retNs: the resulting ns-decl
8203 * @prefixed: if the found ns-decl must have a prefix (for attributes)
8205 * Dynamically searches for a ns-declaration which matches
8206 * the given @nsName in the ancestor-or-self axis of @node.
8208 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8209 * and internal errors.
8212 xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
8213 const xmlChar* nsName,
8214 xmlNsPtr *retNs, int prefixed)
8216 xmlNodePtr cur, prev = NULL, out = NULL;
8217 xmlNsPtr ns, prevns;
8219 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
8223 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
8224 *retNs = xmlTreeEnsureXMLDecl(doc);
8231 if (cur->type == XML_ELEMENT_NODE) {
8232 if (cur->nsDef != NULL) {
8233 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8234 if (prefixed && (ns->prefix == NULL))
8238 * Check the last level of ns-decls for a
8241 prevns = prev->nsDef;
8243 if ((prevns->prefix == ns->prefix) ||
8244 ((prevns->prefix != NULL) &&
8245 (ns->prefix != NULL) &&
8246 xmlStrEqual(prevns->prefix, ns->prefix))) {
8252 prevns = prevns->next;
8253 } while (prevns != NULL);
8258 * Ns-name comparison.
8260 if ((nsName == ns->href) ||
8261 xmlStrEqual(nsName, ns->href)) {
8263 * At this point the prefix can only be shadowed,
8264 * if we are the the (at least) 3rd level of
8270 ret = xmlNsInScope(doc, node, prev, ns->prefix);
8274 * TODO: Should we try to find a matching ns-name
8275 * only once? This here keeps on searching.
8276 * I think we should try further since, there might
8277 * be an other matching ns-decl with an unshadowed
8290 } else if ((cur->type == XML_ENTITY_NODE) ||
8291 (cur->type == XML_ENTITY_DECL))
8294 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8299 * xmlSearchNsByPrefixStrict:
8300 * @doc: the document
8301 * @node: the start node
8302 * @prefix: the searched namespace prefix
8303 * @retNs: the resulting ns-decl
8305 * Dynamically searches for a ns-declaration which matches
8306 * the given @nsName in the ancestor-or-self axis of @node.
8308 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8309 * and internal errors.
8312 xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
8313 const xmlChar* prefix,
8319 if ((doc == NULL) || (node == NULL))
8324 if (IS_STR_XML(prefix)) {
8326 *retNs = xmlTreeEnsureXMLDecl(doc);
8334 if (cur->type == XML_ELEMENT_NODE) {
8335 if (cur->nsDef != NULL) {
8338 if ((prefix == ns->prefix) ||
8339 xmlStrEqual(prefix, ns->prefix))
8342 * Disabled namespaces, e.g. xmlns:abc="".
8344 if (ns->href == NULL)
8351 } while (ns != NULL);
8353 } else if ((cur->type == XML_ENTITY_NODE) ||
8354 (cur->type == XML_ENTITY_DECL))
8357 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8362 * xmlDOMWrapNSNormDeclareNsForced:
8364 * @elem: the element-node to declare on
8365 * @nsName: the namespace-name of the ns-decl
8366 * @prefix: the preferred prefix of the ns-decl
8367 * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
8369 * Declares a new namespace on @elem. It tries to use the
8370 * given @prefix; if a ns-decl with the given prefix is already existent
8371 * on @elem, it will generate an other prefix.
8373 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8374 * and internal errors.
8377 xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
8379 const xmlChar *nsName,
8380 const xmlChar *prefix,
8386 const xmlChar *pref;
8389 * Create a ns-decl on @anchor.
8394 * Lookup whether the prefix is unused in elem's ns-decls.
8396 if ((elem->nsDef != NULL) &&
8397 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
8398 goto ns_next_prefix;
8399 if (checkShadow && elem->parent &&
8400 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8402 * Does it shadow ancestor ns-decls?
8404 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
8405 goto ns_next_prefix;
8407 ret = xmlNewNs(NULL, nsName, pref);
8410 if (elem->nsDef == NULL)
8413 xmlNsPtr ns2 = elem->nsDef;
8414 while (ns2->next != NULL)
8423 if (prefix == NULL) {
8424 snprintf((char *) buf, sizeof(buf),
8427 snprintf((char *) buf, sizeof(buf),
8428 "%.30s_%d", (char *)prefix, counter);
8429 pref = BAD_CAST buf;
8434 * xmlDOMWrapNSNormAquireNormalizedNs:
8436 * @elem: the element-node to declare namespaces on
8437 * @ns: the ns-struct to use for the search
8438 * @retNs: the found/created ns-struct
8439 * @nsMap: the ns-map
8440 * @depth: the current tree depth
8441 * @ancestorsOnly: search in ancestor ns-decls only
8442 * @prefixed: if the searched ns-decl must have a prefix (for attributes)
8444 * Searches for a matching ns-name in the ns-decls of @nsMap, if not
8445 * found it will either declare it on @elem, or store it in doc->oldNs.
8446 * If a new ns-decl needs to be declared on @elem, it tries to use the
8447 * @ns->prefix for it, if this prefix is already in use on @elem, it will
8448 * change the prefix or the new ns-decl.
8450 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8453 xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8465 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
8471 * Handle XML namespace.
8473 if (IS_STR_XML(ns->prefix)) {
8475 * Insert XML namespace mapping.
8477 *retNs = xmlTreeEnsureXMLDecl(doc);
8483 * If the search should be done in ancestors only and no
8484 * @elem (the first ancestor) was specified, then skip the search.
8486 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8487 (! (ancestorsOnly && (elem == NULL))))
8490 * Try to find an equal ns-name in in-scope ns-decls.
8492 XML_NSMAP_FOREACH(*nsMap, mi) {
8493 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8495 * ancestorsOnly: This should be turned on to gain speed,
8496 * if one knows that the branch itself was already
8497 * ns-wellformed and no stale references existed.
8498 * I.e. it searches in the ancestor axis only.
8500 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8501 /* Skip shadowed prefixes. */
8502 (mi->shadowDepth == -1) &&
8503 /* Skip xmlns="" or xmlns:foo="". */
8504 ((mi->newNs->href != NULL) &&
8505 (mi->newNs->href[0] != 0)) &&
8506 /* Ensure a prefix if wanted. */
8507 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8509 ((mi->newNs->href == ns->href) ||
8510 xmlStrEqual(mi->newNs->href, ns->href))) {
8511 /* Set the mapping. */
8519 * No luck, the namespace is out of scope or shadowed.
8525 * Store ns-decls in "oldNs" of the document-node.
8527 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8533 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
8534 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8542 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8547 if (*nsMap != NULL) {
8549 * Does it shadow ancestor ns-decls?
8551 XML_NSMAP_FOREACH(*nsMap, mi) {
8552 if ((mi->depth < depth) &&
8553 (mi->shadowDepth == -1) &&
8554 ((ns->prefix == mi->newNs->prefix) ||
8555 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8559 mi->shadowDepth = depth;
8564 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
8574 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
8575 } xmlDOMReconcileNSOptions;
8578 * xmlDOMWrapReconcileNamespaces:
8579 * @ctxt: DOM wrapper context, unused at the moment
8580 * @elem: the element-node
8581 * @options: option flags
8583 * Ensures that ns-references point to ns-decls hold on element-nodes.
8584 * Ensures that the tree is namespace wellformed by creating additional
8585 * ns-decls where needed. Note that, since prefixes of already existent
8586 * ns-decls can be shadowed by this process, it could break QNames in
8587 * attribute values or element content.
8589 * NOTE: This function was not intensively tested.
8591 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8595 xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8599 int depth = -1, adoptns = 0, parnsdone = 0;
8600 xmlNsPtr ns, prevns;
8602 xmlNodePtr cur, curElem = NULL;
8603 xmlNsMapPtr nsMap = NULL;
8604 xmlNsMapItemPtr /* topmi = NULL, */ mi;
8605 /* @ancestorsOnly should be set by an option flag. */
8606 int ancestorsOnly = 0;
8607 int optRemoveRedundantNS =
8608 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8609 xmlNsPtr *listRedund = NULL;
8610 int sizeRedund = 0, nbRedund = 0, ret, i, j;
8612 if ((elem == NULL) || (elem->doc == NULL) ||
8613 (elem->type != XML_ELEMENT_NODE))
8619 switch (cur->type) {
8620 case XML_ELEMENT_NODE:
8625 * Namespace declarations.
8627 if (cur->nsDef != NULL) {
8630 while (ns != NULL) {
8632 if ((elem->parent) &&
8633 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8635 * Gather ancestor in-scope ns-decls.
8637 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8638 elem->parent) == -1)
8639 goto internal_error;
8645 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8647 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
8648 XML_NSMAP_FOREACH(nsMap, mi) {
8649 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8650 (mi->shadowDepth == -1) &&
8651 ((ns->prefix == mi->newNs->prefix) ||
8652 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8653 ((ns->href == mi->newNs->href) ||
8654 xmlStrEqual(ns->href, mi->newNs->href)))
8657 * A redundant ns-decl was found.
8658 * Add it to the list of redundant ns-decls.
8660 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8661 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8662 goto internal_error;
8664 * Remove the ns-decl from the element-node.
8667 prevns->next = ns->next;
8669 cur->nsDef = ns->next;
8676 * Skip ns-references handling if the referenced
8677 * ns-decl is declared on the same element.
8679 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
8682 * Does it shadow any ns-decl?
8684 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8685 XML_NSMAP_FOREACH(nsMap, mi) {
8686 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8687 (mi->shadowDepth == -1) &&
8688 ((ns->prefix == mi->newNs->prefix) ||
8689 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8691 mi->shadowDepth = depth;
8698 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
8700 goto internal_error;
8709 /* No break on purpose. */
8710 case XML_ATTRIBUTE_NODE:
8711 /* No ns, no fun. */
8712 if (cur->ns == NULL)
8716 if ((elem->parent) &&
8717 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8718 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8719 elem->parent) == -1)
8720 goto internal_error;
8725 * Adjust the reference if this was a redundant ns-decl.
8728 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8729 if (cur->ns == listRedund[j]) {
8730 cur->ns = listRedund[++j];
8736 * Adopt ns-references.
8738 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8740 * Search for a mapping.
8742 XML_NSMAP_FOREACH(nsMap, mi) {
8743 if ((mi->shadowDepth == -1) &&
8744 (cur->ns == mi->oldNs)) {
8746 cur->ns = mi->newNs;
8752 * Aquire a normalized ns-decl and add it to the map.
8754 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8758 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8759 goto internal_error;
8763 if ((cur->type == XML_ELEMENT_NODE) &&
8764 (cur->properties != NULL)) {
8766 * Process attributes.
8768 cur = (xmlNodePtr) cur->properties;
8776 if ((cur->type == XML_ELEMENT_NODE) &&
8777 (cur->children != NULL)) {
8779 * Process content of element-nodes only.
8781 cur = cur->children;
8787 if (cur->type == XML_ELEMENT_NODE) {
8788 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8792 while ((nsMap->last != NULL) &&
8793 (nsMap->last->depth >= depth))
8795 XML_NSMAP_POP(nsMap, mi)
8800 XML_NSMAP_FOREACH(nsMap, mi) {
8801 if (mi->shadowDepth >= depth)
8802 mi->shadowDepth = -1;
8807 if (cur->next != NULL)
8810 if (cur->type == XML_ATTRIBUTE_NODE) {
8817 } while (cur != NULL);
8825 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8826 xmlFreeNs(listRedund[j]);
8828 xmlFree(listRedund);
8831 xmlDOMWrapNsMapFree(nsMap);
8836 * xmlDOMWrapAdoptBranch:
8837 * @ctxt: the optional context for custom processing
8838 * @sourceDoc: the optional sourceDoc
8839 * @node: the element-node to start with
8840 * @destDoc: the destination doc for adoption
8841 * @destParent: the optional new parent of @node in @destDoc
8842 * @options: option flags
8844 * Ensures that ns-references point to @destDoc: either to
8845 * elements->nsDef entries if @destParent is given, or to
8846 * @destDoc->oldNs otherwise.
8847 * If @destParent is given, it ensures that the tree is namespace
8848 * wellformed by creating additional ns-decls where needed.
8849 * Note that, since prefixes of already existent ns-decls can be
8850 * shadowed by this process, it could break QNames in attribute
8851 * values or element content.
8853 * NOTE: This function was not intensively tested.
8855 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8858 xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8859 xmlDocPtr sourceDoc,
8862 xmlNodePtr destParent,
8863 int options ATTRIBUTE_UNUSED)
8866 xmlNodePtr cur, curElem = NULL;
8867 xmlNsMapPtr nsMap = NULL;
8870 int depth = -1, adoptStr = 1;
8871 /* gather @parent's ns-decls. */
8873 /* @ancestorsOnly should be set per option. */
8874 int ancestorsOnly = 0;
8877 * Optimize string adoption for equal or none dicts.
8879 if ((sourceDoc != NULL) &&
8880 (sourceDoc->dict == destDoc->dict))
8886 * Get the ns-map from the context if available.
8889 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8891 * Disable search for ns-decls in the parent-axis of the
8892 * desination element, if:
8893 * 1) there's no destination parent
8894 * 2) custom ns-reference handling is used
8896 if ((destParent == NULL) ||
8897 (ctxt && ctxt->getNsForNodeFunc))
8904 while (cur != NULL) {
8906 * Paranoid source-doc sanity check.
8908 if (cur->doc != sourceDoc) {
8910 * We'll assume XIncluded nodes if the doc differs.
8911 * TODO: Do we need to reconciliate XIncluded nodes?
8912 * This here skips XIncluded nodes and tries to handle
8915 if (cur->next == NULL)
8919 if ((cur->type == XML_XINCLUDE_END) ||
8920 (cur->doc == node->doc))
8922 } while (cur->next != NULL);
8924 if (cur->doc != node->doc)
8928 switch (cur->type) {
8929 case XML_XINCLUDE_START:
8930 case XML_XINCLUDE_END:
8935 case XML_ELEMENT_NODE:
8939 * Namespace declarations.
8940 * - ns->href and ns->prefix are never in the dict, so
8941 * we need not move the values over to the destination dict.
8942 * - Note that for custom handling of ns-references,
8943 * the ns-decls need not be stored in the ns-map,
8944 * since they won't be referenced by node->ns.
8947 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
8951 * Gather @parent's in-scope ns-decls.
8953 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8955 goto internal_error;
8958 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8960 * NOTE: ns->prefix and ns->href are never in the dict.
8961 * XML_TREE_ADOPT_STR(ns->prefix)
8962 * XML_TREE_ADOPT_STR(ns->href)
8965 * Does it shadow any ns-decl?
8967 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8968 XML_NSMAP_FOREACH(nsMap, mi) {
8969 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8970 (mi->shadowDepth == -1) &&
8971 ((ns->prefix == mi->newNs->prefix) ||
8972 xmlStrEqual(ns->prefix,
8973 mi->newNs->prefix))) {
8975 mi->shadowDepth = depth;
8982 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
8983 ns, ns, depth) == NULL)
8984 goto internal_error;
8987 /* No break on purpose. */
8988 case XML_ATTRIBUTE_NODE:
8989 /* No namespace, no fun. */
8990 if (cur->ns == NULL)
8994 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8996 goto internal_error;
9000 * Adopt ns-references.
9002 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9004 * Search for a mapping.
9006 XML_NSMAP_FOREACH(nsMap, mi) {
9007 if ((mi->shadowDepth == -1) &&
9008 (cur->ns == mi->oldNs)) {
9010 cur->ns = mi->newNs;
9016 * No matching namespace in scope. We need a new one.
9018 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
9020 * User-defined behaviour.
9022 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9023 cur->ns->href, cur->ns->prefix);
9025 * Insert mapping if ns is available; it's the users fault
9028 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9029 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9030 goto internal_error;
9034 * Aquire a normalized ns-decl and add it to the map.
9036 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9037 /* ns-decls on curElem or on destDoc->oldNs */
9038 destParent ? curElem : NULL,
9042 /* ns-decls must be prefixed for attributes. */
9043 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9044 goto internal_error;
9049 * Further node properties.
9050 * TODO: Is this all?
9052 XML_TREE_ADOPT_STR(cur->name)
9053 if (cur->type == XML_ELEMENT_NODE) {
9060 if (cur->properties != NULL) {
9062 * Process first attribute node.
9064 cur = (xmlNodePtr) cur->properties;
9071 if ((sourceDoc != NULL) &&
9072 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
9074 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
9076 ((xmlAttrPtr) cur)->atype = 0;
9077 ((xmlAttrPtr) cur)->psvi = NULL;
9081 case XML_CDATA_SECTION_NODE:
9083 * This puts the content in the dest dict, only if
9084 * it was previously in the source dict.
9086 XML_TREE_ADOPT_STR_2(cur->content)
9088 case XML_ENTITY_REF_NODE:
9090 * Remove reference to the entitity-node.
9092 cur->content = NULL;
9093 cur->children = NULL;
9095 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9098 * Assign new entity-node if available.
9100 ent = xmlGetDocEntity(destDoc, cur->name);
9102 cur->content = ent->content;
9103 cur->children = (xmlNodePtr) ent;
9104 cur->last = (xmlNodePtr) ent;
9109 XML_TREE_ADOPT_STR(cur->name)
9110 XML_TREE_ADOPT_STR_2(cur->content)
9112 case XML_COMMENT_NODE:
9115 goto internal_error;
9120 if (cur->children != NULL) {
9121 cur = cur->children;
9128 if ((cur->type == XML_ELEMENT_NODE) ||
9129 (cur->type == XML_XINCLUDE_START) ||
9130 (cur->type == XML_XINCLUDE_END))
9133 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9135 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9139 while ((nsMap->last != NULL) &&
9140 (nsMap->last->depth >= depth))
9142 XML_NSMAP_POP(nsMap, mi)
9147 XML_NSMAP_FOREACH(nsMap, mi) {
9148 if (mi->shadowDepth >= depth)
9149 mi->shadowDepth = -1;
9154 if (cur->next != NULL)
9156 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
9157 (cur->parent->children != NULL))
9159 cur = cur->parent->children;
9175 if (nsMap != NULL) {
9176 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9178 * Just cleanup the map but don't free.
9182 nsMap->last->next = nsMap->pool;
9183 nsMap->pool = nsMap->first;
9184 nsMap->first = NULL;
9187 xmlDOMWrapNsMapFree(nsMap);
9193 * xmlDOMWrapCloneNode:
9194 * @ctxt: the optional context for custom processing
9195 * @sourceDoc: the optional sourceDoc
9196 * @node: the node to start with
9197 * @resNode: the clone of the given @node
9198 * @destDoc: the destination doc
9199 * @destParent: the optional new parent of @node in @destDoc
9200 * @deep: descend into child if set
9201 * @options: option flags
9203 * References of out-of scope ns-decls are remapped to point to @destDoc:
9204 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9205 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
9206 * This is the case when you don't know already where the cloned branch
9209 * If @destParent is given, it ensures that the tree is namespace
9210 * wellformed by creating additional ns-decls where needed.
9211 * Note that, since prefixes of already existent ns-decls can be
9212 * shadowed by this process, it could break QNames in attribute
9213 * values or element content.
9215 * 1) What to do with XInclude? Currently this returns an error for XInclude.
9217 * Returns 0 if the operation succeeded,
9218 * 1 if a node of unsupported (or not yet supported) type was given,
9219 * -1 on API/internal errors.
9223 xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
9224 xmlDocPtr sourceDoc,
9226 xmlNodePtr *resNode,
9228 xmlNodePtr destParent,
9230 int options ATTRIBUTE_UNUSED)
9233 xmlNodePtr cur, curElem = NULL;
9234 xmlNsMapPtr nsMap = NULL;
9238 /* int adoptStr = 1; */
9239 /* gather @parent's ns-decls. */
9243 * TODO: @ancestorsOnly should be set per option.
9246 int ancestorsOnly = 0;
9247 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
9248 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
9249 xmlDictPtr dict; /* The destination dict */
9251 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
9254 * TODO: Initially we support only element-nodes.
9256 if (node->type != XML_ELEMENT_NODE)
9259 * Check node->doc sanity.
9261 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9262 (node->doc != sourceDoc)) {
9264 * Might be an XIncluded node.
9268 if (sourceDoc == NULL)
9269 sourceDoc = node->doc;
9270 if (sourceDoc == NULL)
9273 dict = destDoc->dict;
9275 * Reuse the namespace map of the context.
9278 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9283 while (cur != NULL) {
9284 if (cur->doc != sourceDoc) {
9286 * We'll assume XIncluded nodes if the doc differs.
9287 * TODO: Do we need to reconciliate XIncluded nodes?
9288 * TODO: This here returns -1 in this case.
9290 goto internal_error;
9293 * Create a new node.
9295 switch (cur->type) {
9296 case XML_XINCLUDE_START:
9297 case XML_XINCLUDE_END:
9299 * TODO: What to do with XInclude?
9301 goto internal_error;
9303 case XML_ELEMENT_NODE:
9305 case XML_CDATA_SECTION_NODE:
9306 case XML_COMMENT_NODE:
9308 case XML_DOCUMENT_FRAG_NODE:
9309 case XML_ENTITY_REF_NODE:
9310 case XML_ENTITY_NODE:
9312 * Nodes of xmlNode structure.
9314 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
9315 if (clone == NULL) {
9316 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
9317 goto internal_error;
9319 memset(clone, 0, sizeof(xmlNode));
9321 * Set hierachical links.
9323 if (resultClone != NULL) {
9324 clone->parent = parentClone;
9326 prevClone->next = clone;
9327 clone->prev = prevClone;
9329 parentClone->children = clone;
9331 resultClone = clone;
9334 case XML_ATTRIBUTE_NODE:
9336 * Attributes (xmlAttr).
9338 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
9339 if (clone == NULL) {
9340 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
9341 goto internal_error;
9343 memset(clone, 0, sizeof(xmlAttr));
9345 * Set hierachical links.
9346 * TODO: Change this to add to the end of attributes.
9348 if (resultClone != NULL) {
9349 clone->parent = parentClone;
9351 prevClone->next = clone;
9352 clone->prev = prevClone;
9354 parentClone->properties = (xmlAttrPtr) clone;
9356 resultClone = clone;
9360 * TODO QUESTION: Any other nodes expected?
9362 goto internal_error;
9365 clone->type = cur->type;
9366 clone->doc = destDoc;
9369 * Clone the name of the node if any.
9371 if (cur->name == xmlStringText)
9372 clone->name = xmlStringText;
9373 else if (cur->name == xmlStringTextNoenc)
9375 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9376 * in tree.c, it might be set in Libxslt via
9377 * "xsl:disable-output-escaping".
9379 clone->name = xmlStringTextNoenc;
9380 else if (cur->name == xmlStringComment)
9381 clone->name = xmlStringComment;
9382 else if (cur->name != NULL) {
9383 DICT_CONST_COPY(cur->name, clone->name);
9386 switch (cur->type) {
9387 case XML_XINCLUDE_START:
9388 case XML_XINCLUDE_END:
9393 case XML_ELEMENT_NODE:
9397 * Namespace declarations.
9399 if (cur->nsDef != NULL) {
9401 if (destParent && (ctxt == NULL)) {
9403 * Gather @parent's in-scope ns-decls.
9405 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9407 goto internal_error;
9412 * Clone namespace declarations.
9414 cloneNsDefSlot = &(clone->nsDef);
9415 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9417 * Create a new xmlNs.
9419 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
9420 if (cloneNs == NULL) {
9421 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
9422 "allocating namespace");
9425 memset(cloneNs, 0, sizeof(xmlNs));
9426 cloneNs->type = XML_LOCAL_NAMESPACE;
9428 if (ns->href != NULL)
9429 cloneNs->href = xmlStrdup(ns->href);
9430 if (ns->prefix != NULL)
9431 cloneNs->prefix = xmlStrdup(ns->prefix);
9433 *cloneNsDefSlot = cloneNs;
9434 cloneNsDefSlot = &(cloneNs->next);
9437 * Note that for custom handling of ns-references,
9438 * the ns-decls need not be stored in the ns-map,
9439 * since they won't be referenced by node->ns.
9441 if ((ctxt == NULL) ||
9442 (ctxt->getNsForNodeFunc == NULL))
9445 * Does it shadow any ns-decl?
9447 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9448 XML_NSMAP_FOREACH(nsMap, mi) {
9449 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9450 (mi->shadowDepth == -1) &&
9451 ((ns->prefix == mi->newNs->prefix) ||
9452 xmlStrEqual(ns->prefix,
9453 mi->newNs->prefix))) {
9455 * Mark as shadowed at the current
9458 mi->shadowDepth = depth;
9465 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9466 ns, cloneNs, depth) == NULL)
9467 goto internal_error;
9471 /* cur->ns will be processed further down. */
9473 case XML_ATTRIBUTE_NODE:
9474 /* IDs will be processed further down. */
9475 /* cur->ns will be processed further down. */
9478 case XML_CDATA_SECTION_NODE:
9480 * Note that this will also cover the values of attributes.
9482 DICT_COPY(cur->content, clone->content);
9484 case XML_ENTITY_NODE:
9485 /* TODO: What to do here? */
9487 case XML_ENTITY_REF_NODE:
9488 if (sourceDoc != destDoc) {
9489 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9492 * Different doc: Assign new entity-node if available.
9494 ent = xmlGetDocEntity(destDoc, cur->name);
9496 clone->content = ent->content;
9497 clone->children = (xmlNodePtr) ent;
9498 clone->last = (xmlNodePtr) ent;
9503 * Same doc: Use the current node's entity declaration
9506 clone->content = cur->content;
9507 clone->children = cur->children;
9508 clone->last = cur->last;
9512 DICT_COPY(cur->content, clone->content);
9514 case XML_COMMENT_NODE:
9515 DICT_COPY(cur->content, clone->content);
9518 goto internal_error;
9521 if (cur->ns == NULL)
9522 goto end_ns_reference;
9524 /* handle_ns_reference: */
9526 ** The following will take care of references to ns-decls ********
9527 ** and is intended only for element- and attribute-nodes.
9531 if (destParent && (ctxt == NULL)) {
9532 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
9533 goto internal_error;
9538 * Adopt ns-references.
9540 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9542 * Search for a mapping.
9544 XML_NSMAP_FOREACH(nsMap, mi) {
9545 if ((mi->shadowDepth == -1) &&
9546 (cur->ns == mi->oldNs)) {
9548 * This is the nice case: a mapping was found.
9550 clone->ns = mi->newNs;
9551 goto end_ns_reference;
9556 * No matching namespace in scope. We need a new one.
9558 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
9560 * User-defined behaviour.
9562 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9563 cur->ns->href, cur->ns->prefix);
9565 * Add user's mapping.
9567 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9568 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9569 goto internal_error;
9573 * Aquire a normalized ns-decl and add it to the map.
9575 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9576 /* ns-decls on curElem or on destDoc->oldNs */
9577 destParent ? curElem : NULL,
9580 /* if we need to search only in the ancestor-axis */
9582 /* ns-decls must be prefixed for attributes. */
9583 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9584 goto internal_error;
9591 * Some post-processing.
9593 * Handle ID attributes.
9595 if ((clone->type == XML_ATTRIBUTE_NODE) &&
9596 (clone->parent != NULL))
9598 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
9602 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9603 if (idVal != NULL) {
9604 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9605 /* TODO: error message. */
9607 goto internal_error;
9615 ** The following will traverse the tree **************************
9618 * Walk the element's attributes before descending into child-nodes.
9620 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9622 parentClone = clone;
9623 cur = (xmlNodePtr) cur->properties;
9628 * Descend into child-nodes.
9630 if (cur->children != NULL) {
9631 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9633 parentClone = clone;
9634 cur = cur->children;
9641 * At this point we are done with the node, its content
9642 * and an element-nodes's attribute-nodes.
9646 if ((cur->type == XML_ELEMENT_NODE) ||
9647 (cur->type == XML_XINCLUDE_START) ||
9648 (cur->type == XML_XINCLUDE_END)) {
9650 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9652 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9656 while ((nsMap->last != NULL) &&
9657 (nsMap->last->depth >= depth))
9659 XML_NSMAP_POP(nsMap, mi)
9664 XML_NSMAP_FOREACH(nsMap, mi) {
9665 if (mi->shadowDepth >= depth)
9666 mi->shadowDepth = -1;
9671 if (cur->next != NULL) {
9674 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9678 if (clone->parent != NULL)
9679 clone->parent->last = clone;
9680 clone = clone->parent;
9681 parentClone = clone->parent;
9683 * Process parent --> next;
9688 /* This is for attributes only. */
9689 clone = clone->parent;
9690 parentClone = clone->parent;
9692 * Process parent-element --> children.
9707 if (nsMap != NULL) {
9708 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9710 * Just cleanup the map but don't free.
9714 nsMap->last->next = nsMap->pool;
9715 nsMap->pool = nsMap->first;
9716 nsMap->first = NULL;
9719 xmlDOMWrapNsMapFree(nsMap);
9722 * TODO: Should we try a cleanup of the cloned node in case of a
9725 *resNode = resultClone;
9730 * xmlDOMWrapAdoptAttr:
9731 * @ctxt: the optional context for custom processing
9732 * @sourceDoc: the optional source document of attr
9733 * @attr: the attribute-node to be adopted
9734 * @destDoc: the destination doc for adoption
9735 * @destParent: the optional new parent of @attr in @destDoc
9736 * @options: option flags
9738 * @attr is adopted by @destDoc.
9739 * Ensures that ns-references point to @destDoc: either to
9740 * elements->nsDef entries if @destParent is given, or to
9741 * @destDoc->oldNs otherwise.
9743 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9746 xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9747 xmlDocPtr sourceDoc,
9750 xmlNodePtr destParent,
9751 int options ATTRIBUTE_UNUSED)
9756 if ((attr == NULL) || (destDoc == NULL))
9759 attr->doc = destDoc;
9760 if (attr->ns != NULL) {
9764 /* TODO: User defined. */
9766 /* XML Namespace. */
9767 if (IS_STR_XML(attr->ns->prefix)) {
9768 ns = xmlTreeEnsureXMLDecl(destDoc);
9769 } else if (destParent == NULL) {
9771 * Store in @destDoc->oldNs.
9773 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9776 * Declare on @destParent.
9778 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
9780 goto internal_error;
9782 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9783 attr->ns->href, attr->ns->prefix, 1);
9787 goto internal_error;
9791 XML_TREE_ADOPT_STR(attr->name);
9797 if (attr->children == NULL)
9799 cur = attr->children;
9800 while (cur != NULL) {
9802 switch (cur->type) {
9804 case XML_CDATA_SECTION_NODE:
9805 XML_TREE_ADOPT_STR_2(cur->content)
9807 case XML_ENTITY_REF_NODE:
9809 * Remove reference to the entitity-node.
9811 cur->content = NULL;
9812 cur->children = NULL;
9814 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9817 * Assign new entity-node if available.
9819 ent = xmlGetDocEntity(destDoc, cur->name);
9821 cur->content = ent->content;
9822 cur->children = (xmlNodePtr) ent;
9823 cur->last = (xmlNodePtr) ent;
9830 if (cur->children != NULL) {
9831 cur = cur->children;
9835 if (cur == (xmlNodePtr) attr)
9837 if (cur->next != NULL)
9850 * xmlDOMWrapAdoptNode:
9851 * @ctxt: the optional context for custom processing
9852 * @sourceDoc: the optional sourceDoc
9853 * @node: the node to start with
9854 * @destDoc: the destination doc
9855 * @destParent: the optional new parent of @node in @destDoc
9856 * @options: option flags
9858 * References of out-of scope ns-decls are remapped to point to @destDoc:
9859 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9860 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
9861 * This is the case when you have an unliked node and just want to move it
9864 * If @destParent is given, it ensures that the tree is namespace
9865 * wellformed by creating additional ns-decls where needed.
9866 * Note that, since prefixes of already existent ns-decls can be
9867 * shadowed by this process, it could break QNames in attribute
9868 * values or element content.
9869 * NOTE: This function was not intensively tested.
9871 * Returns 0 if the operation succeeded,
9872 * 1 if a node of unsupported type was given,
9873 * 2 if a node of not yet supported type was given and
9874 * -1 on API/internal errors.
9877 xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
9878 xmlDocPtr sourceDoc,
9881 xmlNodePtr destParent,
9884 if ((node == NULL) || (destDoc == NULL) ||
9885 ((destParent != NULL) && (destParent->doc != destDoc)))
9888 * Check node->doc sanity.
9890 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9891 (node->doc != sourceDoc)) {
9893 * Might be an XIncluded node.
9897 if (sourceDoc == NULL)
9898 sourceDoc = node->doc;
9899 if (sourceDoc == destDoc)
9901 switch (node->type) {
9902 case XML_ELEMENT_NODE:
9903 case XML_ATTRIBUTE_NODE:
9905 case XML_CDATA_SECTION_NODE:
9906 case XML_ENTITY_REF_NODE:
9908 case XML_COMMENT_NODE:
9910 case XML_DOCUMENT_FRAG_NODE:
9911 /* TODO: Support document-fragment-nodes. */
9917 * Unlink only if @node was not already added to @destParent.
9919 if ((node->parent != NULL) && (destParent != node->parent))
9920 xmlUnlinkNode(node);
9922 if (node->type == XML_ELEMENT_NODE) {
9923 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
9924 destDoc, destParent, options));
9925 } else if (node->type == XML_ATTRIBUTE_NODE) {
9926 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
9927 (xmlAttrPtr) node, destDoc, destParent, options));
9929 xmlNodePtr cur = node;
9934 * Optimize string adoption.
9936 if ((sourceDoc != NULL) &&
9937 (sourceDoc->dict == destDoc->dict))
9939 switch (node->type) {
9941 case XML_CDATA_SECTION_NODE:
9942 XML_TREE_ADOPT_STR_2(node->content)
9944 case XML_ENTITY_REF_NODE:
9946 * Remove reference to the entitity-node.
9948 node->content = NULL;
9949 node->children = NULL;
9951 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9954 * Assign new entity-node if available.
9956 ent = xmlGetDocEntity(destDoc, node->name);
9958 node->content = ent->content;
9959 node->children = (xmlNodePtr) ent;
9960 node->last = (xmlNodePtr) ent;
9963 XML_TREE_ADOPT_STR(node->name)
9966 XML_TREE_ADOPT_STR(node->name)
9967 XML_TREE_ADOPT_STR_2(node->content)
9978 #include "elfgcchack.h"