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" dictionary 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" dictionary 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" dictionary 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 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 = (xmlNodePtr) -1;
1597 ent->children = xmlStringGetNodeList(doc,
1598 (const xmlChar*)node->content);
1600 temp = ent->children;
1602 temp->parent = (xmlNodePtr)ent;
1609 last = xmlAddNextSibling(last, node);
1621 len = xmlCopyCharMultiByte(buffer, charval);
1624 if (xmlBufCat(buf, buffer))
1631 if ((cur != q) || (ret == NULL)) {
1633 * Handle the last piece of text.
1635 xmlBufAdd(buf, q, cur - q);
1638 if (!xmlBufIsEmpty(buf)) {
1639 node = xmlNewDocText(doc, NULL);
1640 node->content = xmlBufDetach(buf);
1645 xmlAddNextSibling(last, node);
1655 * xmlNodeListGetString:
1656 * @doc: the document
1657 * @list: a Node list
1658 * @inLine: should we replace entity contents or show their external form
1660 * Build the string equivalent to the text contained in the Node list
1661 * made of TEXTs and ENTITY_REFs
1663 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1666 xmlNodeListGetString(xmlDocPtr doc, const xmlNode *list, int inLine)
1668 const xmlNode *node = list;
1669 xmlChar *ret = NULL;
1675 if ((list->parent != NULL) && (list->parent->type == XML_ATTRIBUTE_NODE))
1680 while (node != NULL) {
1681 if ((node->type == XML_TEXT_NODE) ||
1682 (node->type == XML_CDATA_SECTION_NODE)) {
1684 ret = xmlStrcat(ret, node->content);
1689 buffer = xmlEncodeAttributeEntities(doc, node->content);
1691 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1692 if (buffer != NULL) {
1693 ret = xmlStrcat(ret, buffer);
1697 } else if (node->type == XML_ENTITY_REF_NODE) {
1699 ent = xmlGetDocEntity(doc, node->name);
1703 /* an entity content can be any "well balanced chunk",
1704 * i.e. the result of the content [43] production:
1705 * http://www.w3.org/TR/REC-xml#NT-content.
1706 * So it can contain text, CDATA section or nested
1707 * entity reference nodes (among others).
1708 * -> we recursive call xmlNodeListGetString()
1709 * which handles these types */
1710 buffer = xmlNodeListGetString(doc, ent->children, 1);
1711 if (buffer != NULL) {
1712 ret = xmlStrcat(ret, buffer);
1716 ret = xmlStrcat(ret, node->content);
1723 ret = xmlStrncat(ret, buf, 1);
1724 ret = xmlStrcat(ret, node->name);
1727 ret = xmlStrncat(ret, buf, 1);
1732 xmlGenericError(xmlGenericErrorContext,
1733 "xmlGetNodeListString : invalid node type %d\n",
1742 #ifdef LIBXML_TREE_ENABLED
1744 * xmlNodeListGetRawString:
1745 * @doc: the document
1746 * @list: a Node list
1747 * @inLine: should we replace entity contents or show their external form
1749 * Builds the string equivalent to the text contained in the Node list
1750 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1751 * this function doesn't do any character encoding handling.
1753 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1756 xmlNodeListGetRawString(const xmlDoc *doc, const xmlNode *list, int inLine)
1758 const xmlNode *node = list;
1759 xmlChar *ret = NULL;
1765 while (node != NULL) {
1766 if ((node->type == XML_TEXT_NODE) ||
1767 (node->type == XML_CDATA_SECTION_NODE)) {
1769 ret = xmlStrcat(ret, node->content);
1773 buffer = xmlEncodeSpecialChars(doc, node->content);
1774 if (buffer != NULL) {
1775 ret = xmlStrcat(ret, buffer);
1779 } else if (node->type == XML_ENTITY_REF_NODE) {
1781 ent = xmlGetDocEntity(doc, node->name);
1785 /* an entity content can be any "well balanced chunk",
1786 * i.e. the result of the content [43] production:
1787 * http://www.w3.org/TR/REC-xml#NT-content.
1788 * So it can contain text, CDATA section or nested
1789 * entity reference nodes (among others).
1790 * -> we recursive call xmlNodeListGetRawString()
1791 * which handles these types */
1793 xmlNodeListGetRawString(doc, ent->children, 1);
1794 if (buffer != NULL) {
1795 ret = xmlStrcat(ret, buffer);
1799 ret = xmlStrcat(ret, node->content);
1806 ret = xmlStrncat(ret, buf, 1);
1807 ret = xmlStrcat(ret, node->name);
1810 ret = xmlStrncat(ret, buf, 1);
1815 xmlGenericError(xmlGenericErrorContext,
1816 "xmlGetNodeListString : invalid node type %d\n",
1824 #endif /* LIBXML_TREE_ENABLED */
1827 xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1828 const xmlChar * name, const xmlChar * value,
1832 xmlDocPtr doc = NULL;
1834 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
1835 if ((eatname == 1) &&
1836 ((node->doc == NULL) ||
1837 (!(xmlDictOwns(node->doc->dict, name)))))
1838 xmlFree((xmlChar *) name);
1843 * Allocate a new property and fill the fields.
1845 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1847 if ((eatname == 1) &&
1848 ((node == NULL) || (node->doc == NULL) ||
1849 (!(xmlDictOwns(node->doc->dict, name)))))
1850 xmlFree((xmlChar *) name);
1851 xmlTreeErrMemory("building attribute");
1854 memset(cur, 0, sizeof(xmlAttr));
1855 cur->type = XML_ATTRIBUTE_NODE;
1865 if ((doc != NULL) && (doc->dict != NULL))
1866 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1868 cur->name = xmlStrdup(name);
1872 if (value != NULL) {
1875 if(!xmlCheckUTF8(value)) {
1876 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc,
1879 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1881 cur->children = xmlNewDocText(doc, value);
1883 tmp = cur->children;
1884 while (tmp != NULL) {
1885 tmp->parent = (xmlNodePtr) cur;
1886 if (tmp->next == NULL)
1893 * Add it at the end to preserve parsing order ...
1896 if (node->properties == NULL) {
1897 node->properties = cur;
1899 xmlAttrPtr prev = node->properties;
1901 while (prev->next != NULL)
1908 if ((value != NULL) && (node != NULL) &&
1909 (xmlIsID(node->doc, node, cur) == 1))
1910 xmlAddID(NULL, node->doc, value, cur);
1912 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1913 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1917 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1918 defined(LIBXML_SCHEMAS_ENABLED)
1921 * @node: the holding node
1922 * @name: the name of the attribute
1923 * @value: the value of the attribute
1925 * Create a new property carried by a node.
1926 * Returns a pointer to the attribute
1929 xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1933 xmlGenericError(xmlGenericErrorContext,
1934 "xmlNewProp : name == NULL\n");
1939 return xmlNewPropInternal(node, NULL, name, value, 0);
1941 #endif /* LIBXML_TREE_ENABLED */
1945 * @node: the holding node
1946 * @ns: the namespace
1947 * @name: the name of the attribute
1948 * @value: the value of the attribute
1950 * Create a new property tagged with a namespace and carried by a node.
1951 * Returns a pointer to the attribute
1954 xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1955 const xmlChar *value) {
1959 xmlGenericError(xmlGenericErrorContext,
1960 "xmlNewNsProp : name == NULL\n");
1965 return xmlNewPropInternal(node, ns, name, value, 0);
1969 * xmlNewNsPropEatName:
1970 * @node: the holding node
1971 * @ns: the namespace
1972 * @name: the name of the attribute
1973 * @value: the value of the attribute
1975 * Create a new property tagged with a namespace and carried by a node.
1976 * Returns a pointer to the attribute
1979 xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1980 const xmlChar *value) {
1984 xmlGenericError(xmlGenericErrorContext,
1985 "xmlNewNsPropEatName : name == NULL\n");
1990 return xmlNewPropInternal(node, ns, name, value, 1);
1995 * @doc: the document
1996 * @name: the name of the attribute
1997 * @value: the value of the attribute
1999 * Create a new property carried by a document.
2000 * Returns a pointer to the attribute
2003 xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
2008 xmlGenericError(xmlGenericErrorContext,
2009 "xmlNewDocProp : name == NULL\n");
2015 * Allocate a new property and fill the fields.
2017 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
2019 xmlTreeErrMemory("building attribute");
2022 memset(cur, 0, sizeof(xmlAttr));
2023 cur->type = XML_ATTRIBUTE_NODE;
2025 if ((doc != NULL) && (doc->dict != NULL))
2026 cur->name = xmlDictLookup(doc->dict, name, -1);
2028 cur->name = xmlStrdup(name);
2030 if (value != NULL) {
2033 cur->children = xmlStringGetNodeList(doc, value);
2036 tmp = cur->children;
2037 while (tmp != NULL) {
2038 tmp->parent = (xmlNodePtr) cur;
2039 if (tmp->next == NULL)
2045 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2046 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2052 * @cur: the first property in the list
2054 * Free a property and all its siblings, all the children are freed too.
2057 xmlFreePropList(xmlAttrPtr cur) {
2059 if (cur == NULL) return;
2060 while (cur != NULL) {
2069 * @cur: an attribute
2071 * Free one attribute, all the content is freed too
2074 xmlFreeProp(xmlAttrPtr cur) {
2075 xmlDictPtr dict = NULL;
2076 if (cur == NULL) return;
2078 if (cur->doc != NULL) dict = cur->doc->dict;
2080 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
2081 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2083 /* Check for ID removal -> leading to invalid references ! */
2084 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2085 xmlRemoveID(cur->doc, cur);
2087 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2088 DICT_FREE(cur->name)
2094 * @cur: an attribute
2096 * Unlink and free one attribute, all the content is freed too
2097 * Note this doesn't work for namespace definition attributes
2099 * Returns 0 if success and -1 in case of error.
2102 xmlRemoveProp(xmlAttrPtr cur) {
2106 xmlGenericError(xmlGenericErrorContext,
2107 "xmlRemoveProp : cur == NULL\n");
2111 if (cur->parent == NULL) {
2113 xmlGenericError(xmlGenericErrorContext,
2114 "xmlRemoveProp : cur->parent == NULL\n");
2118 tmp = cur->parent->properties;
2120 cur->parent->properties = cur->next;
2121 if (cur->next != NULL)
2122 cur->next->prev = NULL;
2126 while (tmp != NULL) {
2127 if (tmp->next == cur) {
2128 tmp->next = cur->next;
2129 if (tmp->next != NULL)
2130 tmp->next->prev = tmp;
2137 xmlGenericError(xmlGenericErrorContext,
2138 "xmlRemoveProp : attribute not owned by its node\n");
2145 * @doc: the target document
2146 * @name: the processing instruction name
2147 * @content: the PI content
2149 * Creation of a processing instruction element.
2150 * Returns a pointer to the new node object.
2153 xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
2158 xmlGenericError(xmlGenericErrorContext,
2159 "xmlNewPI : name == NULL\n");
2165 * Allocate a new node and fill the fields.
2167 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2169 xmlTreeErrMemory("building PI");
2172 memset(cur, 0, sizeof(xmlNode));
2173 cur->type = XML_PI_NODE;
2175 if ((doc != NULL) && (doc->dict != NULL))
2176 cur->name = xmlDictLookup(doc->dict, name, -1);
2178 cur->name = xmlStrdup(name);
2179 if (content != NULL) {
2180 cur->content = xmlStrdup(content);
2184 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2185 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2191 * @name: the processing instruction name
2192 * @content: the PI content
2194 * Creation of a processing instruction element.
2195 * Use xmlDocNewPI preferably to get string interning
2197 * Returns a pointer to the new node object.
2200 xmlNewPI(const xmlChar *name, const xmlChar *content) {
2201 return(xmlNewDocPI(NULL, name, content));
2206 * @ns: namespace if any
2207 * @name: the node name
2209 * Creation of a new node element. @ns is optional (NULL).
2211 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2215 xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2220 xmlGenericError(xmlGenericErrorContext,
2221 "xmlNewNode : name == NULL\n");
2227 * Allocate a new node and fill the fields.
2229 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2231 xmlTreeErrMemory("building node");
2234 memset(cur, 0, sizeof(xmlNode));
2235 cur->type = XML_ELEMENT_NODE;
2237 cur->name = xmlStrdup(name);
2240 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2241 xmlRegisterNodeDefaultValue(cur);
2246 * xmlNewNodeEatName:
2247 * @ns: namespace if any
2248 * @name: the node name
2250 * Creation of a new node element. @ns is optional (NULL).
2252 * Returns a pointer to the new node object, with pointer @name as
2253 * new node's name. Use xmlNewNode() if a copy of @name string is
2254 * is needed as new node's name.
2257 xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2262 xmlGenericError(xmlGenericErrorContext,
2263 "xmlNewNode : name == NULL\n");
2269 * Allocate a new node and fill the fields.
2271 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2273 xmlTreeErrMemory("building node");
2274 /* we can't check here that name comes from the doc dictionary */
2277 memset(cur, 0, sizeof(xmlNode));
2278 cur->type = XML_ELEMENT_NODE;
2283 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2284 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2290 * @doc: the document
2291 * @ns: namespace if any
2292 * @name: the node name
2293 * @content: the XML text content if any
2295 * Creation of a new node element within a document. @ns and @content
2296 * are optional (NULL).
2297 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2298 * references, but XML special chars need to be escaped first by using
2299 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2300 * need entities support.
2302 * Returns a pointer to the new node object.
2305 xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2306 const xmlChar *name, const xmlChar *content) {
2309 if ((doc != NULL) && (doc->dict != NULL))
2310 cur = xmlNewNodeEatName(ns, (xmlChar *)
2311 xmlDictLookup(doc->dict, name, -1));
2313 cur = xmlNewNode(ns, name);
2316 if (content != NULL) {
2317 cur->children = xmlStringGetNodeList(doc, content);
2318 UPDATE_LAST_CHILD_AND_PARENT(cur)
2326 * xmlNewDocNodeEatName:
2327 * @doc: the document
2328 * @ns: namespace if any
2329 * @name: the node name
2330 * @content: the XML text content if any
2332 * Creation of a new node element within a document. @ns and @content
2333 * are optional (NULL).
2334 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2335 * references, but XML special chars need to be escaped first by using
2336 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2337 * need entities support.
2339 * Returns a pointer to the new node object.
2342 xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2343 xmlChar *name, const xmlChar *content) {
2346 cur = xmlNewNodeEatName(ns, name);
2349 if (content != NULL) {
2350 cur->children = xmlStringGetNodeList(doc, content);
2351 UPDATE_LAST_CHILD_AND_PARENT(cur)
2354 /* if name don't come from the doc dictionary free it here */
2355 if ((name != NULL) && (doc != NULL) &&
2356 (!(xmlDictOwns(doc->dict, name))))
2362 #ifdef LIBXML_TREE_ENABLED
2365 * @doc: the document
2366 * @ns: namespace if any
2367 * @name: the node name
2368 * @content: the text content if any
2370 * Creation of a new node element within a document. @ns and @content
2371 * are optional (NULL).
2373 * Returns a pointer to the new node object.
2376 xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2377 const xmlChar *name, const xmlChar *content) {
2380 cur = xmlNewDocNode(doc, ns, name, NULL);
2383 if (content != NULL) {
2384 cur->children = xmlNewDocText(doc, content);
2385 UPDATE_LAST_CHILD_AND_PARENT(cur)
2392 * xmlNewDocFragment:
2393 * @doc: the document owning the fragment
2395 * Creation of a new Fragment node.
2396 * Returns a pointer to the new node object.
2399 xmlNewDocFragment(xmlDocPtr doc) {
2403 * Allocate a new DocumentFragment node and fill the fields.
2405 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2407 xmlTreeErrMemory("building fragment");
2410 memset(cur, 0, sizeof(xmlNode));
2411 cur->type = XML_DOCUMENT_FRAG_NODE;
2415 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2416 xmlRegisterNodeDefaultValue(cur);
2419 #endif /* LIBXML_TREE_ENABLED */
2423 * @content: the text content
2425 * Creation of a new text node.
2426 * Returns a pointer to the new node object.
2429 xmlNewText(const xmlChar *content) {
2433 * Allocate a new node and fill the fields.
2435 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2437 xmlTreeErrMemory("building text");
2440 memset(cur, 0, sizeof(xmlNode));
2441 cur->type = XML_TEXT_NODE;
2443 cur->name = xmlStringText;
2444 if (content != NULL) {
2445 cur->content = xmlStrdup(content);
2448 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2449 xmlRegisterNodeDefaultValue(cur);
2453 #ifdef LIBXML_TREE_ENABLED
2456 * @parent: the parent node
2457 * @ns: a namespace if any
2458 * @name: the name of the child
2459 * @content: the text content of the child if any.
2461 * Creation of a new child element, added at the end of @parent children list.
2462 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2463 * created element inherits the namespace of @parent. If @content is non NULL,
2464 * a child TEXT node will be created containing the string @content.
2465 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2466 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
2467 * reserved XML chars that might appear in @content, such as the ampersand,
2468 * greater-than or less-than signs, are automatically replaced by their XML
2469 * escaped entity representations.
2471 * Returns a pointer to the new node object.
2474 xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2475 const xmlChar *name, const xmlChar *content) {
2476 xmlNodePtr cur, prev;
2478 if (parent == NULL) {
2480 xmlGenericError(xmlGenericErrorContext,
2481 "xmlNewTextChild : parent == NULL\n");
2488 xmlGenericError(xmlGenericErrorContext,
2489 "xmlNewTextChild : name == NULL\n");
2495 * Allocate a new node
2497 if (parent->type == XML_ELEMENT_NODE) {
2499 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2501 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2502 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2503 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2505 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2507 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2508 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2509 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2513 if (cur == NULL) return(NULL);
2516 * add the new element at the end of the children list.
2518 cur->type = XML_ELEMENT_NODE;
2519 cur->parent = parent;
2520 cur->doc = parent->doc;
2521 if (parent->children == NULL) {
2522 parent->children = cur;
2525 prev = parent->last;
2533 #endif /* LIBXML_TREE_ENABLED */
2537 * @doc: the document
2538 * @name: the char ref string, starting with # or "&# ... ;"
2540 * Creation of a new character reference node.
2541 * Returns a pointer to the new node object.
2544 xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2551 * Allocate a new node and fill the fields.
2553 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2555 xmlTreeErrMemory("building character reference");
2558 memset(cur, 0, sizeof(xmlNode));
2559 cur->type = XML_ENTITY_REF_NODE;
2562 if (name[0] == '&') {
2565 len = xmlStrlen(name);
2566 if (name[len - 1] == ';')
2567 cur->name = xmlStrndup(name, len - 1);
2569 cur->name = xmlStrndup(name, len);
2571 cur->name = xmlStrdup(name);
2573 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2574 xmlRegisterNodeDefaultValue(cur);
2580 * @doc: the document
2581 * @name: the reference name, or the reference string with & and ;
2583 * Creation of a new reference node.
2584 * Returns a pointer to the new node object.
2587 xmlNewReference(const xmlDoc *doc, const xmlChar *name) {
2595 * Allocate a new node and fill the fields.
2597 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2599 xmlTreeErrMemory("building reference");
2602 memset(cur, 0, sizeof(xmlNode));
2603 cur->type = XML_ENTITY_REF_NODE;
2605 cur->doc = (xmlDoc *)doc;
2606 if (name[0] == '&') {
2609 len = xmlStrlen(name);
2610 if (name[len - 1] == ';')
2611 cur->name = xmlStrndup(name, len - 1);
2613 cur->name = xmlStrndup(name, len);
2615 cur->name = xmlStrdup(name);
2617 ent = xmlGetDocEntity(doc, cur->name);
2619 cur->content = ent->content;
2621 * The parent pointer in entity is a DTD pointer and thus is NOT
2622 * updated. Not sure if this is 100% correct.
2625 cur->children = (xmlNodePtr) ent;
2626 cur->last = (xmlNodePtr) ent;
2629 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2630 xmlRegisterNodeDefaultValue(cur);
2636 * @doc: the document
2637 * @content: the text content
2639 * Creation of a new text node within a document.
2640 * Returns a pointer to the new node object.
2643 xmlNewDocText(const xmlDoc *doc, const xmlChar *content) {
2646 cur = xmlNewText(content);
2647 if (cur != NULL) cur->doc = (xmlDoc *)doc;
2653 * @content: the text content
2654 * @len: the text len.
2656 * Creation of a new text node with an extra parameter for the content's length
2657 * Returns a pointer to the new node object.
2660 xmlNewTextLen(const xmlChar *content, int len) {
2664 * Allocate a new node and fill the fields.
2666 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2668 xmlTreeErrMemory("building text");
2671 memset(cur, 0, sizeof(xmlNode));
2672 cur->type = XML_TEXT_NODE;
2674 cur->name = xmlStringText;
2675 if (content != NULL) {
2676 cur->content = xmlStrndup(content, len);
2679 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2680 xmlRegisterNodeDefaultValue(cur);
2686 * @doc: the document
2687 * @content: the text content
2688 * @len: the text len.
2690 * Creation of a new text node with an extra content length parameter. The
2691 * text node pertain to a given document.
2692 * Returns a pointer to the new node object.
2695 xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2698 cur = xmlNewTextLen(content, len);
2699 if (cur != NULL) cur->doc = doc;
2705 * @content: the comment content
2707 * Creation of a new node containing a comment.
2708 * Returns a pointer to the new node object.
2711 xmlNewComment(const xmlChar *content) {
2715 * Allocate a new node and fill the fields.
2717 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2719 xmlTreeErrMemory("building comment");
2722 memset(cur, 0, sizeof(xmlNode));
2723 cur->type = XML_COMMENT_NODE;
2725 cur->name = xmlStringComment;
2726 if (content != NULL) {
2727 cur->content = xmlStrdup(content);
2730 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2731 xmlRegisterNodeDefaultValue(cur);
2737 * @doc: the document
2738 * @content: the CDATA block content content
2739 * @len: the length of the block
2741 * Creation of a new node containing a CDATA block.
2742 * Returns a pointer to the new node object.
2745 xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2749 * Allocate a new node and fill the fields.
2751 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2753 xmlTreeErrMemory("building CDATA");
2756 memset(cur, 0, sizeof(xmlNode));
2757 cur->type = XML_CDATA_SECTION_NODE;
2760 if (content != NULL) {
2761 cur->content = xmlStrndup(content, len);
2764 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2765 xmlRegisterNodeDefaultValue(cur);
2771 * @doc: the document
2772 * @content: the comment content
2774 * Creation of a new node containing a comment within a document.
2775 * Returns a pointer to the new node object.
2778 xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2781 cur = xmlNewComment(content);
2782 if (cur != NULL) cur->doc = doc;
2788 * @tree: the top element
2789 * @doc: the document
2791 * update all nodes under the tree to point to the right document
2794 xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
2797 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
2799 if (tree->doc != doc) {
2800 if(tree->type == XML_ELEMENT_NODE) {
2801 prop = tree->properties;
2802 while (prop != NULL) {
2803 if (prop->atype == XML_ATTRIBUTE_ID) {
2804 xmlRemoveID(tree->doc, prop);
2808 xmlSetListDoc(prop->children, doc);
2811 * TODO: ID attributes should be also added to the new
2812 * document, but this breaks things like xmlReplaceNode.
2813 * The underlying problem is that xmlRemoveID is only called
2814 * if a node is destroyed, not if it's unlinked.
2817 if (xmlIsID(doc, tree, prop)) {
2818 xmlChar *idVal = xmlNodeListGetString(doc, prop->children,
2820 xmlAddID(NULL, doc, idVal, prop);
2827 if (tree->children != NULL)
2828 xmlSetListDoc(tree->children, doc);
2835 * @list: the first element
2836 * @doc: the document
2838 * update all nodes in the list to point to the right document
2841 xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2844 if ((list == NULL) || (list->type == XML_NAMESPACE_DECL))
2847 while (cur != NULL) {
2848 if (cur->doc != doc)
2849 xmlSetTreeDoc(cur, doc);
2854 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
2857 * @parent: the parent node
2858 * @ns: a namespace if any
2859 * @name: the name of the child
2860 * @content: the XML content of the child if any.
2862 * Creation of a new child element, added at the end of @parent children list.
2863 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2864 * created element inherits the namespace of @parent. If @content is non NULL,
2865 * a child list containing the TEXTs and ENTITY_REFs node will be created.
2866 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2867 * references. XML special chars must be escaped first by using
2868 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
2870 * Returns a pointer to the new node object.
2873 xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2874 const xmlChar *name, const xmlChar *content) {
2875 xmlNodePtr cur, prev;
2877 if (parent == NULL) {
2879 xmlGenericError(xmlGenericErrorContext,
2880 "xmlNewChild : parent == NULL\n");
2887 xmlGenericError(xmlGenericErrorContext,
2888 "xmlNewChild : name == NULL\n");
2894 * Allocate a new node
2896 if (parent->type == XML_ELEMENT_NODE) {
2898 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2900 cur = xmlNewDocNode(parent->doc, ns, name, content);
2901 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2902 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2904 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2906 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
2907 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2908 cur = xmlNewDocNode( parent->doc, ns, name, content);
2912 if (cur == NULL) return(NULL);
2915 * add the new element at the end of the children list.
2917 cur->type = XML_ELEMENT_NODE;
2918 cur->parent = parent;
2919 cur->doc = parent->doc;
2920 if (parent->children == NULL) {
2921 parent->children = cur;
2924 prev = parent->last;
2932 #endif /* LIBXML_TREE_ENABLED */
2935 * xmlAddPropSibling:
2936 * @prev: the attribute to which @prop is added after
2937 * @cur: the base attribute passed to calling function
2938 * @prop: the new attribute
2940 * Add a new attribute after @prev using @cur as base attribute.
2941 * When inserting before @cur, @prev is passed as @cur->prev.
2942 * When inserting after @cur, @prev is passed as @cur.
2943 * If an existing attribute is found it is detroyed prior to adding @prop.
2945 * Returns the attribute being inserted or NULL in case of error.
2948 xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2951 if ((cur == NULL) || (cur->type != XML_ATTRIBUTE_NODE) ||
2952 (prop == NULL) || (prop->type != XML_ATTRIBUTE_NODE) ||
2953 ((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)))
2956 /* check if an attribute with the same name exists */
2957 if (prop->ns == NULL)
2958 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2960 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2962 if (prop->doc != cur->doc) {
2963 xmlSetTreeDoc(prop, cur->doc);
2965 prop->parent = cur->parent;
2968 prop->next = prev->next;
2971 prop->next->prev = prop;
2976 if (prop->prev == NULL && prop->parent != NULL)
2977 prop->parent->properties = (xmlAttrPtr) prop;
2978 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2979 /* different instance, destroy it (attributes must be unique) */
2980 xmlRemoveProp((xmlAttrPtr) attr);
2986 * xmlAddNextSibling:
2987 * @cur: the child node
2988 * @elem: the new node
2990 * Add a new node @elem as the next sibling of @cur
2991 * If the new node was already inserted in a document it is
2992 * first unlinked from its existing context.
2993 * As a result of text merging @elem may be freed.
2994 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2995 * If there is an attribute with equal name, it is first destroyed.
2997 * Returns the new node or NULL in case of error.
3000 xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
3001 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3003 xmlGenericError(xmlGenericErrorContext,
3004 "xmlAddNextSibling : cur == NULL\n");
3008 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
3010 xmlGenericError(xmlGenericErrorContext,
3011 "xmlAddNextSibling : elem == NULL\n");
3018 xmlGenericError(xmlGenericErrorContext,
3019 "xmlAddNextSibling : cur == elem\n");
3024 xmlUnlinkNode(elem);
3026 if (elem->type == XML_TEXT_NODE) {
3027 if (cur->type == XML_TEXT_NODE) {
3028 xmlNodeAddContent(cur, elem->content);
3032 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
3033 (cur->name == cur->next->name)) {
3036 tmp = xmlStrdup(elem->content);
3037 tmp = xmlStrcat(tmp, cur->next->content);
3038 xmlNodeSetContent(cur->next, tmp);
3043 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3044 return xmlAddPropSibling(cur, cur, elem);
3047 if (elem->doc != cur->doc) {
3048 xmlSetTreeDoc(elem, cur->doc);
3050 elem->parent = cur->parent;
3052 elem->next = cur->next;
3054 if (elem->next != NULL)
3055 elem->next->prev = elem;
3056 if ((elem->parent != NULL) && (elem->parent->last == cur))
3057 elem->parent->last = elem;
3061 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
3062 defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
3064 * xmlAddPrevSibling:
3065 * @cur: the child node
3066 * @elem: the new node
3068 * Add a new node @elem as the previous sibling of @cur
3069 * merging adjacent TEXT nodes (@elem may be freed)
3070 * If the new node was already inserted in a document it is
3071 * first unlinked from its existing context.
3072 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3073 * If there is an attribute with equal name, it is first destroyed.
3075 * Returns the new node or NULL in case of error.
3078 xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
3079 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3081 xmlGenericError(xmlGenericErrorContext,
3082 "xmlAddPrevSibling : cur == NULL\n");
3086 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
3088 xmlGenericError(xmlGenericErrorContext,
3089 "xmlAddPrevSibling : elem == NULL\n");
3096 xmlGenericError(xmlGenericErrorContext,
3097 "xmlAddPrevSibling : cur == elem\n");
3102 xmlUnlinkNode(elem);
3104 if (elem->type == XML_TEXT_NODE) {
3105 if (cur->type == XML_TEXT_NODE) {
3108 tmp = xmlStrdup(elem->content);
3109 tmp = xmlStrcat(tmp, cur->content);
3110 xmlNodeSetContent(cur, tmp);
3115 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3116 (cur->name == cur->prev->name)) {
3117 xmlNodeAddContent(cur->prev, elem->content);
3121 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3122 return xmlAddPropSibling(cur->prev, cur, elem);
3125 if (elem->doc != cur->doc) {
3126 xmlSetTreeDoc(elem, cur->doc);
3128 elem->parent = cur->parent;
3130 elem->prev = cur->prev;
3132 if (elem->prev != NULL)
3133 elem->prev->next = elem;
3134 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
3135 elem->parent->children = elem;
3139 #endif /* LIBXML_TREE_ENABLED */
3143 * @cur: the child node
3144 * @elem: the new node
3146 * Add a new element @elem to the list of siblings of @cur
3147 * merging adjacent TEXT nodes (@elem may be freed)
3148 * If the new element was already inserted in a document it is
3149 * first unlinked from its existing context.
3151 * Returns the new element or NULL in case of error.
3154 xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3157 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3159 xmlGenericError(xmlGenericErrorContext,
3160 "xmlAddSibling : cur == NULL\n");
3165 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
3167 xmlGenericError(xmlGenericErrorContext,
3168 "xmlAddSibling : elem == NULL\n");
3175 xmlGenericError(xmlGenericErrorContext,
3176 "xmlAddSibling : cur == elem\n");
3182 * Constant time is we can rely on the ->parent->last to find
3185 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
3186 (cur->parent->children != NULL) &&
3187 (cur->parent->last != NULL) &&
3188 (cur->parent->last->next == NULL)) {
3189 cur = cur->parent->last;
3191 while (cur->next != NULL) cur = cur->next;
3194 xmlUnlinkNode(elem);
3196 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3197 (cur->name == elem->name)) {
3198 xmlNodeAddContent(cur, elem->content);
3201 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3202 return xmlAddPropSibling(cur, cur, elem);
3205 if (elem->doc != cur->doc) {
3206 xmlSetTreeDoc(elem, cur->doc);
3208 parent = cur->parent;
3211 elem->parent = parent;
3214 parent->last = elem;
3221 * @parent: the parent node
3222 * @cur: the first node in the list
3224 * Add a list of node at the end of the child list of the parent
3225 * merging adjacent TEXT nodes (@cur may be freed)
3227 * Returns the last child or NULL in case of error.
3230 xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3233 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3235 xmlGenericError(xmlGenericErrorContext,
3236 "xmlAddChildList : parent == NULL\n");
3241 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3243 xmlGenericError(xmlGenericErrorContext,
3244 "xmlAddChildList : child == NULL\n");
3249 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3250 (cur->doc != parent->doc)) {
3252 xmlGenericError(xmlGenericErrorContext,
3253 "Elements moved to a different document\n");
3258 * add the first element at the end of the children list.
3261 if (parent->children == NULL) {
3262 parent->children = cur;
3265 * If cur and parent->last both are TEXT nodes, then merge them.
3267 if ((cur->type == XML_TEXT_NODE) &&
3268 (parent->last->type == XML_TEXT_NODE) &&
3269 (cur->name == parent->last->name)) {
3270 xmlNodeAddContent(parent->last, cur->content);
3272 * if it's the only child, nothing more to be done.
3274 if (cur->next == NULL) {
3276 return(parent->last);
3282 prev = parent->last;
3286 while (cur->next != NULL) {
3287 cur->parent = parent;
3288 if (cur->doc != parent->doc) {
3289 xmlSetTreeDoc(cur, parent->doc);
3293 cur->parent = parent;
3294 /* the parent may not be linked to a doc ! */
3295 if (cur->doc != parent->doc) {
3296 xmlSetTreeDoc(cur, parent->doc);
3305 * @parent: the parent node
3306 * @cur: the child node
3308 * Add a new node to @parent, at the end of the child (or property) list
3309 * merging adjacent TEXT nodes (in which case @cur is freed)
3310 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3311 * If there is an attribute with equal name, it is first destroyed.
3313 * Returns the child or NULL in case of error.
3316 xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3319 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3321 xmlGenericError(xmlGenericErrorContext,
3322 "xmlAddChild : parent == NULL\n");
3327 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3329 xmlGenericError(xmlGenericErrorContext,
3330 "xmlAddChild : child == NULL\n");
3335 if (parent == cur) {
3337 xmlGenericError(xmlGenericErrorContext,
3338 "xmlAddChild : parent == cur\n");
3343 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
3344 * cur is then freed.
3346 if (cur->type == XML_TEXT_NODE) {
3347 if ((parent->type == XML_TEXT_NODE) &&
3348 (parent->content != NULL) &&
3349 (parent->name == cur->name)) {
3350 xmlNodeAddContent(parent, cur->content);
3354 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
3355 (parent->last->name == cur->name) &&
3356 (parent->last != cur)) {
3357 xmlNodeAddContent(parent->last, cur->content);
3359 return(parent->last);
3364 * add the new element at the end of the children list.
3367 cur->parent = parent;
3368 if (cur->doc != parent->doc) {
3369 xmlSetTreeDoc(cur, parent->doc);
3371 /* this check prevents a loop on tree-traversions if a developer
3372 * tries to add a node to its parent multiple times
3380 if ((parent->type == XML_TEXT_NODE) &&
3381 (parent->content != NULL) &&
3383 xmlNodeAddContent(parent, cur->content);
3387 if (cur->type == XML_ATTRIBUTE_NODE) {
3388 if (parent->type != XML_ELEMENT_NODE)
3390 if (parent->properties != NULL) {
3391 /* check if an attribute with the same name exists */
3392 xmlAttrPtr lastattr;
3394 if (cur->ns == NULL)
3395 lastattr = xmlHasNsProp(parent, cur->name, NULL);
3397 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
3398 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
3399 /* different instance, destroy it (attributes must be unique) */
3400 xmlUnlinkNode((xmlNodePtr) lastattr);
3401 xmlFreeProp(lastattr);
3403 if (lastattr == (xmlAttrPtr) cur)
3407 if (parent->properties == NULL) {
3408 parent->properties = (xmlAttrPtr) cur;
3411 xmlAttrPtr lastattr = parent->properties;
3412 while (lastattr->next != NULL) {
3413 lastattr = lastattr->next;
3415 lastattr->next = (xmlAttrPtr) cur;
3416 ((xmlAttrPtr) cur)->prev = lastattr;
3419 if (parent->children == NULL) {
3420 parent->children = cur;
3423 prev = parent->last;
3434 * @parent: the parent node
3436 * Search the last child of a node.
3437 * Returns the last child or NULL if none.
3440 xmlGetLastChild(const xmlNode *parent) {
3441 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3443 xmlGenericError(xmlGenericErrorContext,
3444 "xmlGetLastChild : parent == NULL\n");
3448 return(parent->last);
3451 #ifdef LIBXML_TREE_ENABLED
3453 * 5 interfaces from DOM ElementTraversal
3457 * xmlChildElementCount:
3458 * @parent: the parent node
3460 * Finds the current number of child nodes of that element which are
3462 * Note the handling of entities references is different than in
3463 * the W3C DOM element traversal spec since we don't have back reference
3464 * from entities content to entities references.
3466 * Returns the count of element child or 0 if not available
3469 xmlChildElementCount(xmlNodePtr parent) {
3470 unsigned long ret = 0;
3471 xmlNodePtr cur = NULL;
3475 switch (parent->type) {
3476 case XML_ELEMENT_NODE:
3477 case XML_ENTITY_NODE:
3478 case XML_DOCUMENT_NODE:
3479 case XML_DOCUMENT_FRAG_NODE:
3480 case XML_HTML_DOCUMENT_NODE:
3481 cur = parent->children;
3486 while (cur != NULL) {
3487 if (cur->type == XML_ELEMENT_NODE)
3495 * xmlFirstElementChild:
3496 * @parent: the parent node
3498 * Finds the first child node of that element which is a Element node
3499 * Note the handling of entities references is different than in
3500 * the W3C DOM element traversal spec since we don't have back reference
3501 * from entities content to entities references.
3503 * Returns the first element child or NULL if not available
3506 xmlFirstElementChild(xmlNodePtr parent) {
3507 xmlNodePtr cur = NULL;
3511 switch (parent->type) {
3512 case XML_ELEMENT_NODE:
3513 case XML_ENTITY_NODE:
3514 case XML_DOCUMENT_NODE:
3515 case XML_DOCUMENT_FRAG_NODE:
3516 case XML_HTML_DOCUMENT_NODE:
3517 cur = parent->children;
3522 while (cur != NULL) {
3523 if (cur->type == XML_ELEMENT_NODE)
3531 * xmlLastElementChild:
3532 * @parent: the parent node
3534 * Finds the last child node of that element which is a Element node
3535 * Note the handling of entities references is different than in
3536 * the W3C DOM element traversal spec since we don't have back reference
3537 * from entities content to entities references.
3539 * Returns the last element child or NULL if not available
3542 xmlLastElementChild(xmlNodePtr parent) {
3543 xmlNodePtr cur = NULL;
3547 switch (parent->type) {
3548 case XML_ELEMENT_NODE:
3549 case XML_ENTITY_NODE:
3550 case XML_DOCUMENT_NODE:
3551 case XML_DOCUMENT_FRAG_NODE:
3552 case XML_HTML_DOCUMENT_NODE:
3558 while (cur != NULL) {
3559 if (cur->type == XML_ELEMENT_NODE)
3567 * xmlPreviousElementSibling:
3568 * @node: the current node
3570 * Finds the first closest previous sibling of the node which is an
3572 * Note the handling of entities references is different than in
3573 * the W3C DOM element traversal spec since we don't have back reference
3574 * from entities content to entities references.
3576 * Returns the previous element sibling or NULL if not available
3579 xmlPreviousElementSibling(xmlNodePtr node) {
3582 switch (node->type) {
3583 case XML_ELEMENT_NODE:
3585 case XML_CDATA_SECTION_NODE:
3586 case XML_ENTITY_REF_NODE:
3587 case XML_ENTITY_NODE:
3589 case XML_COMMENT_NODE:
3590 case XML_XINCLUDE_START:
3591 case XML_XINCLUDE_END:
3597 while (node != NULL) {
3598 if (node->type == XML_ELEMENT_NODE)
3606 * xmlNextElementSibling:
3607 * @node: the current node
3609 * Finds the first closest next sibling of the node which is an
3611 * Note the handling of entities references is different than in
3612 * the W3C DOM element traversal spec since we don't have back reference
3613 * from entities content to entities references.
3615 * Returns the next element sibling or NULL if not available
3618 xmlNextElementSibling(xmlNodePtr node) {
3621 switch (node->type) {
3622 case XML_ELEMENT_NODE:
3624 case XML_CDATA_SECTION_NODE:
3625 case XML_ENTITY_REF_NODE:
3626 case XML_ENTITY_NODE:
3628 case XML_COMMENT_NODE:
3630 case XML_XINCLUDE_START:
3631 case XML_XINCLUDE_END:
3637 while (node != NULL) {
3638 if (node->type == XML_ELEMENT_NODE)
3645 #endif /* LIBXML_TREE_ENABLED */
3649 * @cur: the first node in the list
3651 * Free a node and all its siblings, this is a recursive behaviour, all
3652 * the children are freed too.
3655 xmlFreeNodeList(xmlNodePtr cur) {
3657 xmlDictPtr dict = NULL;
3659 if (cur == NULL) return;
3660 if (cur->type == XML_NAMESPACE_DECL) {
3661 xmlFreeNsList((xmlNsPtr) cur);
3664 if ((cur->type == XML_DOCUMENT_NODE) ||
3665 #ifdef LIBXML_DOCB_ENABLED
3666 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
3668 (cur->type == XML_HTML_DOCUMENT_NODE)) {
3669 xmlFreeDoc((xmlDocPtr) cur);
3672 if (cur->doc != NULL) dict = cur->doc->dict;
3673 while (cur != NULL) {
3675 if (cur->type != XML_DTD_NODE) {
3677 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3678 xmlDeregisterNodeDefaultValue(cur);
3680 if ((cur->children != NULL) &&
3681 (cur->type != XML_ENTITY_REF_NODE))
3682 xmlFreeNodeList(cur->children);
3683 if (((cur->type == XML_ELEMENT_NODE) ||
3684 (cur->type == XML_XINCLUDE_START) ||
3685 (cur->type == XML_XINCLUDE_END)) &&
3686 (cur->properties != NULL))
3687 xmlFreePropList(cur->properties);
3688 if ((cur->type != XML_ELEMENT_NODE) &&
3689 (cur->type != XML_XINCLUDE_START) &&
3690 (cur->type != XML_XINCLUDE_END) &&
3691 (cur->type != XML_ENTITY_REF_NODE) &&
3692 (cur->content != (xmlChar *) &(cur->properties))) {
3693 DICT_FREE(cur->content)
3695 if (((cur->type == XML_ELEMENT_NODE) ||
3696 (cur->type == XML_XINCLUDE_START) ||
3697 (cur->type == XML_XINCLUDE_END)) &&
3698 (cur->nsDef != NULL))
3699 xmlFreeNsList(cur->nsDef);
3702 * When a node is a text node or a comment, it uses a global static
3703 * variable for the name of the node.
3704 * Otherwise the node name might come from the document's
3707 if ((cur->name != NULL) &&
3708 (cur->type != XML_TEXT_NODE) &&
3709 (cur->type != XML_COMMENT_NODE))
3710 DICT_FREE(cur->name)
3721 * Free a node, this is a recursive behaviour, all the children are freed too.
3722 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3725 xmlFreeNode(xmlNodePtr cur) {
3726 xmlDictPtr dict = NULL;
3728 if (cur == NULL) return;
3730 /* use xmlFreeDtd for DTD nodes */
3731 if (cur->type == XML_DTD_NODE) {
3732 xmlFreeDtd((xmlDtdPtr) cur);
3735 if (cur->type == XML_NAMESPACE_DECL) {
3736 xmlFreeNs((xmlNsPtr) cur);
3739 if (cur->type == XML_ATTRIBUTE_NODE) {
3740 xmlFreeProp((xmlAttrPtr) cur);
3744 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3745 xmlDeregisterNodeDefaultValue(cur);
3747 if (cur->doc != NULL) dict = cur->doc->dict;
3749 if (cur->type == XML_ENTITY_DECL) {
3750 xmlEntityPtr ent = (xmlEntityPtr) cur;
3751 DICT_FREE(ent->SystemID);
3752 DICT_FREE(ent->ExternalID);
3754 if ((cur->children != NULL) &&
3755 (cur->type != XML_ENTITY_REF_NODE))
3756 xmlFreeNodeList(cur->children);
3757 if (((cur->type == XML_ELEMENT_NODE) ||
3758 (cur->type == XML_XINCLUDE_START) ||
3759 (cur->type == XML_XINCLUDE_END)) &&
3760 (cur->properties != NULL))
3761 xmlFreePropList(cur->properties);
3762 if ((cur->type != XML_ELEMENT_NODE) &&
3763 (cur->content != NULL) &&
3764 (cur->type != XML_ENTITY_REF_NODE) &&
3765 (cur->type != XML_XINCLUDE_END) &&
3766 (cur->type != XML_XINCLUDE_START) &&
3767 (cur->content != (xmlChar *) &(cur->properties))) {
3768 DICT_FREE(cur->content)
3772 * When a node is a text node or a comment, it uses a global static
3773 * variable for the name of the node.
3774 * Otherwise the node name might come from the document's dictionary
3776 if ((cur->name != NULL) &&
3777 (cur->type != XML_TEXT_NODE) &&
3778 (cur->type != XML_COMMENT_NODE))
3779 DICT_FREE(cur->name)
3781 if (((cur->type == XML_ELEMENT_NODE) ||
3782 (cur->type == XML_XINCLUDE_START) ||
3783 (cur->type == XML_XINCLUDE_END)) &&
3784 (cur->nsDef != NULL))
3785 xmlFreeNsList(cur->nsDef);
3793 * Unlink a node from it's current context, the node is not freed
3794 * If one need to free the node, use xmlFreeNode() routine after the
3795 * unlink to discard it.
3796 * Note that namespace nodes can't be unlinked as they do not have
3797 * pointer to their parent.
3800 xmlUnlinkNode(xmlNodePtr cur) {
3803 xmlGenericError(xmlGenericErrorContext,
3804 "xmlUnlinkNode : node == NULL\n");
3808 if (cur->type == XML_NAMESPACE_DECL)
3810 if (cur->type == XML_DTD_NODE) {
3814 if (doc->intSubset == (xmlDtdPtr) cur)
3815 doc->intSubset = NULL;
3816 if (doc->extSubset == (xmlDtdPtr) cur)
3817 doc->extSubset = NULL;
3820 if (cur->type == XML_ENTITY_DECL) {
3824 if (doc->intSubset != NULL) {
3825 if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur)
3826 xmlHashRemoveEntry(doc->intSubset->entities, cur->name,
3828 if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur)
3829 xmlHashRemoveEntry(doc->intSubset->pentities, cur->name,
3832 if (doc->extSubset != NULL) {
3833 if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur)
3834 xmlHashRemoveEntry(doc->extSubset->entities, cur->name,
3836 if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur)
3837 xmlHashRemoveEntry(doc->extSubset->pentities, cur->name,
3842 if (cur->parent != NULL) {
3844 parent = cur->parent;
3845 if (cur->type == XML_ATTRIBUTE_NODE) {
3846 if (parent->properties == (xmlAttrPtr) cur)
3847 parent->properties = ((xmlAttrPtr) cur)->next;
3849 if (parent->children == cur)
3850 parent->children = cur->next;
3851 if (parent->last == cur)
3852 parent->last = cur->prev;
3856 if (cur->next != NULL)
3857 cur->next->prev = cur->prev;
3858 if (cur->prev != NULL)
3859 cur->prev->next = cur->next;
3860 cur->next = cur->prev = NULL;
3863 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
3866 * @old: the old node
3869 * Unlink the old node from its current context, prune the new one
3870 * at the same place. If @cur was already inserted in a document it is
3871 * first unlinked from its existing context.
3873 * Returns the @old node
3876 xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
3877 if (old == cur) return(NULL);
3878 if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) ||
3879 (old->parent == NULL)) {
3881 xmlGenericError(xmlGenericErrorContext,
3882 "xmlReplaceNode : old == NULL or without parent\n");
3886 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3893 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3895 xmlGenericError(xmlGenericErrorContext,
3896 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3900 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3902 xmlGenericError(xmlGenericErrorContext,
3903 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3908 xmlSetTreeDoc(cur, old->doc);
3909 cur->parent = old->parent;
3910 cur->next = old->next;
3911 if (cur->next != NULL)
3912 cur->next->prev = cur;
3913 cur->prev = old->prev;
3914 if (cur->prev != NULL)
3915 cur->prev->next = cur;
3916 if (cur->parent != NULL) {
3917 if (cur->type == XML_ATTRIBUTE_NODE) {
3918 if (cur->parent->properties == (xmlAttrPtr)old)
3919 cur->parent->properties = ((xmlAttrPtr) cur);
3921 if (cur->parent->children == old)
3922 cur->parent->children = cur;
3923 if (cur->parent->last == old)
3924 cur->parent->last = cur;
3927 old->next = old->prev = NULL;
3931 #endif /* LIBXML_TREE_ENABLED */
3933 /************************************************************************
3937 ************************************************************************/
3941 * @cur: the namespace
3943 * Do a copy of the namespace.
3945 * Returns: a new #xmlNsPtr, or NULL in case of error.
3948 xmlCopyNamespace(xmlNsPtr cur) {
3951 if (cur == NULL) return(NULL);
3952 switch (cur->type) {
3953 case XML_LOCAL_NAMESPACE:
3954 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3958 xmlGenericError(xmlGenericErrorContext,
3959 "xmlCopyNamespace: invalid type %d\n", cur->type);
3967 * xmlCopyNamespaceList:
3968 * @cur: the first namespace
3970 * Do a copy of an namespace list.
3972 * Returns: a new #xmlNsPtr, or NULL in case of error.
3975 xmlCopyNamespaceList(xmlNsPtr cur) {
3976 xmlNsPtr ret = NULL;
3977 xmlNsPtr p = NULL,q;
3979 while (cur != NULL) {
3980 q = xmlCopyNamespace(cur);
3993 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
3996 xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
3999 if (cur == NULL) return(NULL);
4000 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4003 ret = xmlNewDocProp(target->doc, cur->name, NULL);
4004 else if (doc != NULL)
4005 ret = xmlNewDocProp(doc, cur->name, NULL);
4006 else if (cur->parent != NULL)
4007 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
4008 else if (cur->children != NULL)
4009 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
4011 ret = xmlNewDocProp(NULL, cur->name, NULL);
4012 if (ret == NULL) return(NULL);
4013 ret->parent = target;
4015 if ((cur->ns != NULL) && (target != NULL)) {
4018 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
4021 * Humm, we are copying an element whose namespace is defined
4022 * out of the new tree scope. Search it in the original tree
4023 * and add it at the top of the new tree
4025 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
4027 xmlNodePtr root = target;
4028 xmlNodePtr pred = NULL;
4030 while (root->parent != NULL) {
4032 root = root->parent;
4034 if (root == (xmlNodePtr) target->doc) {
4035 /* correct possibly cycling above the document elt */
4038 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4042 * we have to find something appropriate here since
4043 * we cant be sure, that the namespce we found is identified
4046 if (xmlStrEqual(ns->href, cur->ns->href)) {
4047 /* this is the nice case */
4051 * we are in trouble: we need a new reconcilied namespace.
4054 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
4061 if (cur->children != NULL) {
4064 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
4066 tmp = ret->children;
4067 while (tmp != NULL) {
4068 /* tmp->parent = (xmlNodePtr)ret; */
4069 if (tmp->next == NULL)
4077 if ((target!= NULL) && (cur!= NULL) &&
4078 (target->doc != NULL) && (cur->doc != NULL) &&
4079 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
4080 if (xmlIsID(cur->doc, cur->parent, cur)) {
4083 id = xmlNodeListGetString(cur->doc, cur->children, 1);
4085 xmlAddID(NULL, target->doc, id, ret);
4095 * @target: the element where the attribute will be grafted
4096 * @cur: the attribute
4098 * Do a copy of the attribute.
4100 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4103 xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
4104 return xmlCopyPropInternal(NULL, target, cur);
4109 * @target: the element where the attributes will be grafted
4110 * @cur: the first attribute
4112 * Do a copy of an attribute list.
4114 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4117 xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
4118 xmlAttrPtr ret = NULL;
4119 xmlAttrPtr p = NULL,q;
4121 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4123 while (cur != NULL) {
4124 q = xmlCopyProp(target, cur);
4140 * NOTE about the CopyNode operations !
4142 * They are split into external and internal parts for one
4143 * tricky reason: namespaces. Doing a direct copy of a node
4144 * say RPM:Copyright without changing the namespace pointer to
4145 * something else can produce stale links. One way to do it is
4146 * to keep a reference counter but this doesn't work as soon
4147 * as one move the element or the subtree out of the scope of
4148 * the existing namespace. The actual solution seems to add
4149 * a copy of the namespace at the top of the copied tree if
4150 * not available in the subtree.
4151 * Hence two functions, the public front-end call the inner ones
4152 * The argument "recursive" normally indicates a recursive copy
4153 * of the node with values 0 (no) and 1 (yes). For XInclude,
4154 * however, we allow a value of 2 to indicate copy properties and
4155 * namespace info, but don't recurse on children.
4159 xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
4163 if (node == NULL) return(NULL);
4164 switch (node->type) {
4166 case XML_CDATA_SECTION_NODE:
4167 case XML_ELEMENT_NODE:
4168 case XML_DOCUMENT_FRAG_NODE:
4169 case XML_ENTITY_REF_NODE:
4170 case XML_ENTITY_NODE:
4172 case XML_COMMENT_NODE:
4173 case XML_XINCLUDE_START:
4174 case XML_XINCLUDE_END:
4176 case XML_ATTRIBUTE_NODE:
4177 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
4178 case XML_NAMESPACE_DECL:
4179 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
4181 case XML_DOCUMENT_NODE:
4182 case XML_HTML_DOCUMENT_NODE:
4183 #ifdef LIBXML_DOCB_ENABLED
4184 case XML_DOCB_DOCUMENT_NODE:
4186 #ifdef LIBXML_TREE_ENABLED
4187 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
4188 #endif /* LIBXML_TREE_ENABLED */
4189 case XML_DOCUMENT_TYPE_NODE:
4190 case XML_NOTATION_NODE:
4192 case XML_ELEMENT_DECL:
4193 case XML_ATTRIBUTE_DECL:
4194 case XML_ENTITY_DECL:
4199 * Allocate a new node and fill the fields.
4201 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4203 xmlTreeErrMemory("copying node");
4206 memset(ret, 0, sizeof(xmlNode));
4207 ret->type = node->type;
4210 ret->parent = parent;
4211 if (node->name == xmlStringText)
4212 ret->name = xmlStringText;
4213 else if (node->name == xmlStringTextNoenc)
4214 ret->name = xmlStringTextNoenc;
4215 else if (node->name == xmlStringComment)
4216 ret->name = xmlStringComment;
4217 else if (node->name != NULL) {
4218 if ((doc != NULL) && (doc->dict != NULL))
4219 ret->name = xmlDictLookup(doc->dict, node->name, -1);
4221 ret->name = xmlStrdup(node->name);
4223 if ((node->type != XML_ELEMENT_NODE) &&
4224 (node->content != NULL) &&
4225 (node->type != XML_ENTITY_REF_NODE) &&
4226 (node->type != XML_XINCLUDE_END) &&
4227 (node->type != XML_XINCLUDE_START)) {
4228 ret->content = xmlStrdup(node->content);
4230 if (node->type == XML_ELEMENT_NODE)
4231 ret->line = node->line;
4233 if (parent != NULL) {
4237 * this is a tricky part for the node register thing:
4238 * in case ret does get coalesced in xmlAddChild
4239 * the deregister-node callback is called; so we register ret now already
4241 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
4242 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4244 tmp = xmlAddChild(parent, ret);
4245 /* node could have coalesced */
4252 if (((node->type == XML_ELEMENT_NODE) ||
4253 (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
4254 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
4256 if (node->ns != NULL) {
4259 ns = xmlSearchNs(doc, ret, node->ns->prefix);
4262 * Humm, we are copying an element whose namespace is defined
4263 * out of the new tree scope. Search it in the original tree
4264 * and add it at the top of the new tree
4266 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
4268 xmlNodePtr root = ret;
4270 while (root->parent != NULL) root = root->parent;
4271 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4273 ret->ns = xmlNewReconciliedNs(doc, ret, node->ns);
4277 * reference the existing namespace definition in our own tree.
4282 if (((node->type == XML_ELEMENT_NODE) ||
4283 (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
4284 ret->properties = xmlCopyPropList(ret, node->properties);
4285 if (node->type == XML_ENTITY_REF_NODE) {
4286 if ((doc == NULL) || (node->doc != doc)) {
4288 * The copied node will go into a separate document, so
4289 * to avoid dangling references to the ENTITY_DECL node
4290 * we cannot keep the reference. Try to find it in the
4293 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
4295 ret->children = node->children;
4297 ret->last = ret->children;
4298 } else if ((node->children != NULL) && (extended != 2)) {
4299 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
4300 UPDATE_LAST_CHILD_AND_PARENT(ret)
4304 /* if parent != NULL we already registered the node above */
4305 if ((parent == NULL) &&
4306 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
4307 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4312 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
4313 xmlNodePtr ret = NULL;
4314 xmlNodePtr p = NULL,q;
4316 while (node != NULL) {
4317 #ifdef LIBXML_TREE_ENABLED
4318 if (node->type == XML_DTD_NODE ) {
4323 if (doc->intSubset == NULL) {
4324 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
4325 if (q == NULL) return(NULL);
4328 doc->intSubset = (xmlDtdPtr) q;
4329 xmlAddChild(parent, q);
4331 q = (xmlNodePtr) doc->intSubset;
4332 xmlAddChild(parent, q);
4335 #endif /* LIBXML_TREE_ENABLED */
4336 q = xmlStaticCopyNode(node, doc, parent, 1);
4337 if (q == NULL) return(NULL);
4341 } else if (p != q) {
4342 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
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.
4361 * Returns: a new #xmlNodePtr, or NULL in case of error.
4364 xmlCopyNode(xmlNodePtr node, int extended) {
4367 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
4374 * @doc: the document
4375 * @extended: if 1 do a recursive copy (properties, namespaces and children
4377 * if 2 copy properties and namespaces (when applicable)
4379 * Do a copy of the node to a given document.
4381 * Returns: a new #xmlNodePtr, or NULL in case of error.
4384 xmlDocCopyNode(xmlNodePtr node, xmlDocPtr doc, int extended) {
4387 ret = xmlStaticCopyNode(node, doc, NULL, extended);
4392 * xmlDocCopyNodeList:
4393 * @doc: the target document
4394 * @node: the first node in the list.
4396 * Do a recursive copy of the node list.
4398 * Returns: a new #xmlNodePtr, or NULL in case of error.
4400 xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, xmlNodePtr node) {
4401 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4407 * @node: the first node in the list.
4409 * Do a recursive copy of the node list.
4410 * Use xmlDocCopyNodeList() if possible to ensure string interning.
4412 * Returns: a new #xmlNodePtr, or NULL in case of error.
4414 xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
4415 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4419 #if defined(LIBXML_TREE_ENABLED)
4424 * Do a copy of the dtd.
4426 * Returns: a new #xmlDtdPtr, or NULL in case of error.
4429 xmlCopyDtd(xmlDtdPtr dtd) {
4431 xmlNodePtr cur, p = NULL, q;
4433 if (dtd == NULL) return(NULL);
4434 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4435 if (ret == NULL) return(NULL);
4436 if (dtd->entities != NULL)
4437 ret->entities = (void *) xmlCopyEntitiesTable(
4438 (xmlEntitiesTablePtr) dtd->entities);
4439 if (dtd->notations != NULL)
4440 ret->notations = (void *) xmlCopyNotationTable(
4441 (xmlNotationTablePtr) dtd->notations);
4442 if (dtd->elements != NULL)
4443 ret->elements = (void *) xmlCopyElementTable(
4444 (xmlElementTablePtr) dtd->elements);
4445 if (dtd->attributes != NULL)
4446 ret->attributes = (void *) xmlCopyAttributeTable(
4447 (xmlAttributeTablePtr) dtd->attributes);
4448 if (dtd->pentities != NULL)
4449 ret->pentities = (void *) xmlCopyEntitiesTable(
4450 (xmlEntitiesTablePtr) dtd->pentities);
4452 cur = dtd->children;
4453 while (cur != NULL) {
4456 if (cur->type == XML_ENTITY_DECL) {
4457 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4458 switch (tmp->etype) {
4459 case XML_INTERNAL_GENERAL_ENTITY:
4460 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4461 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4462 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4464 case XML_INTERNAL_PARAMETER_ENTITY:
4465 case XML_EXTERNAL_PARAMETER_ENTITY:
4467 xmlGetParameterEntityFromDtd(ret, tmp->name);
4469 case XML_INTERNAL_PREDEFINED_ENTITY:
4472 } else if (cur->type == XML_ELEMENT_DECL) {
4473 xmlElementPtr tmp = (xmlElementPtr) cur;
4475 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4476 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4477 xmlAttributePtr tmp = (xmlAttributePtr) cur;
4479 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4480 } else if (cur->type == XML_COMMENT_NODE) {
4481 q = xmlCopyNode(cur, 0);
4495 q->parent = (xmlNodePtr) ret;
4506 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
4509 * @doc: the document
4510 * @recursive: if not zero do a recursive copy.
4512 * Do a copy of the document info. If recursive, the content tree will
4513 * be copied too as well as DTD, namespaces and entities.
4515 * Returns: a new #xmlDocPtr, or NULL in case of error.
4518 xmlCopyDoc(xmlDocPtr doc, int recursive) {
4521 if (doc == NULL) return(NULL);
4522 ret = xmlNewDoc(doc->version);
4523 if (ret == NULL) return(NULL);
4524 if (doc->name != NULL)
4525 ret->name = xmlMemStrdup(doc->name);
4526 if (doc->encoding != NULL)
4527 ret->encoding = xmlStrdup(doc->encoding);
4528 if (doc->URL != NULL)
4529 ret->URL = xmlStrdup(doc->URL);
4530 ret->charset = doc->charset;
4531 ret->compression = doc->compression;
4532 ret->standalone = doc->standalone;
4533 if (!recursive) return(ret);
4536 ret->children = NULL;
4537 #ifdef LIBXML_TREE_ENABLED
4538 if (doc->intSubset != NULL) {
4539 ret->intSubset = xmlCopyDtd(doc->intSubset);
4540 if (ret->intSubset == NULL) {
4544 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
4545 ret->intSubset->parent = ret;
4548 if (doc->oldNs != NULL)
4549 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4550 if (doc->children != NULL) {
4553 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4556 tmp = ret->children;
4557 while (tmp != NULL) {
4558 if (tmp->next == NULL)
4565 #endif /* LIBXML_TREE_ENABLED */
4567 /************************************************************************
4569 * Content access functions *
4571 ************************************************************************/
4574 * xmlGetLineNoInternal:
4576 * @depth: used to limit any risk of recursion
4578 * Get line number of @node.
4579 * Try to override the limitation of lines being store in 16 bits ints
4581 * Returns the line number if successful, -1 otherwise
4584 xmlGetLineNoInternal(const xmlNode *node, int depth)
4593 if ((node->type == XML_ELEMENT_NODE) ||
4594 (node->type == XML_TEXT_NODE) ||
4595 (node->type == XML_COMMENT_NODE) ||
4596 (node->type == XML_PI_NODE)) {
4597 if (node->line == 65535) {
4598 if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL))
4599 result = (long) node->psvi;
4600 else if ((node->type == XML_ELEMENT_NODE) &&
4601 (node->children != NULL))
4602 result = xmlGetLineNoInternal(node->children, depth + 1);
4603 else if (node->next != NULL)
4604 result = xmlGetLineNoInternal(node->next, depth + 1);
4605 else if (node->prev != NULL)
4606 result = xmlGetLineNoInternal(node->prev, depth + 1);
4608 if ((result == -1) || (result == 65535))
4609 result = (long) node->line;
4610 } else if ((node->prev != NULL) &&
4611 ((node->prev->type == XML_ELEMENT_NODE) ||
4612 (node->prev->type == XML_TEXT_NODE) ||
4613 (node->prev->type == XML_COMMENT_NODE) ||
4614 (node->prev->type == XML_PI_NODE)))
4615 result = xmlGetLineNoInternal(node->prev, depth + 1);
4616 else if ((node->parent != NULL) &&
4617 (node->parent->type == XML_ELEMENT_NODE))
4618 result = xmlGetLineNoInternal(node->parent, depth + 1);
4627 * Get line number of @node.
4628 * Try to override the limitation of lines being store in 16 bits ints
4629 * if XML_PARSE_BIG_LINES parser option was used
4631 * Returns the line number if successful, -1 otherwise
4634 xmlGetLineNo(const xmlNode *node)
4636 return(xmlGetLineNoInternal(node, 0));
4639 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
4644 * Build a structure based Path for the given node
4646 * Returns the new path or NULL in case of error. The caller must free
4647 * the returned string
4650 xmlGetNodePath(const xmlNode *node)
4652 const xmlNode *cur, *tmp, *next;
4653 xmlChar *buffer = NULL, *temp;
4659 int occur = 0, generic;
4661 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
4665 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4666 if (buffer == NULL) {
4667 xmlTreeErrMemory("getting node path");
4670 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4672 xmlTreeErrMemory("getting node path");
4683 if ((cur->type == XML_DOCUMENT_NODE) ||
4684 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4685 if (buffer[0] == '/')
4689 } else if (cur->type == XML_ELEMENT_NODE) {
4692 name = (const char *) cur->name;
4694 if (cur->ns->prefix != NULL) {
4695 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4696 (char *)cur->ns->prefix, (char *)cur->name);
4697 nametemp[sizeof(nametemp) - 1] = 0;
4701 * We cannot express named elements in the default
4702 * namespace, so use "*".
4711 * Thumbler index computation
4712 * TODO: the ocurence test seems bogus for namespaced names
4715 while (tmp != NULL) {
4716 if ((tmp->type == XML_ELEMENT_NODE) &&
4718 (xmlStrEqual(cur->name, tmp->name) &&
4719 ((tmp->ns == cur->ns) ||
4720 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4721 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4727 while (tmp != NULL && occur == 0) {
4728 if ((tmp->type == XML_ELEMENT_NODE) &&
4730 (xmlStrEqual(cur->name, tmp->name) &&
4731 ((tmp->ns == cur->ns) ||
4732 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4733 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4741 } else if (cur->type == XML_COMMENT_NODE) {
4747 * Thumbler index computation
4750 while (tmp != NULL) {
4751 if (tmp->type == XML_COMMENT_NODE)
4757 while (tmp != NULL && occur == 0) {
4758 if (tmp->type == XML_COMMENT_NODE)
4766 } else if ((cur->type == XML_TEXT_NODE) ||
4767 (cur->type == XML_CDATA_SECTION_NODE)) {
4773 * Thumbler index computation
4776 while (tmp != NULL) {
4777 if ((tmp->type == XML_TEXT_NODE) ||
4778 (tmp->type == XML_CDATA_SECTION_NODE))
4783 * Evaluate if this is the only text- or CDATA-section-node;
4784 * if yes, then we'll get "text()", otherwise "text()[1]".
4788 while (tmp != NULL) {
4789 if ((tmp->type == XML_TEXT_NODE) ||
4790 (tmp->type == XML_CDATA_SECTION_NODE))
4799 } else if (cur->type == XML_PI_NODE) {
4801 snprintf(nametemp, sizeof(nametemp) - 1,
4802 "processing-instruction('%s')", (char *)cur->name);
4803 nametemp[sizeof(nametemp) - 1] = 0;
4809 * Thumbler index computation
4812 while (tmp != NULL) {
4813 if ((tmp->type == XML_PI_NODE) &&
4814 (xmlStrEqual(cur->name, tmp->name)))
4820 while (tmp != NULL && occur == 0) {
4821 if ((tmp->type == XML_PI_NODE) &&
4822 (xmlStrEqual(cur->name, tmp->name)))
4831 } else if (cur->type == XML_ATTRIBUTE_NODE) {
4833 name = (const char *) (((xmlAttrPtr) cur)->name);
4835 if (cur->ns->prefix != NULL)
4836 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4837 (char *)cur->ns->prefix, (char *)cur->name);
4839 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4841 nametemp[sizeof(nametemp) - 1] = 0;
4844 next = ((xmlAttrPtr) cur)->parent;
4850 * Make sure there is enough room
4852 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4854 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4855 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4857 xmlTreeErrMemory("getting node path");
4863 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4865 xmlTreeErrMemory("getting node path");
4873 snprintf((char *) buf, buf_len, "%s%s%s",
4874 sep, name, (char *) buffer);
4876 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
4877 sep, name, occur, (char *) buffer);
4878 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
4880 } while (cur != NULL);
4884 #endif /* LIBXML_TREE_ENABLED */
4887 * xmlDocGetRootElement:
4888 * @doc: the document
4890 * Get the root element of the document (doc->children is a list
4891 * containing possibly comments, PIs, etc ...).
4893 * Returns the #xmlNodePtr for the root or NULL
4896 xmlDocGetRootElement(const xmlDoc *doc) {
4899 if (doc == NULL) return(NULL);
4900 ret = doc->children;
4901 while (ret != NULL) {
4902 if (ret->type == XML_ELEMENT_NODE)
4909 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
4911 * xmlDocSetRootElement:
4912 * @doc: the document
4913 * @root: the new document root element, if root is NULL no action is taken,
4914 * to remove a node from a document use xmlUnlinkNode(root) instead.
4916 * Set the root element of the document (doc->children is a list
4917 * containing possibly comments, PIs, etc ...).
4919 * Returns the old root element if any was found, NULL if root was NULL
4922 xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4923 xmlNodePtr old = NULL;
4925 if (doc == NULL) return(NULL);
4926 if ((root == NULL) || (root->type == XML_NAMESPACE_DECL))
4928 xmlUnlinkNode(root);
4929 xmlSetTreeDoc(root, doc);
4930 root->parent = (xmlNodePtr) doc;
4931 old = doc->children;
4932 while (old != NULL) {
4933 if (old->type == XML_ELEMENT_NODE)
4938 if (doc->children == NULL) {
4939 doc->children = root;
4942 xmlAddSibling(doc->children, root);
4945 xmlReplaceNode(old, root);
4951 #if defined(LIBXML_TREE_ENABLED)
4954 * @cur: the node being changed
4955 * @lang: the language description
4957 * Set the language of a node, i.e. the values of the xml:lang
4961 xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
4964 if (cur == NULL) return;
4967 case XML_CDATA_SECTION_NODE:
4968 case XML_COMMENT_NODE:
4969 case XML_DOCUMENT_NODE:
4970 case XML_DOCUMENT_TYPE_NODE:
4971 case XML_DOCUMENT_FRAG_NODE:
4972 case XML_NOTATION_NODE:
4973 case XML_HTML_DOCUMENT_NODE:
4975 case XML_ELEMENT_DECL:
4976 case XML_ATTRIBUTE_DECL:
4977 case XML_ENTITY_DECL:
4979 case XML_ENTITY_REF_NODE:
4980 case XML_ENTITY_NODE:
4981 case XML_NAMESPACE_DECL:
4982 #ifdef LIBXML_DOCB_ENABLED
4983 case XML_DOCB_DOCUMENT_NODE:
4985 case XML_XINCLUDE_START:
4986 case XML_XINCLUDE_END:
4988 case XML_ELEMENT_NODE:
4989 case XML_ATTRIBUTE_NODE:
4992 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4995 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
4997 #endif /* LIBXML_TREE_ENABLED */
5001 * @cur: the node being checked
5003 * Searches the language of a node, i.e. the values of the xml:lang
5004 * attribute or the one carried by the nearest ancestor.
5006 * Returns a pointer to the lang value, or NULL if not found
5007 * It's up to the caller to free the memory with xmlFree().
5010 xmlNodeGetLang(const xmlNode *cur) {
5013 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
5015 while (cur != NULL) {
5016 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
5025 #ifdef LIBXML_TREE_ENABLED
5027 * xmlNodeSetSpacePreserve:
5028 * @cur: the node being changed
5029 * @val: the xml:space value ("0": default, 1: "preserve")
5031 * Set (or reset) the space preserving behaviour of a node, i.e. the
5032 * value of the xml:space attribute.
5035 xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
5038 if (cur == NULL) return;
5041 case XML_CDATA_SECTION_NODE:
5042 case XML_COMMENT_NODE:
5043 case XML_DOCUMENT_NODE:
5044 case XML_DOCUMENT_TYPE_NODE:
5045 case XML_DOCUMENT_FRAG_NODE:
5046 case XML_NOTATION_NODE:
5047 case XML_HTML_DOCUMENT_NODE:
5049 case XML_ELEMENT_DECL:
5050 case XML_ATTRIBUTE_DECL:
5051 case XML_ENTITY_DECL:
5053 case XML_ENTITY_REF_NODE:
5054 case XML_ENTITY_NODE:
5055 case XML_NAMESPACE_DECL:
5056 case XML_XINCLUDE_START:
5057 case XML_XINCLUDE_END:
5058 #ifdef LIBXML_DOCB_ENABLED
5059 case XML_DOCB_DOCUMENT_NODE:
5062 case XML_ELEMENT_NODE:
5063 case XML_ATTRIBUTE_NODE:
5066 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5071 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
5074 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
5078 #endif /* LIBXML_TREE_ENABLED */
5081 * xmlNodeGetSpacePreserve:
5082 * @cur: the node being checked
5084 * Searches the space preserving behaviour of a node, i.e. the values
5085 * of the xml:space attribute or the one carried by the nearest
5088 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
5091 xmlNodeGetSpacePreserve(const xmlNode *cur) {
5094 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
5096 while (cur != NULL) {
5097 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
5098 if (space != NULL) {
5099 if (xmlStrEqual(space, BAD_CAST "preserve")) {
5103 if (xmlStrEqual(space, BAD_CAST "default")) {
5114 #ifdef LIBXML_TREE_ENABLED
5117 * @cur: the node being changed
5118 * @name: the new tag name
5120 * Set (or reset) the name of a node.
5123 xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
5126 const xmlChar *freeme = NULL;
5128 if (cur == NULL) return;
5129 if (name == NULL) return;
5132 case XML_CDATA_SECTION_NODE:
5133 case XML_COMMENT_NODE:
5134 case XML_DOCUMENT_TYPE_NODE:
5135 case XML_DOCUMENT_FRAG_NODE:
5136 case XML_NOTATION_NODE:
5137 case XML_HTML_DOCUMENT_NODE:
5138 case XML_NAMESPACE_DECL:
5139 case XML_XINCLUDE_START:
5140 case XML_XINCLUDE_END:
5141 #ifdef LIBXML_DOCB_ENABLED
5142 case XML_DOCB_DOCUMENT_NODE:
5145 case XML_ELEMENT_NODE:
5146 case XML_ATTRIBUTE_NODE:
5148 case XML_ENTITY_REF_NODE:
5149 case XML_ENTITY_NODE:
5151 case XML_DOCUMENT_NODE:
5152 case XML_ELEMENT_DECL:
5153 case XML_ATTRIBUTE_DECL:
5154 case XML_ENTITY_DECL:
5163 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
5165 cur->name = xmlDictLookup(dict, name, -1);
5167 if (cur->name != NULL)
5169 cur->name = xmlStrdup(name);
5173 xmlFree((xmlChar *) freeme);
5177 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
5180 * @cur: the node being changed
5181 * @uri: the new base URI
5183 * Set (or reset) the base URI of a node, i.e. the value of the
5184 * xml:base attribute.
5187 xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
5191 if (cur == NULL) return;
5194 case XML_CDATA_SECTION_NODE:
5195 case XML_COMMENT_NODE:
5196 case XML_DOCUMENT_TYPE_NODE:
5197 case XML_DOCUMENT_FRAG_NODE:
5198 case XML_NOTATION_NODE:
5200 case XML_ELEMENT_DECL:
5201 case XML_ATTRIBUTE_DECL:
5202 case XML_ENTITY_DECL:
5204 case XML_ENTITY_REF_NODE:
5205 case XML_ENTITY_NODE:
5206 case XML_NAMESPACE_DECL:
5207 case XML_XINCLUDE_START:
5208 case XML_XINCLUDE_END:
5210 case XML_ELEMENT_NODE:
5211 case XML_ATTRIBUTE_NODE:
5213 case XML_DOCUMENT_NODE:
5214 #ifdef LIBXML_DOCB_ENABLED
5215 case XML_DOCB_DOCUMENT_NODE:
5217 case XML_HTML_DOCUMENT_NODE: {
5218 xmlDocPtr doc = (xmlDocPtr) cur;
5220 if (doc->URL != NULL)
5221 xmlFree((xmlChar *) doc->URL);
5225 doc->URL = xmlPathToURI(uri);
5230 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5233 fixed = xmlPathToURI(uri);
5234 if (fixed != NULL) {
5235 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
5238 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
5241 #endif /* LIBXML_TREE_ENABLED */
5245 * @doc: the document the node pertains to
5246 * @cur: the node being checked
5248 * Searches for the BASE URL. The code should work on both XML
5249 * and HTML document even if base mechanisms are completely different.
5250 * It returns the base as defined in RFC 2396 sections
5251 * 5.1.1. Base URI within Document Content
5253 * 5.1.2. Base URI from the Encapsulating Entity
5254 * However it does not return the document base (5.1.3), use
5255 * doc->URL in this case
5257 * Returns a pointer to the base URL, or NULL if not found
5258 * It's up to the caller to free the memory with xmlFree().
5261 xmlNodeGetBase(const xmlDoc *doc, const xmlNode *cur) {
5262 xmlChar *oldbase = NULL;
5263 xmlChar *base, *newbase;
5265 if ((cur == NULL) && (doc == NULL))
5267 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
5269 if (doc == NULL) doc = cur->doc;
5270 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
5271 cur = doc->children;
5272 while ((cur != NULL) && (cur->name != NULL)) {
5273 if (cur->type != XML_ELEMENT_NODE) {
5277 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5278 cur = cur->children;
5281 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5282 cur = cur->children;
5285 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5286 return(xmlGetProp(cur, BAD_CAST "href"));
5292 while (cur != NULL) {
5293 if (cur->type == XML_ENTITY_DECL) {
5294 xmlEntityPtr ent = (xmlEntityPtr) cur;
5295 return(xmlStrdup(ent->URI));
5297 if (cur->type == XML_ELEMENT_NODE) {
5298 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
5300 if (oldbase != NULL) {
5301 newbase = xmlBuildURI(oldbase, base);
5302 if (newbase != NULL) {
5314 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
5315 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
5316 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
5322 if ((doc != NULL) && (doc->URL != NULL)) {
5323 if (oldbase == NULL)
5324 return(xmlStrdup(doc->URL));
5325 newbase = xmlBuildURI(oldbase, doc->URL);
5333 * xmlNodeBufGetContent:
5335 * @cur: the node being read
5337 * Read the value of a node @cur, this can be either the text carried
5338 * directly by this node if it's a TEXT node or the aggregate string
5339 * of the values carried by this node child's (TEXT and ENTITY_REF).
5340 * Entity references are substituted.
5341 * Fills up the buffer @buffer with this value
5343 * Returns 0 in case of success and -1 in case of error.
5346 xmlNodeBufGetContent(xmlBufferPtr buffer, const xmlNode *cur)
5351 if ((cur == NULL) || (buffer == NULL)) return(-1);
5352 buf = xmlBufFromBuffer(buffer);
5353 ret = xmlBufGetNodeContent(buf, cur);
5354 buffer = xmlBufBackToBuffer(buf);
5355 if ((ret < 0) || (buffer == NULL))
5361 * xmlBufGetNodeContent:
5362 * @buf: a buffer xmlBufPtr
5363 * @cur: the node being read
5365 * Read the value of a node @cur, this can be either the text carried
5366 * directly by this node if it's a TEXT node or the aggregate string
5367 * of the values carried by this node child's (TEXT and ENTITY_REF).
5368 * Entity references are substituted.
5369 * Fills up the buffer @buf with this value
5371 * Returns 0 in case of success and -1 in case of error.
5374 xmlBufGetNodeContent(xmlBufPtr buf, const xmlNode *cur)
5376 if ((cur == NULL) || (buf == NULL)) return(-1);
5377 switch (cur->type) {
5378 case XML_CDATA_SECTION_NODE:
5380 xmlBufCat(buf, cur->content);
5382 case XML_DOCUMENT_FRAG_NODE:
5383 case XML_ELEMENT_NODE:{
5384 const xmlNode *tmp = cur;
5386 while (tmp != NULL) {
5387 switch (tmp->type) {
5388 case XML_CDATA_SECTION_NODE:
5390 if (tmp->content != NULL)
5391 xmlBufCat(buf, tmp->content);
5393 case XML_ENTITY_REF_NODE:
5394 xmlBufGetNodeContent(buf, tmp);
5402 if (tmp->children != NULL) {
5403 if (tmp->children->type != XML_ENTITY_DECL) {
5404 tmp = tmp->children;
5411 if (tmp->next != NULL) {
5424 if (tmp->next != NULL) {
5428 } while (tmp != NULL);
5432 case XML_ATTRIBUTE_NODE:{
5433 xmlAttrPtr attr = (xmlAttrPtr) cur;
5434 xmlNodePtr tmp = attr->children;
5436 while (tmp != NULL) {
5437 if (tmp->type == XML_TEXT_NODE)
5438 xmlBufCat(buf, tmp->content);
5440 xmlBufGetNodeContent(buf, tmp);
5445 case XML_COMMENT_NODE:
5447 xmlBufCat(buf, cur->content);
5449 case XML_ENTITY_REF_NODE:{
5453 /* lookup entity declaration */
5454 ent = xmlGetDocEntity(cur->doc, cur->name);
5458 /* an entity content can be any "well balanced chunk",
5459 * i.e. the result of the content [43] production:
5460 * http://www.w3.org/TR/REC-xml#NT-content
5461 * -> we iterate through child nodes and recursive call
5462 * xmlNodeGetContent() which handles all possible node types */
5463 tmp = ent->children;
5465 xmlBufGetNodeContent(buf, tmp);
5470 case XML_ENTITY_NODE:
5471 case XML_DOCUMENT_TYPE_NODE:
5472 case XML_NOTATION_NODE:
5474 case XML_XINCLUDE_START:
5475 case XML_XINCLUDE_END:
5477 case XML_DOCUMENT_NODE:
5478 #ifdef LIBXML_DOCB_ENABLED
5479 case XML_DOCB_DOCUMENT_NODE:
5481 case XML_HTML_DOCUMENT_NODE:
5482 cur = cur->children;
5483 while (cur!= NULL) {
5484 if ((cur->type == XML_ELEMENT_NODE) ||
5485 (cur->type == XML_TEXT_NODE) ||
5486 (cur->type == XML_CDATA_SECTION_NODE)) {
5487 xmlBufGetNodeContent(buf, cur);
5492 case XML_NAMESPACE_DECL:
5493 xmlBufCat(buf, ((xmlNsPtr) cur)->href);
5495 case XML_ELEMENT_DECL:
5496 case XML_ATTRIBUTE_DECL:
5497 case XML_ENTITY_DECL:
5504 * xmlNodeGetContent:
5505 * @cur: the node being read
5507 * Read the value of a node, this can be either the text carried
5508 * directly by this node if it's a TEXT node or the aggregate string
5509 * of the values carried by this node child's (TEXT and ENTITY_REF).
5510 * Entity references are substituted.
5511 * Returns a new #xmlChar * or NULL if no content is available.
5512 * It's up to the caller to free the memory with xmlFree().
5515 xmlNodeGetContent(const xmlNode *cur)
5519 switch (cur->type) {
5520 case XML_DOCUMENT_FRAG_NODE:
5521 case XML_ELEMENT_NODE:{
5525 buf = xmlBufCreateSize(64);
5528 xmlBufGetNodeContent(buf, cur);
5529 ret = xmlBufDetach(buf);
5533 case XML_ATTRIBUTE_NODE:
5534 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
5535 case XML_COMMENT_NODE:
5537 if (cur->content != NULL)
5538 return (xmlStrdup(cur->content));
5540 case XML_ENTITY_REF_NODE:{
5545 /* lookup entity declaration */
5546 ent = xmlGetDocEntity(cur->doc, cur->name);
5550 buf = xmlBufCreate();
5554 xmlBufGetNodeContent(buf, cur);
5556 ret = xmlBufDetach(buf);
5560 case XML_ENTITY_NODE:
5561 case XML_DOCUMENT_TYPE_NODE:
5562 case XML_NOTATION_NODE:
5564 case XML_XINCLUDE_START:
5565 case XML_XINCLUDE_END:
5567 case XML_DOCUMENT_NODE:
5568 #ifdef LIBXML_DOCB_ENABLED
5569 case XML_DOCB_DOCUMENT_NODE:
5571 case XML_HTML_DOCUMENT_NODE: {
5575 buf = xmlBufCreate();
5579 xmlBufGetNodeContent(buf, (xmlNodePtr) cur);
5581 ret = xmlBufDetach(buf);
5585 case XML_NAMESPACE_DECL: {
5588 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5591 case XML_ELEMENT_DECL:
5594 case XML_ATTRIBUTE_DECL:
5597 case XML_ENTITY_DECL:
5600 case XML_CDATA_SECTION_NODE:
5602 if (cur->content != NULL)
5603 return (xmlStrdup(cur->content));
5610 * xmlNodeSetContent:
5611 * @cur: the node being modified
5612 * @content: the new value of the content
5614 * Replace the content of a node.
5615 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5616 * references, but XML special chars need to be escaped first by using
5617 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5620 xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5623 xmlGenericError(xmlGenericErrorContext,
5624 "xmlNodeSetContent : node == NULL\n");
5628 switch (cur->type) {
5629 case XML_DOCUMENT_FRAG_NODE:
5630 case XML_ELEMENT_NODE:
5631 case XML_ATTRIBUTE_NODE:
5632 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5633 cur->children = xmlStringGetNodeList(cur->doc, content);
5634 UPDATE_LAST_CHILD_AND_PARENT(cur)
5637 case XML_CDATA_SECTION_NODE:
5638 case XML_ENTITY_REF_NODE:
5639 case XML_ENTITY_NODE:
5641 case XML_COMMENT_NODE:
5642 if ((cur->content != NULL) &&
5643 (cur->content != (xmlChar *) &(cur->properties))) {
5644 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5645 (xmlDictOwns(cur->doc->dict, cur->content))))
5646 xmlFree(cur->content);
5648 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5649 cur->last = cur->children = NULL;
5650 if (content != NULL) {
5651 cur->content = xmlStrdup(content);
5653 cur->content = NULL;
5654 cur->properties = NULL;
5657 case XML_DOCUMENT_NODE:
5658 case XML_HTML_DOCUMENT_NODE:
5659 case XML_DOCUMENT_TYPE_NODE:
5660 case XML_XINCLUDE_START:
5661 case XML_XINCLUDE_END:
5662 #ifdef LIBXML_DOCB_ENABLED
5663 case XML_DOCB_DOCUMENT_NODE:
5666 case XML_NOTATION_NODE:
5670 case XML_NAMESPACE_DECL:
5672 case XML_ELEMENT_DECL:
5675 case XML_ATTRIBUTE_DECL:
5678 case XML_ENTITY_DECL:
5684 #ifdef LIBXML_TREE_ENABLED
5686 * xmlNodeSetContentLen:
5687 * @cur: the node being modified
5688 * @content: the new value of the content
5689 * @len: the size of @content
5691 * Replace the content of a node.
5692 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5693 * references, but XML special chars need to be escaped first by using
5694 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5697 xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5700 xmlGenericError(xmlGenericErrorContext,
5701 "xmlNodeSetContentLen : node == NULL\n");
5705 switch (cur->type) {
5706 case XML_DOCUMENT_FRAG_NODE:
5707 case XML_ELEMENT_NODE:
5708 case XML_ATTRIBUTE_NODE:
5709 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5710 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5711 UPDATE_LAST_CHILD_AND_PARENT(cur)
5714 case XML_CDATA_SECTION_NODE:
5715 case XML_ENTITY_REF_NODE:
5716 case XML_ENTITY_NODE:
5718 case XML_COMMENT_NODE:
5719 case XML_NOTATION_NODE:
5720 if ((cur->content != NULL) &&
5721 (cur->content != (xmlChar *) &(cur->properties))) {
5722 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5723 (xmlDictOwns(cur->doc->dict, cur->content))))
5724 xmlFree(cur->content);
5726 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5727 cur->children = cur->last = NULL;
5728 if (content != NULL) {
5729 cur->content = xmlStrndup(content, len);
5731 cur->content = NULL;
5732 cur->properties = NULL;
5735 case XML_DOCUMENT_NODE:
5737 case XML_HTML_DOCUMENT_NODE:
5738 case XML_DOCUMENT_TYPE_NODE:
5739 case XML_NAMESPACE_DECL:
5740 case XML_XINCLUDE_START:
5741 case XML_XINCLUDE_END:
5742 #ifdef LIBXML_DOCB_ENABLED
5743 case XML_DOCB_DOCUMENT_NODE:
5746 case XML_ELEMENT_DECL:
5749 case XML_ATTRIBUTE_DECL:
5752 case XML_ENTITY_DECL:
5757 #endif /* LIBXML_TREE_ENABLED */
5760 * xmlNodeAddContentLen:
5761 * @cur: the node being modified
5762 * @content: extra content
5763 * @len: the size of @content
5765 * Append the extra substring to the node content.
5766 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5767 * raw text, so unescaped XML special chars are allowed, entity
5768 * references are not supported.
5771 xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5774 xmlGenericError(xmlGenericErrorContext,
5775 "xmlNodeAddContentLen : node == NULL\n");
5779 if (len <= 0) return;
5780 switch (cur->type) {
5781 case XML_DOCUMENT_FRAG_NODE:
5782 case XML_ELEMENT_NODE: {
5783 xmlNodePtr last, newNode, tmp;
5786 newNode = xmlNewTextLen(content, len);
5787 if (newNode != NULL) {
5788 tmp = xmlAddChild(cur, newNode);
5791 if ((last != NULL) && (last->next == newNode)) {
5792 xmlTextMerge(last, newNode);
5797 case XML_ATTRIBUTE_NODE:
5800 case XML_CDATA_SECTION_NODE:
5801 case XML_ENTITY_REF_NODE:
5802 case XML_ENTITY_NODE:
5804 case XML_COMMENT_NODE:
5805 case XML_NOTATION_NODE:
5806 if (content != NULL) {
5807 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5808 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5809 xmlDictOwns(cur->doc->dict, cur->content))) {
5810 cur->content = xmlStrncatNew(cur->content, content, len);
5811 cur->properties = NULL;
5815 cur->content = xmlStrncat(cur->content, content, len);
5817 case XML_DOCUMENT_NODE:
5819 case XML_HTML_DOCUMENT_NODE:
5820 case XML_DOCUMENT_TYPE_NODE:
5821 case XML_NAMESPACE_DECL:
5822 case XML_XINCLUDE_START:
5823 case XML_XINCLUDE_END:
5824 #ifdef LIBXML_DOCB_ENABLED
5825 case XML_DOCB_DOCUMENT_NODE:
5828 case XML_ELEMENT_DECL:
5829 case XML_ATTRIBUTE_DECL:
5830 case XML_ENTITY_DECL:
5836 * xmlNodeAddContent:
5837 * @cur: the node being modified
5838 * @content: extra content
5840 * Append the extra substring to the node content.
5841 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5842 * raw text, so unescaped XML special chars are allowed, entity
5843 * references are not supported.
5846 xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5851 xmlGenericError(xmlGenericErrorContext,
5852 "xmlNodeAddContent : node == NULL\n");
5856 if (content == NULL) return;
5857 len = xmlStrlen(content);
5858 xmlNodeAddContentLen(cur, content, len);
5863 * @first: the first text node
5864 * @second: the second text node being merged
5866 * Merge two text nodes into one
5867 * Returns the first text node augmented
5870 xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5871 if (first == NULL) return(second);
5872 if (second == NULL) return(first);
5873 if (first->type != XML_TEXT_NODE) return(first);
5874 if (second->type != XML_TEXT_NODE) return(first);
5875 if (second->name != first->name)
5877 xmlNodeAddContent(first, second->content);
5878 xmlUnlinkNode(second);
5879 xmlFreeNode(second);
5883 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
5886 * @doc: the document
5887 * @node: the current node
5889 * Search all the namespace applying to a given element.
5890 * Returns an NULL terminated array of all the #xmlNsPtr found
5891 * that need to be freed by the caller or NULL if no
5892 * namespace if defined
5895 xmlGetNsList(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node)
5898 xmlNsPtr *ret = NULL;
5903 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5906 while (node != NULL) {
5907 if (node->type == XML_ELEMENT_NODE) {
5909 while (cur != NULL) {
5912 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5915 xmlTreeErrMemory("getting namespace list");
5920 for (i = 0; i < nbns; i++) {
5921 if ((cur->prefix == ret[i]->prefix) ||
5922 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5926 if (nbns >= maxns) {
5928 ret = (xmlNsPtr *) xmlRealloc(ret,
5933 xmlTreeErrMemory("getting namespace list");
5944 node = node->parent;
5948 #endif /* LIBXML_TREE_ENABLED */
5951 * xmlTreeEnsureXMLDecl:
5954 * Ensures that there is an XML namespace declaration on the doc.
5956 * Returns the XML ns-struct or NULL on API and internal errors.
5959 xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5963 if (doc->oldNs != NULL)
5964 return (doc->oldNs);
5967 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5970 "allocating the XML namespace");
5973 memset(ns, 0, sizeof(xmlNs));
5974 ns->type = XML_LOCAL_NAMESPACE;
5975 ns->href = xmlStrdup(XML_XML_NAMESPACE);
5976 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5984 * @doc: the document
5985 * @node: the current node
5986 * @nameSpace: the namespace prefix
5988 * Search a Ns registered under a given name space for a document.
5989 * recurse on the parents until it finds the defined namespace
5990 * or return NULL otherwise.
5991 * @nameSpace can be NULL, this is a search for the default namespace.
5992 * We don't allow to cross entities boundaries. If you don't declare
5993 * the namespace within those you will be in troubles !!! A warning
5994 * is generated to cover this case.
5996 * Returns the namespace pointer or NULL.
5999 xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
6002 const xmlNode *orig = node;
6004 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL);
6005 if ((nameSpace != NULL) &&
6006 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
6007 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6009 * The XML-1.0 namespace is normally held on the root
6010 * element. In this case exceptionally create it on the
6013 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6015 xmlTreeErrMemory("searching namespace");
6018 memset(cur, 0, sizeof(xmlNs));
6019 cur->type = XML_LOCAL_NAMESPACE;
6020 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6021 cur->prefix = xmlStrdup((const xmlChar *)"xml");
6022 cur->next = node->nsDef;
6032 * Return the XML namespace declaration held by the doc.
6034 if (doc->oldNs == NULL)
6035 return(xmlTreeEnsureXMLDecl(doc));
6039 while (node != NULL) {
6040 if ((node->type == XML_ENTITY_REF_NODE) ||
6041 (node->type == XML_ENTITY_NODE) ||
6042 (node->type == XML_ENTITY_DECL))
6044 if (node->type == XML_ELEMENT_NODE) {
6046 while (cur != NULL) {
6047 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6048 (cur->href != NULL))
6050 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6051 (cur->href != NULL) &&
6052 (xmlStrEqual(cur->prefix, nameSpace)))
6059 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6060 (cur->href != NULL))
6062 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6063 (cur->href != NULL) &&
6064 (xmlStrEqual(cur->prefix, nameSpace)))
6069 node = node->parent;
6076 * @doc: the document
6077 * @node: the current node
6078 * @ancestor: the ancestor carrying the namespace
6079 * @prefix: the namespace prefix
6081 * Verify that the given namespace held on @ancestor is still in scope
6084 * Returns 1 if true, 0 if false and -1 in case of error.
6087 xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
6088 xmlNodePtr ancestor, const xmlChar * prefix)
6092 while ((node != NULL) && (node != ancestor)) {
6093 if ((node->type == XML_ENTITY_REF_NODE) ||
6094 (node->type == XML_ENTITY_NODE) ||
6095 (node->type == XML_ENTITY_DECL))
6097 if (node->type == XML_ELEMENT_NODE) {
6099 while (tst != NULL) {
6100 if ((tst->prefix == NULL)
6101 && (prefix == NULL))
6103 if ((tst->prefix != NULL)
6105 && (xmlStrEqual(tst->prefix, prefix)))
6110 node = node->parent;
6112 if (node != ancestor)
6118 * xmlSearchNsByHref:
6119 * @doc: the document
6120 * @node: the current node
6121 * @href: the namespace value
6123 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
6124 * the defined namespace or return NULL otherwise.
6125 * Returns the namespace pointer or NULL.
6128 xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
6131 xmlNodePtr orig = node;
6134 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL))
6136 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
6138 * Only the document can hold the XML spec namespace.
6140 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6142 * The XML-1.0 namespace is normally held on the root
6143 * element. In this case exceptionally create it on the
6146 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6148 xmlTreeErrMemory("searching namespace");
6151 memset(cur, 0, sizeof(xmlNs));
6152 cur->type = XML_LOCAL_NAMESPACE;
6153 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6154 cur->prefix = xmlStrdup((const xmlChar *) "xml");
6155 cur->next = node->nsDef;
6165 * Return the XML namespace declaration held by the doc.
6167 if (doc->oldNs == NULL)
6168 return(xmlTreeEnsureXMLDecl(doc));
6172 is_attr = (node->type == XML_ATTRIBUTE_NODE);
6173 while (node != NULL) {
6174 if ((node->type == XML_ENTITY_REF_NODE) ||
6175 (node->type == XML_ENTITY_NODE) ||
6176 (node->type == XML_ENTITY_DECL))
6178 if (node->type == XML_ELEMENT_NODE) {
6180 while (cur != NULL) {
6181 if ((cur->href != NULL) && (href != NULL) &&
6182 (xmlStrEqual(cur->href, href))) {
6183 if (((!is_attr) || (cur->prefix != NULL)) &&
6184 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6192 if ((cur->href != NULL) && (href != NULL) &&
6193 (xmlStrEqual(cur->href, href))) {
6194 if (((!is_attr) || (cur->prefix != NULL)) &&
6195 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6201 node = node->parent;
6207 * xmlNewReconciliedNs:
6208 * @doc: the document
6209 * @tree: a node expected to hold the new namespace
6210 * @ns: the original namespace
6212 * This function tries to locate a namespace definition in a tree
6213 * ancestors, or create a new namespace definition node similar to
6214 * @ns trying to reuse the same prefix. However if the given prefix is
6215 * null (default namespace) or reused within the subtree defined by
6216 * @tree or on one of its ancestors then a new prefix is generated.
6217 * Returns the (new) namespace definition or NULL in case of error
6220 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
6225 if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) {
6227 xmlGenericError(xmlGenericErrorContext,
6228 "xmlNewReconciliedNs : tree == NULL\n");
6232 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
6234 xmlGenericError(xmlGenericErrorContext,
6235 "xmlNewReconciliedNs : ns == NULL\n");
6240 * Search an existing namespace definition inherited.
6242 def = xmlSearchNsByHref(doc, tree, ns->href);
6247 * Find a close prefix which is not already in use.
6248 * Let's strip namespace prefixes longer than 20 chars !
6250 if (ns->prefix == NULL)
6251 snprintf((char *) prefix, sizeof(prefix), "default");
6253 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
6255 def = xmlSearchNs(doc, tree, prefix);
6256 while (def != NULL) {
6257 if (counter > 1000) return(NULL);
6258 if (ns->prefix == NULL)
6259 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
6261 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
6262 (char *)ns->prefix, counter++);
6263 def = xmlSearchNs(doc, tree, prefix);
6267 * OK, now we are ready to create a new one.
6269 def = xmlNewNs(tree, ns->href, prefix);
6273 #ifdef LIBXML_TREE_ENABLED
6275 * xmlReconciliateNs:
6276 * @doc: the document
6277 * @tree: a node defining the subtree to reconciliate
6279 * This function checks that all the namespaces declared within the given
6280 * tree are properly declared. This is needed for example after Copy or Cut
6281 * and then paste operations. The subtree may still hold pointers to
6282 * namespace declarations outside the subtree or invalid/masked. As much
6283 * as possible the function try to reuse the existing namespaces found in
6284 * the new environment. If not possible the new namespaces are redeclared
6285 * on @tree at the top of the given subtree.
6286 * Returns the number of namespace declarations created or -1 in case of error.
6289 xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
6290 xmlNsPtr *oldNs = NULL;
6291 xmlNsPtr *newNs = NULL;
6296 xmlNodePtr node = tree;
6300 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6301 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
6302 if (node->doc != doc) return(-1);
6303 while (node != NULL) {
6305 * Reconciliate the node namespace
6307 if (node->ns != NULL) {
6309 * initialize the cache if needed
6311 if (sizeCache == 0) {
6313 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6315 if (oldNs == NULL) {
6316 xmlTreeErrMemory("fixing namespaces");
6319 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6321 if (newNs == NULL) {
6322 xmlTreeErrMemory("fixing namespaces");
6327 for (i = 0;i < nbCache;i++) {
6328 if (oldNs[i] == node->ns) {
6329 node->ns = newNs[i];
6335 * OK we need to recreate a new namespace definition
6337 n = xmlNewReconciliedNs(doc, tree, node->ns);
6338 if (n != NULL) { /* :-( what if else ??? */
6340 * check if we need to grow the cache buffers.
6342 if (sizeCache <= nbCache) {
6344 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
6346 if (oldNs == NULL) {
6347 xmlTreeErrMemory("fixing namespaces");
6351 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
6353 if (newNs == NULL) {
6354 xmlTreeErrMemory("fixing namespaces");
6360 oldNs[nbCache++] = node->ns;
6366 * now check for namespace hold by attributes on the node.
6368 if (node->type == XML_ELEMENT_NODE) {
6369 attr = node->properties;
6370 while (attr != NULL) {
6371 if (attr->ns != NULL) {
6373 * initialize the cache if needed
6375 if (sizeCache == 0) {
6377 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6379 if (oldNs == NULL) {
6380 xmlTreeErrMemory("fixing namespaces");
6383 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6385 if (newNs == NULL) {
6386 xmlTreeErrMemory("fixing namespaces");
6391 for (i = 0;i < nbCache;i++) {
6392 if (oldNs[i] == attr->ns) {
6393 attr->ns = newNs[i];
6399 * OK we need to recreate a new namespace definition
6401 n = xmlNewReconciliedNs(doc, tree, attr->ns);
6402 if (n != NULL) { /* :-( what if else ??? */
6404 * check if we need to grow the cache buffers.
6406 if (sizeCache <= nbCache) {
6408 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
6409 sizeCache * sizeof(xmlNsPtr));
6410 if (oldNs == NULL) {
6411 xmlTreeErrMemory("fixing namespaces");
6415 newNs = (xmlNsPtr *) xmlRealloc(newNs,
6416 sizeCache * sizeof(xmlNsPtr));
6417 if (newNs == NULL) {
6418 xmlTreeErrMemory("fixing namespaces");
6424 oldNs[nbCache++] = attr->ns;
6434 * Browse the full subtree, deep first
6436 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
6438 node = node->children;
6439 } else if ((node != tree) && (node->next != NULL)) {
6442 } else if (node != tree) {
6443 /* go up to parents->next if needed */
6444 while (node != tree) {
6445 if (node->parent != NULL)
6446 node = node->parent;
6447 if ((node != tree) && (node->next != NULL)) {
6451 if (node->parent == NULL) {
6456 /* exit condition */
6468 #endif /* LIBXML_TREE_ENABLED */
6471 xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name,
6472 const xmlChar *nsName, int useDTD)
6476 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6479 if (node->properties != NULL) {
6480 prop = node->properties;
6481 if (nsName == NULL) {
6483 * We want the attr to be in no namespace.
6486 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6490 } while (prop != NULL);
6493 * We want the attr to be in the specified namespace.
6496 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6497 ((prop->ns->href == nsName) ||
6498 xmlStrEqual(prop->ns->href, nsName)))
6503 } while (prop != NULL);
6507 #ifdef LIBXML_TREE_ENABLED
6511 * Check if there is a default/fixed attribute declaration in
6512 * the internal or external subset.
6514 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6515 xmlDocPtr doc = node->doc;
6516 xmlAttributePtr attrDecl = NULL;
6517 xmlChar *elemQName, *tmpstr = NULL;
6520 * We need the QName of the element for the DTD-lookup.
6522 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6523 tmpstr = xmlStrdup(node->ns->prefix);
6524 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6525 tmpstr = xmlStrcat(tmpstr, node->name);
6530 elemQName = (xmlChar *) node->name;
6531 if (nsName == NULL) {
6533 * The common and nice case: Attr in no namespace.
6535 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6536 elemQName, name, NULL);
6537 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6538 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6539 elemQName, name, NULL);
6542 xmlNsPtr *nsList, *cur;
6545 * The ugly case: Search using the prefixes of in-scope
6546 * ns-decls corresponding to @nsName.
6548 nsList = xmlGetNsList(node->doc, node);
6549 if (nsList == NULL) {
6555 while (*cur != NULL) {
6556 if (xmlStrEqual((*cur)->href, nsName)) {
6557 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6558 name, (*cur)->prefix);
6561 if (doc->extSubset != NULL) {
6562 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6563 name, (*cur)->prefix);
6575 * Only default/fixed attrs are relevant.
6577 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6578 return((xmlAttrPtr) attrDecl);
6580 #endif /* LIBXML_TREE_ENABLED */
6585 xmlGetPropNodeValueInternal(const xmlAttr *prop)
6589 if (prop->type == XML_ATTRIBUTE_NODE) {
6591 * Note that we return at least the empty string.
6592 * TODO: Do we really always want that?
6594 if (prop->children != NULL) {
6595 if ((prop->children->next == NULL) &&
6596 ((prop->children->type == XML_TEXT_NODE) ||
6597 (prop->children->type == XML_CDATA_SECTION_NODE)))
6600 * Optimization for the common case: only 1 text node.
6602 return(xmlStrdup(prop->children->content));
6606 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6611 return(xmlStrdup((xmlChar *)""));
6612 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6613 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6621 * @name: the attribute name
6623 * Search an attribute associated to a node
6624 * This function also looks in DTD attribute declaration for #FIXED or
6625 * default declaration values unless DTD use has been turned off.
6627 * Returns the attribute or the attribute declaration or NULL if
6628 * neither was found.
6631 xmlHasProp(const xmlNode *node, const xmlChar *name) {
6635 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6638 * Check on the properties attached to the node
6640 prop = node->properties;
6641 while (prop != NULL) {
6642 if (xmlStrEqual(prop->name, name)) {
6647 if (!xmlCheckDTD) return(NULL);
6650 * Check if there is a default declaration in the internal
6651 * or external subsets
6655 xmlAttributePtr attrDecl;
6656 if (doc->intSubset != NULL) {
6657 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6658 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6659 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6660 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6661 /* return attribute declaration only if a default value is given
6662 (that includes #FIXED declarations) */
6663 return((xmlAttrPtr) attrDecl);
6672 * @name: the attribute name
6673 * @nameSpace: the URI of the namespace
6675 * Search for an attribute associated to a node
6676 * This attribute has to be anchored in the namespace specified.
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 that a namespace of NULL indicates to use the default namespace.
6682 * Returns the attribute or the attribute declaration or NULL
6683 * if neither was found.
6686 xmlHasNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
6688 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
6694 * @name: the attribute name
6696 * Search and get the value of an attribute associated to a node
6697 * This does the entity substitution.
6698 * This function looks in DTD attribute declaration for #FIXED or
6699 * default declaration values unless DTD use has been turned off.
6700 * NOTE: this function acts independently of namespaces associated
6701 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6702 * for namespace aware processing.
6704 * Returns the attribute value or NULL if not found.
6705 * It's up to the caller to free the memory with xmlFree().
6708 xmlGetProp(const xmlNode *node, const xmlChar *name) {
6711 prop = xmlHasProp(node, name);
6714 return(xmlGetPropNodeValueInternal(prop));
6720 * @name: the attribute name
6722 * Search and get the value of an attribute associated to a node
6723 * This does the entity substitution.
6724 * This function looks in DTD attribute declaration for #FIXED or
6725 * default declaration values unless DTD use has been turned off.
6726 * This function is similar to xmlGetProp except it will accept only
6727 * an attribute in no namespace.
6729 * Returns the attribute value or NULL if not found.
6730 * It's up to the caller to free the memory with xmlFree().
6733 xmlGetNoNsProp(const xmlNode *node, const xmlChar *name) {
6736 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6739 return(xmlGetPropNodeValueInternal(prop));
6745 * @name: the attribute name
6746 * @nameSpace: the URI of the namespace
6748 * Search and get the value of an attribute associated to a node
6749 * This attribute has to be anchored in the namespace specified.
6750 * This does the entity substitution.
6751 * This function looks in DTD attribute declaration for #FIXED or
6752 * default declaration values unless DTD use has been turned off.
6754 * Returns the attribute value or NULL if not found.
6755 * It's up to the caller to free the memory with xmlFree().
6758 xmlGetNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
6761 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6764 return(xmlGetPropNodeValueInternal(prop));
6767 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6771 * @name: the attribute name
6773 * Remove an attribute carried by a node.
6774 * This handles only attributes in no namespace.
6775 * Returns 0 if successful, -1 if not found
6778 xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
6781 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6784 xmlUnlinkNode((xmlNodePtr) prop);
6792 * @ns: the namespace definition
6793 * @name: the attribute name
6795 * Remove an attribute carried by a node.
6796 * Returns 0 if successful, -1 if not found
6799 xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
6802 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6805 xmlUnlinkNode((xmlNodePtr) prop);
6811 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
6815 * @name: the attribute name (a QName)
6816 * @value: the attribute value
6818 * Set (or reset) an attribute carried by a node.
6819 * If @name has a prefix, then the corresponding
6820 * namespace-binding will be used, if in scope; it is an
6821 * error it there's no such ns-binding for the prefix in
6823 * Returns the attribute pointer.
6827 xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
6829 const xmlChar *nqname;
6831 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
6837 nqname = xmlSplitQName3(name, &len);
6838 if (nqname != NULL) {
6840 xmlChar *prefix = xmlStrndup(name, len);
6841 ns = xmlSearchNs(node->doc, node, prefix);
6845 return(xmlSetNsProp(node, ns, nqname, value));
6847 return(xmlSetNsProp(node, NULL, name, value));
6853 * @ns: the namespace definition
6854 * @name: the attribute name
6855 * @value: the attribute value
6857 * Set (or reset) an attribute carried by a node.
6858 * The ns structure must be in scope, this is not checked
6860 * Returns the attribute pointer.
6863 xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6864 const xmlChar *value)
6868 if (ns && (ns->href == NULL))
6870 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6873 * Modify the attribute's value.
6875 if (prop->atype == XML_ATTRIBUTE_ID) {
6876 xmlRemoveID(node->doc, prop);
6877 prop->atype = XML_ATTRIBUTE_ID;
6879 if (prop->children != NULL)
6880 xmlFreeNodeList(prop->children);
6881 prop->children = NULL;
6884 if (value != NULL) {
6887 if(!xmlCheckUTF8(value)) {
6888 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc,
6890 if (node->doc != NULL)
6891 node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
6893 prop->children = xmlNewDocText(node->doc, value);
6895 tmp = prop->children;
6896 while (tmp != NULL) {
6897 tmp->parent = (xmlNodePtr) prop;
6898 if (tmp->next == NULL)
6903 if (prop->atype == XML_ATTRIBUTE_ID)
6904 xmlAddID(NULL, node->doc, value, prop);
6908 * No equal attr found; create a new one.
6910 return(xmlNewPropInternal(node, ns, name, value, 0));
6913 #endif /* LIBXML_TREE_ENABLED */
6919 * Is this node a Text node ?
6920 * Returns 1 yes, 0 no
6923 xmlNodeIsText(const xmlNode *node) {
6924 if (node == NULL) return(0);
6926 if (node->type == XML_TEXT_NODE) return(1);
6934 * Checks whether this node is an empty or whitespace only
6935 * (and possibly ignorable) text-node.
6937 * Returns 1 yes, 0 no
6940 xmlIsBlankNode(const xmlNode *node) {
6942 if (node == NULL) return(0);
6944 if ((node->type != XML_TEXT_NODE) &&
6945 (node->type != XML_CDATA_SECTION_NODE))
6947 if (node->content == NULL) return(1);
6948 cur = node->content;
6950 if (!IS_BLANK_CH(*cur)) return(0);
6960 * @content: the content
6961 * @len: @content length
6963 * Concat the given string at the end of the existing node content
6965 * Returns -1 in case of error, 0 otherwise
6969 xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
6970 if (node == NULL) return(-1);
6972 if ((node->type != XML_TEXT_NODE) &&
6973 (node->type != XML_CDATA_SECTION_NODE) &&
6974 (node->type != XML_COMMENT_NODE) &&
6975 (node->type != XML_PI_NODE)) {
6977 xmlGenericError(xmlGenericErrorContext,
6978 "xmlTextConcat: node is not text nor CDATA\n");
6982 /* need to check if content is currently in the dictionary */
6983 if ((node->content == (xmlChar *) &(node->properties)) ||
6984 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6985 xmlDictOwns(node->doc->dict, node->content))) {
6986 node->content = xmlStrncatNew(node->content, content, len);
6988 node->content = xmlStrncat(node->content, content, len);
6990 node->properties = NULL;
6991 if (node->content == NULL)
6996 /************************************************************************
6998 * Output : to a FILE or in memory *
7000 ************************************************************************/
7005 * routine to create an XML buffer.
7006 * returns the new structure.
7009 xmlBufferCreate(void) {
7012 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7014 xmlTreeErrMemory("creating buffer");
7018 ret->size = xmlDefaultBufferSize;
7019 ret->alloc = xmlBufferAllocScheme;
7020 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
7021 if (ret->content == NULL) {
7022 xmlTreeErrMemory("creating buffer");
7026 ret->content[0] = 0;
7027 ret->contentIO = NULL;
7032 * xmlBufferCreateSize:
7033 * @size: initial size of buffer
7035 * routine to create an XML buffer.
7036 * returns the new structure.
7039 xmlBufferCreateSize(size_t size) {
7042 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7044 xmlTreeErrMemory("creating buffer");
7048 ret->alloc = xmlBufferAllocScheme;
7049 ret->size = (size ? size+2 : 0); /* +1 for ending null */
7051 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
7052 if (ret->content == NULL) {
7053 xmlTreeErrMemory("creating buffer");
7057 ret->content[0] = 0;
7059 ret->content = NULL;
7060 ret->contentIO = NULL;
7068 * Remove the string contained in a buffer and gie it back to the
7069 * caller. The buffer is reset to an empty content.
7070 * This doesn't work with immutable buffers as they can't be reset.
7072 * Returns the previous string contained by the buffer.
7075 xmlBufferDetach(xmlBufferPtr buf) {
7080 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
7084 buf->content = NULL;
7093 * xmlBufferCreateStatic:
7094 * @mem: the memory area
7095 * @size: the size in byte
7097 * routine to create an XML buffer from an immutable memory area.
7098 * The area won't be modified nor copied, and is expected to be
7099 * present until the end of the buffer lifetime.
7101 * returns the new structure.
7104 xmlBufferCreateStatic(void *mem, size_t size) {
7107 if ((mem == NULL) || (size == 0))
7110 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7112 xmlTreeErrMemory("creating buffer");
7117 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
7118 ret->content = (xmlChar *) mem;
7123 * xmlBufferSetAllocationScheme:
7124 * @buf: the buffer to tune
7125 * @scheme: allocation scheme to use
7127 * Sets the allocation scheme for this buffer
7130 xmlBufferSetAllocationScheme(xmlBufferPtr buf,
7131 xmlBufferAllocationScheme scheme) {
7134 xmlGenericError(xmlGenericErrorContext,
7135 "xmlBufferSetAllocationScheme: buf == NULL\n");
7139 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7140 (buf->alloc == XML_BUFFER_ALLOC_IO)) return;
7141 if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
7142 (scheme == XML_BUFFER_ALLOC_EXACT) ||
7143 (scheme == XML_BUFFER_ALLOC_HYBRID) ||
7144 (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
7145 buf->alloc = scheme;
7150 * @buf: the buffer to free
7152 * Frees an XML buffer. It frees both the content and the structure which
7156 xmlBufferFree(xmlBufferPtr buf) {
7159 xmlGenericError(xmlGenericErrorContext,
7160 "xmlBufferFree: buf == NULL\n");
7165 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7166 (buf->contentIO != NULL)) {
7167 xmlFree(buf->contentIO);
7168 } else if ((buf->content != NULL) &&
7169 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
7170 xmlFree(buf->content);
7182 xmlBufferEmpty(xmlBufferPtr buf) {
7183 if (buf == NULL) return;
7184 if (buf->content == NULL) return;
7186 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
7187 buf->content = BAD_CAST "";
7188 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7189 (buf->contentIO != NULL)) {
7190 size_t start_buf = buf->content - buf->contentIO;
7192 buf->size += start_buf;
7193 buf->content = buf->contentIO;
7194 buf->content[0] = 0;
7196 buf->content[0] = 0;
7202 * @buf: the buffer to dump
7203 * @len: the number of xmlChar to remove
7205 * Remove the beginning of an XML buffer.
7207 * Returns the number of #xmlChar removed, or -1 in case of failure.
7210 xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
7211 if (buf == NULL) return(-1);
7212 if (len == 0) return(0);
7213 if (len > buf->use) return(-1);
7216 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7217 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
7219 * we just move the content pointer, but also make sure
7220 * the perceived buffer size has shrinked accordingly
7222 buf->content += len;
7226 * sometimes though it maybe be better to really shrink
7229 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7230 size_t start_buf = buf->content - buf->contentIO;
7231 if (start_buf >= buf->size) {
7232 memmove(buf->contentIO, &buf->content[0], buf->use);
7233 buf->content = buf->contentIO;
7234 buf->content[buf->use] = 0;
7235 buf->size += start_buf;
7239 memmove(buf->content, &buf->content[len], buf->use);
7240 buf->content[buf->use] = 0;
7248 * @len: the minimum free size to allocate
7250 * Grow the available space of an XML buffer.
7252 * Returns the new available space or -1 in case of error
7255 xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
7259 if (buf == NULL) return(-1);
7261 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7262 if (len + buf->use < buf->size) return(0);
7265 * Windows has a BIG problem on realloc timing, so we try to double
7266 * the buffer size (if that's enough) (bug 146697)
7267 * Apparently BSD too, and it's probably best for linux too
7268 * On an embedded system this may be something to change
7271 if (buf->size > len)
7272 size = buf->size * 2;
7274 size = buf->use + len + 100;
7276 size = buf->use + len + 100;
7279 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7280 size_t start_buf = buf->content - buf->contentIO;
7282 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
7283 if (newbuf == NULL) {
7284 xmlTreeErrMemory("growing buffer");
7287 buf->contentIO = newbuf;
7288 buf->content = newbuf + start_buf;
7290 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
7291 if (newbuf == NULL) {
7292 xmlTreeErrMemory("growing buffer");
7295 buf->content = newbuf;
7298 return(buf->size - buf->use);
7303 * @file: the file output
7304 * @buf: the buffer to dump
7306 * Dumps an XML buffer to a FILE *.
7307 * Returns the number of #xmlChar written
7310 xmlBufferDump(FILE *file, xmlBufferPtr buf) {
7315 xmlGenericError(xmlGenericErrorContext,
7316 "xmlBufferDump: buf == NULL\n");
7320 if (buf->content == NULL) {
7322 xmlGenericError(xmlGenericErrorContext,
7323 "xmlBufferDump: buf->content == NULL\n");
7329 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
7337 * Function to extract the content of a buffer
7339 * Returns the internal content
7343 xmlBufferContent(const xmlBuffer *buf)
7348 return buf->content;
7355 * Function to get the length of a buffer
7357 * Returns the length of data in the internal content
7361 xmlBufferLength(const xmlBuffer *buf)
7371 * @buf: the buffer to resize
7372 * @size: the desired size
7374 * Resize a buffer to accommodate minimum size of @size.
7376 * Returns 0 in case of problems, 1 otherwise
7379 xmlBufferResize(xmlBufferPtr buf, unsigned int size)
7381 unsigned int newSize;
7382 xmlChar* rebuf = NULL;
7388 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7390 /* Don't resize if we don't have to */
7391 if (size < buf->size)
7394 /* figure out new size */
7395 switch (buf->alloc){
7396 case XML_BUFFER_ALLOC_IO:
7397 case XML_BUFFER_ALLOC_DOUBLEIT:
7398 /*take care of empty case*/
7399 newSize = (buf->size ? buf->size*2 : size + 10);
7400 while (size > newSize) {
7401 if (newSize > UINT_MAX / 2) {
7402 xmlTreeErrMemory("growing buffer");
7408 case XML_BUFFER_ALLOC_EXACT:
7411 case XML_BUFFER_ALLOC_HYBRID:
7412 if (buf->use < BASE_BUFFER_SIZE)
7415 newSize = buf->size * 2;
7416 while (size > newSize) {
7417 if (newSize > UINT_MAX / 2) {
7418 xmlTreeErrMemory("growing buffer");
7431 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7432 start_buf = buf->content - buf->contentIO;
7434 if (start_buf > newSize) {
7435 /* move data back to start */
7436 memmove(buf->contentIO, buf->content, buf->use);
7437 buf->content = buf->contentIO;
7438 buf->content[buf->use] = 0;
7439 buf->size += start_buf;
7441 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
7442 if (rebuf == NULL) {
7443 xmlTreeErrMemory("growing buffer");
7446 buf->contentIO = rebuf;
7447 buf->content = rebuf + start_buf;
7450 if (buf->content == NULL) {
7451 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7452 } else if (buf->size - buf->use < 100) {
7453 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
7456 * if we are reallocating a buffer far from being full, it's
7457 * better to make a new allocation and copy only the used range
7458 * and free the old one.
7460 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7461 if (rebuf != NULL) {
7462 memcpy(rebuf, buf->content, buf->use);
7463 xmlFree(buf->content);
7464 rebuf[buf->use] = 0;
7467 if (rebuf == NULL) {
7468 xmlTreeErrMemory("growing buffer");
7471 buf->content = rebuf;
7473 buf->size = newSize;
7480 * @buf: the buffer to dump
7481 * @str: the #xmlChar string
7482 * @len: the number of #xmlChar to add
7484 * Add a string range to an XML buffer. if len == -1, the length of
7485 * str is recomputed.
7487 * Returns 0 successful, a positive error code number otherwise
7488 * and -1 in case of internal or API error.
7491 xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
7492 unsigned int needSize;
7494 if ((str == NULL) || (buf == NULL)) {
7497 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7500 xmlGenericError(xmlGenericErrorContext,
7501 "xmlBufferAdd: len < 0\n");
7505 if (len == 0) return 0;
7508 len = xmlStrlen(str);
7510 if (len < 0) return -1;
7511 if (len == 0) return 0;
7513 needSize = buf->use + len + 2;
7514 if (needSize > buf->size){
7515 if (!xmlBufferResize(buf, needSize)){
7516 xmlTreeErrMemory("growing buffer");
7517 return XML_ERR_NO_MEMORY;
7521 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
7523 buf->content[buf->use] = 0;
7530 * @str: the #xmlChar string
7531 * @len: the number of #xmlChar to add
7533 * Add a string range to the beginning of an XML buffer.
7534 * if len == -1, the length of @str is recomputed.
7536 * Returns 0 successful, a positive error code number otherwise
7537 * and -1 in case of internal or API error.
7540 xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7541 unsigned int needSize;
7545 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7548 xmlGenericError(xmlGenericErrorContext,
7549 "xmlBufferAddHead: str == NULL\n");
7555 xmlGenericError(xmlGenericErrorContext,
7556 "xmlBufferAddHead: len < 0\n");
7560 if (len == 0) return 0;
7563 len = xmlStrlen(str);
7565 if (len <= 0) return -1;
7567 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7568 size_t start_buf = buf->content - buf->contentIO;
7570 if (start_buf > (unsigned int) len) {
7572 * We can add it in the space previously shrinked
7574 buf->content -= len;
7575 memmove(&buf->content[0], str, len);
7581 needSize = buf->use + len + 2;
7582 if (needSize > buf->size){
7583 if (!xmlBufferResize(buf, needSize)){
7584 xmlTreeErrMemory("growing buffer");
7585 return XML_ERR_NO_MEMORY;
7589 memmove(&buf->content[len], &buf->content[0], buf->use);
7590 memmove(&buf->content[0], str, len);
7592 buf->content[buf->use] = 0;
7598 * @buf: the buffer to add to
7599 * @str: the #xmlChar string
7601 * Append a zero terminated string to an XML buffer.
7603 * Returns 0 successful, a positive error code number otherwise
7604 * and -1 in case of internal or API error.
7607 xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
7610 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7611 if (str == NULL) return -1;
7612 return xmlBufferAdd(buf, str, -1);
7617 * @buf: the buffer to dump
7618 * @str: the C char string
7620 * Append a zero terminated C string to an XML buffer.
7622 * Returns 0 successful, a positive error code number otherwise
7623 * and -1 in case of internal or API error.
7626 xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7631 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7634 xmlGenericError(xmlGenericErrorContext,
7635 "xmlBufferCCat: str == NULL\n");
7639 for (cur = str;*cur != 0;cur++) {
7640 if (buf->use + 10 >= buf->size) {
7641 if (!xmlBufferResize(buf, buf->use+10)){
7642 xmlTreeErrMemory("growing buffer");
7643 return XML_ERR_NO_MEMORY;
7646 buf->content[buf->use++] = *cur;
7648 buf->content[buf->use] = 0;
7653 * xmlBufferWriteCHAR:
7654 * @buf: the XML buffer
7655 * @string: the string to add
7657 * routine which manages and grows an output buffer. This one adds
7658 * xmlChars at the end of the buffer.
7661 xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
7664 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7665 xmlBufferCat(buf, string);
7669 * xmlBufferWriteChar:
7670 * @buf: the XML buffer output
7671 * @string: the string to add
7673 * routine which manage and grows an output buffer. This one add
7674 * C chars at the end of the array.
7677 xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
7680 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7681 xmlBufferCCat(buf, string);
7686 * xmlBufferWriteQuotedString:
7687 * @buf: the XML buffer output
7688 * @string: the string to add
7690 * routine which manage and grows an output buffer. This one writes
7691 * a quoted or double quoted #xmlChar string, checking first if it holds
7692 * quote or double-quotes internally
7695 xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
7696 const xmlChar *cur, *base;
7699 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7700 if (xmlStrchr(string, '\"')) {
7701 if (xmlStrchr(string, '\'')) {
7703 xmlGenericError(xmlGenericErrorContext,
7704 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7706 xmlBufferCCat(buf, "\"");
7707 base = cur = string;
7711 xmlBufferAdd(buf, base, cur - base);
7712 xmlBufferAdd(buf, BAD_CAST """, 6);
7721 xmlBufferAdd(buf, base, cur - base);
7722 xmlBufferCCat(buf, "\"");
7725 xmlBufferCCat(buf, "\'");
7726 xmlBufferCat(buf, string);
7727 xmlBufferCCat(buf, "\'");
7730 xmlBufferCCat(buf, "\"");
7731 xmlBufferCat(buf, string);
7732 xmlBufferCCat(buf, "\"");
7738 * xmlGetDocCompressMode:
7739 * @doc: the document
7741 * get the compression ratio for a document, ZLIB based
7742 * Returns 0 (uncompressed) to 9 (max compression)
7745 xmlGetDocCompressMode (const xmlDoc *doc) {
7746 if (doc == NULL) return(-1);
7747 return(doc->compression);
7751 * xmlSetDocCompressMode:
7752 * @doc: the document
7753 * @mode: the compression ratio
7755 * set the compression ratio for a document, ZLIB based
7756 * Correct values: 0 (uncompressed) to 9 (max compression)
7759 xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7760 if (doc == NULL) return;
7761 if (mode < 0) doc->compression = 0;
7762 else if (mode > 9) doc->compression = 9;
7763 else doc->compression = mode;
7767 * xmlGetCompressMode:
7769 * get the default compression mode used, ZLIB based.
7770 * Returns 0 (uncompressed) to 9 (max compression)
7773 xmlGetCompressMode(void)
7775 return (xmlCompressMode);
7779 * xmlSetCompressMode:
7780 * @mode: the compression ratio
7782 * set the default compression mode used, ZLIB based
7783 * Correct values: 0 (uncompressed) to 9 (max compression)
7786 xmlSetCompressMode(int mode) {
7787 if (mode < 0) xmlCompressMode = 0;
7788 else if (mode > 9) xmlCompressMode = 9;
7789 else xmlCompressMode = mode;
7792 #define XML_TREE_NSMAP_PARENT -1
7793 #define XML_TREE_NSMAP_XML -2
7794 #define XML_TREE_NSMAP_DOC -3
7795 #define XML_TREE_NSMAP_CUSTOM -4
7797 typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7798 struct xmlNsMapItem {
7799 xmlNsMapItemPtr next;
7800 xmlNsMapItemPtr prev;
7801 xmlNsPtr oldNs; /* old ns decl reference */
7802 xmlNsPtr newNs; /* new ns decl reference */
7803 int shadowDepth; /* Shadowed at this depth */
7806 * >= 0 == @node's ns-decls
7807 * -1 == @parent's ns-decls
7808 * -2 == the doc->oldNs XML ns-decl
7809 * -3 == the doc->oldNs storage ns-decls
7810 * -4 == ns-decls provided via custom ns-handling
7815 typedef struct xmlNsMap *xmlNsMapPtr;
7817 xmlNsMapItemPtr first;
7818 xmlNsMapItemPtr last;
7819 xmlNsMapItemPtr pool;
7822 #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7823 #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7824 #define XML_NSMAP_POP(m, i) \
7826 (m)->last = (i)->prev; \
7827 if ((m)->last == NULL) \
7828 (m)->first = NULL; \
7830 (m)->last->next = NULL; \
7831 (i)->next = (m)->pool; \
7835 * xmlDOMWrapNsMapFree:
7841 xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7843 xmlNsMapItemPtr cur, tmp;
7848 while (cur != NULL) {
7854 while (cur != NULL) {
7863 * xmlDOMWrapNsMapAddItem:
7865 * @oldNs: the old ns-struct
7866 * @newNs: the new ns-struct
7867 * @depth: depth and ns-kind information
7869 * Adds an ns-mapping item.
7871 static xmlNsMapItemPtr
7872 xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
7873 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
7875 xmlNsMapItemPtr ret;
7880 if ((position != -1) && (position != 0))
7886 * Create the ns-map.
7888 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7890 xmlTreeErrMemory("allocating namespace map");
7893 memset(map, 0, sizeof(struct xmlNsMap));
7897 if (map->pool != NULL) {
7899 * Reuse an item from the pool.
7902 map->pool = ret->next;
7903 memset(ret, 0, sizeof(struct xmlNsMapItem));
7906 * Create a new item.
7908 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7910 xmlTreeErrMemory("allocating namespace map item");
7913 memset(ret, 0, sizeof(struct xmlNsMapItem));
7916 if (map->first == NULL) {
7922 } else if (position == -1) {
7926 ret->prev = map->last;
7927 map->last->next = ret;
7929 } else if (position == 0) {
7931 * Set on first position.
7933 map->first->prev = ret;
7934 ret->next = map->first;
7940 ret->shadowDepth = -1;
7946 * xmlDOMWrapStoreNs:
7948 * @nsName: the namespace name
7949 * @prefix: the prefix
7951 * Creates or reuses an xmlNs struct on doc->oldNs with
7952 * the given prefix and namespace name.
7954 * Returns the aquired ns struct or NULL in case of an API
7955 * or internal error.
7958 xmlDOMWrapStoreNs(xmlDocPtr doc,
7959 const xmlChar *nsName,
7960 const xmlChar *prefix)
7966 ns = xmlTreeEnsureXMLDecl(doc);
7969 if (ns->next != NULL) {
7972 while (ns != NULL) {
7973 if (((ns->prefix == prefix) ||
7974 xmlStrEqual(ns->prefix, prefix)) &&
7975 xmlStrEqual(ns->href, nsName)) {
7978 if (ns->next == NULL)
7985 ns->next = xmlNewNs(NULL, nsName, prefix);
7992 * xmlDOMWrapNewCtxt:
7994 * Allocates and initializes a new DOM-wrapper context.
7996 * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal error.
7999 xmlDOMWrapNewCtxt(void)
8001 xmlDOMWrapCtxtPtr ret;
8003 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
8005 xmlTreeErrMemory("allocating DOM-wrapper context");
8008 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
8013 * xmlDOMWrapFreeCtxt:
8014 * @ctxt: the DOM-wrapper context
8016 * Frees the DOM-wrapper context.
8019 xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
8023 if (ctxt->namespaceMap != NULL)
8024 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
8026 * TODO: Store the namespace map in the context.
8032 * xmlTreeLookupNsListByPrefix:
8033 * @nsList: a list of ns-structs
8034 * @prefix: the searched prefix
8036 * Searches for a ns-decl with the given prefix in @nsList.
8038 * Returns the ns-decl if found, NULL if not found and on
8042 xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
8050 if ((prefix == ns->prefix) ||
8051 xmlStrEqual(prefix, ns->prefix)) {
8055 } while (ns != NULL);
8062 * xmlDOMWrapNSNormGatherInScopeNs:
8063 * @map: the namespace map
8064 * @node: the node to start with
8066 * Puts in-scope namespaces into the ns-map.
8068 * Returns 0 on success, -1 on API or internal errors.
8071 xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
8079 if ((map == NULL) || (*map != NULL))
8081 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8084 * Get in-scope ns-decls of @parent.
8087 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
8088 if (cur->type == XML_ELEMENT_NODE) {
8089 if (cur->nsDef != NULL) {
8093 if (XML_NSMAP_NOTEMPTY(*map)) {
8095 * Skip shadowed prefixes.
8097 XML_NSMAP_FOREACH(*map, mi) {
8098 if ((ns->prefix == mi->newNs->prefix) ||
8099 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
8108 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
8109 ns, XML_TREE_NSMAP_PARENT);
8113 mi->shadowDepth = 0;
8115 } while (ns != NULL);
8124 * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
8125 * otherwise copy it, when it was in the source-dict.
8127 #define XML_TREE_ADOPT_STR(str) \
8128 if (adoptStr && (str != NULL)) { \
8129 if (destDoc->dict) { \
8130 const xmlChar *old = str; \
8131 str = xmlDictLookup(destDoc->dict, str, -1); \
8132 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
8133 (!xmlDictOwns(sourceDoc->dict, old))) \
8134 xmlFree((char *)old); \
8135 } else if ((sourceDoc) && (sourceDoc->dict) && \
8136 xmlDictOwns(sourceDoc->dict, str)) { \
8137 str = BAD_CAST xmlStrdup(str); \
8142 * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
8143 * put it in dest-dict or copy it.
8145 #define XML_TREE_ADOPT_STR_2(str) \
8146 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
8147 (sourceDoc->dict != NULL) && \
8148 xmlDictOwns(sourceDoc->dict, cur->content)) { \
8149 if (destDoc->dict) \
8150 cur->content = (xmlChar *) \
8151 xmlDictLookup(destDoc->dict, cur->content, -1); \
8153 cur->content = xmlStrdup(BAD_CAST cur->content); \
8157 * xmlDOMWrapNSNormAddNsMapItem2:
8159 * For internal use. Adds a ns-decl mapping.
8161 * Returns 0 on success, -1 on internal errors.
8164 xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
8165 xmlNsPtr oldNs, xmlNsPtr newNs)
8167 if (*list == NULL) {
8168 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
8169 if (*list == NULL) {
8170 xmlTreeErrMemory("alloc ns map item");
8175 } else if ((*number) >= (*size)) {
8177 *list = (xmlNsPtr *) xmlRealloc(*list,
8178 (*size) * 2 * sizeof(xmlNsPtr));
8179 if (*list == NULL) {
8180 xmlTreeErrMemory("realloc ns map item");
8184 (*list)[2 * (*number)] = oldNs;
8185 (*list)[2 * (*number) +1] = newNs;
8191 * xmlDOMWrapRemoveNode:
8192 * @ctxt: a DOM wrapper context
8194 * @node: the node to be removed.
8195 * @options: set of options, unused at the moment
8197 * Unlinks the given node from its owner.
8198 * This will substitute ns-references to node->nsDef for
8199 * ns-references to doc->oldNs, thus ensuring the removed
8200 * branch to be autark wrt ns-references.
8202 * NOTE: This function was not intensively tested.
8204 * Returns 0 on success, 1 if the node is not supported,
8205 * -1 on API and internal errors.
8208 xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
8209 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
8211 xmlNsPtr *list = NULL;
8212 int sizeList, nbList, i, j;
8215 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
8218 /* TODO: 0 or -1 ? */
8219 if (node->parent == NULL)
8222 switch (node->type) {
8224 case XML_CDATA_SECTION_NODE:
8225 case XML_ENTITY_REF_NODE:
8227 case XML_COMMENT_NODE:
8228 xmlUnlinkNode(node);
8230 case XML_ELEMENT_NODE:
8231 case XML_ATTRIBUTE_NODE:
8236 xmlUnlinkNode(node);
8238 * Save out-of-scope ns-references in doc->oldNs.
8241 switch (node->type) {
8242 case XML_ELEMENT_NODE:
8243 if ((ctxt == NULL) && (node->nsDef != NULL)) {
8246 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8247 &nbList, ns, ns) == -1)
8248 goto internal_error;
8250 } while (ns != NULL);
8252 /* No break on purpose. */
8253 case XML_ATTRIBUTE_NODE:
8254 if (node->ns != NULL) {
8259 for (i = 0, j = 0; i < nbList; i++, j += 2) {
8260 if (node->ns == list[j]) {
8261 node->ns = list[++j];
8273 * Add to doc's oldNs.
8275 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
8278 goto internal_error;
8284 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8285 &nbList, node->ns, ns) == -1)
8286 goto internal_error;
8290 if ((node->type == XML_ELEMENT_NODE) &&
8291 (node->properties != NULL)) {
8292 node = (xmlNodePtr) node->properties;
8300 if ((node->type == XML_ELEMENT_NODE) &&
8301 (node->children != NULL)) {
8302 node = node->children;
8308 if (node->next != NULL)
8311 node = node->parent;
8314 } while (node != NULL);
8327 * xmlSearchNsByNamespaceStrict:
8328 * @doc: the document
8329 * @node: the start node
8330 * @nsName: the searched namespace name
8331 * @retNs: the resulting ns-decl
8332 * @prefixed: if the found ns-decl must have a prefix (for attributes)
8334 * Dynamically searches for a ns-declaration which matches
8335 * the given @nsName in the ancestor-or-self axis of @node.
8337 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8338 * and internal errors.
8341 xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
8342 const xmlChar* nsName,
8343 xmlNsPtr *retNs, int prefixed)
8345 xmlNodePtr cur, prev = NULL, out = NULL;
8346 xmlNsPtr ns, prevns;
8348 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
8350 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8354 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
8355 *retNs = xmlTreeEnsureXMLDecl(doc);
8362 if (cur->type == XML_ELEMENT_NODE) {
8363 if (cur->nsDef != NULL) {
8364 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8365 if (prefixed && (ns->prefix == NULL))
8369 * Check the last level of ns-decls for a
8372 prevns = prev->nsDef;
8374 if ((prevns->prefix == ns->prefix) ||
8375 ((prevns->prefix != NULL) &&
8376 (ns->prefix != NULL) &&
8377 xmlStrEqual(prevns->prefix, ns->prefix))) {
8383 prevns = prevns->next;
8384 } while (prevns != NULL);
8389 * Ns-name comparison.
8391 if ((nsName == ns->href) ||
8392 xmlStrEqual(nsName, ns->href)) {
8394 * At this point the prefix can only be shadowed,
8395 * if we are the the (at least) 3rd level of
8401 ret = xmlNsInScope(doc, node, prev, ns->prefix);
8405 * TODO: Should we try to find a matching ns-name
8406 * only once? This here keeps on searching.
8407 * I think we should try further since, there might
8408 * be an other matching ns-decl with an unshadowed
8421 } else if ((cur->type == XML_ENTITY_NODE) ||
8422 (cur->type == XML_ENTITY_DECL))
8425 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8430 * xmlSearchNsByPrefixStrict:
8431 * @doc: the document
8432 * @node: the start node
8433 * @prefix: the searched namespace prefix
8434 * @retNs: the resulting ns-decl
8436 * Dynamically searches for a ns-declaration which matches
8437 * the given @nsName in the ancestor-or-self axis of @node.
8439 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8440 * and internal errors.
8443 xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
8444 const xmlChar* prefix,
8450 if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL))
8455 if (IS_STR_XML(prefix)) {
8457 *retNs = xmlTreeEnsureXMLDecl(doc);
8465 if (cur->type == XML_ELEMENT_NODE) {
8466 if (cur->nsDef != NULL) {
8469 if ((prefix == ns->prefix) ||
8470 xmlStrEqual(prefix, ns->prefix))
8473 * Disabled namespaces, e.g. xmlns:abc="".
8475 if (ns->href == NULL)
8482 } while (ns != NULL);
8484 } else if ((cur->type == XML_ENTITY_NODE) ||
8485 (cur->type == XML_ENTITY_DECL))
8488 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8493 * xmlDOMWrapNSNormDeclareNsForced:
8495 * @elem: the element-node to declare on
8496 * @nsName: the namespace-name of the ns-decl
8497 * @prefix: the preferred prefix of the ns-decl
8498 * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
8500 * Declares a new namespace on @elem. It tries to use the
8501 * given @prefix; if a ns-decl with the given prefix is already existent
8502 * on @elem, it will generate an other prefix.
8504 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8505 * and internal errors.
8508 xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
8510 const xmlChar *nsName,
8511 const xmlChar *prefix,
8517 const xmlChar *pref;
8520 if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
8523 * Create a ns-decl on @anchor.
8528 * Lookup whether the prefix is unused in elem's ns-decls.
8530 if ((elem->nsDef != NULL) &&
8531 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
8532 goto ns_next_prefix;
8533 if (checkShadow && elem->parent &&
8534 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8536 * Does it shadow ancestor ns-decls?
8538 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
8539 goto ns_next_prefix;
8541 ret = xmlNewNs(NULL, nsName, pref);
8544 if (elem->nsDef == NULL)
8547 xmlNsPtr ns2 = elem->nsDef;
8548 while (ns2->next != NULL)
8557 if (prefix == NULL) {
8558 snprintf((char *) buf, sizeof(buf),
8561 snprintf((char *) buf, sizeof(buf),
8562 "%.30s_%d", (char *)prefix, counter);
8563 pref = BAD_CAST buf;
8568 * xmlDOMWrapNSNormAquireNormalizedNs:
8570 * @elem: the element-node to declare namespaces on
8571 * @ns: the ns-struct to use for the search
8572 * @retNs: the found/created ns-struct
8573 * @nsMap: the ns-map
8574 * @depth: the current tree depth
8575 * @ancestorsOnly: search in ancestor ns-decls only
8576 * @prefixed: if the searched ns-decl must have a prefix (for attributes)
8578 * Searches for a matching ns-name in the ns-decls of @nsMap, if not
8579 * found it will either declare it on @elem, or store it in doc->oldNs.
8580 * If a new ns-decl needs to be declared on @elem, it tries to use the
8581 * @ns->prefix for it, if this prefix is already in use on @elem, it will
8582 * change the prefix or the new ns-decl.
8584 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8587 xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8599 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
8605 * Handle XML namespace.
8607 if (IS_STR_XML(ns->prefix)) {
8609 * Insert XML namespace mapping.
8611 *retNs = xmlTreeEnsureXMLDecl(doc);
8617 * If the search should be done in ancestors only and no
8618 * @elem (the first ancestor) was specified, then skip the search.
8620 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8621 (! (ancestorsOnly && (elem == NULL))))
8624 * Try to find an equal ns-name in in-scope ns-decls.
8626 XML_NSMAP_FOREACH(*nsMap, mi) {
8627 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8629 * ancestorsOnly: This should be turned on to gain speed,
8630 * if one knows that the branch itself was already
8631 * ns-wellformed and no stale references existed.
8632 * I.e. it searches in the ancestor axis only.
8634 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8635 /* Skip shadowed prefixes. */
8636 (mi->shadowDepth == -1) &&
8637 /* Skip xmlns="" or xmlns:foo="". */
8638 ((mi->newNs->href != NULL) &&
8639 (mi->newNs->href[0] != 0)) &&
8640 /* Ensure a prefix if wanted. */
8641 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8643 ((mi->newNs->href == ns->href) ||
8644 xmlStrEqual(mi->newNs->href, ns->href))) {
8645 /* Set the mapping. */
8653 * No luck, the namespace is out of scope or shadowed.
8659 * Store ns-decls in "oldNs" of the document-node.
8661 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8667 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
8668 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8676 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8681 if (*nsMap != NULL) {
8683 * Does it shadow ancestor ns-decls?
8685 XML_NSMAP_FOREACH(*nsMap, mi) {
8686 if ((mi->depth < depth) &&
8687 (mi->shadowDepth == -1) &&
8688 ((ns->prefix == mi->newNs->prefix) ||
8689 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8693 mi->shadowDepth = depth;
8698 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
8708 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
8709 } xmlDOMReconcileNSOptions;
8712 * xmlDOMWrapReconcileNamespaces:
8713 * @ctxt: DOM wrapper context, unused at the moment
8714 * @elem: the element-node
8715 * @options: option flags
8717 * Ensures that ns-references point to ns-decls hold on element-nodes.
8718 * Ensures that the tree is namespace wellformed by creating additional
8719 * ns-decls where needed. Note that, since prefixes of already existent
8720 * ns-decls can be shadowed by this process, it could break QNames in
8721 * attribute values or element content.
8723 * NOTE: This function was not intensively tested.
8725 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8729 xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8733 int depth = -1, adoptns = 0, parnsdone = 0;
8734 xmlNsPtr ns, prevns;
8736 xmlNodePtr cur, curElem = NULL;
8737 xmlNsMapPtr nsMap = NULL;
8738 xmlNsMapItemPtr /* topmi = NULL, */ mi;
8739 /* @ancestorsOnly should be set by an option flag. */
8740 int ancestorsOnly = 0;
8741 int optRemoveRedundantNS =
8742 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8743 xmlNsPtr *listRedund = NULL;
8744 int sizeRedund = 0, nbRedund = 0, ret, i, j;
8746 if ((elem == NULL) || (elem->doc == NULL) ||
8747 (elem->type != XML_ELEMENT_NODE))
8753 switch (cur->type) {
8754 case XML_ELEMENT_NODE:
8759 * Namespace declarations.
8761 if (cur->nsDef != NULL) {
8764 while (ns != NULL) {
8766 if ((elem->parent) &&
8767 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8769 * Gather ancestor in-scope ns-decls.
8771 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8772 elem->parent) == -1)
8773 goto internal_error;
8779 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8781 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
8782 XML_NSMAP_FOREACH(nsMap, mi) {
8783 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8784 (mi->shadowDepth == -1) &&
8785 ((ns->prefix == mi->newNs->prefix) ||
8786 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8787 ((ns->href == mi->newNs->href) ||
8788 xmlStrEqual(ns->href, mi->newNs->href)))
8791 * A redundant ns-decl was found.
8792 * Add it to the list of redundant ns-decls.
8794 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8795 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8796 goto internal_error;
8798 * Remove the ns-decl from the element-node.
8801 prevns->next = ns->next;
8803 cur->nsDef = ns->next;
8810 * Skip ns-references handling if the referenced
8811 * ns-decl is declared on the same element.
8813 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
8816 * Does it shadow any ns-decl?
8818 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8819 XML_NSMAP_FOREACH(nsMap, mi) {
8820 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8821 (mi->shadowDepth == -1) &&
8822 ((ns->prefix == mi->newNs->prefix) ||
8823 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8825 mi->shadowDepth = depth;
8832 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
8834 goto internal_error;
8843 /* No break on purpose. */
8844 case XML_ATTRIBUTE_NODE:
8845 /* No ns, no fun. */
8846 if (cur->ns == NULL)
8850 if ((elem->parent) &&
8851 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8852 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8853 elem->parent) == -1)
8854 goto internal_error;
8859 * Adjust the reference if this was a redundant ns-decl.
8862 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8863 if (cur->ns == listRedund[j]) {
8864 cur->ns = listRedund[++j];
8870 * Adopt ns-references.
8872 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8874 * Search for a mapping.
8876 XML_NSMAP_FOREACH(nsMap, mi) {
8877 if ((mi->shadowDepth == -1) &&
8878 (cur->ns == mi->oldNs)) {
8880 cur->ns = mi->newNs;
8886 * Aquire a normalized ns-decl and add it to the map.
8888 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8892 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8893 goto internal_error;
8897 if ((cur->type == XML_ELEMENT_NODE) &&
8898 (cur->properties != NULL)) {
8900 * Process attributes.
8902 cur = (xmlNodePtr) cur->properties;
8910 if ((cur->type == XML_ELEMENT_NODE) &&
8911 (cur->children != NULL)) {
8913 * Process content of element-nodes only.
8915 cur = cur->children;
8921 if (cur->type == XML_ELEMENT_NODE) {
8922 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8926 while ((nsMap->last != NULL) &&
8927 (nsMap->last->depth >= depth))
8929 XML_NSMAP_POP(nsMap, mi)
8934 XML_NSMAP_FOREACH(nsMap, mi) {
8935 if (mi->shadowDepth >= depth)
8936 mi->shadowDepth = -1;
8941 if (cur->next != NULL)
8944 if (cur->type == XML_ATTRIBUTE_NODE) {
8951 } while (cur != NULL);
8959 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8960 xmlFreeNs(listRedund[j]);
8962 xmlFree(listRedund);
8965 xmlDOMWrapNsMapFree(nsMap);
8970 * xmlDOMWrapAdoptBranch:
8971 * @ctxt: the optional context for custom processing
8972 * @sourceDoc: the optional sourceDoc
8973 * @node: the element-node to start with
8974 * @destDoc: the destination doc for adoption
8975 * @destParent: the optional new parent of @node in @destDoc
8976 * @options: option flags
8978 * Ensures that ns-references point to @destDoc: either to
8979 * elements->nsDef entries if @destParent is given, or to
8980 * @destDoc->oldNs otherwise.
8981 * If @destParent is given, it ensures that the tree is namespace
8982 * wellformed by creating additional ns-decls where needed.
8983 * Note that, since prefixes of already existent ns-decls can be
8984 * shadowed by this process, it could break QNames in attribute
8985 * values or element content.
8987 * NOTE: This function was not intensively tested.
8989 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8992 xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8993 xmlDocPtr sourceDoc,
8996 xmlNodePtr destParent,
8997 int options ATTRIBUTE_UNUSED)
9000 xmlNodePtr cur, curElem = NULL;
9001 xmlNsMapPtr nsMap = NULL;
9004 int depth = -1, adoptStr = 1;
9005 /* gather @parent's ns-decls. */
9007 /* @ancestorsOnly should be set per option. */
9008 int ancestorsOnly = 0;
9011 * Optimize string adoption for equal or none dicts.
9013 if ((sourceDoc != NULL) &&
9014 (sourceDoc->dict == destDoc->dict))
9020 * Get the ns-map from the context if available.
9023 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9025 * Disable search for ns-decls in the parent-axis of the
9026 * desination element, if:
9027 * 1) there's no destination parent
9028 * 2) custom ns-reference handling is used
9030 if ((destParent == NULL) ||
9031 (ctxt && ctxt->getNsForNodeFunc))
9038 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9039 goto internal_error;
9041 while (cur != NULL) {
9043 * Paranoid source-doc sanity check.
9045 if (cur->doc != sourceDoc) {
9047 * We'll assume XIncluded nodes if the doc differs.
9048 * TODO: Do we need to reconciliate XIncluded nodes?
9049 * This here skips XIncluded nodes and tries to handle
9052 if (cur->next == NULL)
9056 if ((cur->type == XML_XINCLUDE_END) ||
9057 (cur->doc == node->doc))
9059 } while (cur->next != NULL);
9061 if (cur->doc != node->doc)
9065 switch (cur->type) {
9066 case XML_XINCLUDE_START:
9067 case XML_XINCLUDE_END:
9072 case XML_ELEMENT_NODE:
9076 * Namespace declarations.
9077 * - ns->href and ns->prefix are never in the dict, so
9078 * we need not move the values over to the destination dict.
9079 * - Note that for custom handling of ns-references,
9080 * the ns-decls need not be stored in the ns-map,
9081 * since they won't be referenced by node->ns.
9084 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
9088 * Gather @parent's in-scope ns-decls.
9090 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9092 goto internal_error;
9095 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9097 * NOTE: ns->prefix and ns->href are never in the dict.
9098 * XML_TREE_ADOPT_STR(ns->prefix)
9099 * XML_TREE_ADOPT_STR(ns->href)
9102 * Does it shadow any ns-decl?
9104 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9105 XML_NSMAP_FOREACH(nsMap, mi) {
9106 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9107 (mi->shadowDepth == -1) &&
9108 ((ns->prefix == mi->newNs->prefix) ||
9109 xmlStrEqual(ns->prefix,
9110 mi->newNs->prefix))) {
9112 mi->shadowDepth = depth;
9119 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9120 ns, ns, depth) == NULL)
9121 goto internal_error;
9124 /* No break on purpose. */
9125 case XML_ATTRIBUTE_NODE:
9126 /* No namespace, no fun. */
9127 if (cur->ns == NULL)
9131 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9133 goto internal_error;
9137 * Adopt ns-references.
9139 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9141 * Search for a mapping.
9143 XML_NSMAP_FOREACH(nsMap, mi) {
9144 if ((mi->shadowDepth == -1) &&
9145 (cur->ns == mi->oldNs)) {
9147 cur->ns = mi->newNs;
9153 * No matching namespace in scope. We need a new one.
9155 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
9157 * User-defined behaviour.
9159 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9160 cur->ns->href, cur->ns->prefix);
9162 * Insert mapping if ns is available; it's the users fault
9165 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9166 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9167 goto internal_error;
9171 * Aquire a normalized ns-decl and add it to the map.
9173 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9174 /* ns-decls on curElem or on destDoc->oldNs */
9175 destParent ? curElem : NULL,
9179 /* ns-decls must be prefixed for attributes. */
9180 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9181 goto internal_error;
9186 * Further node properties.
9187 * TODO: Is this all?
9189 XML_TREE_ADOPT_STR(cur->name)
9190 if (cur->type == XML_ELEMENT_NODE) {
9197 if (cur->properties != NULL) {
9199 * Process first attribute node.
9201 cur = (xmlNodePtr) cur->properties;
9208 if ((sourceDoc != NULL) &&
9209 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
9211 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
9213 ((xmlAttrPtr) cur)->atype = 0;
9214 ((xmlAttrPtr) cur)->psvi = NULL;
9218 case XML_CDATA_SECTION_NODE:
9220 * This puts the content in the dest dict, only if
9221 * it was previously in the source dict.
9223 XML_TREE_ADOPT_STR_2(cur->content)
9225 case XML_ENTITY_REF_NODE:
9227 * Remove reference to the entitity-node.
9229 cur->content = NULL;
9230 cur->children = NULL;
9232 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9235 * Assign new entity-node if available.
9237 ent = xmlGetDocEntity(destDoc, cur->name);
9239 cur->content = ent->content;
9240 cur->children = (xmlNodePtr) ent;
9241 cur->last = (xmlNodePtr) ent;
9246 XML_TREE_ADOPT_STR(cur->name)
9247 XML_TREE_ADOPT_STR_2(cur->content)
9249 case XML_COMMENT_NODE:
9252 goto internal_error;
9257 if (cur->children != NULL) {
9258 cur = cur->children;
9265 if ((cur->type == XML_ELEMENT_NODE) ||
9266 (cur->type == XML_XINCLUDE_START) ||
9267 (cur->type == XML_XINCLUDE_END))
9270 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9272 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9276 while ((nsMap->last != NULL) &&
9277 (nsMap->last->depth >= depth))
9279 XML_NSMAP_POP(nsMap, mi)
9284 XML_NSMAP_FOREACH(nsMap, mi) {
9285 if (mi->shadowDepth >= depth)
9286 mi->shadowDepth = -1;
9291 if (cur->next != NULL)
9293 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
9294 (cur->parent->children != NULL))
9296 cur = cur->parent->children;
9312 if (nsMap != NULL) {
9313 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9315 * Just cleanup the map but don't free.
9319 nsMap->last->next = nsMap->pool;
9320 nsMap->pool = nsMap->first;
9321 nsMap->first = NULL;
9324 xmlDOMWrapNsMapFree(nsMap);
9330 * xmlDOMWrapCloneNode:
9331 * @ctxt: the optional context for custom processing
9332 * @sourceDoc: the optional sourceDoc
9333 * @node: the node to start with
9334 * @resNode: the clone of the given @node
9335 * @destDoc: the destination doc
9336 * @destParent: the optional new parent of @node in @destDoc
9337 * @deep: descend into child if set
9338 * @options: option flags
9340 * References of out-of scope ns-decls are remapped to point to @destDoc:
9341 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9342 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
9343 * This is the case when you don't know already where the cloned branch
9346 * If @destParent is given, it ensures that the tree is namespace
9347 * wellformed by creating additional ns-decls where needed.
9348 * Note that, since prefixes of already existent ns-decls can be
9349 * shadowed by this process, it could break QNames in attribute
9350 * values or element content.
9352 * 1) What to do with XInclude? Currently this returns an error for XInclude.
9354 * Returns 0 if the operation succeeded,
9355 * 1 if a node of unsupported (or not yet supported) type was given,
9356 * -1 on API/internal errors.
9360 xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
9361 xmlDocPtr sourceDoc,
9363 xmlNodePtr *resNode,
9365 xmlNodePtr destParent,
9367 int options ATTRIBUTE_UNUSED)
9370 xmlNodePtr cur, curElem = NULL;
9371 xmlNsMapPtr nsMap = NULL;
9375 /* int adoptStr = 1; */
9376 /* gather @parent's ns-decls. */
9380 * TODO: @ancestorsOnly should be set per option.
9383 int ancestorsOnly = 0;
9384 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
9385 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
9386 xmlDictPtr dict; /* The destination dict */
9388 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
9391 * TODO: Initially we support only element-nodes.
9393 if (node->type != XML_ELEMENT_NODE)
9396 * Check node->doc sanity.
9398 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9399 (node->doc != sourceDoc)) {
9401 * Might be an XIncluded node.
9405 if (sourceDoc == NULL)
9406 sourceDoc = node->doc;
9407 if (sourceDoc == NULL)
9410 dict = destDoc->dict;
9412 * Reuse the namespace map of the context.
9415 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9420 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9423 while (cur != NULL) {
9424 if (cur->doc != sourceDoc) {
9426 * We'll assume XIncluded nodes if the doc differs.
9427 * TODO: Do we need to reconciliate XIncluded nodes?
9428 * TODO: This here returns -1 in this case.
9430 goto internal_error;
9433 * Create a new node.
9435 switch (cur->type) {
9436 case XML_XINCLUDE_START:
9437 case XML_XINCLUDE_END:
9439 * TODO: What to do with XInclude?
9441 goto internal_error;
9443 case XML_ELEMENT_NODE:
9445 case XML_CDATA_SECTION_NODE:
9446 case XML_COMMENT_NODE:
9448 case XML_DOCUMENT_FRAG_NODE:
9449 case XML_ENTITY_REF_NODE:
9450 case XML_ENTITY_NODE:
9452 * Nodes of xmlNode structure.
9454 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
9455 if (clone == NULL) {
9456 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
9457 goto internal_error;
9459 memset(clone, 0, sizeof(xmlNode));
9461 * Set hierachical links.
9463 if (resultClone != NULL) {
9464 clone->parent = parentClone;
9466 prevClone->next = clone;
9467 clone->prev = prevClone;
9469 parentClone->children = clone;
9471 resultClone = clone;
9474 case XML_ATTRIBUTE_NODE:
9476 * Attributes (xmlAttr).
9478 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
9479 if (clone == NULL) {
9480 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
9481 goto internal_error;
9483 memset(clone, 0, sizeof(xmlAttr));
9485 * Set hierachical links.
9486 * TODO: Change this to add to the end of attributes.
9488 if (resultClone != NULL) {
9489 clone->parent = parentClone;
9491 prevClone->next = clone;
9492 clone->prev = prevClone;
9494 parentClone->properties = (xmlAttrPtr) clone;
9496 resultClone = clone;
9500 * TODO QUESTION: Any other nodes expected?
9502 goto internal_error;
9505 clone->type = cur->type;
9506 clone->doc = destDoc;
9509 * Clone the name of the node if any.
9511 if (cur->name == xmlStringText)
9512 clone->name = xmlStringText;
9513 else if (cur->name == xmlStringTextNoenc)
9515 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9516 * in tree.c, it might be set in Libxslt via
9517 * "xsl:disable-output-escaping".
9519 clone->name = xmlStringTextNoenc;
9520 else if (cur->name == xmlStringComment)
9521 clone->name = xmlStringComment;
9522 else if (cur->name != NULL) {
9523 DICT_CONST_COPY(cur->name, clone->name);
9526 switch (cur->type) {
9527 case XML_XINCLUDE_START:
9528 case XML_XINCLUDE_END:
9533 case XML_ELEMENT_NODE:
9537 * Namespace declarations.
9539 if (cur->nsDef != NULL) {
9541 if (destParent && (ctxt == NULL)) {
9543 * Gather @parent's in-scope ns-decls.
9545 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9547 goto internal_error;
9552 * Clone namespace declarations.
9554 cloneNsDefSlot = &(clone->nsDef);
9555 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9557 * Create a new xmlNs.
9559 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
9560 if (cloneNs == NULL) {
9561 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
9562 "allocating namespace");
9565 memset(cloneNs, 0, sizeof(xmlNs));
9566 cloneNs->type = XML_LOCAL_NAMESPACE;
9568 if (ns->href != NULL)
9569 cloneNs->href = xmlStrdup(ns->href);
9570 if (ns->prefix != NULL)
9571 cloneNs->prefix = xmlStrdup(ns->prefix);
9573 *cloneNsDefSlot = cloneNs;
9574 cloneNsDefSlot = &(cloneNs->next);
9577 * Note that for custom handling of ns-references,
9578 * the ns-decls need not be stored in the ns-map,
9579 * since they won't be referenced by node->ns.
9581 if ((ctxt == NULL) ||
9582 (ctxt->getNsForNodeFunc == NULL))
9585 * Does it shadow any ns-decl?
9587 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9588 XML_NSMAP_FOREACH(nsMap, mi) {
9589 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9590 (mi->shadowDepth == -1) &&
9591 ((ns->prefix == mi->newNs->prefix) ||
9592 xmlStrEqual(ns->prefix,
9593 mi->newNs->prefix))) {
9595 * Mark as shadowed at the current
9598 mi->shadowDepth = depth;
9605 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9606 ns, cloneNs, depth) == NULL)
9607 goto internal_error;
9611 /* cur->ns will be processed further down. */
9613 case XML_ATTRIBUTE_NODE:
9614 /* IDs will be processed further down. */
9615 /* cur->ns will be processed further down. */
9618 case XML_CDATA_SECTION_NODE:
9620 * Note that this will also cover the values of attributes.
9622 DICT_COPY(cur->content, clone->content);
9624 case XML_ENTITY_NODE:
9625 /* TODO: What to do here? */
9627 case XML_ENTITY_REF_NODE:
9628 if (sourceDoc != destDoc) {
9629 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9632 * Different doc: Assign new entity-node if available.
9634 ent = xmlGetDocEntity(destDoc, cur->name);
9636 clone->content = ent->content;
9637 clone->children = (xmlNodePtr) ent;
9638 clone->last = (xmlNodePtr) ent;
9643 * Same doc: Use the current node's entity declaration
9646 clone->content = cur->content;
9647 clone->children = cur->children;
9648 clone->last = cur->last;
9652 DICT_COPY(cur->content, clone->content);
9654 case XML_COMMENT_NODE:
9655 DICT_COPY(cur->content, clone->content);
9658 goto internal_error;
9661 if (cur->ns == NULL)
9662 goto end_ns_reference;
9664 /* handle_ns_reference: */
9666 ** The following will take care of references to ns-decls ********
9667 ** and is intended only for element- and attribute-nodes.
9671 if (destParent && (ctxt == NULL)) {
9672 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
9673 goto internal_error;
9678 * Adopt ns-references.
9680 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9682 * Search for a mapping.
9684 XML_NSMAP_FOREACH(nsMap, mi) {
9685 if ((mi->shadowDepth == -1) &&
9686 (cur->ns == mi->oldNs)) {
9688 * This is the nice case: a mapping was found.
9690 clone->ns = mi->newNs;
9691 goto end_ns_reference;
9696 * No matching namespace in scope. We need a new one.
9698 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
9700 * User-defined behaviour.
9702 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9703 cur->ns->href, cur->ns->prefix);
9705 * Add user's mapping.
9707 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9708 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9709 goto internal_error;
9713 * Aquire a normalized ns-decl and add it to the map.
9715 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9716 /* ns-decls on curElem or on destDoc->oldNs */
9717 destParent ? curElem : NULL,
9720 /* if we need to search only in the ancestor-axis */
9722 /* ns-decls must be prefixed for attributes. */
9723 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9724 goto internal_error;
9731 * Some post-processing.
9733 * Handle ID attributes.
9735 if ((clone->type == XML_ATTRIBUTE_NODE) &&
9736 (clone->parent != NULL))
9738 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
9742 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9743 if (idVal != NULL) {
9744 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9745 /* TODO: error message. */
9747 goto internal_error;
9755 ** The following will traverse the tree **************************
9758 * Walk the element's attributes before descending into child-nodes.
9760 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9762 parentClone = clone;
9763 cur = (xmlNodePtr) cur->properties;
9768 * Descend into child-nodes.
9770 if (cur->children != NULL) {
9771 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9773 parentClone = clone;
9774 cur = cur->children;
9781 * At this point we are done with the node, its content
9782 * and an element-nodes's attribute-nodes.
9786 if ((cur->type == XML_ELEMENT_NODE) ||
9787 (cur->type == XML_XINCLUDE_START) ||
9788 (cur->type == XML_XINCLUDE_END)) {
9790 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9792 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9796 while ((nsMap->last != NULL) &&
9797 (nsMap->last->depth >= depth))
9799 XML_NSMAP_POP(nsMap, mi)
9804 XML_NSMAP_FOREACH(nsMap, mi) {
9805 if (mi->shadowDepth >= depth)
9806 mi->shadowDepth = -1;
9811 if (cur->next != NULL) {
9814 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9818 if (clone->parent != NULL)
9819 clone->parent->last = clone;
9820 clone = clone->parent;
9822 parentClone = clone->parent;
9824 * Process parent --> next;
9829 /* This is for attributes only. */
9830 clone = clone->parent;
9831 parentClone = clone->parent;
9833 * Process parent-element --> children.
9848 if (nsMap != NULL) {
9849 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9851 * Just cleanup the map but don't free.
9855 nsMap->last->next = nsMap->pool;
9856 nsMap->pool = nsMap->first;
9857 nsMap->first = NULL;
9860 xmlDOMWrapNsMapFree(nsMap);
9863 * TODO: Should we try a cleanup of the cloned node in case of a
9866 *resNode = resultClone;
9871 * xmlDOMWrapAdoptAttr:
9872 * @ctxt: the optional context for custom processing
9873 * @sourceDoc: the optional source document of attr
9874 * @attr: the attribute-node to be adopted
9875 * @destDoc: the destination doc for adoption
9876 * @destParent: the optional new parent of @attr in @destDoc
9877 * @options: option flags
9879 * @attr is adopted by @destDoc.
9880 * Ensures that ns-references point to @destDoc: either to
9881 * elements->nsDef entries if @destParent is given, or to
9882 * @destDoc->oldNs otherwise.
9884 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9887 xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9888 xmlDocPtr sourceDoc,
9891 xmlNodePtr destParent,
9892 int options ATTRIBUTE_UNUSED)
9897 if ((attr == NULL) || (destDoc == NULL))
9900 attr->doc = destDoc;
9901 if (attr->ns != NULL) {
9905 /* TODO: User defined. */
9907 /* XML Namespace. */
9908 if (IS_STR_XML(attr->ns->prefix)) {
9909 ns = xmlTreeEnsureXMLDecl(destDoc);
9910 } else if (destParent == NULL) {
9912 * Store in @destDoc->oldNs.
9914 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9917 * Declare on @destParent.
9919 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
9921 goto internal_error;
9923 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9924 attr->ns->href, attr->ns->prefix, 1);
9928 goto internal_error;
9932 XML_TREE_ADOPT_STR(attr->name);
9938 if (attr->children == NULL)
9940 cur = attr->children;
9941 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9942 goto internal_error;
9943 while (cur != NULL) {
9945 switch (cur->type) {
9947 case XML_CDATA_SECTION_NODE:
9948 XML_TREE_ADOPT_STR_2(cur->content)
9950 case XML_ENTITY_REF_NODE:
9952 * Remove reference to the entitity-node.
9954 cur->content = NULL;
9955 cur->children = NULL;
9957 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9960 * Assign new entity-node if available.
9962 ent = xmlGetDocEntity(destDoc, cur->name);
9964 cur->content = ent->content;
9965 cur->children = (xmlNodePtr) ent;
9966 cur->last = (xmlNodePtr) ent;
9973 if (cur->children != NULL) {
9974 cur = cur->children;
9978 if (cur == (xmlNodePtr) attr)
9980 if (cur->next != NULL)
9993 * xmlDOMWrapAdoptNode:
9994 * @ctxt: the optional context for custom processing
9995 * @sourceDoc: the optional sourceDoc
9996 * @node: the node to start with
9997 * @destDoc: the destination doc
9998 * @destParent: the optional new parent of @node in @destDoc
9999 * @options: option flags
10001 * References of out-of scope ns-decls are remapped to point to @destDoc:
10002 * 1) If @destParent is given, then nsDef entries on element-nodes are used
10003 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
10004 * This is the case when you have an unlinked node and just want to move it
10005 * to the context of
10007 * If @destParent is given, it ensures that the tree is namespace
10008 * wellformed by creating additional ns-decls where needed.
10009 * Note that, since prefixes of already existent ns-decls can be
10010 * shadowed by this process, it could break QNames in attribute
10011 * values or element content.
10012 * NOTE: This function was not intensively tested.
10014 * Returns 0 if the operation succeeded,
10015 * 1 if a node of unsupported type was given,
10016 * 2 if a node of not yet supported type was given and
10017 * -1 on API/internal errors.
10020 xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
10021 xmlDocPtr sourceDoc,
10024 xmlNodePtr destParent,
10027 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
10028 (destDoc == NULL) ||
10029 ((destParent != NULL) && (destParent->doc != destDoc)))
10032 * Check node->doc sanity.
10034 if ((node->doc != NULL) && (sourceDoc != NULL) &&
10035 (node->doc != sourceDoc)) {
10037 * Might be an XIncluded node.
10041 if (sourceDoc == NULL)
10042 sourceDoc = node->doc;
10043 if (sourceDoc == destDoc)
10045 switch (node->type) {
10046 case XML_ELEMENT_NODE:
10047 case XML_ATTRIBUTE_NODE:
10048 case XML_TEXT_NODE:
10049 case XML_CDATA_SECTION_NODE:
10050 case XML_ENTITY_REF_NODE:
10052 case XML_COMMENT_NODE:
10054 case XML_DOCUMENT_FRAG_NODE:
10055 /* TODO: Support document-fragment-nodes. */
10061 * Unlink only if @node was not already added to @destParent.
10063 if ((node->parent != NULL) && (destParent != node->parent))
10064 xmlUnlinkNode(node);
10066 if (node->type == XML_ELEMENT_NODE) {
10067 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
10068 destDoc, destParent, options));
10069 } else if (node->type == XML_ATTRIBUTE_NODE) {
10070 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
10071 (xmlAttrPtr) node, destDoc, destParent, options));
10073 xmlNodePtr cur = node;
10076 cur->doc = destDoc;
10078 * Optimize string adoption.
10080 if ((sourceDoc != NULL) &&
10081 (sourceDoc->dict == destDoc->dict))
10083 switch (node->type) {
10084 case XML_TEXT_NODE:
10085 case XML_CDATA_SECTION_NODE:
10086 XML_TREE_ADOPT_STR_2(node->content)
10088 case XML_ENTITY_REF_NODE:
10090 * Remove reference to the entitity-node.
10092 node->content = NULL;
10093 node->children = NULL;
10095 if ((destDoc->intSubset) || (destDoc->extSubset)) {
10098 * Assign new entity-node if available.
10100 ent = xmlGetDocEntity(destDoc, node->name);
10102 node->content = ent->content;
10103 node->children = (xmlNodePtr) ent;
10104 node->last = (xmlNodePtr) ent;
10107 XML_TREE_ADOPT_STR(node->name)
10109 case XML_PI_NODE: {
10110 XML_TREE_ADOPT_STR(node->name)
10111 XML_TREE_ADOPT_STR_2(node->content)
10121 #define bottom_tree
10122 #include "elfgcchack.h"