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>
47 int __xmlRegisterCallbacks = 0;
49 /************************************************************************
51 * Forward declarations *
53 ************************************************************************/
56 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
58 static xmlChar* xmlGetPropNodeValueInternal(const xmlAttr *prop);
60 /************************************************************************
62 * Tree memory error handler *
64 ************************************************************************/
67 * @extra: extra informations
69 * Handle an out of memory condition
72 xmlTreeErrMemory(const char *extra)
74 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
79 * @code: the error number
80 * @extra: extra informations
82 * Handle an out of memory condition
85 xmlTreeErr(int code, xmlNodePtr node, const char *extra)
87 const char *msg = NULL;
90 case XML_TREE_INVALID_HEX:
91 msg = "invalid hexadecimal character value\n";
93 case XML_TREE_INVALID_DEC:
94 msg = "invalid decimal character value\n";
96 case XML_TREE_UNTERMINATED_ENTITY:
97 msg = "unterminated entity reference %15s\n";
99 case XML_TREE_NOT_UTF8:
100 msg = "string is not in UTF-8\n";
103 msg = "unexpected error number\n";
105 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
108 /************************************************************************
110 * A few static variables and macros *
112 ************************************************************************/
113 /* #undef xmlStringText */
114 const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
115 /* #undef xmlStringTextNoenc */
116 const xmlChar xmlStringTextNoenc[] =
117 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
118 /* #undef xmlStringComment */
119 const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
121 static int xmlCompressMode = 0;
122 static int xmlCheckDTD = 1;
124 #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
125 xmlNodePtr ulccur = (n)->children; \
126 if (ulccur == NULL) { \
129 while (ulccur->next != NULL) { \
130 ulccur->parent = (n); \
131 ulccur = ulccur->next; \
133 ulccur->parent = (n); \
134 (n)->last = ulccur; \
137 #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
138 (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
140 /* #define DEBUG_BUFFER */
141 /* #define DEBUG_TREE */
143 /************************************************************************
145 * Functions to move to entities.c once the *
146 * API freeze is smoothen and they can be made public. *
148 ************************************************************************/
149 #include <libxml/hash.h>
151 #ifdef LIBXML_TREE_ENABLED
153 * xmlGetEntityFromDtd:
154 * @dtd: A pointer to the DTD to search
155 * @name: The entity name
157 * Do an entity lookup in the DTD entity hash table and
158 * return the corresponding entity, if found.
160 * Returns A pointer to the entity structure or NULL if not found.
163 xmlGetEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
164 xmlEntitiesTablePtr table;
166 if((dtd != NULL) && (dtd->entities != NULL)) {
167 table = (xmlEntitiesTablePtr) dtd->entities;
168 return((xmlEntityPtr) xmlHashLookup(table, name));
169 /* return(xmlGetEntityFromTable(table, name)); */
174 * xmlGetParameterEntityFromDtd:
175 * @dtd: A pointer to the DTD to search
176 * @name: The entity name
178 * Do an entity lookup in the DTD pararmeter entity hash table and
179 * return the corresponding entity, if found.
181 * Returns A pointer to the entity structure or NULL if not found.
184 xmlGetParameterEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
185 xmlEntitiesTablePtr table;
187 if ((dtd != NULL) && (dtd->pentities != NULL)) {
188 table = (xmlEntitiesTablePtr) dtd->pentities;
189 return((xmlEntityPtr) xmlHashLookup(table, name));
190 /* return(xmlGetEntityFromTable(table, name)); */
194 #endif /* LIBXML_TREE_ENABLED */
196 /************************************************************************
198 * QName handling helper *
200 ************************************************************************/
205 * @prefix: the prefix
206 * @memory: preallocated memory
207 * @len: preallocated memory length
209 * Builds the QName @prefix:@ncname in @memory if there is enough space
210 * and prefix is not NULL nor empty, otherwise allocate a new string.
211 * If prefix is NULL or empty it returns ncname.
213 * Returns the new string which must be freed by the caller if different from
214 * @memory and @ncname or NULL in case of error
217 xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
218 xmlChar *memory, int len) {
222 if (ncname == NULL) return(NULL);
223 if (prefix == NULL) return((xmlChar *) ncname);
225 lenn = strlen((char *) ncname);
226 lenp = strlen((char *) prefix);
228 if ((memory == NULL) || (len < lenn + lenp + 2)) {
229 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
231 xmlTreeErrMemory("building QName");
237 memcpy(&ret[0], prefix, lenp);
239 memcpy(&ret[lenp + 1], ncname, lenn);
240 ret[lenn + lenp + 1] = 0;
246 * @name: the full QName
247 * @prefix: a xmlChar **
249 * parse an XML qualified name string
251 * [NS 5] QName ::= (Prefix ':')? LocalPart
253 * [NS 6] Prefix ::= NCName
255 * [NS 7] LocalPart ::= NCName
257 * Returns NULL if not a QName, otherwise the local part, and prefix
258 * is updated to get the Prefix if any.
262 xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
266 if (prefix == NULL) return(NULL);
268 if (name == NULL) return(NULL);
270 #ifndef XML_XML_NAMESPACE
271 /* xml: prefix is not really a namespace */
272 if ((name[0] == 'x') && (name[1] == 'm') &&
273 (name[2] == 'l') && (name[3] == ':'))
277 /* nasty but valid */
282 * we are not trying to validate but just to cut, and yes it will
283 * work even if this is as set of UTF-8 encoded chars
285 while ((name[len] != 0) && (name[len] != ':'))
291 *prefix = xmlStrndup(name, len);
292 if (*prefix == NULL) {
293 xmlTreeErrMemory("QName split");
296 ret = xmlStrdup(&name[len + 1]);
298 xmlTreeErrMemory("QName split");
299 if (*prefix != NULL) {
311 * @name: the full QName
314 * parse an XML qualified name string,i
316 * returns NULL if it is not a Qualified Name, otherwise, update len
317 * with the length in byte of the prefix and return a pointer
318 * to the start of the name without the prefix
322 xmlSplitQName3(const xmlChar *name, int *len) {
325 if (name == NULL) return(NULL);
326 if (len == NULL) return(NULL);
328 /* nasty but valid */
333 * we are not trying to validate but just to cut, and yes it will
334 * work even if this is as set of UTF-8 encoded chars
336 while ((name[l] != 0) && (name[l] != ':'))
347 /************************************************************************
349 * Check Name, NCName and QName strings *
351 ************************************************************************/
353 #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
355 #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) || defined(LIBXML_LEGACY_ENABLED)
358 * @value: the value to check
359 * @space: allow spaces in front and end of the string
361 * Check that a value conforms to the lexical space of NCName
363 * Returns 0 if this validates, a positive error code number otherwise
364 * and -1 in case of internal or API error.
367 xmlValidateNCName(const xmlChar *value, int space) {
368 const xmlChar *cur = value;
375 * First quick algorithm for ASCII range
378 while (IS_BLANK_CH(*cur)) cur++;
379 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
384 while (((*cur >= 'a') && (*cur <= 'z')) ||
385 ((*cur >= 'A') && (*cur <= 'Z')) ||
386 ((*cur >= '0') && (*cur <= '9')) ||
387 (*cur == '_') || (*cur == '-') || (*cur == '.'))
390 while (IS_BLANK_CH(*cur)) cur++;
396 * Second check for chars outside the ASCII range
399 c = CUR_SCHAR(cur, l);
401 while (IS_BLANK(c)) {
403 c = CUR_SCHAR(cur, l);
406 if ((!IS_LETTER(c)) && (c != '_'))
409 c = CUR_SCHAR(cur, l);
410 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
411 (c == '-') || (c == '_') || IS_COMBINING(c) ||
414 c = CUR_SCHAR(cur, l);
417 while (IS_BLANK(c)) {
419 c = CUR_SCHAR(cur, l);
429 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
432 * @value: the value to check
433 * @space: allow spaces in front and end of the string
435 * Check that a value conforms to the lexical space of QName
437 * Returns 0 if this validates, a positive error code number otherwise
438 * and -1 in case of internal or API error.
441 xmlValidateQName(const xmlChar *value, int space) {
442 const xmlChar *cur = value;
448 * First quick algorithm for ASCII range
451 while (IS_BLANK_CH(*cur)) cur++;
452 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
457 while (((*cur >= 'a') && (*cur <= 'z')) ||
458 ((*cur >= 'A') && (*cur <= 'Z')) ||
459 ((*cur >= '0') && (*cur <= '9')) ||
460 (*cur == '_') || (*cur == '-') || (*cur == '.'))
464 if (((*cur >= 'a') && (*cur <= 'z')) ||
465 ((*cur >= 'A') && (*cur <= 'Z')) ||
470 while (((*cur >= 'a') && (*cur <= 'z')) ||
471 ((*cur >= 'A') && (*cur <= 'Z')) ||
472 ((*cur >= '0') && (*cur <= '9')) ||
473 (*cur == '_') || (*cur == '-') || (*cur == '.'))
477 while (IS_BLANK_CH(*cur)) cur++;
483 * Second check for chars outside the ASCII range
486 c = CUR_SCHAR(cur, l);
488 while (IS_BLANK(c)) {
490 c = CUR_SCHAR(cur, l);
493 if ((!IS_LETTER(c)) && (c != '_'))
496 c = CUR_SCHAR(cur, l);
497 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
498 (c == '-') || (c == '_') || IS_COMBINING(c) ||
501 c = CUR_SCHAR(cur, l);
505 c = CUR_SCHAR(cur, l);
506 if ((!IS_LETTER(c)) && (c != '_'))
509 c = CUR_SCHAR(cur, l);
510 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
511 (c == '-') || (c == '_') || IS_COMBINING(c) ||
514 c = CUR_SCHAR(cur, l);
518 while (IS_BLANK(c)) {
520 c = CUR_SCHAR(cur, l);
530 * @value: the value to check
531 * @space: allow spaces in front and end of the string
533 * Check that a value conforms to the lexical space of Name
535 * Returns 0 if this validates, a positive error code number otherwise
536 * and -1 in case of internal or API error.
539 xmlValidateName(const xmlChar *value, int space) {
540 const xmlChar *cur = value;
546 * First quick algorithm for ASCII range
549 while (IS_BLANK_CH(*cur)) cur++;
550 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
551 (*cur == '_') || (*cur == ':'))
555 while (((*cur >= 'a') && (*cur <= 'z')) ||
556 ((*cur >= 'A') && (*cur <= 'Z')) ||
557 ((*cur >= '0') && (*cur <= '9')) ||
558 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
561 while (IS_BLANK_CH(*cur)) cur++;
567 * Second check for chars outside the ASCII range
570 c = CUR_SCHAR(cur, l);
572 while (IS_BLANK(c)) {
574 c = CUR_SCHAR(cur, l);
577 if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
580 c = CUR_SCHAR(cur, l);
581 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
582 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
584 c = CUR_SCHAR(cur, l);
587 while (IS_BLANK(c)) {
589 c = CUR_SCHAR(cur, l);
598 * xmlValidateNMToken:
599 * @value: the value to check
600 * @space: allow spaces in front and end of the string
602 * Check that a value conforms to the lexical space of NMToken
604 * Returns 0 if this validates, a positive error code number otherwise
605 * and -1 in case of internal or API error.
608 xmlValidateNMToken(const xmlChar *value, int space) {
609 const xmlChar *cur = value;
615 * First quick algorithm for ASCII range
618 while (IS_BLANK_CH(*cur)) cur++;
619 if (((*cur >= 'a') && (*cur <= 'z')) ||
620 ((*cur >= 'A') && (*cur <= 'Z')) ||
621 ((*cur >= '0') && (*cur <= '9')) ||
622 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
626 while (((*cur >= 'a') && (*cur <= 'z')) ||
627 ((*cur >= 'A') && (*cur <= 'Z')) ||
628 ((*cur >= '0') && (*cur <= '9')) ||
629 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
632 while (IS_BLANK_CH(*cur)) cur++;
638 * Second check for chars outside the ASCII range
641 c = CUR_SCHAR(cur, l);
643 while (IS_BLANK(c)) {
645 c = CUR_SCHAR(cur, l);
648 if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
649 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
652 c = CUR_SCHAR(cur, l);
653 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
654 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
656 c = CUR_SCHAR(cur, l);
659 while (IS_BLANK(c)) {
661 c = CUR_SCHAR(cur, l);
668 #endif /* LIBXML_TREE_ENABLED */
670 /************************************************************************
672 * Allocation and deallocation of basic structures *
674 ************************************************************************/
677 * xmlSetBufferAllocationScheme:
678 * @scheme: allocation method to use
680 * Set the buffer allocation method. Types are
681 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
682 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
683 * improves performance
686 xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
687 if ((scheme == XML_BUFFER_ALLOC_EXACT) ||
688 (scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
689 (scheme == XML_BUFFER_ALLOC_HYBRID))
690 xmlBufferAllocScheme = scheme;
694 * xmlGetBufferAllocationScheme:
697 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
698 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
699 * improves performance
700 * XML_BUFFER_ALLOC_HYBRID - use exact sizes on small strings to keep memory usage tight
701 * in normal usage, and doubleit on large strings to avoid
702 * pathological performance.
704 * Returns the current allocation scheme
706 xmlBufferAllocationScheme
707 xmlGetBufferAllocationScheme(void) {
708 return(xmlBufferAllocScheme);
713 * @node: the element carrying the namespace
714 * @href: the URI associated
715 * @prefix: the prefix for the namespace
717 * Creation of a new Namespace. This function will refuse to create
718 * a namespace with a similar prefix than an existing one present on this
720 * Note that for a default namespace, @prefix should be NULL.
722 * We use href==NULL in the case of an element creation where the namespace
725 * Returns a new namespace pointer or NULL
728 xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
731 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
734 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
735 /* xml namespace is predefined, no need to add it */
736 if (xmlStrEqual(href, XML_XML_NAMESPACE))
740 * Problem, this is an attempt to bind xml prefix to a wrong
741 * namespace, which breaks
742 * Namespace constraint: Reserved Prefixes and Namespace Names
743 * from XML namespace. But documents authors may not care in
744 * their context so let's proceed.
749 * Allocate a new Namespace and fill the fields.
751 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
753 xmlTreeErrMemory("building namespace");
756 memset(cur, 0, sizeof(xmlNs));
757 cur->type = XML_LOCAL_NAMESPACE;
760 cur->href = xmlStrdup(href);
762 cur->prefix = xmlStrdup(prefix);
765 * Add it at the end to preserve parsing order ...
766 * and checks for existing use of the prefix
769 if (node->nsDef == NULL) {
772 xmlNsPtr prev = node->nsDef;
774 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
775 (xmlStrEqual(prev->prefix, cur->prefix))) {
779 while (prev->next != NULL) {
781 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
782 (xmlStrEqual(prev->prefix, cur->prefix))) {
795 * @node: a node in the document
796 * @ns: a namespace pointer
798 * Associate a namespace to a node, a posteriori.
801 xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
804 xmlGenericError(xmlGenericErrorContext,
805 "xmlSetNs: node == NULL\n");
809 if ((node->type == XML_ELEMENT_NODE) ||
810 (node->type == XML_ATTRIBUTE_NODE))
816 * @cur: the namespace pointer
818 * Free up the structures associated to a namespace
821 xmlFreeNs(xmlNsPtr cur) {
824 xmlGenericError(xmlGenericErrorContext,
825 "xmlFreeNs : ns == NULL\n");
829 if (cur->href != NULL) xmlFree((char *) cur->href);
830 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
836 * @cur: the first namespace pointer
838 * Free up all the structures associated to the chained namespaces.
841 xmlFreeNsList(xmlNsPtr cur) {
845 xmlGenericError(xmlGenericErrorContext,
846 "xmlFreeNsList : ns == NULL\n");
850 while (cur != NULL) {
859 * @doc: the document pointer
860 * @name: the DTD name
861 * @ExternalID: the external ID
862 * @SystemID: the system ID
864 * Creation of a new DTD for the external subset. To create an
865 * internal subset, use xmlCreateIntSubset().
867 * Returns a pointer to the new DTD structure
870 xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
871 const xmlChar *ExternalID, const xmlChar *SystemID) {
874 if ((doc != NULL) && (doc->extSubset != NULL)) {
876 xmlGenericError(xmlGenericErrorContext,
877 "xmlNewDtd(%s): document %s already have a DTD %s\n",
878 /* !!! */ (char *) name, doc->name,
879 /* !!! */ (char *)doc->extSubset->name);
885 * Allocate a new DTD and fill the fields.
887 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
889 xmlTreeErrMemory("building DTD");
892 memset(cur, 0 , sizeof(xmlDtd));
893 cur->type = XML_DTD_NODE;
896 cur->name = xmlStrdup(name);
897 if (ExternalID != NULL)
898 cur->ExternalID = xmlStrdup(ExternalID);
899 if (SystemID != NULL)
900 cur->SystemID = xmlStrdup(SystemID);
902 doc->extSubset = cur;
905 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
906 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
912 * @doc: the document pointer
914 * Get the internal subset of a document
915 * Returns a pointer to the DTD structure or NULL if not found
919 xmlGetIntSubset(const xmlDoc *doc) {
925 while (cur != NULL) {
926 if (cur->type == XML_DTD_NODE)
927 return((xmlDtdPtr) cur);
930 return((xmlDtdPtr) doc->intSubset);
934 * xmlCreateIntSubset:
935 * @doc: the document pointer
936 * @name: the DTD name
937 * @ExternalID: the external (PUBLIC) ID
938 * @SystemID: the system ID
940 * Create the internal subset of a document
941 * Returns a pointer to the new DTD structure
944 xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
945 const xmlChar *ExternalID, const xmlChar *SystemID) {
948 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
950 xmlGenericError(xmlGenericErrorContext,
952 "xmlCreateIntSubset(): document %s already have an internal subset\n",
959 * Allocate a new DTD and fill the fields.
961 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
963 xmlTreeErrMemory("building internal subset");
966 memset(cur, 0, sizeof(xmlDtd));
967 cur->type = XML_DTD_NODE;
970 cur->name = xmlStrdup(name);
971 if (cur->name == NULL) {
972 xmlTreeErrMemory("building internal subset");
977 if (ExternalID != NULL) {
978 cur->ExternalID = xmlStrdup(ExternalID);
979 if (cur->ExternalID == NULL) {
980 xmlTreeErrMemory("building internal subset");
981 if (cur->name != NULL)
982 xmlFree((char *)cur->name);
987 if (SystemID != NULL) {
988 cur->SystemID = xmlStrdup(SystemID);
989 if (cur->SystemID == NULL) {
990 xmlTreeErrMemory("building internal subset");
991 if (cur->name != NULL)
992 xmlFree((char *)cur->name);
993 if (cur->ExternalID != NULL)
994 xmlFree((char *)cur->ExternalID);
1000 doc->intSubset = cur;
1003 if (doc->children == NULL) {
1004 doc->children = (xmlNodePtr) cur;
1005 doc->last = (xmlNodePtr) cur;
1007 if (doc->type == XML_HTML_DOCUMENT_NODE) {
1010 prev = doc->children;
1011 prev->prev = (xmlNodePtr) cur;
1013 doc->children = (xmlNodePtr) cur;
1017 next = doc->children;
1018 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
1021 cur->prev = doc->last;
1022 cur->prev->next = (xmlNodePtr) cur;
1024 doc->last = (xmlNodePtr) cur;
1027 cur->prev = next->prev;
1028 if (cur->prev == NULL)
1029 doc->children = (xmlNodePtr) cur;
1031 cur->prev->next = (xmlNodePtr) cur;
1032 next->prev = (xmlNodePtr) cur;
1038 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1039 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1047 * Free a string if it is not owned by the "dict" dictionnary in the
1050 #define DICT_FREE(str) \
1051 if ((str) && ((!dict) || \
1052 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1053 xmlFree((char *)(str));
1060 * Copy a string using a "dict" dictionnary in the current scope,
1063 #define DICT_COPY(str, cpy) \
1066 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1067 cpy = (xmlChar *) (str); \
1069 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1071 cpy = xmlStrdup((const xmlChar *)(str)); }
1077 * Copy a string using a "dict" dictionnary in the current scope,
1080 #define DICT_CONST_COPY(str, cpy) \
1083 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1084 cpy = (const xmlChar *) (str); \
1086 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1088 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1093 * @cur: the DTD structure to free up
1095 * Free a DTD structure.
1098 xmlFreeDtd(xmlDtdPtr cur) {
1099 xmlDictPtr dict = NULL;
1104 if (cur->doc != NULL) dict = cur->doc->dict;
1106 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1107 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1109 if (cur->children != NULL) {
1110 xmlNodePtr next, c = cur->children;
1113 * Cleanup all nodes which are not part of the specific lists
1114 * of notations, elements, attributes and entities.
1118 if ((c->type != XML_NOTATION_NODE) &&
1119 (c->type != XML_ELEMENT_DECL) &&
1120 (c->type != XML_ATTRIBUTE_DECL) &&
1121 (c->type != XML_ENTITY_DECL)) {
1128 DICT_FREE(cur->name)
1129 DICT_FREE(cur->SystemID)
1130 DICT_FREE(cur->ExternalID)
1132 if (cur->notations != NULL)
1133 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
1135 if (cur->elements != NULL)
1136 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1137 if (cur->attributes != NULL)
1138 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1139 if (cur->entities != NULL)
1140 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1141 if (cur->pentities != NULL)
1142 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1149 * @version: xmlChar string giving the version of XML "1.0"
1151 * Creates a new XML document
1153 * Returns a new document
1156 xmlNewDoc(const xmlChar *version) {
1159 if (version == NULL)
1160 version = (const xmlChar *) "1.0";
1163 * Allocate a new document and fill the fields.
1165 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1167 xmlTreeErrMemory("building doc");
1170 memset(cur, 0, sizeof(xmlDoc));
1171 cur->type = XML_DOCUMENT_NODE;
1173 cur->version = xmlStrdup(version);
1174 if (cur->version == NULL) {
1175 xmlTreeErrMemory("building doc");
1179 cur->standalone = -1;
1180 cur->compression = -1; /* not initialized */
1182 cur->parseFlags = 0;
1183 cur->properties = XML_DOC_USERBUILT;
1185 * The in memory encoding is always UTF8
1186 * This field will never change and would
1187 * be obsolete if not for binary compatibility.
1189 cur->charset = XML_CHAR_ENCODING_UTF8;
1191 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1192 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1198 * @cur: pointer to the document
1200 * Free up all the structures used by a document, tree included.
1203 xmlFreeDoc(xmlDocPtr cur) {
1204 xmlDtdPtr extSubset, intSubset;
1205 xmlDictPtr dict = NULL;
1209 xmlGenericError(xmlGenericErrorContext,
1210 "xmlFreeDoc : document == NULL\n");
1214 #ifdef LIBXML_DEBUG_RUNTIME
1215 #ifdef LIBXML_DEBUG_ENABLED
1216 xmlDebugCheckDocument(stderr, cur);
1220 if (cur != NULL) dict = cur->dict;
1222 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1223 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1226 * Do this before freeing the children list to avoid ID lookups
1228 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1230 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1232 extSubset = cur->extSubset;
1233 intSubset = cur->intSubset;
1234 if (intSubset == extSubset)
1236 if (extSubset != NULL) {
1237 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
1238 cur->extSubset = NULL;
1239 xmlFreeDtd(extSubset);
1241 if (intSubset != NULL) {
1242 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
1243 cur->intSubset = NULL;
1244 xmlFreeDtd(intSubset);
1247 if (cur->children != NULL) xmlFreeNodeList(cur->children);
1248 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
1250 DICT_FREE(cur->version)
1251 DICT_FREE(cur->name)
1252 DICT_FREE(cur->encoding)
1255 if (dict) xmlDictFree(dict);
1259 * xmlStringLenGetNodeList:
1260 * @doc: the document
1261 * @value: the value of the text
1262 * @len: the length of the string value
1264 * Parse the value string and build the node list associated. Should
1265 * produce a flat tree with only TEXTs and ENTITY_REFs.
1266 * Returns a pointer to the first child
1269 xmlStringLenGetNodeList(const xmlDoc *doc, const xmlChar *value, int len) {
1270 xmlNodePtr ret = NULL, last = NULL;
1273 const xmlChar *cur = value, *end = cur + len;
1278 if (value == NULL) return(NULL);
1280 buf = xmlBufCreateSize(0);
1281 if (buf == NULL) return(NULL);
1282 xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID);
1285 while ((cur < end) && (*cur != 0)) {
1286 if (cur[0] == '&') {
1291 * Save the current text.
1294 if (xmlBufAdd(buf, q, cur - q))
1298 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1304 while (tmp != ';') { /* Non input consuming loop */
1305 if ((tmp >= '0') && (tmp <= '9'))
1306 charval = charval * 16 + (tmp - '0');
1307 else if ((tmp >= 'a') && (tmp <= 'f'))
1308 charval = charval * 16 + (tmp - 'a') + 10;
1309 else if ((tmp >= 'A') && (tmp <= 'F'))
1310 charval = charval * 16 + (tmp - 'A') + 10;
1312 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1326 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1332 while (tmp != ';') { /* Non input consuming loops */
1333 if ((tmp >= '0') && (tmp <= '9'))
1334 charval = charval * 10 + (tmp - '0');
1336 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1352 * Read the entity string
1356 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1357 if ((cur >= end) || (*cur == 0)) {
1358 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1364 * Predefined entities don't generate nodes
1366 val = xmlStrndup(q, cur - q);
1367 ent = xmlGetDocEntity(doc, val);
1368 if ((ent != NULL) &&
1369 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1371 if (xmlBufCat(buf, ent->content))
1376 * Flush buffer so far
1378 if (!xmlBufIsEmpty(buf)) {
1379 node = xmlNewDocText(doc, NULL);
1381 if (val != NULL) xmlFree(val);
1384 node->content = xmlBufDetach(buf);
1389 last = xmlAddNextSibling(last, node);
1394 * Create a new REFERENCE_REF node
1396 node = xmlNewReference(doc, val);
1398 if (val != NULL) xmlFree(val);
1401 else if ((ent != NULL) && (ent->children == NULL)) {
1404 ent->children = xmlStringGetNodeList(doc,
1405 (const xmlChar*)node->content);
1407 temp = ent->children;
1409 temp->parent = (xmlNodePtr)ent;
1417 last = xmlAddNextSibling(last, node);
1429 l = xmlCopyCharMultiByte(buffer, charval);
1432 if (xmlBufCat(buf, buffer))
1442 * Handle the last piece of text.
1444 if (xmlBufAdd(buf, q, cur - q))
1448 if (!xmlBufIsEmpty(buf)) {
1449 node = xmlNewDocText(doc, NULL);
1450 if (node == NULL) goto out;
1451 node->content = xmlBufDetach(buf);
1456 last = xmlAddNextSibling(last, node);
1458 } else if (ret == NULL) {
1459 ret = xmlNewDocText(doc, BAD_CAST "");
1468 * xmlStringGetNodeList:
1469 * @doc: the document
1470 * @value: the value of the attribute
1472 * Parse the value string and build the node list associated. Should
1473 * produce a flat tree with only TEXTs and ENTITY_REFs.
1474 * Returns a pointer to the first child
1477 xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) {
1478 xmlNodePtr ret = NULL, last = NULL;
1481 const xmlChar *cur = value;
1486 if (value == NULL) return(NULL);
1488 buf = xmlBufCreateSize(0);
1489 if (buf == NULL) return(NULL);
1490 xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID);
1494 if (cur[0] == '&') {
1499 * Save the current text.
1502 if (xmlBufAdd(buf, q, cur - q))
1506 if ((cur[1] == '#') && (cur[2] == 'x')) {
1509 while (tmp != ';') { /* Non input consuming loop */
1510 if ((tmp >= '0') && (tmp <= '9'))
1511 charval = charval * 16 + (tmp - '0');
1512 else if ((tmp >= 'a') && (tmp <= 'f'))
1513 charval = charval * 16 + (tmp - 'a') + 10;
1514 else if ((tmp >= 'A') && (tmp <= 'F'))
1515 charval = charval * 16 + (tmp - 'A') + 10;
1517 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1528 } else if (cur[1] == '#') {
1531 while (tmp != ';') { /* Non input consuming loops */
1532 if ((tmp >= '0') && (tmp <= '9'))
1533 charval = charval * 10 + (tmp - '0');
1535 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1548 * Read the entity string
1552 while ((*cur != 0) && (*cur != ';')) cur++;
1554 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1555 (xmlNodePtr) doc, (const char *) q);
1560 * Predefined entities don't generate nodes
1562 val = xmlStrndup(q, cur - q);
1563 ent = xmlGetDocEntity(doc, val);
1564 if ((ent != NULL) &&
1565 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1567 if (xmlBufCat(buf, ent->content))
1572 * Flush buffer so far
1574 if (!xmlBufIsEmpty(buf)) {
1575 node = xmlNewDocText(doc, NULL);
1576 node->content = xmlBufDetach(buf);
1581 last = xmlAddNextSibling(last, node);
1586 * Create a new REFERENCE_REF node
1588 node = xmlNewReference(doc, val);
1590 if (val != NULL) xmlFree(val);
1593 else if ((ent != NULL) && (ent->children == NULL)) {
1596 ent->children = xmlStringGetNodeList(doc,
1597 (const xmlChar*)node->content);
1599 temp = ent->children;
1601 temp->parent = (xmlNodePtr)ent;
1608 last = xmlAddNextSibling(last, node);
1620 len = xmlCopyCharMultiByte(buffer, charval);
1623 if (xmlBufCat(buf, buffer))
1630 if ((cur != q) || (ret == NULL)) {
1632 * Handle the last piece of text.
1634 xmlBufAdd(buf, q, cur - q);
1637 if (!xmlBufIsEmpty(buf)) {
1638 node = xmlNewDocText(doc, NULL);
1639 node->content = xmlBufDetach(buf);
1644 last = xmlAddNextSibling(last, node);
1654 * xmlNodeListGetString:
1655 * @doc: the document
1656 * @list: a Node list
1657 * @inLine: should we replace entity contents or show their external form
1659 * Build the string equivalent to the text contained in the Node list
1660 * made of TEXTs and ENTITY_REFs
1662 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1665 xmlNodeListGetString(xmlDocPtr doc, const xmlNode *list, int inLine)
1667 const xmlNode *node = list;
1668 xmlChar *ret = NULL;
1674 if ((list->parent != NULL) && (list->parent->type == XML_ATTRIBUTE_NODE))
1679 while (node != NULL) {
1680 if ((node->type == XML_TEXT_NODE) ||
1681 (node->type == XML_CDATA_SECTION_NODE)) {
1683 ret = xmlStrcat(ret, node->content);
1688 buffer = xmlEncodeAttributeEntities(doc, node->content);
1690 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1691 if (buffer != NULL) {
1692 ret = xmlStrcat(ret, buffer);
1696 } else if (node->type == XML_ENTITY_REF_NODE) {
1698 ent = xmlGetDocEntity(doc, node->name);
1702 /* an entity content can be any "well balanced chunk",
1703 * i.e. the result of the content [43] production:
1704 * http://www.w3.org/TR/REC-xml#NT-content.
1705 * So it can contain text, CDATA section or nested
1706 * entity reference nodes (among others).
1707 * -> we recursive call xmlNodeListGetString()
1708 * which handles these types */
1709 buffer = xmlNodeListGetString(doc, ent->children, 1);
1710 if (buffer != NULL) {
1711 ret = xmlStrcat(ret, buffer);
1715 ret = xmlStrcat(ret, node->content);
1722 ret = xmlStrncat(ret, buf, 1);
1723 ret = xmlStrcat(ret, node->name);
1726 ret = xmlStrncat(ret, buf, 1);
1731 xmlGenericError(xmlGenericErrorContext,
1732 "xmlGetNodeListString : invalid node type %d\n",
1741 #ifdef LIBXML_TREE_ENABLED
1743 * xmlNodeListGetRawString:
1744 * @doc: the document
1745 * @list: a Node list
1746 * @inLine: should we replace entity contents or show their external form
1748 * Builds the string equivalent to the text contained in the Node list
1749 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1750 * this function doesn't do any character encoding handling.
1752 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1755 xmlNodeListGetRawString(const xmlDoc *doc, const xmlNode *list, int inLine)
1757 const xmlNode *node = list;
1758 xmlChar *ret = NULL;
1764 while (node != NULL) {
1765 if ((node->type == XML_TEXT_NODE) ||
1766 (node->type == XML_CDATA_SECTION_NODE)) {
1768 ret = xmlStrcat(ret, node->content);
1772 buffer = xmlEncodeSpecialChars(doc, node->content);
1773 if (buffer != NULL) {
1774 ret = xmlStrcat(ret, buffer);
1778 } else if (node->type == XML_ENTITY_REF_NODE) {
1780 ent = xmlGetDocEntity(doc, node->name);
1784 /* an entity content can be any "well balanced chunk",
1785 * i.e. the result of the content [43] production:
1786 * http://www.w3.org/TR/REC-xml#NT-content.
1787 * So it can contain text, CDATA section or nested
1788 * entity reference nodes (among others).
1789 * -> we recursive call xmlNodeListGetRawString()
1790 * which handles these types */
1792 xmlNodeListGetRawString(doc, ent->children, 1);
1793 if (buffer != NULL) {
1794 ret = xmlStrcat(ret, buffer);
1798 ret = xmlStrcat(ret, node->content);
1805 ret = xmlStrncat(ret, buf, 1);
1806 ret = xmlStrcat(ret, node->name);
1809 ret = xmlStrncat(ret, buf, 1);
1814 xmlGenericError(xmlGenericErrorContext,
1815 "xmlGetNodeListString : invalid node type %d\n",
1823 #endif /* LIBXML_TREE_ENABLED */
1826 xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1827 const xmlChar * name, const xmlChar * value,
1831 xmlDocPtr doc = NULL;
1833 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
1834 if ((eatname == 1) &&
1835 ((node->doc == NULL) ||
1836 (!(xmlDictOwns(node->doc->dict, name)))))
1837 xmlFree((xmlChar *) name);
1842 * Allocate a new property and fill the fields.
1844 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1846 if ((eatname == 1) &&
1847 ((node == NULL) || (node->doc == NULL) ||
1848 (!(xmlDictOwns(node->doc->dict, name)))))
1849 xmlFree((xmlChar *) name);
1850 xmlTreeErrMemory("building attribute");
1853 memset(cur, 0, sizeof(xmlAttr));
1854 cur->type = XML_ATTRIBUTE_NODE;
1864 if ((doc != NULL) && (doc->dict != NULL))
1865 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1867 cur->name = xmlStrdup(name);
1871 if (value != NULL) {
1874 if(!xmlCheckUTF8(value)) {
1875 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc,
1878 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1880 cur->children = xmlNewDocText(doc, value);
1882 tmp = cur->children;
1883 while (tmp != NULL) {
1884 tmp->parent = (xmlNodePtr) cur;
1885 if (tmp->next == NULL)
1892 * Add it at the end to preserve parsing order ...
1895 if (node->properties == NULL) {
1896 node->properties = cur;
1898 xmlAttrPtr prev = node->properties;
1900 while (prev->next != NULL)
1907 if ((value != NULL) && (node != NULL) &&
1908 (xmlIsID(node->doc, node, cur) == 1))
1909 xmlAddID(NULL, node->doc, value, cur);
1911 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1912 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1916 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1917 defined(LIBXML_SCHEMAS_ENABLED)
1920 * @node: the holding node
1921 * @name: the name of the attribute
1922 * @value: the value of the attribute
1924 * Create a new property carried by a node.
1925 * Returns a pointer to the attribute
1928 xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1932 xmlGenericError(xmlGenericErrorContext,
1933 "xmlNewProp : name == NULL\n");
1938 return xmlNewPropInternal(node, NULL, name, value, 0);
1940 #endif /* LIBXML_TREE_ENABLED */
1944 * @node: the holding node
1945 * @ns: the namespace
1946 * @name: the name of the attribute
1947 * @value: the value of the attribute
1949 * Create a new property tagged with a namespace and carried by a node.
1950 * Returns a pointer to the attribute
1953 xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1954 const xmlChar *value) {
1958 xmlGenericError(xmlGenericErrorContext,
1959 "xmlNewNsProp : name == NULL\n");
1964 return xmlNewPropInternal(node, ns, name, value, 0);
1968 * xmlNewNsPropEatName:
1969 * @node: the holding node
1970 * @ns: the namespace
1971 * @name: the name of the attribute
1972 * @value: the value of the attribute
1974 * Create a new property tagged with a namespace and carried by a node.
1975 * Returns a pointer to the attribute
1978 xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1979 const xmlChar *value) {
1983 xmlGenericError(xmlGenericErrorContext,
1984 "xmlNewNsPropEatName : name == NULL\n");
1989 return xmlNewPropInternal(node, ns, name, value, 1);
1994 * @doc: the document
1995 * @name: the name of the attribute
1996 * @value: the value of the attribute
1998 * Create a new property carried by a document.
1999 * Returns a pointer to the attribute
2002 xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
2007 xmlGenericError(xmlGenericErrorContext,
2008 "xmlNewDocProp : name == NULL\n");
2014 * Allocate a new property and fill the fields.
2016 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
2018 xmlTreeErrMemory("building attribute");
2021 memset(cur, 0, sizeof(xmlAttr));
2022 cur->type = XML_ATTRIBUTE_NODE;
2024 if ((doc != NULL) && (doc->dict != NULL))
2025 cur->name = xmlDictLookup(doc->dict, name, -1);
2027 cur->name = xmlStrdup(name);
2029 if (value != NULL) {
2032 cur->children = xmlStringGetNodeList(doc, value);
2035 tmp = cur->children;
2036 while (tmp != NULL) {
2037 tmp->parent = (xmlNodePtr) cur;
2038 if (tmp->next == NULL)
2044 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2045 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2051 * @cur: the first property in the list
2053 * Free a property and all its siblings, all the children are freed too.
2056 xmlFreePropList(xmlAttrPtr cur) {
2058 if (cur == NULL) return;
2059 while (cur != NULL) {
2068 * @cur: an attribute
2070 * Free one attribute, all the content is freed too
2073 xmlFreeProp(xmlAttrPtr cur) {
2074 xmlDictPtr dict = NULL;
2075 if (cur == NULL) return;
2077 if (cur->doc != NULL) dict = cur->doc->dict;
2079 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
2080 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2082 /* Check for ID removal -> leading to invalid references ! */
2083 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2084 xmlRemoveID(cur->doc, cur);
2086 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2087 DICT_FREE(cur->name)
2093 * @cur: an attribute
2095 * Unlink and free one attribute, all the content is freed too
2096 * Note this doesn't work for namespace definition attributes
2098 * Returns 0 if success and -1 in case of error.
2101 xmlRemoveProp(xmlAttrPtr cur) {
2105 xmlGenericError(xmlGenericErrorContext,
2106 "xmlRemoveProp : cur == NULL\n");
2110 if (cur->parent == NULL) {
2112 xmlGenericError(xmlGenericErrorContext,
2113 "xmlRemoveProp : cur->parent == NULL\n");
2117 tmp = cur->parent->properties;
2119 cur->parent->properties = cur->next;
2120 if (cur->next != NULL)
2121 cur->next->prev = NULL;
2125 while (tmp != NULL) {
2126 if (tmp->next == cur) {
2127 tmp->next = cur->next;
2128 if (tmp->next != NULL)
2129 tmp->next->prev = tmp;
2136 xmlGenericError(xmlGenericErrorContext,
2137 "xmlRemoveProp : attribute not owned by its node\n");
2144 * @doc: the target document
2145 * @name: the processing instruction name
2146 * @content: the PI content
2148 * Creation of a processing instruction element.
2149 * Returns a pointer to the new node object.
2152 xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
2157 xmlGenericError(xmlGenericErrorContext,
2158 "xmlNewPI : name == NULL\n");
2164 * Allocate a new node and fill the fields.
2166 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2168 xmlTreeErrMemory("building PI");
2171 memset(cur, 0, sizeof(xmlNode));
2172 cur->type = XML_PI_NODE;
2174 if ((doc != NULL) && (doc->dict != NULL))
2175 cur->name = xmlDictLookup(doc->dict, name, -1);
2177 cur->name = xmlStrdup(name);
2178 if (content != NULL) {
2179 cur->content = xmlStrdup(content);
2183 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2184 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2190 * @name: the processing instruction name
2191 * @content: the PI content
2193 * Creation of a processing instruction element.
2194 * Use xmlDocNewPI preferably to get string interning
2196 * Returns a pointer to the new node object.
2199 xmlNewPI(const xmlChar *name, const xmlChar *content) {
2200 return(xmlNewDocPI(NULL, name, content));
2205 * @ns: namespace if any
2206 * @name: the node name
2208 * Creation of a new node element. @ns is optional (NULL).
2210 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2214 xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2219 xmlGenericError(xmlGenericErrorContext,
2220 "xmlNewNode : name == NULL\n");
2226 * Allocate a new node and fill the fields.
2228 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2230 xmlTreeErrMemory("building node");
2233 memset(cur, 0, sizeof(xmlNode));
2234 cur->type = XML_ELEMENT_NODE;
2236 cur->name = xmlStrdup(name);
2239 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2240 xmlRegisterNodeDefaultValue(cur);
2245 * xmlNewNodeEatName:
2246 * @ns: namespace if any
2247 * @name: the node name
2249 * Creation of a new node element. @ns is optional (NULL).
2251 * Returns a pointer to the new node object, with pointer @name as
2252 * new node's name. Use xmlNewNode() if a copy of @name string is
2253 * is needed as new node's name.
2256 xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2261 xmlGenericError(xmlGenericErrorContext,
2262 "xmlNewNode : name == NULL\n");
2268 * Allocate a new node and fill the fields.
2270 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2272 xmlTreeErrMemory("building node");
2273 /* we can't check here that name comes from the doc dictionnary */
2276 memset(cur, 0, sizeof(xmlNode));
2277 cur->type = XML_ELEMENT_NODE;
2282 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2283 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2289 * @doc: the document
2290 * @ns: namespace if any
2291 * @name: the node name
2292 * @content: the XML text content if any
2294 * Creation of a new node element within a document. @ns and @content
2295 * are optional (NULL).
2296 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2297 * references, but XML special chars need to be escaped first by using
2298 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2299 * need entities support.
2301 * Returns a pointer to the new node object.
2304 xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2305 const xmlChar *name, const xmlChar *content) {
2308 if ((doc != NULL) && (doc->dict != NULL))
2309 cur = xmlNewNodeEatName(ns, (xmlChar *)
2310 xmlDictLookup(doc->dict, name, -1));
2312 cur = xmlNewNode(ns, name);
2315 if (content != NULL) {
2316 cur->children = xmlStringGetNodeList(doc, content);
2317 UPDATE_LAST_CHILD_AND_PARENT(cur)
2325 * xmlNewDocNodeEatName:
2326 * @doc: the document
2327 * @ns: namespace if any
2328 * @name: the node name
2329 * @content: the XML text content if any
2331 * Creation of a new node element within a document. @ns and @content
2332 * are optional (NULL).
2333 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2334 * references, but XML special chars need to be escaped first by using
2335 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2336 * need entities support.
2338 * Returns a pointer to the new node object.
2341 xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2342 xmlChar *name, const xmlChar *content) {
2345 cur = xmlNewNodeEatName(ns, name);
2348 if (content != NULL) {
2349 cur->children = xmlStringGetNodeList(doc, content);
2350 UPDATE_LAST_CHILD_AND_PARENT(cur)
2353 /* if name don't come from the doc dictionnary free it here */
2354 if ((name != NULL) && (doc != NULL) &&
2355 (!(xmlDictOwns(doc->dict, name))))
2361 #ifdef LIBXML_TREE_ENABLED
2364 * @doc: the document
2365 * @ns: namespace if any
2366 * @name: the node name
2367 * @content: the text content if any
2369 * Creation of a new node element within a document. @ns and @content
2370 * are optional (NULL).
2372 * Returns a pointer to the new node object.
2375 xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2376 const xmlChar *name, const xmlChar *content) {
2379 cur = xmlNewDocNode(doc, ns, name, NULL);
2382 if (content != NULL) {
2383 cur->children = xmlNewDocText(doc, content);
2384 UPDATE_LAST_CHILD_AND_PARENT(cur)
2391 * xmlNewDocFragment:
2392 * @doc: the document owning the fragment
2394 * Creation of a new Fragment node.
2395 * Returns a pointer to the new node object.
2398 xmlNewDocFragment(xmlDocPtr doc) {
2402 * Allocate a new DocumentFragment node and fill the fields.
2404 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2406 xmlTreeErrMemory("building fragment");
2409 memset(cur, 0, sizeof(xmlNode));
2410 cur->type = XML_DOCUMENT_FRAG_NODE;
2414 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2415 xmlRegisterNodeDefaultValue(cur);
2418 #endif /* LIBXML_TREE_ENABLED */
2422 * @content: the text content
2424 * Creation of a new text node.
2425 * Returns a pointer to the new node object.
2428 xmlNewText(const xmlChar *content) {
2432 * Allocate a new node and fill the fields.
2434 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2436 xmlTreeErrMemory("building text");
2439 memset(cur, 0, sizeof(xmlNode));
2440 cur->type = XML_TEXT_NODE;
2442 cur->name = xmlStringText;
2443 if (content != NULL) {
2444 cur->content = xmlStrdup(content);
2447 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2448 xmlRegisterNodeDefaultValue(cur);
2452 #ifdef LIBXML_TREE_ENABLED
2455 * @parent: the parent node
2456 * @ns: a namespace if any
2457 * @name: the name of the child
2458 * @content: the text content of the child if any.
2460 * Creation of a new child element, added at the end of @parent children list.
2461 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2462 * created element inherits the namespace of @parent. If @content is non NULL,
2463 * a child TEXT node will be created containing the string @content.
2464 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2465 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
2466 * reserved XML chars that might appear in @content, such as the ampersand,
2467 * greater-than or less-than signs, are automatically replaced by their XML
2468 * escaped entity representations.
2470 * Returns a pointer to the new node object.
2473 xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2474 const xmlChar *name, const xmlChar *content) {
2475 xmlNodePtr cur, prev;
2477 if (parent == NULL) {
2479 xmlGenericError(xmlGenericErrorContext,
2480 "xmlNewTextChild : parent == NULL\n");
2487 xmlGenericError(xmlGenericErrorContext,
2488 "xmlNewTextChild : name == NULL\n");
2494 * Allocate a new node
2496 if (parent->type == XML_ELEMENT_NODE) {
2498 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2500 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2501 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2502 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2504 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2506 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2507 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2508 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2512 if (cur == NULL) return(NULL);
2515 * add the new element at the end of the children list.
2517 cur->type = XML_ELEMENT_NODE;
2518 cur->parent = parent;
2519 cur->doc = parent->doc;
2520 if (parent->children == NULL) {
2521 parent->children = cur;
2524 prev = parent->last;
2532 #endif /* LIBXML_TREE_ENABLED */
2536 * @doc: the document
2537 * @name: the char ref string, starting with # or "&# ... ;"
2539 * Creation of a new character reference node.
2540 * Returns a pointer to the new node object.
2543 xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2550 * Allocate a new node and fill the fields.
2552 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2554 xmlTreeErrMemory("building character reference");
2557 memset(cur, 0, sizeof(xmlNode));
2558 cur->type = XML_ENTITY_REF_NODE;
2561 if (name[0] == '&') {
2564 len = xmlStrlen(name);
2565 if (name[len - 1] == ';')
2566 cur->name = xmlStrndup(name, len - 1);
2568 cur->name = xmlStrndup(name, len);
2570 cur->name = xmlStrdup(name);
2572 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2573 xmlRegisterNodeDefaultValue(cur);
2579 * @doc: the document
2580 * @name: the reference name, or the reference string with & and ;
2582 * Creation of a new reference node.
2583 * Returns a pointer to the new node object.
2586 xmlNewReference(const xmlDoc *doc, const xmlChar *name) {
2594 * Allocate a new node and fill the fields.
2596 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2598 xmlTreeErrMemory("building reference");
2601 memset(cur, 0, sizeof(xmlNode));
2602 cur->type = XML_ENTITY_REF_NODE;
2604 cur->doc = (xmlDoc *)doc;
2605 if (name[0] == '&') {
2608 len = xmlStrlen(name);
2609 if (name[len - 1] == ';')
2610 cur->name = xmlStrndup(name, len - 1);
2612 cur->name = xmlStrndup(name, len);
2614 cur->name = xmlStrdup(name);
2616 ent = xmlGetDocEntity(doc, cur->name);
2618 cur->content = ent->content;
2620 * The parent pointer in entity is a DTD pointer and thus is NOT
2621 * updated. Not sure if this is 100% correct.
2624 cur->children = (xmlNodePtr) ent;
2625 cur->last = (xmlNodePtr) ent;
2628 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2629 xmlRegisterNodeDefaultValue(cur);
2635 * @doc: the document
2636 * @content: the text content
2638 * Creation of a new text node within a document.
2639 * Returns a pointer to the new node object.
2642 xmlNewDocText(const xmlDoc *doc, const xmlChar *content) {
2645 cur = xmlNewText(content);
2646 if (cur != NULL) cur->doc = (xmlDoc *)doc;
2652 * @content: the text content
2653 * @len: the text len.
2655 * Creation of a new text node with an extra parameter for the content's length
2656 * Returns a pointer to the new node object.
2659 xmlNewTextLen(const xmlChar *content, int len) {
2663 * Allocate a new node and fill the fields.
2665 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2667 xmlTreeErrMemory("building text");
2670 memset(cur, 0, sizeof(xmlNode));
2671 cur->type = XML_TEXT_NODE;
2673 cur->name = xmlStringText;
2674 if (content != NULL) {
2675 cur->content = xmlStrndup(content, len);
2678 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2679 xmlRegisterNodeDefaultValue(cur);
2685 * @doc: the document
2686 * @content: the text content
2687 * @len: the text len.
2689 * Creation of a new text node with an extra content length parameter. The
2690 * text node pertain to a given document.
2691 * Returns a pointer to the new node object.
2694 xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2697 cur = xmlNewTextLen(content, len);
2698 if (cur != NULL) cur->doc = doc;
2704 * @content: the comment content
2706 * Creation of a new node containing a comment.
2707 * Returns a pointer to the new node object.
2710 xmlNewComment(const xmlChar *content) {
2714 * Allocate a new node and fill the fields.
2716 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2718 xmlTreeErrMemory("building comment");
2721 memset(cur, 0, sizeof(xmlNode));
2722 cur->type = XML_COMMENT_NODE;
2724 cur->name = xmlStringComment;
2725 if (content != NULL) {
2726 cur->content = xmlStrdup(content);
2729 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2730 xmlRegisterNodeDefaultValue(cur);
2736 * @doc: the document
2737 * @content: the CDATA block content content
2738 * @len: the length of the block
2740 * Creation of a new node containing a CDATA block.
2741 * Returns a pointer to the new node object.
2744 xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2748 * Allocate a new node and fill the fields.
2750 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2752 xmlTreeErrMemory("building CDATA");
2755 memset(cur, 0, sizeof(xmlNode));
2756 cur->type = XML_CDATA_SECTION_NODE;
2759 if (content != NULL) {
2760 cur->content = xmlStrndup(content, len);
2763 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2764 xmlRegisterNodeDefaultValue(cur);
2770 * @doc: the document
2771 * @content: the comment content
2773 * Creation of a new node containing a comment within a document.
2774 * Returns a pointer to the new node object.
2777 xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2780 cur = xmlNewComment(content);
2781 if (cur != NULL) cur->doc = doc;
2787 * @tree: the top element
2788 * @doc: the document
2790 * update all nodes under the tree to point to the right document
2793 xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
2796 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
2798 if (tree->doc != doc) {
2799 if(tree->type == XML_ELEMENT_NODE) {
2800 prop = tree->properties;
2801 while (prop != NULL) {
2803 xmlSetListDoc(prop->children, doc);
2807 if (tree->children != NULL)
2808 xmlSetListDoc(tree->children, doc);
2815 * @list: the first element
2816 * @doc: the document
2818 * update all nodes in the list to point to the right document
2821 xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2824 if ((list == NULL) || (list->type == XML_NAMESPACE_DECL))
2827 while (cur != NULL) {
2828 if (cur->doc != doc)
2829 xmlSetTreeDoc(cur, doc);
2834 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
2837 * @parent: the parent node
2838 * @ns: a namespace if any
2839 * @name: the name of the child
2840 * @content: the XML content of the child if any.
2842 * Creation of a new child element, added at the end of @parent children list.
2843 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2844 * created element inherits the namespace of @parent. If @content is non NULL,
2845 * a child list containing the TEXTs and ENTITY_REFs node will be created.
2846 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2847 * references. XML special chars must be escaped first by using
2848 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
2850 * Returns a pointer to the new node object.
2853 xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2854 const xmlChar *name, const xmlChar *content) {
2855 xmlNodePtr cur, prev;
2857 if (parent == NULL) {
2859 xmlGenericError(xmlGenericErrorContext,
2860 "xmlNewChild : parent == NULL\n");
2867 xmlGenericError(xmlGenericErrorContext,
2868 "xmlNewChild : name == NULL\n");
2874 * Allocate a new node
2876 if (parent->type == XML_ELEMENT_NODE) {
2878 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2880 cur = xmlNewDocNode(parent->doc, ns, name, content);
2881 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2882 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2884 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2886 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
2887 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2888 cur = xmlNewDocNode( parent->doc, ns, name, content);
2892 if (cur == NULL) return(NULL);
2895 * add the new element at the end of the children list.
2897 cur->type = XML_ELEMENT_NODE;
2898 cur->parent = parent;
2899 cur->doc = parent->doc;
2900 if (parent->children == NULL) {
2901 parent->children = cur;
2904 prev = parent->last;
2912 #endif /* LIBXML_TREE_ENABLED */
2915 * xmlAddPropSibling:
2916 * @prev: the attribute to which @prop is added after
2917 * @cur: the base attribute passed to calling function
2918 * @prop: the new attribute
2920 * Add a new attribute after @prev using @cur as base attribute.
2921 * When inserting before @cur, @prev is passed as @cur->prev.
2922 * When inserting after @cur, @prev is passed as @cur.
2923 * If an existing attribute is found it is detroyed prior to adding @prop.
2925 * Returns the attribute being inserted or NULL in case of error.
2928 xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2931 if ((cur == NULL) || (cur->type != XML_ATTRIBUTE_NODE) ||
2932 (prop == NULL) || (prop->type != XML_ATTRIBUTE_NODE) ||
2933 ((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)))
2936 /* check if an attribute with the same name exists */
2937 if (prop->ns == NULL)
2938 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2940 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2942 if (prop->doc != cur->doc) {
2943 xmlSetTreeDoc(prop, cur->doc);
2945 prop->parent = cur->parent;
2948 prop->next = prev->next;
2951 prop->next->prev = prop;
2956 if (prop->prev == NULL && prop->parent != NULL)
2957 prop->parent->properties = (xmlAttrPtr) prop;
2958 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2959 /* different instance, destroy it (attributes must be unique) */
2960 xmlRemoveProp((xmlAttrPtr) attr);
2966 * xmlAddNextSibling:
2967 * @cur: the child node
2968 * @elem: the new node
2970 * Add a new node @elem as the next sibling of @cur
2971 * If the new node was already inserted in a document it is
2972 * first unlinked from its existing context.
2973 * As a result of text merging @elem may be freed.
2974 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2975 * If there is an attribute with equal name, it is first destroyed.
2977 * Returns the new node or NULL in case of error.
2980 xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2981 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
2983 xmlGenericError(xmlGenericErrorContext,
2984 "xmlAddNextSibling : cur == NULL\n");
2988 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
2990 xmlGenericError(xmlGenericErrorContext,
2991 "xmlAddNextSibling : elem == NULL\n");
2998 xmlGenericError(xmlGenericErrorContext,
2999 "xmlAddNextSibling : cur == elem\n");
3004 xmlUnlinkNode(elem);
3006 if (elem->type == XML_TEXT_NODE) {
3007 if (cur->type == XML_TEXT_NODE) {
3008 xmlNodeAddContent(cur, elem->content);
3012 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
3013 (cur->name == cur->next->name)) {
3016 tmp = xmlStrdup(elem->content);
3017 tmp = xmlStrcat(tmp, cur->next->content);
3018 xmlNodeSetContent(cur->next, tmp);
3023 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3024 return xmlAddPropSibling(cur, cur, elem);
3027 if (elem->doc != cur->doc) {
3028 xmlSetTreeDoc(elem, cur->doc);
3030 elem->parent = cur->parent;
3032 elem->next = cur->next;
3034 if (elem->next != NULL)
3035 elem->next->prev = elem;
3036 if ((elem->parent != NULL) && (elem->parent->last == cur))
3037 elem->parent->last = elem;
3041 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
3042 defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
3044 * xmlAddPrevSibling:
3045 * @cur: the child node
3046 * @elem: the new node
3048 * Add a new node @elem as the previous sibling of @cur
3049 * merging adjacent TEXT nodes (@elem may be freed)
3050 * If the new node was already inserted in a document it is
3051 * first unlinked from its existing context.
3052 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3053 * If there is an attribute with equal name, it is first destroyed.
3055 * Returns the new node or NULL in case of error.
3058 xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
3059 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3061 xmlGenericError(xmlGenericErrorContext,
3062 "xmlAddPrevSibling : cur == NULL\n");
3066 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
3068 xmlGenericError(xmlGenericErrorContext,
3069 "xmlAddPrevSibling : elem == NULL\n");
3076 xmlGenericError(xmlGenericErrorContext,
3077 "xmlAddPrevSibling : cur == elem\n");
3082 xmlUnlinkNode(elem);
3084 if (elem->type == XML_TEXT_NODE) {
3085 if (cur->type == XML_TEXT_NODE) {
3088 tmp = xmlStrdup(elem->content);
3089 tmp = xmlStrcat(tmp, cur->content);
3090 xmlNodeSetContent(cur, tmp);
3095 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3096 (cur->name == cur->prev->name)) {
3097 xmlNodeAddContent(cur->prev, elem->content);
3101 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3102 return xmlAddPropSibling(cur->prev, cur, elem);
3105 if (elem->doc != cur->doc) {
3106 xmlSetTreeDoc(elem, cur->doc);
3108 elem->parent = cur->parent;
3110 elem->prev = cur->prev;
3112 if (elem->prev != NULL)
3113 elem->prev->next = elem;
3114 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
3115 elem->parent->children = elem;
3119 #endif /* LIBXML_TREE_ENABLED */
3123 * @cur: the child node
3124 * @elem: the new node
3126 * Add a new element @elem to the list of siblings of @cur
3127 * merging adjacent TEXT nodes (@elem may be freed)
3128 * If the new element was already inserted in a document it is
3129 * first unlinked from its existing context.
3131 * Returns the new element or NULL in case of error.
3134 xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3137 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3139 xmlGenericError(xmlGenericErrorContext,
3140 "xmlAddSibling : cur == NULL\n");
3145 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
3147 xmlGenericError(xmlGenericErrorContext,
3148 "xmlAddSibling : elem == NULL\n");
3155 xmlGenericError(xmlGenericErrorContext,
3156 "xmlAddSibling : cur == elem\n");
3162 * Constant time is we can rely on the ->parent->last to find
3165 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
3166 (cur->parent->children != NULL) &&
3167 (cur->parent->last != NULL) &&
3168 (cur->parent->last->next == NULL)) {
3169 cur = cur->parent->last;
3171 while (cur->next != NULL) cur = cur->next;
3174 xmlUnlinkNode(elem);
3176 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3177 (cur->name == elem->name)) {
3178 xmlNodeAddContent(cur, elem->content);
3181 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3182 return xmlAddPropSibling(cur, cur, elem);
3185 if (elem->doc != cur->doc) {
3186 xmlSetTreeDoc(elem, cur->doc);
3188 parent = cur->parent;
3191 elem->parent = parent;
3194 parent->last = elem;
3201 * @parent: the parent node
3202 * @cur: the first node in the list
3204 * Add a list of node at the end of the child list of the parent
3205 * merging adjacent TEXT nodes (@cur may be freed)
3207 * Returns the last child or NULL in case of error.
3210 xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3213 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3215 xmlGenericError(xmlGenericErrorContext,
3216 "xmlAddChildList : parent == NULL\n");
3221 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3223 xmlGenericError(xmlGenericErrorContext,
3224 "xmlAddChildList : child == NULL\n");
3229 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3230 (cur->doc != parent->doc)) {
3232 xmlGenericError(xmlGenericErrorContext,
3233 "Elements moved to a different document\n");
3238 * add the first element at the end of the children list.
3241 if (parent->children == NULL) {
3242 parent->children = cur;
3245 * If cur and parent->last both are TEXT nodes, then merge them.
3247 if ((cur->type == XML_TEXT_NODE) &&
3248 (parent->last->type == XML_TEXT_NODE) &&
3249 (cur->name == parent->last->name)) {
3250 xmlNodeAddContent(parent->last, cur->content);
3252 * if it's the only child, nothing more to be done.
3254 if (cur->next == NULL) {
3256 return(parent->last);
3262 prev = parent->last;
3266 while (cur->next != NULL) {
3267 cur->parent = parent;
3268 if (cur->doc != parent->doc) {
3269 xmlSetTreeDoc(cur, parent->doc);
3273 cur->parent = parent;
3274 /* the parent may not be linked to a doc ! */
3275 if (cur->doc != parent->doc) {
3276 xmlSetTreeDoc(cur, parent->doc);
3285 * @parent: the parent node
3286 * @cur: the child node
3288 * Add a new node to @parent, at the end of the child (or property) list
3289 * merging adjacent TEXT nodes (in which case @cur is freed)
3290 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3291 * If there is an attribute with equal name, it is first destroyed.
3293 * Returns the child or NULL in case of error.
3296 xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3299 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3301 xmlGenericError(xmlGenericErrorContext,
3302 "xmlAddChild : parent == NULL\n");
3307 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3309 xmlGenericError(xmlGenericErrorContext,
3310 "xmlAddChild : child == NULL\n");
3315 if (parent == cur) {
3317 xmlGenericError(xmlGenericErrorContext,
3318 "xmlAddChild : parent == cur\n");
3323 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
3324 * cur is then freed.
3326 if (cur->type == XML_TEXT_NODE) {
3327 if ((parent->type == XML_TEXT_NODE) &&
3328 (parent->content != NULL) &&
3329 (parent->name == cur->name)) {
3330 xmlNodeAddContent(parent, cur->content);
3334 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
3335 (parent->last->name == cur->name) &&
3336 (parent->last != cur)) {
3337 xmlNodeAddContent(parent->last, cur->content);
3339 return(parent->last);
3344 * add the new element at the end of the children list.
3347 cur->parent = parent;
3348 if (cur->doc != parent->doc) {
3349 xmlSetTreeDoc(cur, parent->doc);
3351 /* this check prevents a loop on tree-traversions if a developer
3352 * tries to add a node to its parent multiple times
3360 if ((parent->type == XML_TEXT_NODE) &&
3361 (parent->content != NULL) &&
3363 xmlNodeAddContent(parent, cur->content);
3367 if (cur->type == XML_ATTRIBUTE_NODE) {
3368 if (parent->type != XML_ELEMENT_NODE)
3370 if (parent->properties != NULL) {
3371 /* check if an attribute with the same name exists */
3372 xmlAttrPtr lastattr;
3374 if (cur->ns == NULL)
3375 lastattr = xmlHasNsProp(parent, cur->name, NULL);
3377 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
3378 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
3379 /* different instance, destroy it (attributes must be unique) */
3380 xmlUnlinkNode((xmlNodePtr) lastattr);
3381 xmlFreeProp(lastattr);
3383 if (lastattr == (xmlAttrPtr) cur)
3387 if (parent->properties == NULL) {
3388 parent->properties = (xmlAttrPtr) cur;
3391 xmlAttrPtr lastattr = parent->properties;
3392 while (lastattr->next != NULL) {
3393 lastattr = lastattr->next;
3395 lastattr->next = (xmlAttrPtr) cur;
3396 ((xmlAttrPtr) cur)->prev = lastattr;
3399 if (parent->children == NULL) {
3400 parent->children = cur;
3403 prev = parent->last;
3414 * @parent: the parent node
3416 * Search the last child of a node.
3417 * Returns the last child or NULL if none.
3420 xmlGetLastChild(const xmlNode *parent) {
3421 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3423 xmlGenericError(xmlGenericErrorContext,
3424 "xmlGetLastChild : parent == NULL\n");
3428 return(parent->last);
3431 #ifdef LIBXML_TREE_ENABLED
3433 * 5 interfaces from DOM ElementTraversal
3437 * xmlChildElementCount:
3438 * @parent: the parent node
3440 * Finds the current number of child nodes of that element which are
3442 * Note the handling of entities references is different than in
3443 * the W3C DOM element traversal spec since we don't have back reference
3444 * from entities content to entities references.
3446 * Returns the count of element child or 0 if not available
3449 xmlChildElementCount(xmlNodePtr parent) {
3450 unsigned long ret = 0;
3451 xmlNodePtr cur = NULL;
3455 switch (parent->type) {
3456 case XML_ELEMENT_NODE:
3457 case XML_ENTITY_NODE:
3458 case XML_DOCUMENT_NODE:
3459 case XML_DOCUMENT_FRAG_NODE:
3460 case XML_HTML_DOCUMENT_NODE:
3461 cur = parent->children;
3466 while (cur != NULL) {
3467 if (cur->type == XML_ELEMENT_NODE)
3475 * xmlFirstElementChild:
3476 * @parent: the parent node
3478 * Finds the first child node of that element which is a Element node
3479 * Note the handling of entities references is different than in
3480 * the W3C DOM element traversal spec since we don't have back reference
3481 * from entities content to entities references.
3483 * Returns the first element child or NULL if not available
3486 xmlFirstElementChild(xmlNodePtr parent) {
3487 xmlNodePtr cur = NULL;
3491 switch (parent->type) {
3492 case XML_ELEMENT_NODE:
3493 case XML_ENTITY_NODE:
3494 case XML_DOCUMENT_NODE:
3495 case XML_DOCUMENT_FRAG_NODE:
3496 case XML_HTML_DOCUMENT_NODE:
3497 cur = parent->children;
3502 while (cur != NULL) {
3503 if (cur->type == XML_ELEMENT_NODE)
3511 * xmlLastElementChild:
3512 * @parent: the parent node
3514 * Finds the last child node of that element which is a Element node
3515 * Note the handling of entities references is different than in
3516 * the W3C DOM element traversal spec since we don't have back reference
3517 * from entities content to entities references.
3519 * Returns the last element child or NULL if not available
3522 xmlLastElementChild(xmlNodePtr parent) {
3523 xmlNodePtr cur = NULL;
3527 switch (parent->type) {
3528 case XML_ELEMENT_NODE:
3529 case XML_ENTITY_NODE:
3530 case XML_DOCUMENT_NODE:
3531 case XML_DOCUMENT_FRAG_NODE:
3532 case XML_HTML_DOCUMENT_NODE:
3538 while (cur != NULL) {
3539 if (cur->type == XML_ELEMENT_NODE)
3547 * xmlPreviousElementSibling:
3548 * @node: the current node
3550 * Finds the first closest previous sibling of the node which is an
3552 * Note the handling of entities references is different than in
3553 * the W3C DOM element traversal spec since we don't have back reference
3554 * from entities content to entities references.
3556 * Returns the previous element sibling or NULL if not available
3559 xmlPreviousElementSibling(xmlNodePtr node) {
3562 switch (node->type) {
3563 case XML_ELEMENT_NODE:
3565 case XML_CDATA_SECTION_NODE:
3566 case XML_ENTITY_REF_NODE:
3567 case XML_ENTITY_NODE:
3569 case XML_COMMENT_NODE:
3570 case XML_XINCLUDE_START:
3571 case XML_XINCLUDE_END:
3577 while (node != NULL) {
3578 if (node->type == XML_ELEMENT_NODE)
3586 * xmlNextElementSibling:
3587 * @node: the current node
3589 * Finds the first closest next sibling of the node which is an
3591 * Note the handling of entities references is different than in
3592 * the W3C DOM element traversal spec since we don't have back reference
3593 * from entities content to entities references.
3595 * Returns the next element sibling or NULL if not available
3598 xmlNextElementSibling(xmlNodePtr node) {
3601 switch (node->type) {
3602 case XML_ELEMENT_NODE:
3604 case XML_CDATA_SECTION_NODE:
3605 case XML_ENTITY_REF_NODE:
3606 case XML_ENTITY_NODE:
3608 case XML_COMMENT_NODE:
3610 case XML_XINCLUDE_START:
3611 case XML_XINCLUDE_END:
3617 while (node != NULL) {
3618 if (node->type == XML_ELEMENT_NODE)
3625 #endif /* LIBXML_TREE_ENABLED */
3629 * @cur: the first node in the list
3631 * Free a node and all its siblings, this is a recursive behaviour, all
3632 * the children are freed too.
3635 xmlFreeNodeList(xmlNodePtr cur) {
3637 xmlDictPtr dict = NULL;
3639 if (cur == NULL) return;
3640 if (cur->type == XML_NAMESPACE_DECL) {
3641 xmlFreeNsList((xmlNsPtr) cur);
3644 if ((cur->type == XML_DOCUMENT_NODE) ||
3645 #ifdef LIBXML_DOCB_ENABLED
3646 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
3648 (cur->type == XML_HTML_DOCUMENT_NODE)) {
3649 xmlFreeDoc((xmlDocPtr) cur);
3652 if (cur->doc != NULL) dict = cur->doc->dict;
3653 while (cur != NULL) {
3655 if (cur->type != XML_DTD_NODE) {
3657 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3658 xmlDeregisterNodeDefaultValue(cur);
3660 if ((cur->children != NULL) &&
3661 (cur->type != XML_ENTITY_REF_NODE))
3662 xmlFreeNodeList(cur->children);
3663 if (((cur->type == XML_ELEMENT_NODE) ||
3664 (cur->type == XML_XINCLUDE_START) ||
3665 (cur->type == XML_XINCLUDE_END)) &&
3666 (cur->properties != NULL))
3667 xmlFreePropList(cur->properties);
3668 if ((cur->type != XML_ELEMENT_NODE) &&
3669 (cur->type != XML_XINCLUDE_START) &&
3670 (cur->type != XML_XINCLUDE_END) &&
3671 (cur->type != XML_ENTITY_REF_NODE) &&
3672 (cur->content != (xmlChar *) &(cur->properties))) {
3673 DICT_FREE(cur->content)
3675 if (((cur->type == XML_ELEMENT_NODE) ||
3676 (cur->type == XML_XINCLUDE_START) ||
3677 (cur->type == XML_XINCLUDE_END)) &&
3678 (cur->nsDef != NULL))
3679 xmlFreeNsList(cur->nsDef);
3682 * When a node is a text node or a comment, it uses a global static
3683 * variable for the name of the node.
3684 * Otherwise the node name might come from the document's
3687 if ((cur->name != NULL) &&
3688 (cur->type != XML_TEXT_NODE) &&
3689 (cur->type != XML_COMMENT_NODE))
3690 DICT_FREE(cur->name)
3701 * Free a node, this is a recursive behaviour, all the children are freed too.
3702 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3705 xmlFreeNode(xmlNodePtr cur) {
3706 xmlDictPtr dict = NULL;
3708 if (cur == NULL) return;
3710 /* use xmlFreeDtd for DTD nodes */
3711 if (cur->type == XML_DTD_NODE) {
3712 xmlFreeDtd((xmlDtdPtr) cur);
3715 if (cur->type == XML_NAMESPACE_DECL) {
3716 xmlFreeNs((xmlNsPtr) cur);
3719 if (cur->type == XML_ATTRIBUTE_NODE) {
3720 xmlFreeProp((xmlAttrPtr) cur);
3724 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3725 xmlDeregisterNodeDefaultValue(cur);
3727 if (cur->doc != NULL) dict = cur->doc->dict;
3729 if (cur->type == XML_ENTITY_DECL) {
3730 xmlEntityPtr ent = (xmlEntityPtr) cur;
3731 DICT_FREE(ent->SystemID);
3732 DICT_FREE(ent->ExternalID);
3734 if ((cur->children != NULL) &&
3735 (cur->type != XML_ENTITY_REF_NODE))
3736 xmlFreeNodeList(cur->children);
3737 if (((cur->type == XML_ELEMENT_NODE) ||
3738 (cur->type == XML_XINCLUDE_START) ||
3739 (cur->type == XML_XINCLUDE_END)) &&
3740 (cur->properties != NULL))
3741 xmlFreePropList(cur->properties);
3742 if ((cur->type != XML_ELEMENT_NODE) &&
3743 (cur->content != NULL) &&
3744 (cur->type != XML_ENTITY_REF_NODE) &&
3745 (cur->type != XML_XINCLUDE_END) &&
3746 (cur->type != XML_XINCLUDE_START) &&
3747 (cur->content != (xmlChar *) &(cur->properties))) {
3748 DICT_FREE(cur->content)
3752 * When a node is a text node or a comment, it uses a global static
3753 * variable for the name of the node.
3754 * Otherwise the node name might come from the document's dictionnary
3756 if ((cur->name != NULL) &&
3757 (cur->type != XML_TEXT_NODE) &&
3758 (cur->type != XML_COMMENT_NODE))
3759 DICT_FREE(cur->name)
3761 if (((cur->type == XML_ELEMENT_NODE) ||
3762 (cur->type == XML_XINCLUDE_START) ||
3763 (cur->type == XML_XINCLUDE_END)) &&
3764 (cur->nsDef != NULL))
3765 xmlFreeNsList(cur->nsDef);
3773 * Unlink a node from it's current context, the node is not freed
3774 * If one need to free the node, use xmlFreeNode() routine after the
3775 * unlink to discard it.
3776 * Note that namespace nodes can't be unlinked as they do not have
3777 * pointer to their parent.
3780 xmlUnlinkNode(xmlNodePtr cur) {
3783 xmlGenericError(xmlGenericErrorContext,
3784 "xmlUnlinkNode : node == NULL\n");
3788 if (cur->type == XML_NAMESPACE_DECL)
3790 if (cur->type == XML_DTD_NODE) {
3794 if (doc->intSubset == (xmlDtdPtr) cur)
3795 doc->intSubset = NULL;
3796 if (doc->extSubset == (xmlDtdPtr) cur)
3797 doc->extSubset = NULL;
3800 if (cur->type == XML_ENTITY_DECL) {
3804 if (doc->intSubset != NULL) {
3805 if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur)
3806 xmlHashRemoveEntry(doc->intSubset->entities, cur->name,
3808 if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur)
3809 xmlHashRemoveEntry(doc->intSubset->pentities, cur->name,
3812 if (doc->extSubset != NULL) {
3813 if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur)
3814 xmlHashRemoveEntry(doc->extSubset->entities, cur->name,
3816 if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur)
3817 xmlHashRemoveEntry(doc->extSubset->pentities, cur->name,
3822 if (cur->parent != NULL) {
3824 parent = cur->parent;
3825 if (cur->type == XML_ATTRIBUTE_NODE) {
3826 if (parent->properties == (xmlAttrPtr) cur)
3827 parent->properties = ((xmlAttrPtr) cur)->next;
3829 if (parent->children == cur)
3830 parent->children = cur->next;
3831 if (parent->last == cur)
3832 parent->last = cur->prev;
3836 if (cur->next != NULL)
3837 cur->next->prev = cur->prev;
3838 if (cur->prev != NULL)
3839 cur->prev->next = cur->next;
3840 cur->next = cur->prev = NULL;
3843 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
3846 * @old: the old node
3849 * Unlink the old node from its current context, prune the new one
3850 * at the same place. If @cur was already inserted in a document it is
3851 * first unlinked from its existing context.
3853 * Returns the @old node
3856 xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
3857 if (old == cur) return(NULL);
3858 if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) ||
3859 (old->parent == NULL)) {
3861 xmlGenericError(xmlGenericErrorContext,
3862 "xmlReplaceNode : old == NULL or without parent\n");
3866 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3873 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3875 xmlGenericError(xmlGenericErrorContext,
3876 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3880 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3882 xmlGenericError(xmlGenericErrorContext,
3883 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3888 xmlSetTreeDoc(cur, old->doc);
3889 cur->parent = old->parent;
3890 cur->next = old->next;
3891 if (cur->next != NULL)
3892 cur->next->prev = cur;
3893 cur->prev = old->prev;
3894 if (cur->prev != NULL)
3895 cur->prev->next = cur;
3896 if (cur->parent != NULL) {
3897 if (cur->type == XML_ATTRIBUTE_NODE) {
3898 if (cur->parent->properties == (xmlAttrPtr)old)
3899 cur->parent->properties = ((xmlAttrPtr) cur);
3901 if (cur->parent->children == old)
3902 cur->parent->children = cur;
3903 if (cur->parent->last == old)
3904 cur->parent->last = cur;
3907 old->next = old->prev = NULL;
3911 #endif /* LIBXML_TREE_ENABLED */
3913 /************************************************************************
3917 ************************************************************************/
3921 * @cur: the namespace
3923 * Do a copy of the namespace.
3925 * Returns: a new #xmlNsPtr, or NULL in case of error.
3928 xmlCopyNamespace(xmlNsPtr cur) {
3931 if (cur == NULL) return(NULL);
3932 switch (cur->type) {
3933 case XML_LOCAL_NAMESPACE:
3934 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3938 xmlGenericError(xmlGenericErrorContext,
3939 "xmlCopyNamespace: invalid type %d\n", cur->type);
3947 * xmlCopyNamespaceList:
3948 * @cur: the first namespace
3950 * Do a copy of an namespace list.
3952 * Returns: a new #xmlNsPtr, or NULL in case of error.
3955 xmlCopyNamespaceList(xmlNsPtr cur) {
3956 xmlNsPtr ret = NULL;
3957 xmlNsPtr p = NULL,q;
3959 while (cur != NULL) {
3960 q = xmlCopyNamespace(cur);
3973 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
3976 xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
3979 if (cur == NULL) return(NULL);
3980 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
3983 ret = xmlNewDocProp(target->doc, cur->name, NULL);
3984 else if (doc != NULL)
3985 ret = xmlNewDocProp(doc, cur->name, NULL);
3986 else if (cur->parent != NULL)
3987 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3988 else if (cur->children != NULL)
3989 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3991 ret = xmlNewDocProp(NULL, cur->name, NULL);
3992 if (ret == NULL) return(NULL);
3993 ret->parent = target;
3995 if ((cur->ns != NULL) && (target != NULL)) {
3998 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
4001 * Humm, we are copying an element whose namespace is defined
4002 * out of the new tree scope. Search it in the original tree
4003 * and add it at the top of the new tree
4005 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
4007 xmlNodePtr root = target;
4008 xmlNodePtr pred = NULL;
4010 while (root->parent != NULL) {
4012 root = root->parent;
4014 if (root == (xmlNodePtr) target->doc) {
4015 /* correct possibly cycling above the document elt */
4018 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4022 * we have to find something appropriate here since
4023 * we cant be sure, that the namespce we found is identified
4026 if (xmlStrEqual(ns->href, cur->ns->href)) {
4027 /* this is the nice case */
4031 * we are in trouble: we need a new reconcilied namespace.
4034 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
4041 if (cur->children != NULL) {
4044 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
4046 tmp = ret->children;
4047 while (tmp != NULL) {
4048 /* tmp->parent = (xmlNodePtr)ret; */
4049 if (tmp->next == NULL)
4057 if ((target!= NULL) && (cur!= NULL) &&
4058 (target->doc != NULL) && (cur->doc != NULL) &&
4059 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
4060 if (xmlIsID(cur->doc, cur->parent, cur)) {
4063 id = xmlNodeListGetString(cur->doc, cur->children, 1);
4065 xmlAddID(NULL, target->doc, id, ret);
4075 * @target: the element where the attribute will be grafted
4076 * @cur: the attribute
4078 * Do a copy of the attribute.
4080 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4083 xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
4084 return xmlCopyPropInternal(NULL, target, cur);
4089 * @target: the element where the attributes will be grafted
4090 * @cur: the first attribute
4092 * Do a copy of an attribute list.
4094 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4097 xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
4098 xmlAttrPtr ret = NULL;
4099 xmlAttrPtr p = NULL,q;
4101 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4103 while (cur != NULL) {
4104 q = xmlCopyProp(target, cur);
4120 * NOTE about the CopyNode operations !
4122 * They are split into external and internal parts for one
4123 * tricky reason: namespaces. Doing a direct copy of a node
4124 * say RPM:Copyright without changing the namespace pointer to
4125 * something else can produce stale links. One way to do it is
4126 * to keep a reference counter but this doesn't work as soon
4127 * as one move the element or the subtree out of the scope of
4128 * the existing namespace. The actual solution seems to add
4129 * a copy of the namespace at the top of the copied tree if
4130 * not available in the subtree.
4131 * Hence two functions, the public front-end call the inner ones
4132 * The argument "recursive" normally indicates a recursive copy
4133 * of the node with values 0 (no) and 1 (yes). For XInclude,
4134 * however, we allow a value of 2 to indicate copy properties and
4135 * namespace info, but don't recurse on children.
4139 xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
4143 if (node == NULL) return(NULL);
4144 switch (node->type) {
4146 case XML_CDATA_SECTION_NODE:
4147 case XML_ELEMENT_NODE:
4148 case XML_DOCUMENT_FRAG_NODE:
4149 case XML_ENTITY_REF_NODE:
4150 case XML_ENTITY_NODE:
4152 case XML_COMMENT_NODE:
4153 case XML_XINCLUDE_START:
4154 case XML_XINCLUDE_END:
4156 case XML_ATTRIBUTE_NODE:
4157 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
4158 case XML_NAMESPACE_DECL:
4159 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
4161 case XML_DOCUMENT_NODE:
4162 case XML_HTML_DOCUMENT_NODE:
4163 #ifdef LIBXML_DOCB_ENABLED
4164 case XML_DOCB_DOCUMENT_NODE:
4166 #ifdef LIBXML_TREE_ENABLED
4167 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
4168 #endif /* LIBXML_TREE_ENABLED */
4169 case XML_DOCUMENT_TYPE_NODE:
4170 case XML_NOTATION_NODE:
4172 case XML_ELEMENT_DECL:
4173 case XML_ATTRIBUTE_DECL:
4174 case XML_ENTITY_DECL:
4179 * Allocate a new node and fill the fields.
4181 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4183 xmlTreeErrMemory("copying node");
4186 memset(ret, 0, sizeof(xmlNode));
4187 ret->type = node->type;
4190 ret->parent = parent;
4191 if (node->name == xmlStringText)
4192 ret->name = xmlStringText;
4193 else if (node->name == xmlStringTextNoenc)
4194 ret->name = xmlStringTextNoenc;
4195 else if (node->name == xmlStringComment)
4196 ret->name = xmlStringComment;
4197 else if (node->name != NULL) {
4198 if ((doc != NULL) && (doc->dict != NULL))
4199 ret->name = xmlDictLookup(doc->dict, node->name, -1);
4201 ret->name = xmlStrdup(node->name);
4203 if ((node->type != XML_ELEMENT_NODE) &&
4204 (node->content != NULL) &&
4205 (node->type != XML_ENTITY_REF_NODE) &&
4206 (node->type != XML_XINCLUDE_END) &&
4207 (node->type != XML_XINCLUDE_START)) {
4208 ret->content = xmlStrdup(node->content);
4210 if (node->type == XML_ELEMENT_NODE)
4211 ret->line = node->line;
4213 if (parent != NULL) {
4217 * this is a tricky part for the node register thing:
4218 * in case ret does get coalesced in xmlAddChild
4219 * the deregister-node callback is called; so we register ret now already
4221 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
4222 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4224 tmp = xmlAddChild(parent, ret);
4225 /* node could have coalesced */
4232 if (((node->type == XML_ELEMENT_NODE) ||
4233 (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
4234 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
4236 if (node->ns != NULL) {
4239 ns = xmlSearchNs(doc, ret, node->ns->prefix);
4242 * Humm, we are copying an element whose namespace is defined
4243 * out of the new tree scope. Search it in the original tree
4244 * and add it at the top of the new tree
4246 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
4248 xmlNodePtr root = ret;
4250 while (root->parent != NULL) root = root->parent;
4251 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4253 ret->ns = xmlNewReconciliedNs(doc, ret, node->ns);
4257 * reference the existing namespace definition in our own tree.
4262 if (((node->type == XML_ELEMENT_NODE) ||
4263 (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
4264 ret->properties = xmlCopyPropList(ret, node->properties);
4265 if (node->type == XML_ENTITY_REF_NODE) {
4266 if ((doc == NULL) || (node->doc != doc)) {
4268 * The copied node will go into a separate document, so
4269 * to avoid dangling references to the ENTITY_DECL node
4270 * we cannot keep the reference. Try to find it in the
4273 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
4275 ret->children = node->children;
4277 ret->last = ret->children;
4278 } else if ((node->children != NULL) && (extended != 2)) {
4279 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
4280 UPDATE_LAST_CHILD_AND_PARENT(ret)
4284 /* if parent != NULL we already registered the node above */
4285 if ((parent == NULL) &&
4286 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
4287 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4292 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
4293 xmlNodePtr ret = NULL;
4294 xmlNodePtr p = NULL,q;
4296 while (node != NULL) {
4297 #ifdef LIBXML_TREE_ENABLED
4298 if (node->type == XML_DTD_NODE ) {
4303 if (doc->intSubset == NULL) {
4304 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
4305 if (q == NULL) return(NULL);
4308 doc->intSubset = (xmlDtdPtr) q;
4309 xmlAddChild(parent, q);
4311 q = (xmlNodePtr) doc->intSubset;
4312 xmlAddChild(parent, q);
4315 #endif /* LIBXML_TREE_ENABLED */
4316 q = xmlStaticCopyNode(node, doc, parent, 1);
4317 if (q == NULL) return(NULL);
4321 } else if (p != q) {
4322 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
4335 * @extended: if 1 do a recursive copy (properties, namespaces and children
4337 * if 2 copy properties and namespaces (when applicable)
4339 * Do a copy of the node.
4341 * Returns: a new #xmlNodePtr, or NULL in case of error.
4344 xmlCopyNode(xmlNodePtr node, int extended) {
4347 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
4354 * @doc: the document
4355 * @extended: if 1 do a recursive copy (properties, namespaces and children
4357 * if 2 copy properties and namespaces (when applicable)
4359 * Do a copy of the node to a given document.
4361 * Returns: a new #xmlNodePtr, or NULL in case of error.
4364 xmlDocCopyNode(xmlNodePtr node, xmlDocPtr doc, int extended) {
4367 ret = xmlStaticCopyNode(node, doc, NULL, extended);
4372 * xmlDocCopyNodeList:
4373 * @doc: the target document
4374 * @node: the first node in the list.
4376 * Do a recursive copy of the node list.
4378 * Returns: a new #xmlNodePtr, or NULL in case of error.
4380 xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, xmlNodePtr node) {
4381 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4387 * @node: the first node in the list.
4389 * Do a recursive copy of the node list.
4390 * Use xmlDocCopyNodeList() if possible to ensure string interning.
4392 * Returns: a new #xmlNodePtr, or NULL in case of error.
4394 xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
4395 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4399 #if defined(LIBXML_TREE_ENABLED)
4404 * Do a copy of the dtd.
4406 * Returns: a new #xmlDtdPtr, or NULL in case of error.
4409 xmlCopyDtd(xmlDtdPtr dtd) {
4411 xmlNodePtr cur, p = NULL, q;
4413 if (dtd == NULL) return(NULL);
4414 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4415 if (ret == NULL) return(NULL);
4416 if (dtd->entities != NULL)
4417 ret->entities = (void *) xmlCopyEntitiesTable(
4418 (xmlEntitiesTablePtr) dtd->entities);
4419 if (dtd->notations != NULL)
4420 ret->notations = (void *) xmlCopyNotationTable(
4421 (xmlNotationTablePtr) dtd->notations);
4422 if (dtd->elements != NULL)
4423 ret->elements = (void *) xmlCopyElementTable(
4424 (xmlElementTablePtr) dtd->elements);
4425 if (dtd->attributes != NULL)
4426 ret->attributes = (void *) xmlCopyAttributeTable(
4427 (xmlAttributeTablePtr) dtd->attributes);
4428 if (dtd->pentities != NULL)
4429 ret->pentities = (void *) xmlCopyEntitiesTable(
4430 (xmlEntitiesTablePtr) dtd->pentities);
4432 cur = dtd->children;
4433 while (cur != NULL) {
4436 if (cur->type == XML_ENTITY_DECL) {
4437 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4438 switch (tmp->etype) {
4439 case XML_INTERNAL_GENERAL_ENTITY:
4440 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4441 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4442 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4444 case XML_INTERNAL_PARAMETER_ENTITY:
4445 case XML_EXTERNAL_PARAMETER_ENTITY:
4447 xmlGetParameterEntityFromDtd(ret, tmp->name);
4449 case XML_INTERNAL_PREDEFINED_ENTITY:
4452 } else if (cur->type == XML_ELEMENT_DECL) {
4453 xmlElementPtr tmp = (xmlElementPtr) cur;
4455 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4456 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4457 xmlAttributePtr tmp = (xmlAttributePtr) cur;
4459 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4460 } else if (cur->type == XML_COMMENT_NODE) {
4461 q = xmlCopyNode(cur, 0);
4475 q->parent = (xmlNodePtr) ret;
4486 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
4489 * @doc: the document
4490 * @recursive: if not zero do a recursive copy.
4492 * Do a copy of the document info. If recursive, the content tree will
4493 * be copied too as well as DTD, namespaces and entities.
4495 * Returns: a new #xmlDocPtr, or NULL in case of error.
4498 xmlCopyDoc(xmlDocPtr doc, int recursive) {
4501 if (doc == NULL) return(NULL);
4502 ret = xmlNewDoc(doc->version);
4503 if (ret == NULL) return(NULL);
4504 if (doc->name != NULL)
4505 ret->name = xmlMemStrdup(doc->name);
4506 if (doc->encoding != NULL)
4507 ret->encoding = xmlStrdup(doc->encoding);
4508 if (doc->URL != NULL)
4509 ret->URL = xmlStrdup(doc->URL);
4510 ret->charset = doc->charset;
4511 ret->compression = doc->compression;
4512 ret->standalone = doc->standalone;
4513 if (!recursive) return(ret);
4516 ret->children = NULL;
4517 #ifdef LIBXML_TREE_ENABLED
4518 if (doc->intSubset != NULL) {
4519 ret->intSubset = xmlCopyDtd(doc->intSubset);
4520 if (ret->intSubset == NULL) {
4524 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
4525 ret->intSubset->parent = ret;
4528 if (doc->oldNs != NULL)
4529 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4530 if (doc->children != NULL) {
4533 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4536 tmp = ret->children;
4537 while (tmp != NULL) {
4538 if (tmp->next == NULL)
4545 #endif /* LIBXML_TREE_ENABLED */
4547 /************************************************************************
4549 * Content access functions *
4551 ************************************************************************/
4554 * xmlGetLineNoInternal:
4556 * @depth: used to limit any risk of recursion
4558 * Get line number of @node.
4559 * Try to override the limitation of lines being store in 16 bits ints
4561 * Returns the line number if successful, -1 otherwise
4564 xmlGetLineNoInternal(const xmlNode *node, int depth)
4573 if ((node->type == XML_ELEMENT_NODE) ||
4574 (node->type == XML_TEXT_NODE) ||
4575 (node->type == XML_COMMENT_NODE) ||
4576 (node->type == XML_PI_NODE)) {
4577 if (node->line == 65535) {
4578 if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL))
4579 result = (long) node->psvi;
4580 else if ((node->type == XML_ELEMENT_NODE) &&
4581 (node->children != NULL))
4582 result = xmlGetLineNoInternal(node->children, depth + 1);
4583 else if (node->next != NULL)
4584 result = xmlGetLineNoInternal(node->next, depth + 1);
4585 else if (node->prev != NULL)
4586 result = xmlGetLineNoInternal(node->prev, depth + 1);
4588 if ((result == -1) || (result == 65535))
4589 result = (long) node->line;
4590 } else if ((node->prev != NULL) &&
4591 ((node->prev->type == XML_ELEMENT_NODE) ||
4592 (node->prev->type == XML_TEXT_NODE) ||
4593 (node->prev->type == XML_COMMENT_NODE) ||
4594 (node->prev->type == XML_PI_NODE)))
4595 result = xmlGetLineNoInternal(node->prev, depth + 1);
4596 else if ((node->parent != NULL) &&
4597 (node->parent->type == XML_ELEMENT_NODE))
4598 result = xmlGetLineNoInternal(node->parent, depth + 1);
4607 * Get line number of @node.
4608 * Try to override the limitation of lines being store in 16 bits ints
4609 * if XML_PARSE_BIG_LINES parser option was used
4611 * Returns the line number if successful, -1 otherwise
4614 xmlGetLineNo(const xmlNode *node)
4616 return(xmlGetLineNoInternal(node, 0));
4619 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
4624 * Build a structure based Path for the given node
4626 * Returns the new path or NULL in case of error. The caller must free
4627 * the returned string
4630 xmlGetNodePath(const xmlNode *node)
4632 const xmlNode *cur, *tmp, *next;
4633 xmlChar *buffer = NULL, *temp;
4639 int occur = 0, generic;
4641 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
4645 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4646 if (buffer == NULL) {
4647 xmlTreeErrMemory("getting node path");
4650 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4652 xmlTreeErrMemory("getting node path");
4663 if ((cur->type == XML_DOCUMENT_NODE) ||
4664 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4665 if (buffer[0] == '/')
4669 } else if (cur->type == XML_ELEMENT_NODE) {
4672 name = (const char *) cur->name;
4674 if (cur->ns->prefix != NULL) {
4675 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4676 (char *)cur->ns->prefix, (char *)cur->name);
4677 nametemp[sizeof(nametemp) - 1] = 0;
4681 * We cannot express named elements in the default
4682 * namespace, so use "*".
4691 * Thumbler index computation
4692 * TODO: the ocurence test seems bogus for namespaced names
4695 while (tmp != NULL) {
4696 if ((tmp->type == XML_ELEMENT_NODE) &&
4698 (xmlStrEqual(cur->name, tmp->name) &&
4699 ((tmp->ns == cur->ns) ||
4700 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4701 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4707 while (tmp != NULL && occur == 0) {
4708 if ((tmp->type == XML_ELEMENT_NODE) &&
4710 (xmlStrEqual(cur->name, tmp->name) &&
4711 ((tmp->ns == cur->ns) ||
4712 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4713 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4721 } else if (cur->type == XML_COMMENT_NODE) {
4727 * Thumbler index computation
4730 while (tmp != NULL) {
4731 if (tmp->type == XML_COMMENT_NODE)
4737 while (tmp != NULL && occur == 0) {
4738 if (tmp->type == XML_COMMENT_NODE)
4746 } else if ((cur->type == XML_TEXT_NODE) ||
4747 (cur->type == XML_CDATA_SECTION_NODE)) {
4753 * Thumbler index computation
4756 while (tmp != NULL) {
4757 if ((tmp->type == XML_TEXT_NODE) ||
4758 (tmp->type == XML_CDATA_SECTION_NODE))
4763 * Evaluate if this is the only text- or CDATA-section-node;
4764 * if yes, then we'll get "text()", otherwise "text()[1]".
4768 while (tmp != NULL) {
4769 if ((tmp->type == XML_TEXT_NODE) ||
4770 (tmp->type == XML_CDATA_SECTION_NODE))
4779 } else if (cur->type == XML_PI_NODE) {
4781 snprintf(nametemp, sizeof(nametemp) - 1,
4782 "processing-instruction('%s')", (char *)cur->name);
4783 nametemp[sizeof(nametemp) - 1] = 0;
4789 * Thumbler index computation
4792 while (tmp != NULL) {
4793 if ((tmp->type == XML_PI_NODE) &&
4794 (xmlStrEqual(cur->name, tmp->name)))
4800 while (tmp != NULL && occur == 0) {
4801 if ((tmp->type == XML_PI_NODE) &&
4802 (xmlStrEqual(cur->name, tmp->name)))
4811 } else if (cur->type == XML_ATTRIBUTE_NODE) {
4813 name = (const char *) (((xmlAttrPtr) cur)->name);
4815 if (cur->ns->prefix != NULL)
4816 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4817 (char *)cur->ns->prefix, (char *)cur->name);
4819 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4821 nametemp[sizeof(nametemp) - 1] = 0;
4824 next = ((xmlAttrPtr) cur)->parent;
4830 * Make sure there is enough room
4832 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4834 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4835 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4837 xmlTreeErrMemory("getting node path");
4843 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4845 xmlTreeErrMemory("getting node path");
4853 snprintf((char *) buf, buf_len, "%s%s%s",
4854 sep, name, (char *) buffer);
4856 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
4857 sep, name, occur, (char *) buffer);
4858 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
4860 } while (cur != NULL);
4864 #endif /* LIBXML_TREE_ENABLED */
4867 * xmlDocGetRootElement:
4868 * @doc: the document
4870 * Get the root element of the document (doc->children is a list
4871 * containing possibly comments, PIs, etc ...).
4873 * Returns the #xmlNodePtr for the root or NULL
4876 xmlDocGetRootElement(const xmlDoc *doc) {
4879 if (doc == NULL) return(NULL);
4880 ret = doc->children;
4881 while (ret != NULL) {
4882 if (ret->type == XML_ELEMENT_NODE)
4889 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
4891 * xmlDocSetRootElement:
4892 * @doc: the document
4893 * @root: the new document root element, if root is NULL no action is taken,
4894 * to remove a node from a document use xmlUnlinkNode(root) instead.
4896 * Set the root element of the document (doc->children is a list
4897 * containing possibly comments, PIs, etc ...).
4899 * Returns the old root element if any was found, NULL if root was NULL
4902 xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4903 xmlNodePtr old = NULL;
4905 if (doc == NULL) return(NULL);
4906 if ((root == NULL) || (root->type == XML_NAMESPACE_DECL))
4908 xmlUnlinkNode(root);
4909 xmlSetTreeDoc(root, doc);
4910 root->parent = (xmlNodePtr) doc;
4911 old = doc->children;
4912 while (old != NULL) {
4913 if (old->type == XML_ELEMENT_NODE)
4918 if (doc->children == NULL) {
4919 doc->children = root;
4922 xmlAddSibling(doc->children, root);
4925 xmlReplaceNode(old, root);
4931 #if defined(LIBXML_TREE_ENABLED)
4934 * @cur: the node being changed
4935 * @lang: the language description
4937 * Set the language of a node, i.e. the values of the xml:lang
4941 xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
4944 if (cur == NULL) return;
4947 case XML_CDATA_SECTION_NODE:
4948 case XML_COMMENT_NODE:
4949 case XML_DOCUMENT_NODE:
4950 case XML_DOCUMENT_TYPE_NODE:
4951 case XML_DOCUMENT_FRAG_NODE:
4952 case XML_NOTATION_NODE:
4953 case XML_HTML_DOCUMENT_NODE:
4955 case XML_ELEMENT_DECL:
4956 case XML_ATTRIBUTE_DECL:
4957 case XML_ENTITY_DECL:
4959 case XML_ENTITY_REF_NODE:
4960 case XML_ENTITY_NODE:
4961 case XML_NAMESPACE_DECL:
4962 #ifdef LIBXML_DOCB_ENABLED
4963 case XML_DOCB_DOCUMENT_NODE:
4965 case XML_XINCLUDE_START:
4966 case XML_XINCLUDE_END:
4968 case XML_ELEMENT_NODE:
4969 case XML_ATTRIBUTE_NODE:
4972 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4975 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
4977 #endif /* LIBXML_TREE_ENABLED */
4981 * @cur: the node being checked
4983 * Searches the language of a node, i.e. the values of the xml:lang
4984 * attribute or the one carried by the nearest ancestor.
4986 * Returns a pointer to the lang value, or NULL if not found
4987 * It's up to the caller to free the memory with xmlFree().
4990 xmlNodeGetLang(const xmlNode *cur) {
4993 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
4995 while (cur != NULL) {
4996 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
5005 #ifdef LIBXML_TREE_ENABLED
5007 * xmlNodeSetSpacePreserve:
5008 * @cur: the node being changed
5009 * @val: the xml:space value ("0": default, 1: "preserve")
5011 * Set (or reset) the space preserving behaviour of a node, i.e. the
5012 * value of the xml:space attribute.
5015 xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
5018 if (cur == NULL) return;
5021 case XML_CDATA_SECTION_NODE:
5022 case XML_COMMENT_NODE:
5023 case XML_DOCUMENT_NODE:
5024 case XML_DOCUMENT_TYPE_NODE:
5025 case XML_DOCUMENT_FRAG_NODE:
5026 case XML_NOTATION_NODE:
5027 case XML_HTML_DOCUMENT_NODE:
5029 case XML_ELEMENT_DECL:
5030 case XML_ATTRIBUTE_DECL:
5031 case XML_ENTITY_DECL:
5033 case XML_ENTITY_REF_NODE:
5034 case XML_ENTITY_NODE:
5035 case XML_NAMESPACE_DECL:
5036 case XML_XINCLUDE_START:
5037 case XML_XINCLUDE_END:
5038 #ifdef LIBXML_DOCB_ENABLED
5039 case XML_DOCB_DOCUMENT_NODE:
5042 case XML_ELEMENT_NODE:
5043 case XML_ATTRIBUTE_NODE:
5046 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5051 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
5054 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
5058 #endif /* LIBXML_TREE_ENABLED */
5061 * xmlNodeGetSpacePreserve:
5062 * @cur: the node being checked
5064 * Searches the space preserving behaviour of a node, i.e. the values
5065 * of the xml:space attribute or the one carried by the nearest
5068 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
5071 xmlNodeGetSpacePreserve(const xmlNode *cur) {
5074 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
5076 while (cur != NULL) {
5077 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
5078 if (space != NULL) {
5079 if (xmlStrEqual(space, BAD_CAST "preserve")) {
5083 if (xmlStrEqual(space, BAD_CAST "default")) {
5094 #ifdef LIBXML_TREE_ENABLED
5097 * @cur: the node being changed
5098 * @name: the new tag name
5100 * Set (or reset) the name of a node.
5103 xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
5106 const xmlChar *freeme = NULL;
5108 if (cur == NULL) return;
5109 if (name == NULL) return;
5112 case XML_CDATA_SECTION_NODE:
5113 case XML_COMMENT_NODE:
5114 case XML_DOCUMENT_TYPE_NODE:
5115 case XML_DOCUMENT_FRAG_NODE:
5116 case XML_NOTATION_NODE:
5117 case XML_HTML_DOCUMENT_NODE:
5118 case XML_NAMESPACE_DECL:
5119 case XML_XINCLUDE_START:
5120 case XML_XINCLUDE_END:
5121 #ifdef LIBXML_DOCB_ENABLED
5122 case XML_DOCB_DOCUMENT_NODE:
5125 case XML_ELEMENT_NODE:
5126 case XML_ATTRIBUTE_NODE:
5128 case XML_ENTITY_REF_NODE:
5129 case XML_ENTITY_NODE:
5131 case XML_DOCUMENT_NODE:
5132 case XML_ELEMENT_DECL:
5133 case XML_ATTRIBUTE_DECL:
5134 case XML_ENTITY_DECL:
5143 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
5145 cur->name = xmlDictLookup(dict, name, -1);
5147 if (cur->name != NULL)
5149 cur->name = xmlStrdup(name);
5153 xmlFree((xmlChar *) freeme);
5157 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
5160 * @cur: the node being changed
5161 * @uri: the new base URI
5163 * Set (or reset) the base URI of a node, i.e. the value of the
5164 * xml:base attribute.
5167 xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
5171 if (cur == NULL) return;
5174 case XML_CDATA_SECTION_NODE:
5175 case XML_COMMENT_NODE:
5176 case XML_DOCUMENT_TYPE_NODE:
5177 case XML_DOCUMENT_FRAG_NODE:
5178 case XML_NOTATION_NODE:
5180 case XML_ELEMENT_DECL:
5181 case XML_ATTRIBUTE_DECL:
5182 case XML_ENTITY_DECL:
5184 case XML_ENTITY_REF_NODE:
5185 case XML_ENTITY_NODE:
5186 case XML_NAMESPACE_DECL:
5187 case XML_XINCLUDE_START:
5188 case XML_XINCLUDE_END:
5190 case XML_ELEMENT_NODE:
5191 case XML_ATTRIBUTE_NODE:
5193 case XML_DOCUMENT_NODE:
5194 #ifdef LIBXML_DOCB_ENABLED
5195 case XML_DOCB_DOCUMENT_NODE:
5197 case XML_HTML_DOCUMENT_NODE: {
5198 xmlDocPtr doc = (xmlDocPtr) cur;
5200 if (doc->URL != NULL)
5201 xmlFree((xmlChar *) doc->URL);
5205 doc->URL = xmlPathToURI(uri);
5210 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5213 fixed = xmlPathToURI(uri);
5214 if (fixed != NULL) {
5215 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
5218 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
5221 #endif /* LIBXML_TREE_ENABLED */
5225 * @doc: the document the node pertains to
5226 * @cur: the node being checked
5228 * Searches for the BASE URL. The code should work on both XML
5229 * and HTML document even if base mechanisms are completely different.
5230 * It returns the base as defined in RFC 2396 sections
5231 * 5.1.1. Base URI within Document Content
5233 * 5.1.2. Base URI from the Encapsulating Entity
5234 * However it does not return the document base (5.1.3), use
5235 * doc->URL in this case
5237 * Returns a pointer to the base URL, or NULL if not found
5238 * It's up to the caller to free the memory with xmlFree().
5241 xmlNodeGetBase(const xmlDoc *doc, const xmlNode *cur) {
5242 xmlChar *oldbase = NULL;
5243 xmlChar *base, *newbase;
5245 if ((cur == NULL) && (doc == NULL))
5247 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
5249 if (doc == NULL) doc = cur->doc;
5250 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
5251 cur = doc->children;
5252 while ((cur != NULL) && (cur->name != NULL)) {
5253 if (cur->type != XML_ELEMENT_NODE) {
5257 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5258 cur = cur->children;
5261 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5262 cur = cur->children;
5265 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5266 return(xmlGetProp(cur, BAD_CAST "href"));
5272 while (cur != NULL) {
5273 if (cur->type == XML_ENTITY_DECL) {
5274 xmlEntityPtr ent = (xmlEntityPtr) cur;
5275 return(xmlStrdup(ent->URI));
5277 if (cur->type == XML_ELEMENT_NODE) {
5278 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
5280 if (oldbase != NULL) {
5281 newbase = xmlBuildURI(oldbase, base);
5282 if (newbase != NULL) {
5294 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
5295 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
5296 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
5302 if ((doc != NULL) && (doc->URL != NULL)) {
5303 if (oldbase == NULL)
5304 return(xmlStrdup(doc->URL));
5305 newbase = xmlBuildURI(oldbase, doc->URL);
5313 * xmlNodeBufGetContent:
5315 * @cur: the node being read
5317 * Read the value of a node @cur, this can be either the text carried
5318 * directly by this node if it's a TEXT node or the aggregate string
5319 * of the values carried by this node child's (TEXT and ENTITY_REF).
5320 * Entity references are substituted.
5321 * Fills up the buffer @buffer with this value
5323 * Returns 0 in case of success and -1 in case of error.
5326 xmlNodeBufGetContent(xmlBufferPtr buffer, const xmlNode *cur)
5331 if ((cur == NULL) || (buffer == NULL)) return(-1);
5332 buf = xmlBufFromBuffer(buffer);
5333 ret = xmlBufGetNodeContent(buf, cur);
5334 buffer = xmlBufBackToBuffer(buf);
5335 if ((ret < 0) || (buffer == NULL))
5341 * xmlBufGetNodeContent:
5342 * @buf: a buffer xmlBufPtr
5343 * @cur: the node being read
5345 * Read the value of a node @cur, this can be either the text carried
5346 * directly by this node if it's a TEXT node or the aggregate string
5347 * of the values carried by this node child's (TEXT and ENTITY_REF).
5348 * Entity references are substituted.
5349 * Fills up the buffer @buf with this value
5351 * Returns 0 in case of success and -1 in case of error.
5354 xmlBufGetNodeContent(xmlBufPtr buf, const xmlNode *cur)
5356 if ((cur == NULL) || (buf == NULL)) return(-1);
5357 switch (cur->type) {
5358 case XML_CDATA_SECTION_NODE:
5360 xmlBufCat(buf, cur->content);
5362 case XML_DOCUMENT_FRAG_NODE:
5363 case XML_ELEMENT_NODE:{
5364 const xmlNode *tmp = cur;
5366 while (tmp != NULL) {
5367 switch (tmp->type) {
5368 case XML_CDATA_SECTION_NODE:
5370 if (tmp->content != NULL)
5371 xmlBufCat(buf, tmp->content);
5373 case XML_ENTITY_REF_NODE:
5374 xmlBufGetNodeContent(buf, tmp);
5382 if (tmp->children != NULL) {
5383 if (tmp->children->type != XML_ENTITY_DECL) {
5384 tmp = tmp->children;
5391 if (tmp->next != NULL) {
5404 if (tmp->next != NULL) {
5408 } while (tmp != NULL);
5412 case XML_ATTRIBUTE_NODE:{
5413 xmlAttrPtr attr = (xmlAttrPtr) cur;
5414 xmlNodePtr tmp = attr->children;
5416 while (tmp != NULL) {
5417 if (tmp->type == XML_TEXT_NODE)
5418 xmlBufCat(buf, tmp->content);
5420 xmlBufGetNodeContent(buf, tmp);
5425 case XML_COMMENT_NODE:
5427 xmlBufCat(buf, cur->content);
5429 case XML_ENTITY_REF_NODE:{
5433 /* lookup entity declaration */
5434 ent = xmlGetDocEntity(cur->doc, cur->name);
5438 /* an entity content can be any "well balanced chunk",
5439 * i.e. the result of the content [43] production:
5440 * http://www.w3.org/TR/REC-xml#NT-content
5441 * -> we iterate through child nodes and recursive call
5442 * xmlNodeGetContent() which handles all possible node types */
5443 tmp = ent->children;
5445 xmlBufGetNodeContent(buf, tmp);
5450 case XML_ENTITY_NODE:
5451 case XML_DOCUMENT_TYPE_NODE:
5452 case XML_NOTATION_NODE:
5454 case XML_XINCLUDE_START:
5455 case XML_XINCLUDE_END:
5457 case XML_DOCUMENT_NODE:
5458 #ifdef LIBXML_DOCB_ENABLED
5459 case XML_DOCB_DOCUMENT_NODE:
5461 case XML_HTML_DOCUMENT_NODE:
5462 cur = cur->children;
5463 while (cur!= NULL) {
5464 if ((cur->type == XML_ELEMENT_NODE) ||
5465 (cur->type == XML_TEXT_NODE) ||
5466 (cur->type == XML_CDATA_SECTION_NODE)) {
5467 xmlBufGetNodeContent(buf, cur);
5472 case XML_NAMESPACE_DECL:
5473 xmlBufCat(buf, ((xmlNsPtr) cur)->href);
5475 case XML_ELEMENT_DECL:
5476 case XML_ATTRIBUTE_DECL:
5477 case XML_ENTITY_DECL:
5484 * xmlNodeGetContent:
5485 * @cur: the node being read
5487 * Read the value of a node, this can be either the text carried
5488 * directly by this node if it's a TEXT node or the aggregate string
5489 * of the values carried by this node child's (TEXT and ENTITY_REF).
5490 * Entity references are substituted.
5491 * Returns a new #xmlChar * or NULL if no content is available.
5492 * It's up to the caller to free the memory with xmlFree().
5495 xmlNodeGetContent(const xmlNode *cur)
5499 switch (cur->type) {
5500 case XML_DOCUMENT_FRAG_NODE:
5501 case XML_ELEMENT_NODE:{
5505 buf = xmlBufCreateSize(64);
5508 xmlBufGetNodeContent(buf, cur);
5509 ret = xmlBufDetach(buf);
5513 case XML_ATTRIBUTE_NODE:
5514 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
5515 case XML_COMMENT_NODE:
5517 if (cur->content != NULL)
5518 return (xmlStrdup(cur->content));
5520 case XML_ENTITY_REF_NODE:{
5525 /* lookup entity declaration */
5526 ent = xmlGetDocEntity(cur->doc, cur->name);
5530 buf = xmlBufCreate();
5534 xmlBufGetNodeContent(buf, cur);
5536 ret = xmlBufDetach(buf);
5540 case XML_ENTITY_NODE:
5541 case XML_DOCUMENT_TYPE_NODE:
5542 case XML_NOTATION_NODE:
5544 case XML_XINCLUDE_START:
5545 case XML_XINCLUDE_END:
5547 case XML_DOCUMENT_NODE:
5548 #ifdef LIBXML_DOCB_ENABLED
5549 case XML_DOCB_DOCUMENT_NODE:
5551 case XML_HTML_DOCUMENT_NODE: {
5555 buf = xmlBufCreate();
5559 xmlBufGetNodeContent(buf, (xmlNodePtr) cur);
5561 ret = xmlBufDetach(buf);
5565 case XML_NAMESPACE_DECL: {
5568 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5571 case XML_ELEMENT_DECL:
5574 case XML_ATTRIBUTE_DECL:
5577 case XML_ENTITY_DECL:
5580 case XML_CDATA_SECTION_NODE:
5582 if (cur->content != NULL)
5583 return (xmlStrdup(cur->content));
5590 * xmlNodeSetContent:
5591 * @cur: the node being modified
5592 * @content: the new value of the content
5594 * Replace the content of a node.
5595 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5596 * references, but XML special chars need to be escaped first by using
5597 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5600 xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5603 xmlGenericError(xmlGenericErrorContext,
5604 "xmlNodeSetContent : node == NULL\n");
5608 switch (cur->type) {
5609 case XML_DOCUMENT_FRAG_NODE:
5610 case XML_ELEMENT_NODE:
5611 case XML_ATTRIBUTE_NODE:
5612 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5613 cur->children = xmlStringGetNodeList(cur->doc, content);
5614 UPDATE_LAST_CHILD_AND_PARENT(cur)
5617 case XML_CDATA_SECTION_NODE:
5618 case XML_ENTITY_REF_NODE:
5619 case XML_ENTITY_NODE:
5621 case XML_COMMENT_NODE:
5622 if ((cur->content != NULL) &&
5623 (cur->content != (xmlChar *) &(cur->properties))) {
5624 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5625 (xmlDictOwns(cur->doc->dict, cur->content))))
5626 xmlFree(cur->content);
5628 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5629 cur->last = cur->children = NULL;
5630 if (content != NULL) {
5631 cur->content = xmlStrdup(content);
5633 cur->content = NULL;
5634 cur->properties = NULL;
5637 case XML_DOCUMENT_NODE:
5638 case XML_HTML_DOCUMENT_NODE:
5639 case XML_DOCUMENT_TYPE_NODE:
5640 case XML_XINCLUDE_START:
5641 case XML_XINCLUDE_END:
5642 #ifdef LIBXML_DOCB_ENABLED
5643 case XML_DOCB_DOCUMENT_NODE:
5646 case XML_NOTATION_NODE:
5650 case XML_NAMESPACE_DECL:
5652 case XML_ELEMENT_DECL:
5655 case XML_ATTRIBUTE_DECL:
5658 case XML_ENTITY_DECL:
5664 #ifdef LIBXML_TREE_ENABLED
5666 * xmlNodeSetContentLen:
5667 * @cur: the node being modified
5668 * @content: the new value of the content
5669 * @len: the size of @content
5671 * Replace the content of a node.
5672 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5673 * references, but XML special chars need to be escaped first by using
5674 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5677 xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5680 xmlGenericError(xmlGenericErrorContext,
5681 "xmlNodeSetContentLen : node == NULL\n");
5685 switch (cur->type) {
5686 case XML_DOCUMENT_FRAG_NODE:
5687 case XML_ELEMENT_NODE:
5688 case XML_ATTRIBUTE_NODE:
5689 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5690 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5691 UPDATE_LAST_CHILD_AND_PARENT(cur)
5694 case XML_CDATA_SECTION_NODE:
5695 case XML_ENTITY_REF_NODE:
5696 case XML_ENTITY_NODE:
5698 case XML_COMMENT_NODE:
5699 case XML_NOTATION_NODE:
5700 if ((cur->content != NULL) &&
5701 (cur->content != (xmlChar *) &(cur->properties))) {
5702 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5703 (xmlDictOwns(cur->doc->dict, cur->content))))
5704 xmlFree(cur->content);
5706 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5707 cur->children = cur->last = NULL;
5708 if (content != NULL) {
5709 cur->content = xmlStrndup(content, len);
5711 cur->content = NULL;
5712 cur->properties = NULL;
5715 case XML_DOCUMENT_NODE:
5717 case XML_HTML_DOCUMENT_NODE:
5718 case XML_DOCUMENT_TYPE_NODE:
5719 case XML_NAMESPACE_DECL:
5720 case XML_XINCLUDE_START:
5721 case XML_XINCLUDE_END:
5722 #ifdef LIBXML_DOCB_ENABLED
5723 case XML_DOCB_DOCUMENT_NODE:
5726 case XML_ELEMENT_DECL:
5729 case XML_ATTRIBUTE_DECL:
5732 case XML_ENTITY_DECL:
5737 #endif /* LIBXML_TREE_ENABLED */
5740 * xmlNodeAddContentLen:
5741 * @cur: the node being modified
5742 * @content: extra content
5743 * @len: the size of @content
5745 * Append the extra substring to the node content.
5746 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5747 * raw text, so unescaped XML special chars are allowed, entity
5748 * references are not supported.
5751 xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5754 xmlGenericError(xmlGenericErrorContext,
5755 "xmlNodeAddContentLen : node == NULL\n");
5759 if (len <= 0) return;
5760 switch (cur->type) {
5761 case XML_DOCUMENT_FRAG_NODE:
5762 case XML_ELEMENT_NODE: {
5763 xmlNodePtr last, newNode, tmp;
5766 newNode = xmlNewTextLen(content, len);
5767 if (newNode != NULL) {
5768 tmp = xmlAddChild(cur, newNode);
5771 if ((last != NULL) && (last->next == newNode)) {
5772 xmlTextMerge(last, newNode);
5777 case XML_ATTRIBUTE_NODE:
5780 case XML_CDATA_SECTION_NODE:
5781 case XML_ENTITY_REF_NODE:
5782 case XML_ENTITY_NODE:
5784 case XML_COMMENT_NODE:
5785 case XML_NOTATION_NODE:
5786 if (content != NULL) {
5787 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5788 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5789 xmlDictOwns(cur->doc->dict, cur->content))) {
5790 cur->content = xmlStrncatNew(cur->content, content, len);
5791 cur->properties = NULL;
5795 cur->content = xmlStrncat(cur->content, content, len);
5797 case XML_DOCUMENT_NODE:
5799 case XML_HTML_DOCUMENT_NODE:
5800 case XML_DOCUMENT_TYPE_NODE:
5801 case XML_NAMESPACE_DECL:
5802 case XML_XINCLUDE_START:
5803 case XML_XINCLUDE_END:
5804 #ifdef LIBXML_DOCB_ENABLED
5805 case XML_DOCB_DOCUMENT_NODE:
5808 case XML_ELEMENT_DECL:
5809 case XML_ATTRIBUTE_DECL:
5810 case XML_ENTITY_DECL:
5816 * xmlNodeAddContent:
5817 * @cur: the node being modified
5818 * @content: extra content
5820 * Append the extra substring to the node content.
5821 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5822 * raw text, so unescaped XML special chars are allowed, entity
5823 * references are not supported.
5826 xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5831 xmlGenericError(xmlGenericErrorContext,
5832 "xmlNodeAddContent : node == NULL\n");
5836 if (content == NULL) return;
5837 len = xmlStrlen(content);
5838 xmlNodeAddContentLen(cur, content, len);
5843 * @first: the first text node
5844 * @second: the second text node being merged
5846 * Merge two text nodes into one
5847 * Returns the first text node augmented
5850 xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5851 if (first == NULL) return(second);
5852 if (second == NULL) return(first);
5853 if (first->type != XML_TEXT_NODE) return(first);
5854 if (second->type != XML_TEXT_NODE) return(first);
5855 if (second->name != first->name)
5857 xmlNodeAddContent(first, second->content);
5858 xmlUnlinkNode(second);
5859 xmlFreeNode(second);
5863 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
5866 * @doc: the document
5867 * @node: the current node
5869 * Search all the namespace applying to a given element.
5870 * Returns an NULL terminated array of all the #xmlNsPtr found
5871 * that need to be freed by the caller or NULL if no
5872 * namespace if defined
5875 xmlGetNsList(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node)
5878 xmlNsPtr *ret = NULL;
5883 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5886 while (node != NULL) {
5887 if (node->type == XML_ELEMENT_NODE) {
5889 while (cur != NULL) {
5892 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5895 xmlTreeErrMemory("getting namespace list");
5900 for (i = 0; i < nbns; i++) {
5901 if ((cur->prefix == ret[i]->prefix) ||
5902 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5906 if (nbns >= maxns) {
5908 ret = (xmlNsPtr *) xmlRealloc(ret,
5913 xmlTreeErrMemory("getting namespace list");
5924 node = node->parent;
5928 #endif /* LIBXML_TREE_ENABLED */
5931 * xmlTreeEnsureXMLDecl:
5934 * Ensures that there is an XML namespace declaration on the doc.
5936 * Returns the XML ns-struct or NULL on API and internal errors.
5939 xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5943 if (doc->oldNs != NULL)
5944 return (doc->oldNs);
5947 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5950 "allocating the XML namespace");
5953 memset(ns, 0, sizeof(xmlNs));
5954 ns->type = XML_LOCAL_NAMESPACE;
5955 ns->href = xmlStrdup(XML_XML_NAMESPACE);
5956 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5964 * @doc: the document
5965 * @node: the current node
5966 * @nameSpace: the namespace prefix
5968 * Search a Ns registered under a given name space for a document.
5969 * recurse on the parents until it finds the defined namespace
5970 * or return NULL otherwise.
5971 * @nameSpace can be NULL, this is a search for the default namespace.
5972 * We don't allow to cross entities boundaries. If you don't declare
5973 * the namespace within those you will be in troubles !!! A warning
5974 * is generated to cover this case.
5976 * Returns the namespace pointer or NULL.
5979 xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
5982 const xmlNode *orig = node;
5984 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL);
5985 if ((nameSpace != NULL) &&
5986 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
5987 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5989 * The XML-1.0 namespace is normally held on the root
5990 * element. In this case exceptionally create it on the
5993 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5995 xmlTreeErrMemory("searching namespace");
5998 memset(cur, 0, sizeof(xmlNs));
5999 cur->type = XML_LOCAL_NAMESPACE;
6000 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6001 cur->prefix = xmlStrdup((const xmlChar *)"xml");
6002 cur->next = node->nsDef;
6012 * Return the XML namespace declaration held by the doc.
6014 if (doc->oldNs == NULL)
6015 return(xmlTreeEnsureXMLDecl(doc));
6019 while (node != NULL) {
6020 if ((node->type == XML_ENTITY_REF_NODE) ||
6021 (node->type == XML_ENTITY_NODE) ||
6022 (node->type == XML_ENTITY_DECL))
6024 if (node->type == XML_ELEMENT_NODE) {
6026 while (cur != NULL) {
6027 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6028 (cur->href != NULL))
6030 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6031 (cur->href != NULL) &&
6032 (xmlStrEqual(cur->prefix, nameSpace)))
6039 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6040 (cur->href != NULL))
6042 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6043 (cur->href != NULL) &&
6044 (xmlStrEqual(cur->prefix, nameSpace)))
6049 node = node->parent;
6056 * @doc: the document
6057 * @node: the current node
6058 * @ancestor: the ancestor carrying the namespace
6059 * @prefix: the namespace prefix
6061 * Verify that the given namespace held on @ancestor is still in scope
6064 * Returns 1 if true, 0 if false and -1 in case of error.
6067 xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
6068 xmlNodePtr ancestor, const xmlChar * prefix)
6072 while ((node != NULL) && (node != ancestor)) {
6073 if ((node->type == XML_ENTITY_REF_NODE) ||
6074 (node->type == XML_ENTITY_NODE) ||
6075 (node->type == XML_ENTITY_DECL))
6077 if (node->type == XML_ELEMENT_NODE) {
6079 while (tst != NULL) {
6080 if ((tst->prefix == NULL)
6081 && (prefix == NULL))
6083 if ((tst->prefix != NULL)
6085 && (xmlStrEqual(tst->prefix, prefix)))
6090 node = node->parent;
6092 if (node != ancestor)
6098 * xmlSearchNsByHref:
6099 * @doc: the document
6100 * @node: the current node
6101 * @href: the namespace value
6103 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
6104 * the defined namespace or return NULL otherwise.
6105 * Returns the namespace pointer or NULL.
6108 xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
6111 xmlNodePtr orig = node;
6114 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL))
6116 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
6118 * Only the document can hold the XML spec namespace.
6120 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6122 * The XML-1.0 namespace is normally held on the root
6123 * element. In this case exceptionally create it on the
6126 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6128 xmlTreeErrMemory("searching namespace");
6131 memset(cur, 0, sizeof(xmlNs));
6132 cur->type = XML_LOCAL_NAMESPACE;
6133 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6134 cur->prefix = xmlStrdup((const xmlChar *) "xml");
6135 cur->next = node->nsDef;
6145 * Return the XML namespace declaration held by the doc.
6147 if (doc->oldNs == NULL)
6148 return(xmlTreeEnsureXMLDecl(doc));
6152 is_attr = (node->type == XML_ATTRIBUTE_NODE);
6153 while (node != NULL) {
6154 if ((node->type == XML_ENTITY_REF_NODE) ||
6155 (node->type == XML_ENTITY_NODE) ||
6156 (node->type == XML_ENTITY_DECL))
6158 if (node->type == XML_ELEMENT_NODE) {
6160 while (cur != NULL) {
6161 if ((cur->href != NULL) && (href != NULL) &&
6162 (xmlStrEqual(cur->href, href))) {
6163 if (((!is_attr) || (cur->prefix != NULL)) &&
6164 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6172 if ((cur->href != NULL) && (href != NULL) &&
6173 (xmlStrEqual(cur->href, href))) {
6174 if (((!is_attr) || (cur->prefix != NULL)) &&
6175 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6181 node = node->parent;
6187 * xmlNewReconciliedNs:
6188 * @doc: the document
6189 * @tree: a node expected to hold the new namespace
6190 * @ns: the original namespace
6192 * This function tries to locate a namespace definition in a tree
6193 * ancestors, or create a new namespace definition node similar to
6194 * @ns trying to reuse the same prefix. However if the given prefix is
6195 * null (default namespace) or reused within the subtree defined by
6196 * @tree or on one of its ancestors then a new prefix is generated.
6197 * Returns the (new) namespace definition or NULL in case of error
6200 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
6205 if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) {
6207 xmlGenericError(xmlGenericErrorContext,
6208 "xmlNewReconciliedNs : tree == NULL\n");
6212 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
6214 xmlGenericError(xmlGenericErrorContext,
6215 "xmlNewReconciliedNs : ns == NULL\n");
6220 * Search an existing namespace definition inherited.
6222 def = xmlSearchNsByHref(doc, tree, ns->href);
6227 * Find a close prefix which is not already in use.
6228 * Let's strip namespace prefixes longer than 20 chars !
6230 if (ns->prefix == NULL)
6231 snprintf((char *) prefix, sizeof(prefix), "default");
6233 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
6235 def = xmlSearchNs(doc, tree, prefix);
6236 while (def != NULL) {
6237 if (counter > 1000) return(NULL);
6238 if (ns->prefix == NULL)
6239 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
6241 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
6242 (char *)ns->prefix, counter++);
6243 def = xmlSearchNs(doc, tree, prefix);
6247 * OK, now we are ready to create a new one.
6249 def = xmlNewNs(tree, ns->href, prefix);
6253 #ifdef LIBXML_TREE_ENABLED
6255 * xmlReconciliateNs:
6256 * @doc: the document
6257 * @tree: a node defining the subtree to reconciliate
6259 * This function checks that all the namespaces declared within the given
6260 * tree are properly declared. This is needed for example after Copy or Cut
6261 * and then paste operations. The subtree may still hold pointers to
6262 * namespace declarations outside the subtree or invalid/masked. As much
6263 * as possible the function try to reuse the existing namespaces found in
6264 * the new environment. If not possible the new namespaces are redeclared
6265 * on @tree at the top of the given subtree.
6266 * Returns the number of namespace declarations created or -1 in case of error.
6269 xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
6270 xmlNsPtr *oldNs = NULL;
6271 xmlNsPtr *newNs = NULL;
6276 xmlNodePtr node = tree;
6280 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6281 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
6282 if (node->doc != doc) return(-1);
6283 while (node != NULL) {
6285 * Reconciliate the node namespace
6287 if (node->ns != NULL) {
6289 * initialize the cache if needed
6291 if (sizeCache == 0) {
6293 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6295 if (oldNs == NULL) {
6296 xmlTreeErrMemory("fixing namespaces");
6299 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6301 if (newNs == NULL) {
6302 xmlTreeErrMemory("fixing namespaces");
6307 for (i = 0;i < nbCache;i++) {
6308 if (oldNs[i] == node->ns) {
6309 node->ns = newNs[i];
6315 * OK we need to recreate a new namespace definition
6317 n = xmlNewReconciliedNs(doc, tree, node->ns);
6318 if (n != NULL) { /* :-( what if else ??? */
6320 * check if we need to grow the cache buffers.
6322 if (sizeCache <= nbCache) {
6324 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
6326 if (oldNs == NULL) {
6327 xmlTreeErrMemory("fixing namespaces");
6331 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
6333 if (newNs == NULL) {
6334 xmlTreeErrMemory("fixing namespaces");
6340 oldNs[nbCache++] = node->ns;
6346 * now check for namespace hold by attributes on the node.
6348 if (node->type == XML_ELEMENT_NODE) {
6349 attr = node->properties;
6350 while (attr != NULL) {
6351 if (attr->ns != NULL) {
6353 * initialize the cache if needed
6355 if (sizeCache == 0) {
6357 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6359 if (oldNs == NULL) {
6360 xmlTreeErrMemory("fixing namespaces");
6363 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6365 if (newNs == NULL) {
6366 xmlTreeErrMemory("fixing namespaces");
6371 for (i = 0;i < nbCache;i++) {
6372 if (oldNs[i] == attr->ns) {
6373 attr->ns = newNs[i];
6379 * OK we need to recreate a new namespace definition
6381 n = xmlNewReconciliedNs(doc, tree, attr->ns);
6382 if (n != NULL) { /* :-( what if else ??? */
6384 * check if we need to grow the cache buffers.
6386 if (sizeCache <= nbCache) {
6388 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
6389 sizeCache * sizeof(xmlNsPtr));
6390 if (oldNs == NULL) {
6391 xmlTreeErrMemory("fixing namespaces");
6395 newNs = (xmlNsPtr *) xmlRealloc(newNs,
6396 sizeCache * sizeof(xmlNsPtr));
6397 if (newNs == NULL) {
6398 xmlTreeErrMemory("fixing namespaces");
6404 oldNs[nbCache++] = attr->ns;
6414 * Browse the full subtree, deep first
6416 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
6418 node = node->children;
6419 } else if ((node != tree) && (node->next != NULL)) {
6422 } else if (node != tree) {
6423 /* go up to parents->next if needed */
6424 while (node != tree) {
6425 if (node->parent != NULL)
6426 node = node->parent;
6427 if ((node != tree) && (node->next != NULL)) {
6431 if (node->parent == NULL) {
6436 /* exit condition */
6448 #endif /* LIBXML_TREE_ENABLED */
6451 xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name,
6452 const xmlChar *nsName, int useDTD)
6456 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6459 if (node->properties != NULL) {
6460 prop = node->properties;
6461 if (nsName == NULL) {
6463 * We want the attr to be in no namespace.
6466 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6470 } while (prop != NULL);
6473 * We want the attr to be in the specified namespace.
6476 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6477 ((prop->ns->href == nsName) ||
6478 xmlStrEqual(prop->ns->href, nsName)))
6483 } while (prop != NULL);
6487 #ifdef LIBXML_TREE_ENABLED
6491 * Check if there is a default/fixed attribute declaration in
6492 * the internal or external subset.
6494 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6495 xmlDocPtr doc = node->doc;
6496 xmlAttributePtr attrDecl = NULL;
6497 xmlChar *elemQName, *tmpstr = NULL;
6500 * We need the QName of the element for the DTD-lookup.
6502 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6503 tmpstr = xmlStrdup(node->ns->prefix);
6504 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6505 tmpstr = xmlStrcat(tmpstr, node->name);
6510 elemQName = (xmlChar *) node->name;
6511 if (nsName == NULL) {
6513 * The common and nice case: Attr in no namespace.
6515 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6516 elemQName, name, NULL);
6517 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6518 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6519 elemQName, name, NULL);
6522 xmlNsPtr *nsList, *cur;
6525 * The ugly case: Search using the prefixes of in-scope
6526 * ns-decls corresponding to @nsName.
6528 nsList = xmlGetNsList(node->doc, node);
6529 if (nsList == NULL) {
6535 while (*cur != NULL) {
6536 if (xmlStrEqual((*cur)->href, nsName)) {
6537 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6538 name, (*cur)->prefix);
6541 if (doc->extSubset != NULL) {
6542 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6543 name, (*cur)->prefix);
6555 * Only default/fixed attrs are relevant.
6557 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6558 return((xmlAttrPtr) attrDecl);
6560 #endif /* LIBXML_TREE_ENABLED */
6565 xmlGetPropNodeValueInternal(const xmlAttr *prop)
6569 if (prop->type == XML_ATTRIBUTE_NODE) {
6571 * Note that we return at least the empty string.
6572 * TODO: Do we really always want that?
6574 if (prop->children != NULL) {
6575 if ((prop->children->next == NULL) &&
6576 ((prop->children->type == XML_TEXT_NODE) ||
6577 (prop->children->type == XML_CDATA_SECTION_NODE)))
6580 * Optimization for the common case: only 1 text node.
6582 return(xmlStrdup(prop->children->content));
6586 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6591 return(xmlStrdup((xmlChar *)""));
6592 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6593 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6601 * @name: the attribute name
6603 * Search an attribute associated to a node
6604 * This function also looks in DTD attribute declaration for #FIXED or
6605 * default declaration values unless DTD use has been turned off.
6607 * Returns the attribute or the attribute declaration or NULL if
6608 * neither was found.
6611 xmlHasProp(const xmlNode *node, const xmlChar *name) {
6615 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6618 * Check on the properties attached to the node
6620 prop = node->properties;
6621 while (prop != NULL) {
6622 if (xmlStrEqual(prop->name, name)) {
6627 if (!xmlCheckDTD) return(NULL);
6630 * Check if there is a default declaration in the internal
6631 * or external subsets
6635 xmlAttributePtr attrDecl;
6636 if (doc->intSubset != NULL) {
6637 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6638 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6639 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6640 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6641 /* return attribute declaration only if a default value is given
6642 (that includes #FIXED declarations) */
6643 return((xmlAttrPtr) attrDecl);
6652 * @name: the attribute name
6653 * @nameSpace: the URI of the namespace
6655 * Search for an attribute associated to a node
6656 * This attribute has to be anchored in the namespace specified.
6657 * This does the entity substitution.
6658 * This function looks in DTD attribute declaration for #FIXED or
6659 * default declaration values unless DTD use has been turned off.
6660 * Note that a namespace of NULL indicates to use the default namespace.
6662 * Returns the attribute or the attribute declaration or NULL
6663 * if neither was found.
6666 xmlHasNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
6668 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
6674 * @name: the attribute name
6676 * Search and get the value of an attribute associated to a node
6677 * This does the entity substitution.
6678 * This function looks in DTD attribute declaration for #FIXED or
6679 * default declaration values unless DTD use has been turned off.
6680 * NOTE: this function acts independently of namespaces associated
6681 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6682 * for namespace aware processing.
6684 * Returns the attribute value or NULL if not found.
6685 * It's up to the caller to free the memory with xmlFree().
6688 xmlGetProp(const xmlNode *node, const xmlChar *name) {
6691 prop = xmlHasProp(node, name);
6694 return(xmlGetPropNodeValueInternal(prop));
6700 * @name: the attribute name
6702 * Search and get the value of an attribute associated to a node
6703 * This does the entity substitution.
6704 * This function looks in DTD attribute declaration for #FIXED or
6705 * default declaration values unless DTD use has been turned off.
6706 * This function is similar to xmlGetProp except it will accept only
6707 * an attribute in no namespace.
6709 * Returns the attribute value or NULL if not found.
6710 * It's up to the caller to free the memory with xmlFree().
6713 xmlGetNoNsProp(const xmlNode *node, const xmlChar *name) {
6716 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6719 return(xmlGetPropNodeValueInternal(prop));
6725 * @name: the attribute name
6726 * @nameSpace: the URI of the namespace
6728 * Search and get the value of an attribute associated to a node
6729 * This attribute has to be anchored in the namespace specified.
6730 * This does the entity substitution.
6731 * This function looks in DTD attribute declaration for #FIXED or
6732 * default declaration values unless DTD use has been turned off.
6734 * Returns the attribute value or NULL if not found.
6735 * It's up to the caller to free the memory with xmlFree().
6738 xmlGetNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
6741 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6744 return(xmlGetPropNodeValueInternal(prop));
6747 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6751 * @name: the attribute name
6753 * Remove an attribute carried by a node.
6754 * This handles only attributes in no namespace.
6755 * Returns 0 if successful, -1 if not found
6758 xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
6761 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6764 xmlUnlinkNode((xmlNodePtr) prop);
6772 * @ns: the namespace definition
6773 * @name: the attribute name
6775 * Remove an attribute carried by a node.
6776 * Returns 0 if successful, -1 if not found
6779 xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
6782 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6785 xmlUnlinkNode((xmlNodePtr) prop);
6791 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
6795 * @name: the attribute name (a QName)
6796 * @value: the attribute value
6798 * Set (or reset) an attribute carried by a node.
6799 * If @name has a prefix, then the corresponding
6800 * namespace-binding will be used, if in scope; it is an
6801 * error it there's no such ns-binding for the prefix in
6803 * Returns the attribute pointer.
6807 xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
6809 const xmlChar *nqname;
6811 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
6817 nqname = xmlSplitQName3(name, &len);
6818 if (nqname != NULL) {
6820 xmlChar *prefix = xmlStrndup(name, len);
6821 ns = xmlSearchNs(node->doc, node, prefix);
6825 return(xmlSetNsProp(node, ns, nqname, value));
6827 return(xmlSetNsProp(node, NULL, name, value));
6833 * @ns: the namespace definition
6834 * @name: the attribute name
6835 * @value: the attribute value
6837 * Set (or reset) an attribute carried by a node.
6838 * The ns structure must be in scope, this is not checked
6840 * Returns the attribute pointer.
6843 xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6844 const xmlChar *value)
6848 if (ns && (ns->href == NULL))
6850 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6853 * Modify the attribute's value.
6855 if (prop->atype == XML_ATTRIBUTE_ID) {
6856 xmlRemoveID(node->doc, prop);
6857 prop->atype = XML_ATTRIBUTE_ID;
6859 if (prop->children != NULL)
6860 xmlFreeNodeList(prop->children);
6861 prop->children = NULL;
6864 if (value != NULL) {
6867 if(!xmlCheckUTF8(value)) {
6868 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc,
6870 if (node->doc != NULL)
6871 node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
6873 prop->children = xmlNewDocText(node->doc, value);
6875 tmp = prop->children;
6876 while (tmp != NULL) {
6877 tmp->parent = (xmlNodePtr) prop;
6878 if (tmp->next == NULL)
6883 if (prop->atype == XML_ATTRIBUTE_ID)
6884 xmlAddID(NULL, node->doc, value, prop);
6888 * No equal attr found; create a new one.
6890 return(xmlNewPropInternal(node, ns, name, value, 0));
6893 #endif /* LIBXML_TREE_ENABLED */
6899 * Is this node a Text node ?
6900 * Returns 1 yes, 0 no
6903 xmlNodeIsText(const xmlNode *node) {
6904 if (node == NULL) return(0);
6906 if (node->type == XML_TEXT_NODE) return(1);
6914 * Checks whether this node is an empty or whitespace only
6915 * (and possibly ignorable) text-node.
6917 * Returns 1 yes, 0 no
6920 xmlIsBlankNode(const xmlNode *node) {
6922 if (node == NULL) return(0);
6924 if ((node->type != XML_TEXT_NODE) &&
6925 (node->type != XML_CDATA_SECTION_NODE))
6927 if (node->content == NULL) return(1);
6928 cur = node->content;
6930 if (!IS_BLANK_CH(*cur)) return(0);
6940 * @content: the content
6941 * @len: @content length
6943 * Concat the given string at the end of the existing node content
6945 * Returns -1 in case of error, 0 otherwise
6949 xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
6950 if (node == NULL) return(-1);
6952 if ((node->type != XML_TEXT_NODE) &&
6953 (node->type != XML_CDATA_SECTION_NODE) &&
6954 (node->type != XML_COMMENT_NODE) &&
6955 (node->type != XML_PI_NODE)) {
6957 xmlGenericError(xmlGenericErrorContext,
6958 "xmlTextConcat: node is not text nor CDATA\n");
6962 /* need to check if content is currently in the dictionary */
6963 if ((node->content == (xmlChar *) &(node->properties)) ||
6964 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6965 xmlDictOwns(node->doc->dict, node->content))) {
6966 node->content = xmlStrncatNew(node->content, content, len);
6968 node->content = xmlStrncat(node->content, content, len);
6970 node->properties = NULL;
6971 if (node->content == NULL)
6976 /************************************************************************
6978 * Output : to a FILE or in memory *
6980 ************************************************************************/
6985 * routine to create an XML buffer.
6986 * returns the new structure.
6989 xmlBufferCreate(void) {
6992 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6994 xmlTreeErrMemory("creating buffer");
6998 ret->size = xmlDefaultBufferSize;
6999 ret->alloc = xmlBufferAllocScheme;
7000 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
7001 if (ret->content == NULL) {
7002 xmlTreeErrMemory("creating buffer");
7006 ret->content[0] = 0;
7007 ret->contentIO = NULL;
7012 * xmlBufferCreateSize:
7013 * @size: initial size of buffer
7015 * routine to create an XML buffer.
7016 * returns the new structure.
7019 xmlBufferCreateSize(size_t size) {
7022 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7024 xmlTreeErrMemory("creating buffer");
7028 ret->alloc = xmlBufferAllocScheme;
7029 ret->size = (size ? size+2 : 0); /* +1 for ending null */
7031 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
7032 if (ret->content == NULL) {
7033 xmlTreeErrMemory("creating buffer");
7037 ret->content[0] = 0;
7039 ret->content = NULL;
7040 ret->contentIO = NULL;
7048 * Remove the string contained in a buffer and gie it back to the
7049 * caller. The buffer is reset to an empty content.
7050 * This doesn't work with immutable buffers as they can't be reset.
7052 * Returns the previous string contained by the buffer.
7055 xmlBufferDetach(xmlBufferPtr buf) {
7060 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
7064 buf->content = NULL;
7073 * xmlBufferCreateStatic:
7074 * @mem: the memory area
7075 * @size: the size in byte
7077 * routine to create an XML buffer from an immutable memory area.
7078 * The area won't be modified nor copied, and is expected to be
7079 * present until the end of the buffer lifetime.
7081 * returns the new structure.
7084 xmlBufferCreateStatic(void *mem, size_t size) {
7087 if ((mem == NULL) || (size == 0))
7090 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7092 xmlTreeErrMemory("creating buffer");
7097 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
7098 ret->content = (xmlChar *) mem;
7103 * xmlBufferSetAllocationScheme:
7104 * @buf: the buffer to tune
7105 * @scheme: allocation scheme to use
7107 * Sets the allocation scheme for this buffer
7110 xmlBufferSetAllocationScheme(xmlBufferPtr buf,
7111 xmlBufferAllocationScheme scheme) {
7114 xmlGenericError(xmlGenericErrorContext,
7115 "xmlBufferSetAllocationScheme: buf == NULL\n");
7119 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7120 (buf->alloc == XML_BUFFER_ALLOC_IO)) return;
7121 if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
7122 (scheme == XML_BUFFER_ALLOC_EXACT) ||
7123 (scheme == XML_BUFFER_ALLOC_HYBRID) ||
7124 (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
7125 buf->alloc = scheme;
7130 * @buf: the buffer to free
7132 * Frees an XML buffer. It frees both the content and the structure which
7136 xmlBufferFree(xmlBufferPtr buf) {
7139 xmlGenericError(xmlGenericErrorContext,
7140 "xmlBufferFree: buf == NULL\n");
7145 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7146 (buf->contentIO != NULL)) {
7147 xmlFree(buf->contentIO);
7148 } else if ((buf->content != NULL) &&
7149 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
7150 xmlFree(buf->content);
7162 xmlBufferEmpty(xmlBufferPtr buf) {
7163 if (buf == NULL) return;
7164 if (buf->content == NULL) return;
7166 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
7167 buf->content = BAD_CAST "";
7168 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7169 (buf->contentIO != NULL)) {
7170 size_t start_buf = buf->content - buf->contentIO;
7172 buf->size += start_buf;
7173 buf->content = buf->contentIO;
7174 buf->content[0] = 0;
7176 buf->content[0] = 0;
7182 * @buf: the buffer to dump
7183 * @len: the number of xmlChar to remove
7185 * Remove the beginning of an XML buffer.
7187 * Returns the number of #xmlChar removed, or -1 in case of failure.
7190 xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
7191 if (buf == NULL) return(-1);
7192 if (len == 0) return(0);
7193 if (len > buf->use) return(-1);
7196 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7197 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
7199 * we just move the content pointer, but also make sure
7200 * the perceived buffer size has shrinked accordingly
7202 buf->content += len;
7206 * sometimes though it maybe be better to really shrink
7209 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7210 size_t start_buf = buf->content - buf->contentIO;
7211 if (start_buf >= buf->size) {
7212 memmove(buf->contentIO, &buf->content[0], buf->use);
7213 buf->content = buf->contentIO;
7214 buf->content[buf->use] = 0;
7215 buf->size += start_buf;
7219 memmove(buf->content, &buf->content[len], buf->use);
7220 buf->content[buf->use] = 0;
7228 * @len: the minimum free size to allocate
7230 * Grow the available space of an XML buffer.
7232 * Returns the new available space or -1 in case of error
7235 xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
7239 if (buf == NULL) return(-1);
7241 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7242 if (len + buf->use < buf->size) return(0);
7245 * Windows has a BIG problem on realloc timing, so we try to double
7246 * the buffer size (if that's enough) (bug 146697)
7247 * Apparently BSD too, and it's probably best for linux too
7248 * On an embedded system this may be something to change
7251 if (buf->size > len)
7252 size = buf->size * 2;
7254 size = buf->use + len + 100;
7256 size = buf->use + len + 100;
7259 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7260 size_t start_buf = buf->content - buf->contentIO;
7262 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
7263 if (newbuf == NULL) {
7264 xmlTreeErrMemory("growing buffer");
7267 buf->contentIO = newbuf;
7268 buf->content = newbuf + start_buf;
7270 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
7271 if (newbuf == NULL) {
7272 xmlTreeErrMemory("growing buffer");
7275 buf->content = newbuf;
7278 return(buf->size - buf->use);
7283 * @file: the file output
7284 * @buf: the buffer to dump
7286 * Dumps an XML buffer to a FILE *.
7287 * Returns the number of #xmlChar written
7290 xmlBufferDump(FILE *file, xmlBufferPtr buf) {
7295 xmlGenericError(xmlGenericErrorContext,
7296 "xmlBufferDump: buf == NULL\n");
7300 if (buf->content == NULL) {
7302 xmlGenericError(xmlGenericErrorContext,
7303 "xmlBufferDump: buf->content == NULL\n");
7309 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
7317 * Function to extract the content of a buffer
7319 * Returns the internal content
7323 xmlBufferContent(const xmlBuffer *buf)
7328 return buf->content;
7335 * Function to get the length of a buffer
7337 * Returns the length of data in the internal content
7341 xmlBufferLength(const xmlBuffer *buf)
7351 * @buf: the buffer to resize
7352 * @size: the desired size
7354 * Resize a buffer to accommodate minimum size of @size.
7356 * Returns 0 in case of problems, 1 otherwise
7359 xmlBufferResize(xmlBufferPtr buf, unsigned int size)
7361 unsigned int newSize;
7362 xmlChar* rebuf = NULL;
7368 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7370 /* Don't resize if we don't have to */
7371 if (size < buf->size)
7374 /* figure out new size */
7375 switch (buf->alloc){
7376 case XML_BUFFER_ALLOC_IO:
7377 case XML_BUFFER_ALLOC_DOUBLEIT:
7378 /*take care of empty case*/
7379 newSize = (buf->size ? buf->size*2 : size + 10);
7380 while (size > newSize) {
7381 if (newSize > UINT_MAX / 2) {
7382 xmlTreeErrMemory("growing buffer");
7388 case XML_BUFFER_ALLOC_EXACT:
7391 case XML_BUFFER_ALLOC_HYBRID:
7392 if (buf->use < BASE_BUFFER_SIZE)
7395 newSize = buf->size * 2;
7396 while (size > newSize) {
7397 if (newSize > UINT_MAX / 2) {
7398 xmlTreeErrMemory("growing buffer");
7411 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7412 start_buf = buf->content - buf->contentIO;
7414 if (start_buf > newSize) {
7415 /* move data back to start */
7416 memmove(buf->contentIO, buf->content, buf->use);
7417 buf->content = buf->contentIO;
7418 buf->content[buf->use] = 0;
7419 buf->size += start_buf;
7421 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
7422 if (rebuf == NULL) {
7423 xmlTreeErrMemory("growing buffer");
7426 buf->contentIO = rebuf;
7427 buf->content = rebuf + start_buf;
7430 if (buf->content == NULL) {
7431 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7432 } else if (buf->size - buf->use < 100) {
7433 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
7436 * if we are reallocating a buffer far from being full, it's
7437 * better to make a new allocation and copy only the used range
7438 * and free the old one.
7440 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7441 if (rebuf != NULL) {
7442 memcpy(rebuf, buf->content, buf->use);
7443 xmlFree(buf->content);
7444 rebuf[buf->use] = 0;
7447 if (rebuf == NULL) {
7448 xmlTreeErrMemory("growing buffer");
7451 buf->content = rebuf;
7453 buf->size = newSize;
7460 * @buf: the buffer to dump
7461 * @str: the #xmlChar string
7462 * @len: the number of #xmlChar to add
7464 * Add a string range to an XML buffer. if len == -1, the length of
7465 * str is recomputed.
7467 * Returns 0 successful, a positive error code number otherwise
7468 * and -1 in case of internal or API error.
7471 xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
7472 unsigned int needSize;
7474 if ((str == NULL) || (buf == NULL)) {
7477 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7480 xmlGenericError(xmlGenericErrorContext,
7481 "xmlBufferAdd: len < 0\n");
7485 if (len == 0) return 0;
7488 len = xmlStrlen(str);
7490 if (len < 0) return -1;
7491 if (len == 0) return 0;
7493 needSize = buf->use + len + 2;
7494 if (needSize > buf->size){
7495 if (!xmlBufferResize(buf, needSize)){
7496 xmlTreeErrMemory("growing buffer");
7497 return XML_ERR_NO_MEMORY;
7501 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
7503 buf->content[buf->use] = 0;
7510 * @str: the #xmlChar string
7511 * @len: the number of #xmlChar to add
7513 * Add a string range to the beginning of an XML buffer.
7514 * if len == -1, the length of @str is recomputed.
7516 * Returns 0 successful, a positive error code number otherwise
7517 * and -1 in case of internal or API error.
7520 xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7521 unsigned int needSize;
7525 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7528 xmlGenericError(xmlGenericErrorContext,
7529 "xmlBufferAddHead: str == NULL\n");
7535 xmlGenericError(xmlGenericErrorContext,
7536 "xmlBufferAddHead: len < 0\n");
7540 if (len == 0) return 0;
7543 len = xmlStrlen(str);
7545 if (len <= 0) return -1;
7547 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7548 size_t start_buf = buf->content - buf->contentIO;
7550 if (start_buf > (unsigned int) len) {
7552 * We can add it in the space previously shrinked
7554 buf->content -= len;
7555 memmove(&buf->content[0], str, len);
7561 needSize = buf->use + len + 2;
7562 if (needSize > buf->size){
7563 if (!xmlBufferResize(buf, needSize)){
7564 xmlTreeErrMemory("growing buffer");
7565 return XML_ERR_NO_MEMORY;
7569 memmove(&buf->content[len], &buf->content[0], buf->use);
7570 memmove(&buf->content[0], str, len);
7572 buf->content[buf->use] = 0;
7578 * @buf: the buffer to add to
7579 * @str: the #xmlChar string
7581 * Append a zero terminated string to an XML buffer.
7583 * Returns 0 successful, a positive error code number otherwise
7584 * and -1 in case of internal or API error.
7587 xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
7590 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7591 if (str == NULL) return -1;
7592 return xmlBufferAdd(buf, str, -1);
7597 * @buf: the buffer to dump
7598 * @str: the C char string
7600 * Append a zero terminated C string to an XML buffer.
7602 * Returns 0 successful, a positive error code number otherwise
7603 * and -1 in case of internal or API error.
7606 xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7611 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7614 xmlGenericError(xmlGenericErrorContext,
7615 "xmlBufferCCat: str == NULL\n");
7619 for (cur = str;*cur != 0;cur++) {
7620 if (buf->use + 10 >= buf->size) {
7621 if (!xmlBufferResize(buf, buf->use+10)){
7622 xmlTreeErrMemory("growing buffer");
7623 return XML_ERR_NO_MEMORY;
7626 buf->content[buf->use++] = *cur;
7628 buf->content[buf->use] = 0;
7633 * xmlBufferWriteCHAR:
7634 * @buf: the XML buffer
7635 * @string: the string to add
7637 * routine which manages and grows an output buffer. This one adds
7638 * xmlChars at the end of the buffer.
7641 xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
7644 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7645 xmlBufferCat(buf, string);
7649 * xmlBufferWriteChar:
7650 * @buf: the XML buffer output
7651 * @string: the string to add
7653 * routine which manage and grows an output buffer. This one add
7654 * C chars at the end of the array.
7657 xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
7660 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7661 xmlBufferCCat(buf, string);
7666 * xmlBufferWriteQuotedString:
7667 * @buf: the XML buffer output
7668 * @string: the string to add
7670 * routine which manage and grows an output buffer. This one writes
7671 * a quoted or double quoted #xmlChar string, checking first if it holds
7672 * quote or double-quotes internally
7675 xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
7676 const xmlChar *cur, *base;
7679 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7680 if (xmlStrchr(string, '\"')) {
7681 if (xmlStrchr(string, '\'')) {
7683 xmlGenericError(xmlGenericErrorContext,
7684 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7686 xmlBufferCCat(buf, "\"");
7687 base = cur = string;
7691 xmlBufferAdd(buf, base, cur - base);
7692 xmlBufferAdd(buf, BAD_CAST """, 6);
7701 xmlBufferAdd(buf, base, cur - base);
7702 xmlBufferCCat(buf, "\"");
7705 xmlBufferCCat(buf, "\'");
7706 xmlBufferCat(buf, string);
7707 xmlBufferCCat(buf, "\'");
7710 xmlBufferCCat(buf, "\"");
7711 xmlBufferCat(buf, string);
7712 xmlBufferCCat(buf, "\"");
7718 * xmlGetDocCompressMode:
7719 * @doc: the document
7721 * get the compression ratio for a document, ZLIB based
7722 * Returns 0 (uncompressed) to 9 (max compression)
7725 xmlGetDocCompressMode (const xmlDoc *doc) {
7726 if (doc == NULL) return(-1);
7727 return(doc->compression);
7731 * xmlSetDocCompressMode:
7732 * @doc: the document
7733 * @mode: the compression ratio
7735 * set the compression ratio for a document, ZLIB based
7736 * Correct values: 0 (uncompressed) to 9 (max compression)
7739 xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7740 if (doc == NULL) return;
7741 if (mode < 0) doc->compression = 0;
7742 else if (mode > 9) doc->compression = 9;
7743 else doc->compression = mode;
7747 * xmlGetCompressMode:
7749 * get the default compression mode used, ZLIB based.
7750 * Returns 0 (uncompressed) to 9 (max compression)
7753 xmlGetCompressMode(void)
7755 return (xmlCompressMode);
7759 * xmlSetCompressMode:
7760 * @mode: the compression ratio
7762 * set the default compression mode used, ZLIB based
7763 * Correct values: 0 (uncompressed) to 9 (max compression)
7766 xmlSetCompressMode(int mode) {
7767 if (mode < 0) xmlCompressMode = 0;
7768 else if (mode > 9) xmlCompressMode = 9;
7769 else xmlCompressMode = mode;
7772 #define XML_TREE_NSMAP_PARENT -1
7773 #define XML_TREE_NSMAP_XML -2
7774 #define XML_TREE_NSMAP_DOC -3
7775 #define XML_TREE_NSMAP_CUSTOM -4
7777 typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7778 struct xmlNsMapItem {
7779 xmlNsMapItemPtr next;
7780 xmlNsMapItemPtr prev;
7781 xmlNsPtr oldNs; /* old ns decl reference */
7782 xmlNsPtr newNs; /* new ns decl reference */
7783 int shadowDepth; /* Shadowed at this depth */
7786 * >= 0 == @node's ns-decls
7787 * -1 == @parent's ns-decls
7788 * -2 == the doc->oldNs XML ns-decl
7789 * -3 == the doc->oldNs storage ns-decls
7790 * -4 == ns-decls provided via custom ns-handling
7795 typedef struct xmlNsMap *xmlNsMapPtr;
7797 xmlNsMapItemPtr first;
7798 xmlNsMapItemPtr last;
7799 xmlNsMapItemPtr pool;
7802 #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7803 #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7804 #define XML_NSMAP_POP(m, i) \
7806 (m)->last = (i)->prev; \
7807 if ((m)->last == NULL) \
7808 (m)->first = NULL; \
7810 (m)->last->next = NULL; \
7811 (i)->next = (m)->pool; \
7815 * xmlDOMWrapNsMapFree:
7821 xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7823 xmlNsMapItemPtr cur, tmp;
7828 while (cur != NULL) {
7834 while (cur != NULL) {
7843 * xmlDOMWrapNsMapAddItem:
7845 * @oldNs: the old ns-struct
7846 * @newNs: the new ns-struct
7847 * @depth: depth and ns-kind information
7849 * Adds an ns-mapping item.
7851 static xmlNsMapItemPtr
7852 xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
7853 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
7855 xmlNsMapItemPtr ret;
7860 if ((position != -1) && (position != 0))
7866 * Create the ns-map.
7868 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7870 xmlTreeErrMemory("allocating namespace map");
7873 memset(map, 0, sizeof(struct xmlNsMap));
7877 if (map->pool != NULL) {
7879 * Reuse an item from the pool.
7882 map->pool = ret->next;
7883 memset(ret, 0, sizeof(struct xmlNsMapItem));
7886 * Create a new item.
7888 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7890 xmlTreeErrMemory("allocating namespace map item");
7893 memset(ret, 0, sizeof(struct xmlNsMapItem));
7896 if (map->first == NULL) {
7902 } else if (position == -1) {
7906 ret->prev = map->last;
7907 map->last->next = ret;
7909 } else if (position == 0) {
7911 * Set on first position.
7913 map->first->prev = ret;
7914 ret->next = map->first;
7920 ret->shadowDepth = -1;
7926 * xmlDOMWrapStoreNs:
7928 * @nsName: the namespace name
7929 * @prefix: the prefix
7931 * Creates or reuses an xmlNs struct on doc->oldNs with
7932 * the given prefix and namespace name.
7934 * Returns the aquired ns struct or NULL in case of an API
7935 * or internal error.
7938 xmlDOMWrapStoreNs(xmlDocPtr doc,
7939 const xmlChar *nsName,
7940 const xmlChar *prefix)
7946 ns = xmlTreeEnsureXMLDecl(doc);
7949 if (ns->next != NULL) {
7952 while (ns != NULL) {
7953 if (((ns->prefix == prefix) ||
7954 xmlStrEqual(ns->prefix, prefix)) &&
7955 xmlStrEqual(ns->href, nsName)) {
7958 if (ns->next == NULL)
7965 ns->next = xmlNewNs(NULL, nsName, prefix);
7972 * xmlDOMWrapNewCtxt:
7974 * Allocates and initializes a new DOM-wrapper context.
7976 * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal error.
7979 xmlDOMWrapNewCtxt(void)
7981 xmlDOMWrapCtxtPtr ret;
7983 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
7985 xmlTreeErrMemory("allocating DOM-wrapper context");
7988 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
7993 * xmlDOMWrapFreeCtxt:
7994 * @ctxt: the DOM-wrapper context
7996 * Frees the DOM-wrapper context.
7999 xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
8003 if (ctxt->namespaceMap != NULL)
8004 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
8006 * TODO: Store the namespace map in the context.
8012 * xmlTreeLookupNsListByPrefix:
8013 * @nsList: a list of ns-structs
8014 * @prefix: the searched prefix
8016 * Searches for a ns-decl with the given prefix in @nsList.
8018 * Returns the ns-decl if found, NULL if not found and on
8022 xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
8030 if ((prefix == ns->prefix) ||
8031 xmlStrEqual(prefix, ns->prefix)) {
8035 } while (ns != NULL);
8042 * xmlDOMWrapNSNormGatherInScopeNs:
8043 * @map: the namespace map
8044 * @node: the node to start with
8046 * Puts in-scope namespaces into the ns-map.
8048 * Returns 0 on success, -1 on API or internal errors.
8051 xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
8059 if ((map == NULL) || (*map != NULL))
8061 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8064 * Get in-scope ns-decls of @parent.
8067 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
8068 if (cur->type == XML_ELEMENT_NODE) {
8069 if (cur->nsDef != NULL) {
8073 if (XML_NSMAP_NOTEMPTY(*map)) {
8075 * Skip shadowed prefixes.
8077 XML_NSMAP_FOREACH(*map, mi) {
8078 if ((ns->prefix == mi->newNs->prefix) ||
8079 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
8088 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
8089 ns, XML_TREE_NSMAP_PARENT);
8093 mi->shadowDepth = 0;
8095 } while (ns != NULL);
8104 * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
8105 * otherwise copy it, when it was in the source-dict.
8107 #define XML_TREE_ADOPT_STR(str) \
8108 if (adoptStr && (str != NULL)) { \
8109 if (destDoc->dict) { \
8110 const xmlChar *old = str; \
8111 str = xmlDictLookup(destDoc->dict, str, -1); \
8112 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
8113 (!xmlDictOwns(sourceDoc->dict, old))) \
8114 xmlFree((char *)old); \
8115 } else if ((sourceDoc) && (sourceDoc->dict) && \
8116 xmlDictOwns(sourceDoc->dict, str)) { \
8117 str = BAD_CAST xmlStrdup(str); \
8122 * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
8123 * put it in dest-dict or copy it.
8125 #define XML_TREE_ADOPT_STR_2(str) \
8126 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
8127 (sourceDoc->dict != NULL) && \
8128 xmlDictOwns(sourceDoc->dict, cur->content)) { \
8129 if (destDoc->dict) \
8130 cur->content = (xmlChar *) \
8131 xmlDictLookup(destDoc->dict, cur->content, -1); \
8133 cur->content = xmlStrdup(BAD_CAST cur->content); \
8137 * xmlDOMWrapNSNormAddNsMapItem2:
8139 * For internal use. Adds a ns-decl mapping.
8141 * Returns 0 on success, -1 on internal errors.
8144 xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
8145 xmlNsPtr oldNs, xmlNsPtr newNs)
8147 if (*list == NULL) {
8148 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
8149 if (*list == NULL) {
8150 xmlTreeErrMemory("alloc ns map item");
8155 } else if ((*number) >= (*size)) {
8157 *list = (xmlNsPtr *) xmlRealloc(*list,
8158 (*size) * 2 * sizeof(xmlNsPtr));
8159 if (*list == NULL) {
8160 xmlTreeErrMemory("realloc ns map item");
8164 (*list)[2 * (*number)] = oldNs;
8165 (*list)[2 * (*number) +1] = newNs;
8171 * xmlDOMWrapRemoveNode:
8172 * @ctxt: a DOM wrapper context
8174 * @node: the node to be removed.
8175 * @options: set of options, unused at the moment
8177 * Unlinks the given node from its owner.
8178 * This will substitute ns-references to node->nsDef for
8179 * ns-references to doc->oldNs, thus ensuring the removed
8180 * branch to be autark wrt ns-references.
8182 * NOTE: This function was not intensively tested.
8184 * Returns 0 on success, 1 if the node is not supported,
8185 * -1 on API and internal errors.
8188 xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
8189 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
8191 xmlNsPtr *list = NULL;
8192 int sizeList, nbList, i, j;
8195 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
8198 /* TODO: 0 or -1 ? */
8199 if (node->parent == NULL)
8202 switch (node->type) {
8204 case XML_CDATA_SECTION_NODE:
8205 case XML_ENTITY_REF_NODE:
8207 case XML_COMMENT_NODE:
8208 xmlUnlinkNode(node);
8210 case XML_ELEMENT_NODE:
8211 case XML_ATTRIBUTE_NODE:
8216 xmlUnlinkNode(node);
8218 * Save out-of-scope ns-references in doc->oldNs.
8221 switch (node->type) {
8222 case XML_ELEMENT_NODE:
8223 if ((ctxt == NULL) && (node->nsDef != NULL)) {
8226 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8227 &nbList, ns, ns) == -1)
8228 goto internal_error;
8230 } while (ns != NULL);
8232 /* No break on purpose. */
8233 case XML_ATTRIBUTE_NODE:
8234 if (node->ns != NULL) {
8239 for (i = 0, j = 0; i < nbList; i++, j += 2) {
8240 if (node->ns == list[j]) {
8241 node->ns = list[++j];
8253 * Add to doc's oldNs.
8255 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
8258 goto internal_error;
8264 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8265 &nbList, node->ns, ns) == -1)
8266 goto internal_error;
8270 if ((node->type == XML_ELEMENT_NODE) &&
8271 (node->properties != NULL)) {
8272 node = (xmlNodePtr) node->properties;
8280 if ((node->type == XML_ELEMENT_NODE) &&
8281 (node->children != NULL)) {
8282 node = node->children;
8288 if (node->next != NULL)
8291 node = node->parent;
8294 } while (node != NULL);
8307 * xmlSearchNsByNamespaceStrict:
8308 * @doc: the document
8309 * @node: the start node
8310 * @nsName: the searched namespace name
8311 * @retNs: the resulting ns-decl
8312 * @prefixed: if the found ns-decl must have a prefix (for attributes)
8314 * Dynamically searches for a ns-declaration which matches
8315 * the given @nsName in the ancestor-or-self axis of @node.
8317 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8318 * and internal errors.
8321 xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
8322 const xmlChar* nsName,
8323 xmlNsPtr *retNs, int prefixed)
8325 xmlNodePtr cur, prev = NULL, out = NULL;
8326 xmlNsPtr ns, prevns;
8328 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
8330 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8334 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
8335 *retNs = xmlTreeEnsureXMLDecl(doc);
8342 if (cur->type == XML_ELEMENT_NODE) {
8343 if (cur->nsDef != NULL) {
8344 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8345 if (prefixed && (ns->prefix == NULL))
8349 * Check the last level of ns-decls for a
8352 prevns = prev->nsDef;
8354 if ((prevns->prefix == ns->prefix) ||
8355 ((prevns->prefix != NULL) &&
8356 (ns->prefix != NULL) &&
8357 xmlStrEqual(prevns->prefix, ns->prefix))) {
8363 prevns = prevns->next;
8364 } while (prevns != NULL);
8369 * Ns-name comparison.
8371 if ((nsName == ns->href) ||
8372 xmlStrEqual(nsName, ns->href)) {
8374 * At this point the prefix can only be shadowed,
8375 * if we are the the (at least) 3rd level of
8381 ret = xmlNsInScope(doc, node, prev, ns->prefix);
8385 * TODO: Should we try to find a matching ns-name
8386 * only once? This here keeps on searching.
8387 * I think we should try further since, there might
8388 * be an other matching ns-decl with an unshadowed
8401 } else if ((cur->type == XML_ENTITY_NODE) ||
8402 (cur->type == XML_ENTITY_DECL))
8405 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8410 * xmlSearchNsByPrefixStrict:
8411 * @doc: the document
8412 * @node: the start node
8413 * @prefix: the searched namespace prefix
8414 * @retNs: the resulting ns-decl
8416 * Dynamically searches for a ns-declaration which matches
8417 * the given @nsName in the ancestor-or-self axis of @node.
8419 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8420 * and internal errors.
8423 xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
8424 const xmlChar* prefix,
8430 if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL))
8435 if (IS_STR_XML(prefix)) {
8437 *retNs = xmlTreeEnsureXMLDecl(doc);
8445 if (cur->type == XML_ELEMENT_NODE) {
8446 if (cur->nsDef != NULL) {
8449 if ((prefix == ns->prefix) ||
8450 xmlStrEqual(prefix, ns->prefix))
8453 * Disabled namespaces, e.g. xmlns:abc="".
8455 if (ns->href == NULL)
8462 } while (ns != NULL);
8464 } else if ((cur->type == XML_ENTITY_NODE) ||
8465 (cur->type == XML_ENTITY_DECL))
8468 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8473 * xmlDOMWrapNSNormDeclareNsForced:
8475 * @elem: the element-node to declare on
8476 * @nsName: the namespace-name of the ns-decl
8477 * @prefix: the preferred prefix of the ns-decl
8478 * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
8480 * Declares a new namespace on @elem. It tries to use the
8481 * given @prefix; if a ns-decl with the given prefix is already existent
8482 * on @elem, it will generate an other prefix.
8484 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8485 * and internal errors.
8488 xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
8490 const xmlChar *nsName,
8491 const xmlChar *prefix,
8497 const xmlChar *pref;
8500 if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
8503 * Create a ns-decl on @anchor.
8508 * Lookup whether the prefix is unused in elem's ns-decls.
8510 if ((elem->nsDef != NULL) &&
8511 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
8512 goto ns_next_prefix;
8513 if (checkShadow && elem->parent &&
8514 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8516 * Does it shadow ancestor ns-decls?
8518 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
8519 goto ns_next_prefix;
8521 ret = xmlNewNs(NULL, nsName, pref);
8524 if (elem->nsDef == NULL)
8527 xmlNsPtr ns2 = elem->nsDef;
8528 while (ns2->next != NULL)
8537 if (prefix == NULL) {
8538 snprintf((char *) buf, sizeof(buf),
8541 snprintf((char *) buf, sizeof(buf),
8542 "%.30s_%d", (char *)prefix, counter);
8543 pref = BAD_CAST buf;
8548 * xmlDOMWrapNSNormAquireNormalizedNs:
8550 * @elem: the element-node to declare namespaces on
8551 * @ns: the ns-struct to use for the search
8552 * @retNs: the found/created ns-struct
8553 * @nsMap: the ns-map
8554 * @depth: the current tree depth
8555 * @ancestorsOnly: search in ancestor ns-decls only
8556 * @prefixed: if the searched ns-decl must have a prefix (for attributes)
8558 * Searches for a matching ns-name in the ns-decls of @nsMap, if not
8559 * found it will either declare it on @elem, or store it in doc->oldNs.
8560 * If a new ns-decl needs to be declared on @elem, it tries to use the
8561 * @ns->prefix for it, if this prefix is already in use on @elem, it will
8562 * change the prefix or the new ns-decl.
8564 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8567 xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8579 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
8585 * Handle XML namespace.
8587 if (IS_STR_XML(ns->prefix)) {
8589 * Insert XML namespace mapping.
8591 *retNs = xmlTreeEnsureXMLDecl(doc);
8597 * If the search should be done in ancestors only and no
8598 * @elem (the first ancestor) was specified, then skip the search.
8600 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8601 (! (ancestorsOnly && (elem == NULL))))
8604 * Try to find an equal ns-name in in-scope ns-decls.
8606 XML_NSMAP_FOREACH(*nsMap, mi) {
8607 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8609 * ancestorsOnly: This should be turned on to gain speed,
8610 * if one knows that the branch itself was already
8611 * ns-wellformed and no stale references existed.
8612 * I.e. it searches in the ancestor axis only.
8614 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8615 /* Skip shadowed prefixes. */
8616 (mi->shadowDepth == -1) &&
8617 /* Skip xmlns="" or xmlns:foo="". */
8618 ((mi->newNs->href != NULL) &&
8619 (mi->newNs->href[0] != 0)) &&
8620 /* Ensure a prefix if wanted. */
8621 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8623 ((mi->newNs->href == ns->href) ||
8624 xmlStrEqual(mi->newNs->href, ns->href))) {
8625 /* Set the mapping. */
8633 * No luck, the namespace is out of scope or shadowed.
8639 * Store ns-decls in "oldNs" of the document-node.
8641 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8647 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
8648 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8656 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8661 if (*nsMap != NULL) {
8663 * Does it shadow ancestor ns-decls?
8665 XML_NSMAP_FOREACH(*nsMap, mi) {
8666 if ((mi->depth < depth) &&
8667 (mi->shadowDepth == -1) &&
8668 ((ns->prefix == mi->newNs->prefix) ||
8669 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8673 mi->shadowDepth = depth;
8678 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
8688 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
8689 } xmlDOMReconcileNSOptions;
8692 * xmlDOMWrapReconcileNamespaces:
8693 * @ctxt: DOM wrapper context, unused at the moment
8694 * @elem: the element-node
8695 * @options: option flags
8697 * Ensures that ns-references point to ns-decls hold on element-nodes.
8698 * Ensures that the tree is namespace wellformed by creating additional
8699 * ns-decls where needed. Note that, since prefixes of already existent
8700 * ns-decls can be shadowed by this process, it could break QNames in
8701 * attribute values or element content.
8703 * NOTE: This function was not intensively tested.
8705 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8709 xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8713 int depth = -1, adoptns = 0, parnsdone = 0;
8714 xmlNsPtr ns, prevns;
8716 xmlNodePtr cur, curElem = NULL;
8717 xmlNsMapPtr nsMap = NULL;
8718 xmlNsMapItemPtr /* topmi = NULL, */ mi;
8719 /* @ancestorsOnly should be set by an option flag. */
8720 int ancestorsOnly = 0;
8721 int optRemoveRedundantNS =
8722 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8723 xmlNsPtr *listRedund = NULL;
8724 int sizeRedund = 0, nbRedund = 0, ret, i, j;
8726 if ((elem == NULL) || (elem->doc == NULL) ||
8727 (elem->type != XML_ELEMENT_NODE))
8733 switch (cur->type) {
8734 case XML_ELEMENT_NODE:
8739 * Namespace declarations.
8741 if (cur->nsDef != NULL) {
8744 while (ns != NULL) {
8746 if ((elem->parent) &&
8747 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8749 * Gather ancestor in-scope ns-decls.
8751 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8752 elem->parent) == -1)
8753 goto internal_error;
8759 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8761 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
8762 XML_NSMAP_FOREACH(nsMap, mi) {
8763 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8764 (mi->shadowDepth == -1) &&
8765 ((ns->prefix == mi->newNs->prefix) ||
8766 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8767 ((ns->href == mi->newNs->href) ||
8768 xmlStrEqual(ns->href, mi->newNs->href)))
8771 * A redundant ns-decl was found.
8772 * Add it to the list of redundant ns-decls.
8774 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8775 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8776 goto internal_error;
8778 * Remove the ns-decl from the element-node.
8781 prevns->next = ns->next;
8783 cur->nsDef = ns->next;
8790 * Skip ns-references handling if the referenced
8791 * ns-decl is declared on the same element.
8793 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
8796 * Does it shadow any ns-decl?
8798 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8799 XML_NSMAP_FOREACH(nsMap, mi) {
8800 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8801 (mi->shadowDepth == -1) &&
8802 ((ns->prefix == mi->newNs->prefix) ||
8803 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8805 mi->shadowDepth = depth;
8812 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
8814 goto internal_error;
8823 /* No break on purpose. */
8824 case XML_ATTRIBUTE_NODE:
8825 /* No ns, no fun. */
8826 if (cur->ns == NULL)
8830 if ((elem->parent) &&
8831 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8832 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8833 elem->parent) == -1)
8834 goto internal_error;
8839 * Adjust the reference if this was a redundant ns-decl.
8842 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8843 if (cur->ns == listRedund[j]) {
8844 cur->ns = listRedund[++j];
8850 * Adopt ns-references.
8852 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8854 * Search for a mapping.
8856 XML_NSMAP_FOREACH(nsMap, mi) {
8857 if ((mi->shadowDepth == -1) &&
8858 (cur->ns == mi->oldNs)) {
8860 cur->ns = mi->newNs;
8866 * Aquire a normalized ns-decl and add it to the map.
8868 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8872 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8873 goto internal_error;
8877 if ((cur->type == XML_ELEMENT_NODE) &&
8878 (cur->properties != NULL)) {
8880 * Process attributes.
8882 cur = (xmlNodePtr) cur->properties;
8890 if ((cur->type == XML_ELEMENT_NODE) &&
8891 (cur->children != NULL)) {
8893 * Process content of element-nodes only.
8895 cur = cur->children;
8901 if (cur->type == XML_ELEMENT_NODE) {
8902 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8906 while ((nsMap->last != NULL) &&
8907 (nsMap->last->depth >= depth))
8909 XML_NSMAP_POP(nsMap, mi)
8914 XML_NSMAP_FOREACH(nsMap, mi) {
8915 if (mi->shadowDepth >= depth)
8916 mi->shadowDepth = -1;
8921 if (cur->next != NULL)
8924 if (cur->type == XML_ATTRIBUTE_NODE) {
8931 } while (cur != NULL);
8939 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8940 xmlFreeNs(listRedund[j]);
8942 xmlFree(listRedund);
8945 xmlDOMWrapNsMapFree(nsMap);
8950 * xmlDOMWrapAdoptBranch:
8951 * @ctxt: the optional context for custom processing
8952 * @sourceDoc: the optional sourceDoc
8953 * @node: the element-node to start with
8954 * @destDoc: the destination doc for adoption
8955 * @destParent: the optional new parent of @node in @destDoc
8956 * @options: option flags
8958 * Ensures that ns-references point to @destDoc: either to
8959 * elements->nsDef entries if @destParent is given, or to
8960 * @destDoc->oldNs otherwise.
8961 * If @destParent is given, it ensures that the tree is namespace
8962 * wellformed by creating additional ns-decls where needed.
8963 * Note that, since prefixes of already existent ns-decls can be
8964 * shadowed by this process, it could break QNames in attribute
8965 * values or element content.
8967 * NOTE: This function was not intensively tested.
8969 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8972 xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8973 xmlDocPtr sourceDoc,
8976 xmlNodePtr destParent,
8977 int options ATTRIBUTE_UNUSED)
8980 xmlNodePtr cur, curElem = NULL;
8981 xmlNsMapPtr nsMap = NULL;
8984 int depth = -1, adoptStr = 1;
8985 /* gather @parent's ns-decls. */
8987 /* @ancestorsOnly should be set per option. */
8988 int ancestorsOnly = 0;
8991 * Optimize string adoption for equal or none dicts.
8993 if ((sourceDoc != NULL) &&
8994 (sourceDoc->dict == destDoc->dict))
9000 * Get the ns-map from the context if available.
9003 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9005 * Disable search for ns-decls in the parent-axis of the
9006 * desination element, if:
9007 * 1) there's no destination parent
9008 * 2) custom ns-reference handling is used
9010 if ((destParent == NULL) ||
9011 (ctxt && ctxt->getNsForNodeFunc))
9018 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9019 goto internal_error;
9021 while (cur != NULL) {
9023 * Paranoid source-doc sanity check.
9025 if (cur->doc != sourceDoc) {
9027 * We'll assume XIncluded nodes if the doc differs.
9028 * TODO: Do we need to reconciliate XIncluded nodes?
9029 * This here skips XIncluded nodes and tries to handle
9032 if (cur->next == NULL)
9036 if ((cur->type == XML_XINCLUDE_END) ||
9037 (cur->doc == node->doc))
9039 } while (cur->next != NULL);
9041 if (cur->doc != node->doc)
9045 switch (cur->type) {
9046 case XML_XINCLUDE_START:
9047 case XML_XINCLUDE_END:
9052 case XML_ELEMENT_NODE:
9056 * Namespace declarations.
9057 * - ns->href and ns->prefix are never in the dict, so
9058 * we need not move the values over to the destination dict.
9059 * - Note that for custom handling of ns-references,
9060 * the ns-decls need not be stored in the ns-map,
9061 * since they won't be referenced by node->ns.
9064 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
9068 * Gather @parent's in-scope ns-decls.
9070 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9072 goto internal_error;
9075 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9077 * NOTE: ns->prefix and ns->href are never in the dict.
9078 * XML_TREE_ADOPT_STR(ns->prefix)
9079 * XML_TREE_ADOPT_STR(ns->href)
9082 * Does it shadow any ns-decl?
9084 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9085 XML_NSMAP_FOREACH(nsMap, mi) {
9086 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9087 (mi->shadowDepth == -1) &&
9088 ((ns->prefix == mi->newNs->prefix) ||
9089 xmlStrEqual(ns->prefix,
9090 mi->newNs->prefix))) {
9092 mi->shadowDepth = depth;
9099 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9100 ns, ns, depth) == NULL)
9101 goto internal_error;
9104 /* No break on purpose. */
9105 case XML_ATTRIBUTE_NODE:
9106 /* No namespace, no fun. */
9107 if (cur->ns == NULL)
9111 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9113 goto internal_error;
9117 * Adopt ns-references.
9119 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9121 * Search for a mapping.
9123 XML_NSMAP_FOREACH(nsMap, mi) {
9124 if ((mi->shadowDepth == -1) &&
9125 (cur->ns == mi->oldNs)) {
9127 cur->ns = mi->newNs;
9133 * No matching namespace in scope. We need a new one.
9135 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
9137 * User-defined behaviour.
9139 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9140 cur->ns->href, cur->ns->prefix);
9142 * Insert mapping if ns is available; it's the users fault
9145 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9146 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9147 goto internal_error;
9151 * Aquire a normalized ns-decl and add it to the map.
9153 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9154 /* ns-decls on curElem or on destDoc->oldNs */
9155 destParent ? curElem : NULL,
9159 /* ns-decls must be prefixed for attributes. */
9160 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9161 goto internal_error;
9166 * Further node properties.
9167 * TODO: Is this all?
9169 XML_TREE_ADOPT_STR(cur->name)
9170 if (cur->type == XML_ELEMENT_NODE) {
9177 if (cur->properties != NULL) {
9179 * Process first attribute node.
9181 cur = (xmlNodePtr) cur->properties;
9188 if ((sourceDoc != NULL) &&
9189 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
9191 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
9193 ((xmlAttrPtr) cur)->atype = 0;
9194 ((xmlAttrPtr) cur)->psvi = NULL;
9198 case XML_CDATA_SECTION_NODE:
9200 * This puts the content in the dest dict, only if
9201 * it was previously in the source dict.
9203 XML_TREE_ADOPT_STR_2(cur->content)
9205 case XML_ENTITY_REF_NODE:
9207 * Remove reference to the entitity-node.
9209 cur->content = NULL;
9210 cur->children = NULL;
9212 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9215 * Assign new entity-node if available.
9217 ent = xmlGetDocEntity(destDoc, cur->name);
9219 cur->content = ent->content;
9220 cur->children = (xmlNodePtr) ent;
9221 cur->last = (xmlNodePtr) ent;
9226 XML_TREE_ADOPT_STR(cur->name)
9227 XML_TREE_ADOPT_STR_2(cur->content)
9229 case XML_COMMENT_NODE:
9232 goto internal_error;
9237 if (cur->children != NULL) {
9238 cur = cur->children;
9245 if ((cur->type == XML_ELEMENT_NODE) ||
9246 (cur->type == XML_XINCLUDE_START) ||
9247 (cur->type == XML_XINCLUDE_END))
9250 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9252 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9256 while ((nsMap->last != NULL) &&
9257 (nsMap->last->depth >= depth))
9259 XML_NSMAP_POP(nsMap, mi)
9264 XML_NSMAP_FOREACH(nsMap, mi) {
9265 if (mi->shadowDepth >= depth)
9266 mi->shadowDepth = -1;
9271 if (cur->next != NULL)
9273 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
9274 (cur->parent->children != NULL))
9276 cur = cur->parent->children;
9292 if (nsMap != NULL) {
9293 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9295 * Just cleanup the map but don't free.
9299 nsMap->last->next = nsMap->pool;
9300 nsMap->pool = nsMap->first;
9301 nsMap->first = NULL;
9304 xmlDOMWrapNsMapFree(nsMap);
9310 * xmlDOMWrapCloneNode:
9311 * @ctxt: the optional context for custom processing
9312 * @sourceDoc: the optional sourceDoc
9313 * @node: the node to start with
9314 * @resNode: the clone of the given @node
9315 * @destDoc: the destination doc
9316 * @destParent: the optional new parent of @node in @destDoc
9317 * @deep: descend into child if set
9318 * @options: option flags
9320 * References of out-of scope ns-decls are remapped to point to @destDoc:
9321 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9322 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
9323 * This is the case when you don't know already where the cloned branch
9326 * If @destParent is given, it ensures that the tree is namespace
9327 * wellformed by creating additional ns-decls where needed.
9328 * Note that, since prefixes of already existent ns-decls can be
9329 * shadowed by this process, it could break QNames in attribute
9330 * values or element content.
9332 * 1) What to do with XInclude? Currently this returns an error for XInclude.
9334 * Returns 0 if the operation succeeded,
9335 * 1 if a node of unsupported (or not yet supported) type was given,
9336 * -1 on API/internal errors.
9340 xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
9341 xmlDocPtr sourceDoc,
9343 xmlNodePtr *resNode,
9345 xmlNodePtr destParent,
9347 int options ATTRIBUTE_UNUSED)
9350 xmlNodePtr cur, curElem = NULL;
9351 xmlNsMapPtr nsMap = NULL;
9355 /* int adoptStr = 1; */
9356 /* gather @parent's ns-decls. */
9360 * TODO: @ancestorsOnly should be set per option.
9363 int ancestorsOnly = 0;
9364 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
9365 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
9366 xmlDictPtr dict; /* The destination dict */
9368 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
9371 * TODO: Initially we support only element-nodes.
9373 if (node->type != XML_ELEMENT_NODE)
9376 * Check node->doc sanity.
9378 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9379 (node->doc != sourceDoc)) {
9381 * Might be an XIncluded node.
9385 if (sourceDoc == NULL)
9386 sourceDoc = node->doc;
9387 if (sourceDoc == NULL)
9390 dict = destDoc->dict;
9392 * Reuse the namespace map of the context.
9395 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9400 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9403 while (cur != NULL) {
9404 if (cur->doc != sourceDoc) {
9406 * We'll assume XIncluded nodes if the doc differs.
9407 * TODO: Do we need to reconciliate XIncluded nodes?
9408 * TODO: This here returns -1 in this case.
9410 goto internal_error;
9413 * Create a new node.
9415 switch (cur->type) {
9416 case XML_XINCLUDE_START:
9417 case XML_XINCLUDE_END:
9419 * TODO: What to do with XInclude?
9421 goto internal_error;
9423 case XML_ELEMENT_NODE:
9425 case XML_CDATA_SECTION_NODE:
9426 case XML_COMMENT_NODE:
9428 case XML_DOCUMENT_FRAG_NODE:
9429 case XML_ENTITY_REF_NODE:
9430 case XML_ENTITY_NODE:
9432 * Nodes of xmlNode structure.
9434 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
9435 if (clone == NULL) {
9436 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
9437 goto internal_error;
9439 memset(clone, 0, sizeof(xmlNode));
9441 * Set hierachical links.
9443 if (resultClone != NULL) {
9444 clone->parent = parentClone;
9446 prevClone->next = clone;
9447 clone->prev = prevClone;
9449 parentClone->children = clone;
9451 resultClone = clone;
9454 case XML_ATTRIBUTE_NODE:
9456 * Attributes (xmlAttr).
9458 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
9459 if (clone == NULL) {
9460 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
9461 goto internal_error;
9463 memset(clone, 0, sizeof(xmlAttr));
9465 * Set hierachical links.
9466 * TODO: Change this to add to the end of attributes.
9468 if (resultClone != NULL) {
9469 clone->parent = parentClone;
9471 prevClone->next = clone;
9472 clone->prev = prevClone;
9474 parentClone->properties = (xmlAttrPtr) clone;
9476 resultClone = clone;
9480 * TODO QUESTION: Any other nodes expected?
9482 goto internal_error;
9485 clone->type = cur->type;
9486 clone->doc = destDoc;
9489 * Clone the name of the node if any.
9491 if (cur->name == xmlStringText)
9492 clone->name = xmlStringText;
9493 else if (cur->name == xmlStringTextNoenc)
9495 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9496 * in tree.c, it might be set in Libxslt via
9497 * "xsl:disable-output-escaping".
9499 clone->name = xmlStringTextNoenc;
9500 else if (cur->name == xmlStringComment)
9501 clone->name = xmlStringComment;
9502 else if (cur->name != NULL) {
9503 DICT_CONST_COPY(cur->name, clone->name);
9506 switch (cur->type) {
9507 case XML_XINCLUDE_START:
9508 case XML_XINCLUDE_END:
9513 case XML_ELEMENT_NODE:
9517 * Namespace declarations.
9519 if (cur->nsDef != NULL) {
9521 if (destParent && (ctxt == NULL)) {
9523 * Gather @parent's in-scope ns-decls.
9525 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9527 goto internal_error;
9532 * Clone namespace declarations.
9534 cloneNsDefSlot = &(clone->nsDef);
9535 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9537 * Create a new xmlNs.
9539 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
9540 if (cloneNs == NULL) {
9541 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
9542 "allocating namespace");
9545 memset(cloneNs, 0, sizeof(xmlNs));
9546 cloneNs->type = XML_LOCAL_NAMESPACE;
9548 if (ns->href != NULL)
9549 cloneNs->href = xmlStrdup(ns->href);
9550 if (ns->prefix != NULL)
9551 cloneNs->prefix = xmlStrdup(ns->prefix);
9553 *cloneNsDefSlot = cloneNs;
9554 cloneNsDefSlot = &(cloneNs->next);
9557 * Note that for custom handling of ns-references,
9558 * the ns-decls need not be stored in the ns-map,
9559 * since they won't be referenced by node->ns.
9561 if ((ctxt == NULL) ||
9562 (ctxt->getNsForNodeFunc == NULL))
9565 * Does it shadow any ns-decl?
9567 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9568 XML_NSMAP_FOREACH(nsMap, mi) {
9569 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9570 (mi->shadowDepth == -1) &&
9571 ((ns->prefix == mi->newNs->prefix) ||
9572 xmlStrEqual(ns->prefix,
9573 mi->newNs->prefix))) {
9575 * Mark as shadowed at the current
9578 mi->shadowDepth = depth;
9585 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9586 ns, cloneNs, depth) == NULL)
9587 goto internal_error;
9591 /* cur->ns will be processed further down. */
9593 case XML_ATTRIBUTE_NODE:
9594 /* IDs will be processed further down. */
9595 /* cur->ns will be processed further down. */
9598 case XML_CDATA_SECTION_NODE:
9600 * Note that this will also cover the values of attributes.
9602 DICT_COPY(cur->content, clone->content);
9604 case XML_ENTITY_NODE:
9605 /* TODO: What to do here? */
9607 case XML_ENTITY_REF_NODE:
9608 if (sourceDoc != destDoc) {
9609 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9612 * Different doc: Assign new entity-node if available.
9614 ent = xmlGetDocEntity(destDoc, cur->name);
9616 clone->content = ent->content;
9617 clone->children = (xmlNodePtr) ent;
9618 clone->last = (xmlNodePtr) ent;
9623 * Same doc: Use the current node's entity declaration
9626 clone->content = cur->content;
9627 clone->children = cur->children;
9628 clone->last = cur->last;
9632 DICT_COPY(cur->content, clone->content);
9634 case XML_COMMENT_NODE:
9635 DICT_COPY(cur->content, clone->content);
9638 goto internal_error;
9641 if (cur->ns == NULL)
9642 goto end_ns_reference;
9644 /* handle_ns_reference: */
9646 ** The following will take care of references to ns-decls ********
9647 ** and is intended only for element- and attribute-nodes.
9651 if (destParent && (ctxt == NULL)) {
9652 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
9653 goto internal_error;
9658 * Adopt ns-references.
9660 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9662 * Search for a mapping.
9664 XML_NSMAP_FOREACH(nsMap, mi) {
9665 if ((mi->shadowDepth == -1) &&
9666 (cur->ns == mi->oldNs)) {
9668 * This is the nice case: a mapping was found.
9670 clone->ns = mi->newNs;
9671 goto end_ns_reference;
9676 * No matching namespace in scope. We need a new one.
9678 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
9680 * User-defined behaviour.
9682 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9683 cur->ns->href, cur->ns->prefix);
9685 * Add user's mapping.
9687 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9688 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9689 goto internal_error;
9693 * Aquire a normalized ns-decl and add it to the map.
9695 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9696 /* ns-decls on curElem or on destDoc->oldNs */
9697 destParent ? curElem : NULL,
9700 /* if we need to search only in the ancestor-axis */
9702 /* ns-decls must be prefixed for attributes. */
9703 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9704 goto internal_error;
9711 * Some post-processing.
9713 * Handle ID attributes.
9715 if ((clone->type == XML_ATTRIBUTE_NODE) &&
9716 (clone->parent != NULL))
9718 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
9722 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9723 if (idVal != NULL) {
9724 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9725 /* TODO: error message. */
9727 goto internal_error;
9735 ** The following will traverse the tree **************************
9738 * Walk the element's attributes before descending into child-nodes.
9740 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9742 parentClone = clone;
9743 cur = (xmlNodePtr) cur->properties;
9748 * Descend into child-nodes.
9750 if (cur->children != NULL) {
9751 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9753 parentClone = clone;
9754 cur = cur->children;
9761 * At this point we are done with the node, its content
9762 * and an element-nodes's attribute-nodes.
9766 if ((cur->type == XML_ELEMENT_NODE) ||
9767 (cur->type == XML_XINCLUDE_START) ||
9768 (cur->type == XML_XINCLUDE_END)) {
9770 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9772 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9776 while ((nsMap->last != NULL) &&
9777 (nsMap->last->depth >= depth))
9779 XML_NSMAP_POP(nsMap, mi)
9784 XML_NSMAP_FOREACH(nsMap, mi) {
9785 if (mi->shadowDepth >= depth)
9786 mi->shadowDepth = -1;
9791 if (cur->next != NULL) {
9794 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9798 if (clone->parent != NULL)
9799 clone->parent->last = clone;
9800 clone = clone->parent;
9802 parentClone = clone->parent;
9804 * Process parent --> next;
9809 /* This is for attributes only. */
9810 clone = clone->parent;
9811 parentClone = clone->parent;
9813 * Process parent-element --> children.
9828 if (nsMap != NULL) {
9829 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9831 * Just cleanup the map but don't free.
9835 nsMap->last->next = nsMap->pool;
9836 nsMap->pool = nsMap->first;
9837 nsMap->first = NULL;
9840 xmlDOMWrapNsMapFree(nsMap);
9843 * TODO: Should we try a cleanup of the cloned node in case of a
9846 *resNode = resultClone;
9851 * xmlDOMWrapAdoptAttr:
9852 * @ctxt: the optional context for custom processing
9853 * @sourceDoc: the optional source document of attr
9854 * @attr: the attribute-node to be adopted
9855 * @destDoc: the destination doc for adoption
9856 * @destParent: the optional new parent of @attr in @destDoc
9857 * @options: option flags
9859 * @attr is adopted by @destDoc.
9860 * Ensures that ns-references point to @destDoc: either to
9861 * elements->nsDef entries if @destParent is given, or to
9862 * @destDoc->oldNs otherwise.
9864 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9867 xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9868 xmlDocPtr sourceDoc,
9871 xmlNodePtr destParent,
9872 int options ATTRIBUTE_UNUSED)
9877 if ((attr == NULL) || (destDoc == NULL))
9880 attr->doc = destDoc;
9881 if (attr->ns != NULL) {
9885 /* TODO: User defined. */
9887 /* XML Namespace. */
9888 if (IS_STR_XML(attr->ns->prefix)) {
9889 ns = xmlTreeEnsureXMLDecl(destDoc);
9890 } else if (destParent == NULL) {
9892 * Store in @destDoc->oldNs.
9894 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9897 * Declare on @destParent.
9899 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
9901 goto internal_error;
9903 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9904 attr->ns->href, attr->ns->prefix, 1);
9908 goto internal_error;
9912 XML_TREE_ADOPT_STR(attr->name);
9918 if (attr->children == NULL)
9920 cur = attr->children;
9921 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9922 goto internal_error;
9923 while (cur != NULL) {
9925 switch (cur->type) {
9927 case XML_CDATA_SECTION_NODE:
9928 XML_TREE_ADOPT_STR_2(cur->content)
9930 case XML_ENTITY_REF_NODE:
9932 * Remove reference to the entitity-node.
9934 cur->content = NULL;
9935 cur->children = NULL;
9937 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9940 * Assign new entity-node if available.
9942 ent = xmlGetDocEntity(destDoc, cur->name);
9944 cur->content = ent->content;
9945 cur->children = (xmlNodePtr) ent;
9946 cur->last = (xmlNodePtr) ent;
9953 if (cur->children != NULL) {
9954 cur = cur->children;
9958 if (cur == (xmlNodePtr) attr)
9960 if (cur->next != NULL)
9973 * xmlDOMWrapAdoptNode:
9974 * @ctxt: the optional context for custom processing
9975 * @sourceDoc: the optional sourceDoc
9976 * @node: the node to start with
9977 * @destDoc: the destination doc
9978 * @destParent: the optional new parent of @node in @destDoc
9979 * @options: option flags
9981 * References of out-of scope ns-decls are remapped to point to @destDoc:
9982 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9983 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
9984 * This is the case when you have an unlinked node and just want to move it
9987 * If @destParent is given, it ensures that the tree is namespace
9988 * wellformed by creating additional ns-decls where needed.
9989 * Note that, since prefixes of already existent ns-decls can be
9990 * shadowed by this process, it could break QNames in attribute
9991 * values or element content.
9992 * NOTE: This function was not intensively tested.
9994 * Returns 0 if the operation succeeded,
9995 * 1 if a node of unsupported type was given,
9996 * 2 if a node of not yet supported type was given and
9997 * -1 on API/internal errors.
10000 xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
10001 xmlDocPtr sourceDoc,
10004 xmlNodePtr destParent,
10007 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
10008 (destDoc == NULL) ||
10009 ((destParent != NULL) && (destParent->doc != destDoc)))
10012 * Check node->doc sanity.
10014 if ((node->doc != NULL) && (sourceDoc != NULL) &&
10015 (node->doc != sourceDoc)) {
10017 * Might be an XIncluded node.
10021 if (sourceDoc == NULL)
10022 sourceDoc = node->doc;
10023 if (sourceDoc == destDoc)
10025 switch (node->type) {
10026 case XML_ELEMENT_NODE:
10027 case XML_ATTRIBUTE_NODE:
10028 case XML_TEXT_NODE:
10029 case XML_CDATA_SECTION_NODE:
10030 case XML_ENTITY_REF_NODE:
10032 case XML_COMMENT_NODE:
10034 case XML_DOCUMENT_FRAG_NODE:
10035 /* TODO: Support document-fragment-nodes. */
10041 * Unlink only if @node was not already added to @destParent.
10043 if ((node->parent != NULL) && (destParent != node->parent))
10044 xmlUnlinkNode(node);
10046 if (node->type == XML_ELEMENT_NODE) {
10047 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
10048 destDoc, destParent, options));
10049 } else if (node->type == XML_ATTRIBUTE_NODE) {
10050 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
10051 (xmlAttrPtr) node, destDoc, destParent, options));
10053 xmlNodePtr cur = node;
10056 cur->doc = destDoc;
10058 * Optimize string adoption.
10060 if ((sourceDoc != NULL) &&
10061 (sourceDoc->dict == destDoc->dict))
10063 switch (node->type) {
10064 case XML_TEXT_NODE:
10065 case XML_CDATA_SECTION_NODE:
10066 XML_TREE_ADOPT_STR_2(node->content)
10068 case XML_ENTITY_REF_NODE:
10070 * Remove reference to the entitity-node.
10072 node->content = NULL;
10073 node->children = NULL;
10075 if ((destDoc->intSubset) || (destDoc->extSubset)) {
10078 * Assign new entity-node if available.
10080 ent = xmlGetDocEntity(destDoc, node->name);
10082 node->content = ent->content;
10083 node->children = (xmlNodePtr) ent;
10084 node->last = (xmlNodePtr) ent;
10087 XML_TREE_ADOPT_STR(node->name)
10089 case XML_PI_NODE: {
10090 XML_TREE_ADOPT_STR(node->name)
10091 XML_TREE_ADOPT_STR_2(node->content)
10101 #define bottom_tree
10102 #include "elfgcchack.h"