2 * tree.c : implementation of access function for an XML tree.
5 * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
7 * See Copyright for the status of this software.
16 #include <string.h> /* for memset() only ! */
28 #include <libxml/xmlmemory.h>
29 #include <libxml/tree.h>
30 #include <libxml/parser.h>
31 #include <libxml/uri.h>
32 #include <libxml/entities.h>
33 #include <libxml/valid.h>
34 #include <libxml/xmlerror.h>
35 #include <libxml/parserInternals.h>
36 #include <libxml/globals.h>
37 #ifdef LIBXML_HTML_ENABLED
38 #include <libxml/HTMLtree.h>
40 #ifdef LIBXML_DEBUG_ENABLED
41 #include <libxml/debugXML.h>
47 int __xmlRegisterCallbacks = 0;
49 /************************************************************************
51 * Forward declarations *
53 ************************************************************************/
56 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
58 static xmlChar* xmlGetPropNodeValueInternal(const xmlAttr *prop);
60 /************************************************************************
62 * Tree memory error handler *
64 ************************************************************************/
67 * @extra: extra informations
69 * Handle an out of memory condition
72 xmlTreeErrMemory(const char *extra)
74 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
79 * @code: the error number
80 * @extra: extra informations
82 * Handle an out of memory condition
85 xmlTreeErr(int code, xmlNodePtr node, const char *extra)
87 const char *msg = NULL;
90 case XML_TREE_INVALID_HEX:
91 msg = "invalid hexadecimal character value\n";
93 case XML_TREE_INVALID_DEC:
94 msg = "invalid decimal character value\n";
96 case XML_TREE_UNTERMINATED_ENTITY:
97 msg = "unterminated entity reference %15s\n";
99 case XML_TREE_NOT_UTF8:
100 msg = "string is not in UTF-8\n";
103 msg = "unexpected error number\n";
105 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
108 /************************************************************************
110 * A few static variables and macros *
112 ************************************************************************/
113 /* #undef xmlStringText */
114 const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
115 /* #undef xmlStringTextNoenc */
116 const xmlChar xmlStringTextNoenc[] =
117 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
118 /* #undef xmlStringComment */
119 const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
121 static int xmlCompressMode = 0;
122 static int xmlCheckDTD = 1;
124 #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
125 xmlNodePtr ulccur = (n)->children; \
126 if (ulccur == NULL) { \
129 while (ulccur->next != NULL) { \
130 ulccur->parent = (n); \
131 ulccur = ulccur->next; \
133 ulccur->parent = (n); \
134 (n)->last = ulccur; \
137 #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
138 (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
140 /* #define DEBUG_BUFFER */
141 /* #define DEBUG_TREE */
143 /************************************************************************
145 * Functions to move to entities.c once the *
146 * API freeze is smoothen and they can be made public. *
148 ************************************************************************/
149 #include <libxml/hash.h>
151 #ifdef LIBXML_TREE_ENABLED
153 * xmlGetEntityFromDtd:
154 * @dtd: A pointer to the DTD to search
155 * @name: The entity name
157 * Do an entity lookup in the DTD entity hash table and
158 * return the corresponding entity, if found.
160 * Returns A pointer to the entity structure or NULL if not found.
163 xmlGetEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
164 xmlEntitiesTablePtr table;
166 if((dtd != NULL) && (dtd->entities != NULL)) {
167 table = (xmlEntitiesTablePtr) dtd->entities;
168 return((xmlEntityPtr) xmlHashLookup(table, name));
169 /* return(xmlGetEntityFromTable(table, name)); */
174 * xmlGetParameterEntityFromDtd:
175 * @dtd: A pointer to the DTD to search
176 * @name: The entity name
178 * Do an entity lookup in the DTD pararmeter entity hash table and
179 * return the corresponding entity, if found.
181 * Returns A pointer to the entity structure or NULL if not found.
184 xmlGetParameterEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
185 xmlEntitiesTablePtr table;
187 if ((dtd != NULL) && (dtd->pentities != NULL)) {
188 table = (xmlEntitiesTablePtr) dtd->pentities;
189 return((xmlEntityPtr) xmlHashLookup(table, name));
190 /* return(xmlGetEntityFromTable(table, name)); */
194 #endif /* LIBXML_TREE_ENABLED */
196 /************************************************************************
198 * QName handling helper *
200 ************************************************************************/
205 * @prefix: the prefix
206 * @memory: preallocated memory
207 * @len: preallocated memory length
209 * Builds the QName @prefix:@ncname in @memory if there is enough space
210 * and prefix is not NULL nor empty, otherwise allocate a new string.
211 * If prefix is NULL or empty it returns ncname.
213 * Returns the new string which must be freed by the caller if different from
214 * @memory and @ncname or NULL in case of error
217 xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
218 xmlChar *memory, int len) {
222 if (ncname == NULL) return(NULL);
223 if (prefix == NULL) return((xmlChar *) ncname);
225 lenn = strlen((char *) ncname);
226 lenp = strlen((char *) prefix);
228 if ((memory == NULL) || (len < lenn + lenp + 2)) {
229 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
231 xmlTreeErrMemory("building QName");
237 memcpy(&ret[0], prefix, lenp);
239 memcpy(&ret[lenp + 1], ncname, lenn);
240 ret[lenn + lenp + 1] = 0;
246 * @name: the full QName
247 * @prefix: a xmlChar **
249 * parse an XML qualified name string
251 * [NS 5] QName ::= (Prefix ':')? LocalPart
253 * [NS 6] Prefix ::= NCName
255 * [NS 7] LocalPart ::= NCName
257 * Returns NULL if not a QName, otherwise the local part, and prefix
258 * is updated to get the Prefix if any.
262 xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
266 if (prefix == NULL) return(NULL);
268 if (name == NULL) return(NULL);
270 #ifndef XML_XML_NAMESPACE
271 /* xml: prefix is not really a namespace */
272 if ((name[0] == 'x') && (name[1] == 'm') &&
273 (name[2] == 'l') && (name[3] == ':'))
277 /* nasty but valid */
282 * we are not trying to validate but just to cut, and yes it will
283 * work even if this is as set of UTF-8 encoded chars
285 while ((name[len] != 0) && (name[len] != ':'))
291 *prefix = xmlStrndup(name, len);
292 if (*prefix == NULL) {
293 xmlTreeErrMemory("QName split");
296 ret = xmlStrdup(&name[len + 1]);
298 xmlTreeErrMemory("QName split");
299 if (*prefix != NULL) {
311 * @name: the full QName
314 * parse an XML qualified name string,i
316 * returns NULL if it is not a Qualified Name, otherwise, update len
317 * with the length in byte of the prefix and return a pointer
318 * to the start of the name without the prefix
322 xmlSplitQName3(const xmlChar *name, int *len) {
325 if (name == NULL) return(NULL);
326 if (len == NULL) return(NULL);
328 /* nasty but valid */
333 * we are not trying to validate but just to cut, and yes it will
334 * work even if this is as set of UTF-8 encoded chars
336 while ((name[l] != 0) && (name[l] != ':'))
347 /************************************************************************
349 * Check Name, NCName and QName strings *
351 ************************************************************************/
353 #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
355 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_DEBUG_ENABLED) || defined (LIBXML_HTML_ENABLED) || defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED) || defined(LIBXML_LEGACY_ENABLED)
358 * @value: the value to check
359 * @space: allow spaces in front and end of the string
361 * Check that a value conforms to the lexical space of NCName
363 * Returns 0 if this validates, a positive error code number otherwise
364 * and -1 in case of internal or API error.
367 xmlValidateNCName(const xmlChar *value, int space) {
368 const xmlChar *cur = value;
375 * First quick algorithm for ASCII range
378 while (IS_BLANK_CH(*cur)) cur++;
379 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
384 while (((*cur >= 'a') && (*cur <= 'z')) ||
385 ((*cur >= 'A') && (*cur <= 'Z')) ||
386 ((*cur >= '0') && (*cur <= '9')) ||
387 (*cur == '_') || (*cur == '-') || (*cur == '.'))
390 while (IS_BLANK_CH(*cur)) cur++;
396 * Second check for chars outside the ASCII range
399 c = CUR_SCHAR(cur, l);
401 while (IS_BLANK(c)) {
403 c = CUR_SCHAR(cur, l);
406 if ((!IS_LETTER(c)) && (c != '_'))
409 c = CUR_SCHAR(cur, l);
410 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
411 (c == '-') || (c == '_') || IS_COMBINING(c) ||
414 c = CUR_SCHAR(cur, l);
417 while (IS_BLANK(c)) {
419 c = CUR_SCHAR(cur, l);
429 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
432 * @value: the value to check
433 * @space: allow spaces in front and end of the string
435 * Check that a value conforms to the lexical space of QName
437 * Returns 0 if this validates, a positive error code number otherwise
438 * and -1 in case of internal or API error.
441 xmlValidateQName(const xmlChar *value, int space) {
442 const xmlChar *cur = value;
448 * First quick algorithm for ASCII range
451 while (IS_BLANK_CH(*cur)) cur++;
452 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
457 while (((*cur >= 'a') && (*cur <= 'z')) ||
458 ((*cur >= 'A') && (*cur <= 'Z')) ||
459 ((*cur >= '0') && (*cur <= '9')) ||
460 (*cur == '_') || (*cur == '-') || (*cur == '.'))
464 if (((*cur >= 'a') && (*cur <= 'z')) ||
465 ((*cur >= 'A') && (*cur <= 'Z')) ||
470 while (((*cur >= 'a') && (*cur <= 'z')) ||
471 ((*cur >= 'A') && (*cur <= 'Z')) ||
472 ((*cur >= '0') && (*cur <= '9')) ||
473 (*cur == '_') || (*cur == '-') || (*cur == '.'))
477 while (IS_BLANK_CH(*cur)) cur++;
483 * Second check for chars outside the ASCII range
486 c = CUR_SCHAR(cur, l);
488 while (IS_BLANK(c)) {
490 c = CUR_SCHAR(cur, l);
493 if ((!IS_LETTER(c)) && (c != '_'))
496 c = CUR_SCHAR(cur, l);
497 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
498 (c == '-') || (c == '_') || IS_COMBINING(c) ||
501 c = CUR_SCHAR(cur, l);
505 c = CUR_SCHAR(cur, l);
506 if ((!IS_LETTER(c)) && (c != '_'))
509 c = CUR_SCHAR(cur, l);
510 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
511 (c == '-') || (c == '_') || IS_COMBINING(c) ||
514 c = CUR_SCHAR(cur, l);
518 while (IS_BLANK(c)) {
520 c = CUR_SCHAR(cur, l);
530 * @value: the value to check
531 * @space: allow spaces in front and end of the string
533 * Check that a value conforms to the lexical space of Name
535 * Returns 0 if this validates, a positive error code number otherwise
536 * and -1 in case of internal or API error.
539 xmlValidateName(const xmlChar *value, int space) {
540 const xmlChar *cur = value;
546 * First quick algorithm for ASCII range
549 while (IS_BLANK_CH(*cur)) cur++;
550 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
551 (*cur == '_') || (*cur == ':'))
555 while (((*cur >= 'a') && (*cur <= 'z')) ||
556 ((*cur >= 'A') && (*cur <= 'Z')) ||
557 ((*cur >= '0') && (*cur <= '9')) ||
558 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
561 while (IS_BLANK_CH(*cur)) cur++;
567 * Second check for chars outside the ASCII range
570 c = CUR_SCHAR(cur, l);
572 while (IS_BLANK(c)) {
574 c = CUR_SCHAR(cur, l);
577 if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
580 c = CUR_SCHAR(cur, l);
581 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
582 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
584 c = CUR_SCHAR(cur, l);
587 while (IS_BLANK(c)) {
589 c = CUR_SCHAR(cur, l);
598 * xmlValidateNMToken:
599 * @value: the value to check
600 * @space: allow spaces in front and end of the string
602 * Check that a value conforms to the lexical space of NMToken
604 * Returns 0 if this validates, a positive error code number otherwise
605 * and -1 in case of internal or API error.
608 xmlValidateNMToken(const xmlChar *value, int space) {
609 const xmlChar *cur = value;
615 * First quick algorithm for ASCII range
618 while (IS_BLANK_CH(*cur)) cur++;
619 if (((*cur >= 'a') && (*cur <= 'z')) ||
620 ((*cur >= 'A') && (*cur <= 'Z')) ||
621 ((*cur >= '0') && (*cur <= '9')) ||
622 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
626 while (((*cur >= 'a') && (*cur <= 'z')) ||
627 ((*cur >= 'A') && (*cur <= 'Z')) ||
628 ((*cur >= '0') && (*cur <= '9')) ||
629 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
632 while (IS_BLANK_CH(*cur)) cur++;
638 * Second check for chars outside the ASCII range
641 c = CUR_SCHAR(cur, l);
643 while (IS_BLANK(c)) {
645 c = CUR_SCHAR(cur, l);
648 if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
649 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
652 c = CUR_SCHAR(cur, l);
653 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
654 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
656 c = CUR_SCHAR(cur, l);
659 while (IS_BLANK(c)) {
661 c = CUR_SCHAR(cur, l);
668 #endif /* LIBXML_TREE_ENABLED */
670 /************************************************************************
672 * Allocation and deallocation of basic structures *
674 ************************************************************************/
677 * xmlSetBufferAllocationScheme:
678 * @scheme: allocation method to use
680 * Set the buffer allocation method. Types are
681 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
682 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
683 * improves performance
686 xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
687 if ((scheme == XML_BUFFER_ALLOC_EXACT) ||
688 (scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
689 (scheme == XML_BUFFER_ALLOC_HYBRID))
690 xmlBufferAllocScheme = scheme;
694 * xmlGetBufferAllocationScheme:
697 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
698 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
699 * improves performance
700 * XML_BUFFER_ALLOC_HYBRID - use exact sizes on small strings to keep memory usage tight
701 * in normal usage, and doubleit on large strings to avoid
702 * pathological performance.
704 * Returns the current allocation scheme
706 xmlBufferAllocationScheme
707 xmlGetBufferAllocationScheme(void) {
708 return(xmlBufferAllocScheme);
713 * @node: the element carrying the namespace
714 * @href: the URI associated
715 * @prefix: the prefix for the namespace
717 * Creation of a new Namespace. This function will refuse to create
718 * a namespace with a similar prefix than an existing one present on this
720 * Note that for a default namespace, @prefix should be NULL.
722 * We use href==NULL in the case of an element creation where the namespace
725 * Returns a new namespace pointer or NULL
728 xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
731 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
734 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
735 /* xml namespace is predefined, no need to add it */
736 if (xmlStrEqual(href, XML_XML_NAMESPACE))
740 * Problem, this is an attempt to bind xml prefix to a wrong
741 * namespace, which breaks
742 * Namespace constraint: Reserved Prefixes and Namespace Names
743 * from XML namespace. But documents authors may not care in
744 * their context so let's proceed.
749 * Allocate a new Namespace and fill the fields.
751 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
753 xmlTreeErrMemory("building namespace");
756 memset(cur, 0, sizeof(xmlNs));
757 cur->type = XML_LOCAL_NAMESPACE;
760 cur->href = xmlStrdup(href);
762 cur->prefix = xmlStrdup(prefix);
765 * Add it at the end to preserve parsing order ...
766 * and checks for existing use of the prefix
769 if (node->nsDef == NULL) {
772 xmlNsPtr prev = node->nsDef;
774 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
775 (xmlStrEqual(prev->prefix, cur->prefix))) {
779 while (prev->next != NULL) {
781 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
782 (xmlStrEqual(prev->prefix, cur->prefix))) {
795 * @node: a node in the document
796 * @ns: a namespace pointer
798 * Associate a namespace to a node, a posteriori.
801 xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
804 xmlGenericError(xmlGenericErrorContext,
805 "xmlSetNs: node == NULL\n");
809 if ((node->type == XML_ELEMENT_NODE) ||
810 (node->type == XML_ATTRIBUTE_NODE))
816 * @cur: the namespace pointer
818 * Free up the structures associated to a namespace
821 xmlFreeNs(xmlNsPtr cur) {
824 xmlGenericError(xmlGenericErrorContext,
825 "xmlFreeNs : ns == NULL\n");
829 if (cur->href != NULL) xmlFree((char *) cur->href);
830 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
836 * @cur: the first namespace pointer
838 * Free up all the structures associated to the chained namespaces.
841 xmlFreeNsList(xmlNsPtr cur) {
845 xmlGenericError(xmlGenericErrorContext,
846 "xmlFreeNsList : ns == NULL\n");
850 while (cur != NULL) {
859 * @doc: the document pointer
860 * @name: the DTD name
861 * @ExternalID: the external ID
862 * @SystemID: the system ID
864 * Creation of a new DTD for the external subset. To create an
865 * internal subset, use xmlCreateIntSubset().
867 * Returns a pointer to the new DTD structure
870 xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
871 const xmlChar *ExternalID, const xmlChar *SystemID) {
874 if ((doc != NULL) && (doc->extSubset != NULL)) {
876 xmlGenericError(xmlGenericErrorContext,
877 "xmlNewDtd(%s): document %s already have a DTD %s\n",
878 /* !!! */ (char *) name, doc->name,
879 /* !!! */ (char *)doc->extSubset->name);
885 * Allocate a new DTD and fill the fields.
887 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
889 xmlTreeErrMemory("building DTD");
892 memset(cur, 0 , sizeof(xmlDtd));
893 cur->type = XML_DTD_NODE;
896 cur->name = xmlStrdup(name);
897 if (ExternalID != NULL)
898 cur->ExternalID = xmlStrdup(ExternalID);
899 if (SystemID != NULL)
900 cur->SystemID = xmlStrdup(SystemID);
902 doc->extSubset = cur;
905 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
906 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
912 * @doc: the document pointer
914 * Get the internal subset of a document
915 * Returns a pointer to the DTD structure or NULL if not found
919 xmlGetIntSubset(const xmlDoc *doc) {
925 while (cur != NULL) {
926 if (cur->type == XML_DTD_NODE)
927 return((xmlDtdPtr) cur);
930 return((xmlDtdPtr) doc->intSubset);
934 * xmlCreateIntSubset:
935 * @doc: the document pointer
936 * @name: the DTD name
937 * @ExternalID: the external (PUBLIC) ID
938 * @SystemID: the system ID
940 * Create the internal subset of a document
941 * Returns a pointer to the new DTD structure
944 xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
945 const xmlChar *ExternalID, const xmlChar *SystemID) {
948 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
950 xmlGenericError(xmlGenericErrorContext,
952 "xmlCreateIntSubset(): document %s already have an internal subset\n",
959 * Allocate a new DTD and fill the fields.
961 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
963 xmlTreeErrMemory("building internal subset");
966 memset(cur, 0, sizeof(xmlDtd));
967 cur->type = XML_DTD_NODE;
970 cur->name = xmlStrdup(name);
971 if (cur->name == NULL) {
972 xmlTreeErrMemory("building internal subset");
977 if (ExternalID != NULL) {
978 cur->ExternalID = xmlStrdup(ExternalID);
979 if (cur->ExternalID == NULL) {
980 xmlTreeErrMemory("building internal subset");
981 if (cur->name != NULL)
982 xmlFree((char *)cur->name);
987 if (SystemID != NULL) {
988 cur->SystemID = xmlStrdup(SystemID);
989 if (cur->SystemID == NULL) {
990 xmlTreeErrMemory("building internal subset");
991 if (cur->name != NULL)
992 xmlFree((char *)cur->name);
993 if (cur->ExternalID != NULL)
994 xmlFree((char *)cur->ExternalID);
1000 doc->intSubset = cur;
1003 if (doc->children == NULL) {
1004 doc->children = (xmlNodePtr) cur;
1005 doc->last = (xmlNodePtr) cur;
1007 if (doc->type == XML_HTML_DOCUMENT_NODE) {
1010 prev = doc->children;
1011 prev->prev = (xmlNodePtr) cur;
1013 doc->children = (xmlNodePtr) cur;
1017 next = doc->children;
1018 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
1021 cur->prev = doc->last;
1022 cur->prev->next = (xmlNodePtr) cur;
1024 doc->last = (xmlNodePtr) cur;
1027 cur->prev = next->prev;
1028 if (cur->prev == NULL)
1029 doc->children = (xmlNodePtr) cur;
1031 cur->prev->next = (xmlNodePtr) cur;
1032 next->prev = (xmlNodePtr) cur;
1038 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1039 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1047 * Free a string if it is not owned by the "dict" dictionnary in the
1050 #define DICT_FREE(str) \
1051 if ((str) && ((!dict) || \
1052 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1053 xmlFree((char *)(str));
1060 * Copy a string using a "dict" dictionnary in the current scope,
1063 #define DICT_COPY(str, cpy) \
1066 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1067 cpy = (xmlChar *) (str); \
1069 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1071 cpy = xmlStrdup((const xmlChar *)(str)); }
1077 * Copy a string using a "dict" dictionnary in the current scope,
1080 #define DICT_CONST_COPY(str, cpy) \
1083 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1084 cpy = (const xmlChar *) (str); \
1086 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1088 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1093 * @cur: the DTD structure to free up
1095 * Free a DTD structure.
1098 xmlFreeDtd(xmlDtdPtr cur) {
1099 xmlDictPtr dict = NULL;
1104 if (cur->doc != NULL) dict = cur->doc->dict;
1106 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1107 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1109 if (cur->children != NULL) {
1110 xmlNodePtr next, c = cur->children;
1113 * Cleanup all nodes which are not part of the specific lists
1114 * of notations, elements, attributes and entities.
1118 if ((c->type != XML_NOTATION_NODE) &&
1119 (c->type != XML_ELEMENT_DECL) &&
1120 (c->type != XML_ATTRIBUTE_DECL) &&
1121 (c->type != XML_ENTITY_DECL)) {
1128 DICT_FREE(cur->name)
1129 DICT_FREE(cur->SystemID)
1130 DICT_FREE(cur->ExternalID)
1132 if (cur->notations != NULL)
1133 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
1135 if (cur->elements != NULL)
1136 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1137 if (cur->attributes != NULL)
1138 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1139 if (cur->entities != NULL)
1140 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1141 if (cur->pentities != NULL)
1142 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1149 * @version: xmlChar string giving the version of XML "1.0"
1151 * Creates a new XML document
1153 * Returns a new document
1156 xmlNewDoc(const xmlChar *version) {
1159 if (version == NULL)
1160 version = (const xmlChar *) "1.0";
1163 * Allocate a new document and fill the fields.
1165 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1167 xmlTreeErrMemory("building doc");
1170 memset(cur, 0, sizeof(xmlDoc));
1171 cur->type = XML_DOCUMENT_NODE;
1173 cur->version = xmlStrdup(version);
1174 if (cur->version == NULL) {
1175 xmlTreeErrMemory("building doc");
1179 cur->standalone = -1;
1180 cur->compression = -1; /* not initialized */
1182 cur->parseFlags = 0;
1183 cur->properties = XML_DOC_USERBUILT;
1185 * The in memory encoding is always UTF8
1186 * This field will never change and would
1187 * be obsolete if not for binary compatibility.
1189 cur->charset = XML_CHAR_ENCODING_UTF8;
1191 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1192 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1198 * @cur: pointer to the document
1200 * Free up all the structures used by a document, tree included.
1203 xmlFreeDoc(xmlDocPtr cur) {
1204 xmlDtdPtr extSubset, intSubset;
1205 xmlDictPtr dict = NULL;
1209 xmlGenericError(xmlGenericErrorContext,
1210 "xmlFreeDoc : document == NULL\n");
1214 #ifdef LIBXML_DEBUG_RUNTIME
1215 #ifdef LIBXML_DEBUG_ENABLED
1216 xmlDebugCheckDocument(stderr, cur);
1220 if (cur != NULL) dict = cur->dict;
1222 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1223 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1226 * Do this before freeing the children list to avoid ID lookups
1228 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1230 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1232 extSubset = cur->extSubset;
1233 intSubset = cur->intSubset;
1234 if (intSubset == extSubset)
1236 if (extSubset != NULL) {
1237 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
1238 cur->extSubset = NULL;
1239 xmlFreeDtd(extSubset);
1241 if (intSubset != NULL) {
1242 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
1243 cur->intSubset = NULL;
1244 xmlFreeDtd(intSubset);
1247 if (cur->children != NULL) xmlFreeNodeList(cur->children);
1248 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
1250 DICT_FREE(cur->version)
1251 DICT_FREE(cur->name)
1252 DICT_FREE(cur->encoding)
1255 if (dict) xmlDictFree(dict);
1259 * xmlStringLenGetNodeList:
1260 * @doc: the document
1261 * @value: the value of the text
1262 * @len: the length of the string value
1264 * Parse the value string and build the node list associated. Should
1265 * produce a flat tree with only TEXTs and ENTITY_REFs.
1266 * Returns a pointer to the first child
1269 xmlStringLenGetNodeList(const xmlDoc *doc, const xmlChar *value, int len) {
1270 xmlNodePtr ret = NULL, last = NULL;
1273 const xmlChar *cur = value, *end = cur + len;
1278 if (value == NULL) return(NULL);
1280 buf = xmlBufCreateSize(0);
1281 if (buf == NULL) return(NULL);
1282 xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID);
1285 while ((cur < end) && (*cur != 0)) {
1286 if (cur[0] == '&') {
1291 * Save the current text.
1294 if (xmlBufAdd(buf, q, cur - q))
1298 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1304 while (tmp != ';') { /* Non input consuming loop */
1305 if ((tmp >= '0') && (tmp <= '9'))
1306 charval = charval * 16 + (tmp - '0');
1307 else if ((tmp >= 'a') && (tmp <= 'f'))
1308 charval = charval * 16 + (tmp - 'a') + 10;
1309 else if ((tmp >= 'A') && (tmp <= 'F'))
1310 charval = charval * 16 + (tmp - 'A') + 10;
1312 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1326 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1332 while (tmp != ';') { /* Non input consuming loops */
1333 if ((tmp >= '0') && (tmp <= '9'))
1334 charval = charval * 10 + (tmp - '0');
1336 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1352 * Read the entity string
1356 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1357 if ((cur >= end) || (*cur == 0)) {
1358 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1364 * Predefined entities don't generate nodes
1366 val = xmlStrndup(q, cur - q);
1367 ent = xmlGetDocEntity(doc, val);
1368 if ((ent != NULL) &&
1369 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1371 if (xmlBufCat(buf, ent->content))
1376 * Flush buffer so far
1378 if (!xmlBufIsEmpty(buf)) {
1379 node = xmlNewDocText(doc, NULL);
1381 if (val != NULL) xmlFree(val);
1384 node->content = xmlBufDetach(buf);
1389 last = xmlAddNextSibling(last, node);
1394 * Create a new REFERENCE_REF node
1396 node = xmlNewReference(doc, val);
1398 if (val != NULL) xmlFree(val);
1401 else if ((ent != NULL) && (ent->children == NULL)) {
1404 ent->children = xmlStringGetNodeList(doc,
1405 (const xmlChar*)node->content);
1407 temp = ent->children;
1409 temp->parent = (xmlNodePtr)ent;
1417 last = xmlAddNextSibling(last, node);
1429 l = xmlCopyCharMultiByte(buffer, charval);
1432 if (xmlBufCat(buf, buffer))
1442 * Handle the last piece of text.
1444 if (xmlBufAdd(buf, q, cur - q))
1448 if (!xmlBufIsEmpty(buf)) {
1449 node = xmlNewDocText(doc, NULL);
1450 if (node == NULL) goto out;
1451 node->content = xmlBufDetach(buf);
1456 xmlAddNextSibling(last, node);
1458 } else if (ret == NULL) {
1459 ret = xmlNewDocText(doc, BAD_CAST "");
1468 * xmlStringGetNodeList:
1469 * @doc: the document
1470 * @value: the value of the attribute
1472 * Parse the value string and build the node list associated. Should
1473 * produce a flat tree with only TEXTs and ENTITY_REFs.
1474 * Returns a pointer to the first child
1477 xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) {
1478 xmlNodePtr ret = NULL, last = NULL;
1481 const xmlChar *cur = value;
1486 if (value == NULL) return(NULL);
1488 buf = xmlBufCreateSize(0);
1489 if (buf == NULL) return(NULL);
1490 xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID);
1494 if (cur[0] == '&') {
1499 * Save the current text.
1502 if (xmlBufAdd(buf, q, cur - q))
1506 if ((cur[1] == '#') && (cur[2] == 'x')) {
1509 while (tmp != ';') { /* Non input consuming loop */
1510 if ((tmp >= '0') && (tmp <= '9'))
1511 charval = charval * 16 + (tmp - '0');
1512 else if ((tmp >= 'a') && (tmp <= 'f'))
1513 charval = charval * 16 + (tmp - 'a') + 10;
1514 else if ((tmp >= 'A') && (tmp <= 'F'))
1515 charval = charval * 16 + (tmp - 'A') + 10;
1517 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1528 } else if (cur[1] == '#') {
1531 while (tmp != ';') { /* Non input consuming loops */
1532 if ((tmp >= '0') && (tmp <= '9'))
1533 charval = charval * 10 + (tmp - '0');
1535 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1548 * Read the entity string
1552 while ((*cur != 0) && (*cur != ';')) cur++;
1554 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1555 (xmlNodePtr) doc, (const char *) q);
1560 * Predefined entities don't generate nodes
1562 val = xmlStrndup(q, cur - q);
1563 ent = xmlGetDocEntity(doc, val);
1564 if ((ent != NULL) &&
1565 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1567 if (xmlBufCat(buf, ent->content))
1572 * Flush buffer so far
1574 if (!xmlBufIsEmpty(buf)) {
1575 node = xmlNewDocText(doc, NULL);
1576 node->content = xmlBufDetach(buf);
1581 last = xmlAddNextSibling(last, node);
1586 * Create a new REFERENCE_REF node
1588 node = xmlNewReference(doc, val);
1590 if (val != NULL) xmlFree(val);
1593 else if ((ent != NULL) && (ent->children == NULL)) {
1596 ent->children = xmlStringGetNodeList(doc,
1597 (const xmlChar*)node->content);
1599 temp = ent->children;
1601 temp->parent = (xmlNodePtr)ent;
1608 last = xmlAddNextSibling(last, node);
1620 len = xmlCopyCharMultiByte(buffer, charval);
1623 if (xmlBufCat(buf, buffer))
1630 if ((cur != q) || (ret == NULL)) {
1632 * Handle the last piece of text.
1634 xmlBufAdd(buf, q, cur - q);
1637 if (!xmlBufIsEmpty(buf)) {
1638 node = xmlNewDocText(doc, NULL);
1639 node->content = xmlBufDetach(buf);
1644 xmlAddNextSibling(last, node);
1654 * xmlNodeListGetString:
1655 * @doc: the document
1656 * @list: a Node list
1657 * @inLine: should we replace entity contents or show their external form
1659 * Build the string equivalent to the text contained in the Node list
1660 * made of TEXTs and ENTITY_REFs
1662 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1665 xmlNodeListGetString(xmlDocPtr doc, const xmlNode *list, int inLine)
1667 const xmlNode *node = list;
1668 xmlChar *ret = NULL;
1674 if ((list->parent != NULL) && (list->parent->type == XML_ATTRIBUTE_NODE))
1679 while (node != NULL) {
1680 if ((node->type == XML_TEXT_NODE) ||
1681 (node->type == XML_CDATA_SECTION_NODE)) {
1683 ret = xmlStrcat(ret, node->content);
1688 buffer = xmlEncodeAttributeEntities(doc, node->content);
1690 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1691 if (buffer != NULL) {
1692 ret = xmlStrcat(ret, buffer);
1696 } else if (node->type == XML_ENTITY_REF_NODE) {
1698 ent = xmlGetDocEntity(doc, node->name);
1702 /* an entity content can be any "well balanced chunk",
1703 * i.e. the result of the content [43] production:
1704 * http://www.w3.org/TR/REC-xml#NT-content.
1705 * So it can contain text, CDATA section or nested
1706 * entity reference nodes (among others).
1707 * -> we recursive call xmlNodeListGetString()
1708 * which handles these types */
1709 buffer = xmlNodeListGetString(doc, ent->children, 1);
1710 if (buffer != NULL) {
1711 ret = xmlStrcat(ret, buffer);
1715 ret = xmlStrcat(ret, node->content);
1722 ret = xmlStrncat(ret, buf, 1);
1723 ret = xmlStrcat(ret, node->name);
1726 ret = xmlStrncat(ret, buf, 1);
1731 xmlGenericError(xmlGenericErrorContext,
1732 "xmlGetNodeListString : invalid node type %d\n",
1741 #ifdef LIBXML_TREE_ENABLED
1743 * xmlNodeListGetRawString:
1744 * @doc: the document
1745 * @list: a Node list
1746 * @inLine: should we replace entity contents or show their external form
1748 * Builds the string equivalent to the text contained in the Node list
1749 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1750 * this function doesn't do any character encoding handling.
1752 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1755 xmlNodeListGetRawString(const xmlDoc *doc, const xmlNode *list, int inLine)
1757 const xmlNode *node = list;
1758 xmlChar *ret = NULL;
1764 while (node != NULL) {
1765 if ((node->type == XML_TEXT_NODE) ||
1766 (node->type == XML_CDATA_SECTION_NODE)) {
1768 ret = xmlStrcat(ret, node->content);
1772 buffer = xmlEncodeSpecialChars(doc, node->content);
1773 if (buffer != NULL) {
1774 ret = xmlStrcat(ret, buffer);
1778 } else if (node->type == XML_ENTITY_REF_NODE) {
1780 ent = xmlGetDocEntity(doc, node->name);
1784 /* an entity content can be any "well balanced chunk",
1785 * i.e. the result of the content [43] production:
1786 * http://www.w3.org/TR/REC-xml#NT-content.
1787 * So it can contain text, CDATA section or nested
1788 * entity reference nodes (among others).
1789 * -> we recursive call xmlNodeListGetRawString()
1790 * which handles these types */
1792 xmlNodeListGetRawString(doc, ent->children, 1);
1793 if (buffer != NULL) {
1794 ret = xmlStrcat(ret, buffer);
1798 ret = xmlStrcat(ret, node->content);
1805 ret = xmlStrncat(ret, buf, 1);
1806 ret = xmlStrcat(ret, node->name);
1809 ret = xmlStrncat(ret, buf, 1);
1814 xmlGenericError(xmlGenericErrorContext,
1815 "xmlGetNodeListString : invalid node type %d\n",
1823 #endif /* LIBXML_TREE_ENABLED */
1826 xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1827 const xmlChar * name, const xmlChar * value,
1831 xmlDocPtr doc = NULL;
1833 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
1834 if ((eatname == 1) &&
1835 ((node->doc == NULL) ||
1836 (!(xmlDictOwns(node->doc->dict, name)))))
1837 xmlFree((xmlChar *) name);
1842 * Allocate a new property and fill the fields.
1844 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1846 if ((eatname == 1) &&
1847 ((node == NULL) || (node->doc == NULL) ||
1848 (!(xmlDictOwns(node->doc->dict, name)))))
1849 xmlFree((xmlChar *) name);
1850 xmlTreeErrMemory("building attribute");
1853 memset(cur, 0, sizeof(xmlAttr));
1854 cur->type = XML_ATTRIBUTE_NODE;
1864 if ((doc != NULL) && (doc->dict != NULL))
1865 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1867 cur->name = xmlStrdup(name);
1871 if (value != NULL) {
1874 if(!xmlCheckUTF8(value)) {
1875 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc,
1878 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1880 cur->children = xmlNewDocText(doc, value);
1882 tmp = cur->children;
1883 while (tmp != NULL) {
1884 tmp->parent = (xmlNodePtr) cur;
1885 if (tmp->next == NULL)
1892 * Add it at the end to preserve parsing order ...
1895 if (node->properties == NULL) {
1896 node->properties = cur;
1898 xmlAttrPtr prev = node->properties;
1900 while (prev->next != NULL)
1907 if ((value != NULL) && (node != NULL) &&
1908 (xmlIsID(node->doc, node, cur) == 1))
1909 xmlAddID(NULL, node->doc, value, cur);
1911 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1912 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1916 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1917 defined(LIBXML_SCHEMAS_ENABLED)
1920 * @node: the holding node
1921 * @name: the name of the attribute
1922 * @value: the value of the attribute
1924 * Create a new property carried by a node.
1925 * Returns a pointer to the attribute
1928 xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1932 xmlGenericError(xmlGenericErrorContext,
1933 "xmlNewProp : name == NULL\n");
1938 return xmlNewPropInternal(node, NULL, name, value, 0);
1940 #endif /* LIBXML_TREE_ENABLED */
1944 * @node: the holding node
1945 * @ns: the namespace
1946 * @name: the name of the attribute
1947 * @value: the value of the attribute
1949 * Create a new property tagged with a namespace and carried by a node.
1950 * Returns a pointer to the attribute
1953 xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1954 const xmlChar *value) {
1958 xmlGenericError(xmlGenericErrorContext,
1959 "xmlNewNsProp : name == NULL\n");
1964 return xmlNewPropInternal(node, ns, name, value, 0);
1968 * xmlNewNsPropEatName:
1969 * @node: the holding node
1970 * @ns: the namespace
1971 * @name: the name of the attribute
1972 * @value: the value of the attribute
1974 * Create a new property tagged with a namespace and carried by a node.
1975 * Returns a pointer to the attribute
1978 xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1979 const xmlChar *value) {
1983 xmlGenericError(xmlGenericErrorContext,
1984 "xmlNewNsPropEatName : name == NULL\n");
1989 return xmlNewPropInternal(node, ns, name, value, 1);
1994 * @doc: the document
1995 * @name: the name of the attribute
1996 * @value: the value of the attribute
1998 * Create a new property carried by a document.
1999 * Returns a pointer to the attribute
2002 xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
2007 xmlGenericError(xmlGenericErrorContext,
2008 "xmlNewDocProp : name == NULL\n");
2014 * Allocate a new property and fill the fields.
2016 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
2018 xmlTreeErrMemory("building attribute");
2021 memset(cur, 0, sizeof(xmlAttr));
2022 cur->type = XML_ATTRIBUTE_NODE;
2024 if ((doc != NULL) && (doc->dict != NULL))
2025 cur->name = xmlDictLookup(doc->dict, name, -1);
2027 cur->name = xmlStrdup(name);
2029 if (value != NULL) {
2032 cur->children = xmlStringGetNodeList(doc, value);
2035 tmp = cur->children;
2036 while (tmp != NULL) {
2037 tmp->parent = (xmlNodePtr) cur;
2038 if (tmp->next == NULL)
2044 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2045 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2051 * @cur: the first property in the list
2053 * Free a property and all its siblings, all the children are freed too.
2056 xmlFreePropList(xmlAttrPtr cur) {
2058 if (cur == NULL) return;
2059 while (cur != NULL) {
2068 * @cur: an attribute
2070 * Free one attribute, all the content is freed too
2073 xmlFreeProp(xmlAttrPtr cur) {
2074 xmlDictPtr dict = NULL;
2075 if (cur == NULL) return;
2077 if (cur->doc != NULL) dict = cur->doc->dict;
2079 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
2080 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2082 /* Check for ID removal -> leading to invalid references ! */
2083 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2084 xmlRemoveID(cur->doc, cur);
2086 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2087 DICT_FREE(cur->name)
2093 * @cur: an attribute
2095 * Unlink and free one attribute, all the content is freed too
2096 * Note this doesn't work for namespace definition attributes
2098 * Returns 0 if success and -1 in case of error.
2101 xmlRemoveProp(xmlAttrPtr cur) {
2105 xmlGenericError(xmlGenericErrorContext,
2106 "xmlRemoveProp : cur == NULL\n");
2110 if (cur->parent == NULL) {
2112 xmlGenericError(xmlGenericErrorContext,
2113 "xmlRemoveProp : cur->parent == NULL\n");
2117 tmp = cur->parent->properties;
2119 cur->parent->properties = cur->next;
2120 if (cur->next != NULL)
2121 cur->next->prev = NULL;
2125 while (tmp != NULL) {
2126 if (tmp->next == cur) {
2127 tmp->next = cur->next;
2128 if (tmp->next != NULL)
2129 tmp->next->prev = tmp;
2136 xmlGenericError(xmlGenericErrorContext,
2137 "xmlRemoveProp : attribute not owned by its node\n");
2144 * @doc: the target document
2145 * @name: the processing instruction name
2146 * @content: the PI content
2148 * Creation of a processing instruction element.
2149 * Returns a pointer to the new node object.
2152 xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
2157 xmlGenericError(xmlGenericErrorContext,
2158 "xmlNewPI : name == NULL\n");
2164 * Allocate a new node and fill the fields.
2166 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2168 xmlTreeErrMemory("building PI");
2171 memset(cur, 0, sizeof(xmlNode));
2172 cur->type = XML_PI_NODE;
2174 if ((doc != NULL) && (doc->dict != NULL))
2175 cur->name = xmlDictLookup(doc->dict, name, -1);
2177 cur->name = xmlStrdup(name);
2178 if (content != NULL) {
2179 cur->content = xmlStrdup(content);
2183 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2184 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2190 * @name: the processing instruction name
2191 * @content: the PI content
2193 * Creation of a processing instruction element.
2194 * Use xmlDocNewPI preferably to get string interning
2196 * Returns a pointer to the new node object.
2199 xmlNewPI(const xmlChar *name, const xmlChar *content) {
2200 return(xmlNewDocPI(NULL, name, content));
2205 * @ns: namespace if any
2206 * @name: the node name
2208 * Creation of a new node element. @ns is optional (NULL).
2210 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2214 xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2219 xmlGenericError(xmlGenericErrorContext,
2220 "xmlNewNode : name == NULL\n");
2226 * Allocate a new node and fill the fields.
2228 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2230 xmlTreeErrMemory("building node");
2233 memset(cur, 0, sizeof(xmlNode));
2234 cur->type = XML_ELEMENT_NODE;
2236 cur->name = xmlStrdup(name);
2239 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2240 xmlRegisterNodeDefaultValue(cur);
2245 * xmlNewNodeEatName:
2246 * @ns: namespace if any
2247 * @name: the node name
2249 * Creation of a new node element. @ns is optional (NULL).
2251 * Returns a pointer to the new node object, with pointer @name as
2252 * new node's name. Use xmlNewNode() if a copy of @name string is
2253 * is needed as new node's name.
2256 xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2261 xmlGenericError(xmlGenericErrorContext,
2262 "xmlNewNode : name == NULL\n");
2268 * Allocate a new node and fill the fields.
2270 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2272 xmlTreeErrMemory("building node");
2273 /* we can't check here that name comes from the doc dictionnary */
2276 memset(cur, 0, sizeof(xmlNode));
2277 cur->type = XML_ELEMENT_NODE;
2282 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2283 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2289 * @doc: the document
2290 * @ns: namespace if any
2291 * @name: the node name
2292 * @content: the XML text content if any
2294 * Creation of a new node element within a document. @ns and @content
2295 * are optional (NULL).
2296 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2297 * references, but XML special chars need to be escaped first by using
2298 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2299 * need entities support.
2301 * Returns a pointer to the new node object.
2304 xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2305 const xmlChar *name, const xmlChar *content) {
2308 if ((doc != NULL) && (doc->dict != NULL))
2309 cur = xmlNewNodeEatName(ns, (xmlChar *)
2310 xmlDictLookup(doc->dict, name, -1));
2312 cur = xmlNewNode(ns, name);
2315 if (content != NULL) {
2316 cur->children = xmlStringGetNodeList(doc, content);
2317 UPDATE_LAST_CHILD_AND_PARENT(cur)
2325 * xmlNewDocNodeEatName:
2326 * @doc: the document
2327 * @ns: namespace if any
2328 * @name: the node name
2329 * @content: the XML text content if any
2331 * Creation of a new node element within a document. @ns and @content
2332 * are optional (NULL).
2333 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2334 * references, but XML special chars need to be escaped first by using
2335 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2336 * need entities support.
2338 * Returns a pointer to the new node object.
2341 xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2342 xmlChar *name, const xmlChar *content) {
2345 cur = xmlNewNodeEatName(ns, name);
2348 if (content != NULL) {
2349 cur->children = xmlStringGetNodeList(doc, content);
2350 UPDATE_LAST_CHILD_AND_PARENT(cur)
2353 /* if name don't come from the doc dictionnary free it here */
2354 if ((name != NULL) && (doc != NULL) &&
2355 (!(xmlDictOwns(doc->dict, name))))
2361 #ifdef LIBXML_TREE_ENABLED
2364 * @doc: the document
2365 * @ns: namespace if any
2366 * @name: the node name
2367 * @content: the text content if any
2369 * Creation of a new node element within a document. @ns and @content
2370 * are optional (NULL).
2372 * Returns a pointer to the new node object.
2375 xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2376 const xmlChar *name, const xmlChar *content) {
2379 cur = xmlNewDocNode(doc, ns, name, NULL);
2382 if (content != NULL) {
2383 cur->children = xmlNewDocText(doc, content);
2384 UPDATE_LAST_CHILD_AND_PARENT(cur)
2391 * xmlNewDocFragment:
2392 * @doc: the document owning the fragment
2394 * Creation of a new Fragment node.
2395 * Returns a pointer to the new node object.
2398 xmlNewDocFragment(xmlDocPtr doc) {
2402 * Allocate a new DocumentFragment node and fill the fields.
2404 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2406 xmlTreeErrMemory("building fragment");
2409 memset(cur, 0, sizeof(xmlNode));
2410 cur->type = XML_DOCUMENT_FRAG_NODE;
2414 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2415 xmlRegisterNodeDefaultValue(cur);
2418 #endif /* LIBXML_TREE_ENABLED */
2422 * @content: the text content
2424 * Creation of a new text node.
2425 * Returns a pointer to the new node object.
2428 xmlNewText(const xmlChar *content) {
2432 * Allocate a new node and fill the fields.
2434 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2436 xmlTreeErrMemory("building text");
2439 memset(cur, 0, sizeof(xmlNode));
2440 cur->type = XML_TEXT_NODE;
2442 cur->name = xmlStringText;
2443 if (content != NULL) {
2444 cur->content = xmlStrdup(content);
2447 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2448 xmlRegisterNodeDefaultValue(cur);
2452 #ifdef LIBXML_TREE_ENABLED
2455 * @parent: the parent node
2456 * @ns: a namespace if any
2457 * @name: the name of the child
2458 * @content: the text content of the child if any.
2460 * Creation of a new child element, added at the end of @parent children list.
2461 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2462 * created element inherits the namespace of @parent. If @content is non NULL,
2463 * a child TEXT node will be created containing the string @content.
2464 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2465 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
2466 * reserved XML chars that might appear in @content, such as the ampersand,
2467 * greater-than or less-than signs, are automatically replaced by their XML
2468 * escaped entity representations.
2470 * Returns a pointer to the new node object.
2473 xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2474 const xmlChar *name, const xmlChar *content) {
2475 xmlNodePtr cur, prev;
2477 if (parent == NULL) {
2479 xmlGenericError(xmlGenericErrorContext,
2480 "xmlNewTextChild : parent == NULL\n");
2487 xmlGenericError(xmlGenericErrorContext,
2488 "xmlNewTextChild : name == NULL\n");
2494 * Allocate a new node
2496 if (parent->type == XML_ELEMENT_NODE) {
2498 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2500 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2501 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2502 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2504 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2506 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2507 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2508 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2512 if (cur == NULL) return(NULL);
2515 * add the new element at the end of the children list.
2517 cur->type = XML_ELEMENT_NODE;
2518 cur->parent = parent;
2519 cur->doc = parent->doc;
2520 if (parent->children == NULL) {
2521 parent->children = cur;
2524 prev = parent->last;
2532 #endif /* LIBXML_TREE_ENABLED */
2536 * @doc: the document
2537 * @name: the char ref string, starting with # or "&# ... ;"
2539 * Creation of a new character reference node.
2540 * Returns a pointer to the new node object.
2543 xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2550 * Allocate a new node and fill the fields.
2552 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2554 xmlTreeErrMemory("building character reference");
2557 memset(cur, 0, sizeof(xmlNode));
2558 cur->type = XML_ENTITY_REF_NODE;
2561 if (name[0] == '&') {
2564 len = xmlStrlen(name);
2565 if (name[len - 1] == ';')
2566 cur->name = xmlStrndup(name, len - 1);
2568 cur->name = xmlStrndup(name, len);
2570 cur->name = xmlStrdup(name);
2572 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2573 xmlRegisterNodeDefaultValue(cur);
2579 * @doc: the document
2580 * @name: the reference name, or the reference string with & and ;
2582 * Creation of a new reference node.
2583 * Returns a pointer to the new node object.
2586 xmlNewReference(const xmlDoc *doc, const xmlChar *name) {
2594 * Allocate a new node and fill the fields.
2596 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2598 xmlTreeErrMemory("building reference");
2601 memset(cur, 0, sizeof(xmlNode));
2602 cur->type = XML_ENTITY_REF_NODE;
2604 cur->doc = (xmlDoc *)doc;
2605 if (name[0] == '&') {
2608 len = xmlStrlen(name);
2609 if (name[len - 1] == ';')
2610 cur->name = xmlStrndup(name, len - 1);
2612 cur->name = xmlStrndup(name, len);
2614 cur->name = xmlStrdup(name);
2616 ent = xmlGetDocEntity(doc, cur->name);
2618 cur->content = ent->content;
2620 * The parent pointer in entity is a DTD pointer and thus is NOT
2621 * updated. Not sure if this is 100% correct.
2624 cur->children = (xmlNodePtr) ent;
2625 cur->last = (xmlNodePtr) ent;
2628 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2629 xmlRegisterNodeDefaultValue(cur);
2635 * @doc: the document
2636 * @content: the text content
2638 * Creation of a new text node within a document.
2639 * Returns a pointer to the new node object.
2642 xmlNewDocText(const xmlDoc *doc, const xmlChar *content) {
2645 cur = xmlNewText(content);
2646 if (cur != NULL) cur->doc = (xmlDoc *)doc;
2652 * @content: the text content
2653 * @len: the text len.
2655 * Creation of a new text node with an extra parameter for the content's length
2656 * Returns a pointer to the new node object.
2659 xmlNewTextLen(const xmlChar *content, int len) {
2663 * Allocate a new node and fill the fields.
2665 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2667 xmlTreeErrMemory("building text");
2670 memset(cur, 0, sizeof(xmlNode));
2671 cur->type = XML_TEXT_NODE;
2673 cur->name = xmlStringText;
2674 if (content != NULL) {
2675 cur->content = xmlStrndup(content, len);
2678 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2679 xmlRegisterNodeDefaultValue(cur);
2685 * @doc: the document
2686 * @content: the text content
2687 * @len: the text len.
2689 * Creation of a new text node with an extra content length parameter. The
2690 * text node pertain to a given document.
2691 * Returns a pointer to the new node object.
2694 xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2697 cur = xmlNewTextLen(content, len);
2698 if (cur != NULL) cur->doc = doc;
2704 * @content: the comment content
2706 * Creation of a new node containing a comment.
2707 * Returns a pointer to the new node object.
2710 xmlNewComment(const xmlChar *content) {
2714 * Allocate a new node and fill the fields.
2716 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2718 xmlTreeErrMemory("building comment");
2721 memset(cur, 0, sizeof(xmlNode));
2722 cur->type = XML_COMMENT_NODE;
2724 cur->name = xmlStringComment;
2725 if (content != NULL) {
2726 cur->content = xmlStrdup(content);
2729 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2730 xmlRegisterNodeDefaultValue(cur);
2736 * @doc: the document
2737 * @content: the CDATA block content content
2738 * @len: the length of the block
2740 * Creation of a new node containing a CDATA block.
2741 * Returns a pointer to the new node object.
2744 xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2748 * Allocate a new node and fill the fields.
2750 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2752 xmlTreeErrMemory("building CDATA");
2755 memset(cur, 0, sizeof(xmlNode));
2756 cur->type = XML_CDATA_SECTION_NODE;
2759 if (content != NULL) {
2760 cur->content = xmlStrndup(content, len);
2763 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2764 xmlRegisterNodeDefaultValue(cur);
2770 * @doc: the document
2771 * @content: the comment content
2773 * Creation of a new node containing a comment within a document.
2774 * Returns a pointer to the new node object.
2777 xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2780 cur = xmlNewComment(content);
2781 if (cur != NULL) cur->doc = doc;
2787 * @tree: the top element
2788 * @doc: the document
2790 * update all nodes under the tree to point to the right document
2793 xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
2796 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
2798 if (tree->doc != doc) {
2799 if(tree->type == XML_ELEMENT_NODE) {
2800 prop = tree->properties;
2801 while (prop != NULL) {
2802 if (prop->atype == XML_ATTRIBUTE_ID) {
2803 xmlRemoveID(tree->doc, prop);
2807 xmlSetListDoc(prop->children, doc);
2810 * TODO: ID attributes should be also added to the new
2811 * document, but this breaks things like xmlReplaceNode.
2812 * The underlying problem is that xmlRemoveID is only called
2813 * if a node is destroyed, not if it's unlinked.
2816 if (xmlIsID(doc, tree, prop)) {
2817 xmlChar *idVal = xmlNodeListGetString(doc, prop->children,
2819 xmlAddID(NULL, doc, idVal, prop);
2826 if (tree->children != NULL)
2827 xmlSetListDoc(tree->children, doc);
2834 * @list: the first element
2835 * @doc: the document
2837 * update all nodes in the list to point to the right document
2840 xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2843 if ((list == NULL) || (list->type == XML_NAMESPACE_DECL))
2846 while (cur != NULL) {
2847 if (cur->doc != doc)
2848 xmlSetTreeDoc(cur, doc);
2853 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
2856 * @parent: the parent node
2857 * @ns: a namespace if any
2858 * @name: the name of the child
2859 * @content: the XML content of the child if any.
2861 * Creation of a new child element, added at the end of @parent children list.
2862 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2863 * created element inherits the namespace of @parent. If @content is non NULL,
2864 * a child list containing the TEXTs and ENTITY_REFs node will be created.
2865 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2866 * references. XML special chars must be escaped first by using
2867 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
2869 * Returns a pointer to the new node object.
2872 xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2873 const xmlChar *name, const xmlChar *content) {
2874 xmlNodePtr cur, prev;
2876 if (parent == NULL) {
2878 xmlGenericError(xmlGenericErrorContext,
2879 "xmlNewChild : parent == NULL\n");
2886 xmlGenericError(xmlGenericErrorContext,
2887 "xmlNewChild : name == NULL\n");
2893 * Allocate a new node
2895 if (parent->type == XML_ELEMENT_NODE) {
2897 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2899 cur = xmlNewDocNode(parent->doc, ns, name, content);
2900 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2901 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2903 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2905 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
2906 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2907 cur = xmlNewDocNode( parent->doc, ns, name, content);
2911 if (cur == NULL) return(NULL);
2914 * add the new element at the end of the children list.
2916 cur->type = XML_ELEMENT_NODE;
2917 cur->parent = parent;
2918 cur->doc = parent->doc;
2919 if (parent->children == NULL) {
2920 parent->children = cur;
2923 prev = parent->last;
2931 #endif /* LIBXML_TREE_ENABLED */
2934 * xmlAddPropSibling:
2935 * @prev: the attribute to which @prop is added after
2936 * @cur: the base attribute passed to calling function
2937 * @prop: the new attribute
2939 * Add a new attribute after @prev using @cur as base attribute.
2940 * When inserting before @cur, @prev is passed as @cur->prev.
2941 * When inserting after @cur, @prev is passed as @cur.
2942 * If an existing attribute is found it is detroyed prior to adding @prop.
2944 * Returns the attribute being inserted or NULL in case of error.
2947 xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2950 if ((cur == NULL) || (cur->type != XML_ATTRIBUTE_NODE) ||
2951 (prop == NULL) || (prop->type != XML_ATTRIBUTE_NODE) ||
2952 ((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)))
2955 /* check if an attribute with the same name exists */
2956 if (prop->ns == NULL)
2957 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2959 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2961 if (prop->doc != cur->doc) {
2962 xmlSetTreeDoc(prop, cur->doc);
2964 prop->parent = cur->parent;
2967 prop->next = prev->next;
2970 prop->next->prev = prop;
2975 if (prop->prev == NULL && prop->parent != NULL)
2976 prop->parent->properties = (xmlAttrPtr) prop;
2977 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2978 /* different instance, destroy it (attributes must be unique) */
2979 xmlRemoveProp((xmlAttrPtr) attr);
2985 * xmlAddNextSibling:
2986 * @cur: the child node
2987 * @elem: the new node
2989 * Add a new node @elem as the next sibling of @cur
2990 * If the new node was already inserted in a document it is
2991 * first unlinked from its existing context.
2992 * As a result of text merging @elem may be freed.
2993 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2994 * If there is an attribute with equal name, it is first destroyed.
2996 * Returns the new node or NULL in case of error.
2999 xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
3000 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3002 xmlGenericError(xmlGenericErrorContext,
3003 "xmlAddNextSibling : cur == NULL\n");
3007 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
3009 xmlGenericError(xmlGenericErrorContext,
3010 "xmlAddNextSibling : elem == NULL\n");
3017 xmlGenericError(xmlGenericErrorContext,
3018 "xmlAddNextSibling : cur == elem\n");
3023 xmlUnlinkNode(elem);
3025 if (elem->type == XML_TEXT_NODE) {
3026 if (cur->type == XML_TEXT_NODE) {
3027 xmlNodeAddContent(cur, elem->content);
3031 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
3032 (cur->name == cur->next->name)) {
3035 tmp = xmlStrdup(elem->content);
3036 tmp = xmlStrcat(tmp, cur->next->content);
3037 xmlNodeSetContent(cur->next, tmp);
3042 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3043 return xmlAddPropSibling(cur, cur, elem);
3046 if (elem->doc != cur->doc) {
3047 xmlSetTreeDoc(elem, cur->doc);
3049 elem->parent = cur->parent;
3051 elem->next = cur->next;
3053 if (elem->next != NULL)
3054 elem->next->prev = elem;
3055 if ((elem->parent != NULL) && (elem->parent->last == cur))
3056 elem->parent->last = elem;
3060 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
3061 defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
3063 * xmlAddPrevSibling:
3064 * @cur: the child node
3065 * @elem: the new node
3067 * Add a new node @elem as the previous sibling of @cur
3068 * merging adjacent TEXT nodes (@elem may be freed)
3069 * If the new node was already inserted in a document it is
3070 * first unlinked from its existing context.
3071 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3072 * If there is an attribute with equal name, it is first destroyed.
3074 * Returns the new node or NULL in case of error.
3077 xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
3078 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3080 xmlGenericError(xmlGenericErrorContext,
3081 "xmlAddPrevSibling : cur == NULL\n");
3085 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
3087 xmlGenericError(xmlGenericErrorContext,
3088 "xmlAddPrevSibling : elem == NULL\n");
3095 xmlGenericError(xmlGenericErrorContext,
3096 "xmlAddPrevSibling : cur == elem\n");
3101 xmlUnlinkNode(elem);
3103 if (elem->type == XML_TEXT_NODE) {
3104 if (cur->type == XML_TEXT_NODE) {
3107 tmp = xmlStrdup(elem->content);
3108 tmp = xmlStrcat(tmp, cur->content);
3109 xmlNodeSetContent(cur, tmp);
3114 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3115 (cur->name == cur->prev->name)) {
3116 xmlNodeAddContent(cur->prev, elem->content);
3120 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3121 return xmlAddPropSibling(cur->prev, cur, elem);
3124 if (elem->doc != cur->doc) {
3125 xmlSetTreeDoc(elem, cur->doc);
3127 elem->parent = cur->parent;
3129 elem->prev = cur->prev;
3131 if (elem->prev != NULL)
3132 elem->prev->next = elem;
3133 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
3134 elem->parent->children = elem;
3138 #endif /* LIBXML_TREE_ENABLED */
3142 * @cur: the child node
3143 * @elem: the new node
3145 * Add a new element @elem to the list of siblings of @cur
3146 * merging adjacent TEXT nodes (@elem may be freed)
3147 * If the new element was already inserted in a document it is
3148 * first unlinked from its existing context.
3150 * Returns the new element or NULL in case of error.
3153 xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3156 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3158 xmlGenericError(xmlGenericErrorContext,
3159 "xmlAddSibling : cur == NULL\n");
3164 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
3166 xmlGenericError(xmlGenericErrorContext,
3167 "xmlAddSibling : elem == NULL\n");
3174 xmlGenericError(xmlGenericErrorContext,
3175 "xmlAddSibling : cur == elem\n");
3181 * Constant time is we can rely on the ->parent->last to find
3184 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
3185 (cur->parent->children != NULL) &&
3186 (cur->parent->last != NULL) &&
3187 (cur->parent->last->next == NULL)) {
3188 cur = cur->parent->last;
3190 while (cur->next != NULL) cur = cur->next;
3193 xmlUnlinkNode(elem);
3195 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3196 (cur->name == elem->name)) {
3197 xmlNodeAddContent(cur, elem->content);
3200 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3201 return xmlAddPropSibling(cur, cur, elem);
3204 if (elem->doc != cur->doc) {
3205 xmlSetTreeDoc(elem, cur->doc);
3207 parent = cur->parent;
3210 elem->parent = parent;
3213 parent->last = elem;
3220 * @parent: the parent node
3221 * @cur: the first node in the list
3223 * Add a list of node at the end of the child list of the parent
3224 * merging adjacent TEXT nodes (@cur may be freed)
3226 * Returns the last child or NULL in case of error.
3229 xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3232 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3234 xmlGenericError(xmlGenericErrorContext,
3235 "xmlAddChildList : parent == NULL\n");
3240 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3242 xmlGenericError(xmlGenericErrorContext,
3243 "xmlAddChildList : child == NULL\n");
3248 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3249 (cur->doc != parent->doc)) {
3251 xmlGenericError(xmlGenericErrorContext,
3252 "Elements moved to a different document\n");
3257 * add the first element at the end of the children list.
3260 if (parent->children == NULL) {
3261 parent->children = cur;
3264 * If cur and parent->last both are TEXT nodes, then merge them.
3266 if ((cur->type == XML_TEXT_NODE) &&
3267 (parent->last->type == XML_TEXT_NODE) &&
3268 (cur->name == parent->last->name)) {
3269 xmlNodeAddContent(parent->last, cur->content);
3271 * if it's the only child, nothing more to be done.
3273 if (cur->next == NULL) {
3275 return(parent->last);
3281 prev = parent->last;
3285 while (cur->next != NULL) {
3286 cur->parent = parent;
3287 if (cur->doc != parent->doc) {
3288 xmlSetTreeDoc(cur, parent->doc);
3292 cur->parent = parent;
3293 /* the parent may not be linked to a doc ! */
3294 if (cur->doc != parent->doc) {
3295 xmlSetTreeDoc(cur, parent->doc);
3304 * @parent: the parent node
3305 * @cur: the child node
3307 * Add a new node to @parent, at the end of the child (or property) list
3308 * merging adjacent TEXT nodes (in which case @cur is freed)
3309 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3310 * If there is an attribute with equal name, it is first destroyed.
3312 * Returns the child or NULL in case of error.
3315 xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3318 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3320 xmlGenericError(xmlGenericErrorContext,
3321 "xmlAddChild : parent == NULL\n");
3326 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3328 xmlGenericError(xmlGenericErrorContext,
3329 "xmlAddChild : child == NULL\n");
3334 if (parent == cur) {
3336 xmlGenericError(xmlGenericErrorContext,
3337 "xmlAddChild : parent == cur\n");
3342 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
3343 * cur is then freed.
3345 if (cur->type == XML_TEXT_NODE) {
3346 if ((parent->type == XML_TEXT_NODE) &&
3347 (parent->content != NULL) &&
3348 (parent->name == cur->name)) {
3349 xmlNodeAddContent(parent, cur->content);
3353 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
3354 (parent->last->name == cur->name) &&
3355 (parent->last != cur)) {
3356 xmlNodeAddContent(parent->last, cur->content);
3358 return(parent->last);
3363 * add the new element at the end of the children list.
3366 cur->parent = parent;
3367 if (cur->doc != parent->doc) {
3368 xmlSetTreeDoc(cur, parent->doc);
3370 /* this check prevents a loop on tree-traversions if a developer
3371 * tries to add a node to its parent multiple times
3379 if ((parent->type == XML_TEXT_NODE) &&
3380 (parent->content != NULL) &&
3382 xmlNodeAddContent(parent, cur->content);
3386 if (cur->type == XML_ATTRIBUTE_NODE) {
3387 if (parent->type != XML_ELEMENT_NODE)
3389 if (parent->properties != NULL) {
3390 /* check if an attribute with the same name exists */
3391 xmlAttrPtr lastattr;
3393 if (cur->ns == NULL)
3394 lastattr = xmlHasNsProp(parent, cur->name, NULL);
3396 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
3397 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
3398 /* different instance, destroy it (attributes must be unique) */
3399 xmlUnlinkNode((xmlNodePtr) lastattr);
3400 xmlFreeProp(lastattr);
3402 if (lastattr == (xmlAttrPtr) cur)
3406 if (parent->properties == NULL) {
3407 parent->properties = (xmlAttrPtr) cur;
3410 xmlAttrPtr lastattr = parent->properties;
3411 while (lastattr->next != NULL) {
3412 lastattr = lastattr->next;
3414 lastattr->next = (xmlAttrPtr) cur;
3415 ((xmlAttrPtr) cur)->prev = lastattr;
3418 if (parent->children == NULL) {
3419 parent->children = cur;
3422 prev = parent->last;
3433 * @parent: the parent node
3435 * Search the last child of a node.
3436 * Returns the last child or NULL if none.
3439 xmlGetLastChild(const xmlNode *parent) {
3440 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3442 xmlGenericError(xmlGenericErrorContext,
3443 "xmlGetLastChild : parent == NULL\n");
3447 return(parent->last);
3450 #ifdef LIBXML_TREE_ENABLED
3452 * 5 interfaces from DOM ElementTraversal
3456 * xmlChildElementCount:
3457 * @parent: the parent node
3459 * Finds the current number of child nodes of that element which are
3461 * Note the handling of entities references is different than in
3462 * the W3C DOM element traversal spec since we don't have back reference
3463 * from entities content to entities references.
3465 * Returns the count of element child or 0 if not available
3468 xmlChildElementCount(xmlNodePtr parent) {
3469 unsigned long ret = 0;
3470 xmlNodePtr cur = NULL;
3474 switch (parent->type) {
3475 case XML_ELEMENT_NODE:
3476 case XML_ENTITY_NODE:
3477 case XML_DOCUMENT_NODE:
3478 case XML_DOCUMENT_FRAG_NODE:
3479 case XML_HTML_DOCUMENT_NODE:
3480 cur = parent->children;
3485 while (cur != NULL) {
3486 if (cur->type == XML_ELEMENT_NODE)
3494 * xmlFirstElementChild:
3495 * @parent: the parent node
3497 * Finds the first child node of that element which is a Element node
3498 * Note the handling of entities references is different than in
3499 * the W3C DOM element traversal spec since we don't have back reference
3500 * from entities content to entities references.
3502 * Returns the first element child or NULL if not available
3505 xmlFirstElementChild(xmlNodePtr parent) {
3506 xmlNodePtr cur = NULL;
3510 switch (parent->type) {
3511 case XML_ELEMENT_NODE:
3512 case XML_ENTITY_NODE:
3513 case XML_DOCUMENT_NODE:
3514 case XML_DOCUMENT_FRAG_NODE:
3515 case XML_HTML_DOCUMENT_NODE:
3516 cur = parent->children;
3521 while (cur != NULL) {
3522 if (cur->type == XML_ELEMENT_NODE)
3530 * xmlLastElementChild:
3531 * @parent: the parent node
3533 * Finds the last child node of that element which is a Element node
3534 * Note the handling of entities references is different than in
3535 * the W3C DOM element traversal spec since we don't have back reference
3536 * from entities content to entities references.
3538 * Returns the last element child or NULL if not available
3541 xmlLastElementChild(xmlNodePtr parent) {
3542 xmlNodePtr cur = NULL;
3546 switch (parent->type) {
3547 case XML_ELEMENT_NODE:
3548 case XML_ENTITY_NODE:
3549 case XML_DOCUMENT_NODE:
3550 case XML_DOCUMENT_FRAG_NODE:
3551 case XML_HTML_DOCUMENT_NODE:
3557 while (cur != NULL) {
3558 if (cur->type == XML_ELEMENT_NODE)
3566 * xmlPreviousElementSibling:
3567 * @node: the current node
3569 * Finds the first closest previous sibling of the node which is an
3571 * Note the handling of entities references is different than in
3572 * the W3C DOM element traversal spec since we don't have back reference
3573 * from entities content to entities references.
3575 * Returns the previous element sibling or NULL if not available
3578 xmlPreviousElementSibling(xmlNodePtr node) {
3581 switch (node->type) {
3582 case XML_ELEMENT_NODE:
3584 case XML_CDATA_SECTION_NODE:
3585 case XML_ENTITY_REF_NODE:
3586 case XML_ENTITY_NODE:
3588 case XML_COMMENT_NODE:
3589 case XML_XINCLUDE_START:
3590 case XML_XINCLUDE_END:
3596 while (node != NULL) {
3597 if (node->type == XML_ELEMENT_NODE)
3605 * xmlNextElementSibling:
3606 * @node: the current node
3608 * Finds the first closest next sibling of the node which is an
3610 * Note the handling of entities references is different than in
3611 * the W3C DOM element traversal spec since we don't have back reference
3612 * from entities content to entities references.
3614 * Returns the next element sibling or NULL if not available
3617 xmlNextElementSibling(xmlNodePtr node) {
3620 switch (node->type) {
3621 case XML_ELEMENT_NODE:
3623 case XML_CDATA_SECTION_NODE:
3624 case XML_ENTITY_REF_NODE:
3625 case XML_ENTITY_NODE:
3627 case XML_COMMENT_NODE:
3629 case XML_XINCLUDE_START:
3630 case XML_XINCLUDE_END:
3636 while (node != NULL) {
3637 if (node->type == XML_ELEMENT_NODE)
3644 #endif /* LIBXML_TREE_ENABLED */
3648 * @cur: the first node in the list
3650 * Free a node and all its siblings, this is a recursive behaviour, all
3651 * the children are freed too.
3654 xmlFreeNodeList(xmlNodePtr cur) {
3656 xmlDictPtr dict = NULL;
3658 if (cur == NULL) return;
3659 if (cur->type == XML_NAMESPACE_DECL) {
3660 xmlFreeNsList((xmlNsPtr) cur);
3663 if ((cur->type == XML_DOCUMENT_NODE) ||
3664 #ifdef LIBXML_DOCB_ENABLED
3665 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
3667 (cur->type == XML_HTML_DOCUMENT_NODE)) {
3668 xmlFreeDoc((xmlDocPtr) cur);
3671 if (cur->doc != NULL) dict = cur->doc->dict;
3672 while (cur != NULL) {
3674 if (cur->type != XML_DTD_NODE) {
3676 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3677 xmlDeregisterNodeDefaultValue(cur);
3679 if ((cur->children != NULL) &&
3680 (cur->type != XML_ENTITY_REF_NODE))
3681 xmlFreeNodeList(cur->children);
3682 if (((cur->type == XML_ELEMENT_NODE) ||
3683 (cur->type == XML_XINCLUDE_START) ||
3684 (cur->type == XML_XINCLUDE_END)) &&
3685 (cur->properties != NULL))
3686 xmlFreePropList(cur->properties);
3687 if ((cur->type != XML_ELEMENT_NODE) &&
3688 (cur->type != XML_XINCLUDE_START) &&
3689 (cur->type != XML_XINCLUDE_END) &&
3690 (cur->type != XML_ENTITY_REF_NODE) &&
3691 (cur->content != (xmlChar *) &(cur->properties))) {
3692 DICT_FREE(cur->content)
3694 if (((cur->type == XML_ELEMENT_NODE) ||
3695 (cur->type == XML_XINCLUDE_START) ||
3696 (cur->type == XML_XINCLUDE_END)) &&
3697 (cur->nsDef != NULL))
3698 xmlFreeNsList(cur->nsDef);
3701 * When a node is a text node or a comment, it uses a global static
3702 * variable for the name of the node.
3703 * Otherwise the node name might come from the document's
3706 if ((cur->name != NULL) &&
3707 (cur->type != XML_TEXT_NODE) &&
3708 (cur->type != XML_COMMENT_NODE))
3709 DICT_FREE(cur->name)
3720 * Free a node, this is a recursive behaviour, all the children are freed too.
3721 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3724 xmlFreeNode(xmlNodePtr cur) {
3725 xmlDictPtr dict = NULL;
3727 if (cur == NULL) return;
3729 /* use xmlFreeDtd for DTD nodes */
3730 if (cur->type == XML_DTD_NODE) {
3731 xmlFreeDtd((xmlDtdPtr) cur);
3734 if (cur->type == XML_NAMESPACE_DECL) {
3735 xmlFreeNs((xmlNsPtr) cur);
3738 if (cur->type == XML_ATTRIBUTE_NODE) {
3739 xmlFreeProp((xmlAttrPtr) cur);
3743 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3744 xmlDeregisterNodeDefaultValue(cur);
3746 if (cur->doc != NULL) dict = cur->doc->dict;
3748 if (cur->type == XML_ENTITY_DECL) {
3749 xmlEntityPtr ent = (xmlEntityPtr) cur;
3750 DICT_FREE(ent->SystemID);
3751 DICT_FREE(ent->ExternalID);
3753 if ((cur->children != NULL) &&
3754 (cur->type != XML_ENTITY_REF_NODE))
3755 xmlFreeNodeList(cur->children);
3756 if (((cur->type == XML_ELEMENT_NODE) ||
3757 (cur->type == XML_XINCLUDE_START) ||
3758 (cur->type == XML_XINCLUDE_END)) &&
3759 (cur->properties != NULL))
3760 xmlFreePropList(cur->properties);
3761 if ((cur->type != XML_ELEMENT_NODE) &&
3762 (cur->content != NULL) &&
3763 (cur->type != XML_ENTITY_REF_NODE) &&
3764 (cur->type != XML_XINCLUDE_END) &&
3765 (cur->type != XML_XINCLUDE_START) &&
3766 (cur->content != (xmlChar *) &(cur->properties))) {
3767 DICT_FREE(cur->content)
3771 * When a node is a text node or a comment, it uses a global static
3772 * variable for the name of the node.
3773 * Otherwise the node name might come from the document's dictionnary
3775 if ((cur->name != NULL) &&
3776 (cur->type != XML_TEXT_NODE) &&
3777 (cur->type != XML_COMMENT_NODE))
3778 DICT_FREE(cur->name)
3780 if (((cur->type == XML_ELEMENT_NODE) ||
3781 (cur->type == XML_XINCLUDE_START) ||
3782 (cur->type == XML_XINCLUDE_END)) &&
3783 (cur->nsDef != NULL))
3784 xmlFreeNsList(cur->nsDef);
3792 * Unlink a node from it's current context, the node is not freed
3793 * If one need to free the node, use xmlFreeNode() routine after the
3794 * unlink to discard it.
3795 * Note that namespace nodes can't be unlinked as they do not have
3796 * pointer to their parent.
3799 xmlUnlinkNode(xmlNodePtr cur) {
3802 xmlGenericError(xmlGenericErrorContext,
3803 "xmlUnlinkNode : node == NULL\n");
3807 if (cur->type == XML_NAMESPACE_DECL)
3809 if (cur->type == XML_DTD_NODE) {
3813 if (doc->intSubset == (xmlDtdPtr) cur)
3814 doc->intSubset = NULL;
3815 if (doc->extSubset == (xmlDtdPtr) cur)
3816 doc->extSubset = NULL;
3819 if (cur->type == XML_ENTITY_DECL) {
3823 if (doc->intSubset != NULL) {
3824 if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur)
3825 xmlHashRemoveEntry(doc->intSubset->entities, cur->name,
3827 if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur)
3828 xmlHashRemoveEntry(doc->intSubset->pentities, cur->name,
3831 if (doc->extSubset != NULL) {
3832 if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur)
3833 xmlHashRemoveEntry(doc->extSubset->entities, cur->name,
3835 if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur)
3836 xmlHashRemoveEntry(doc->extSubset->pentities, cur->name,
3841 if (cur->parent != NULL) {
3843 parent = cur->parent;
3844 if (cur->type == XML_ATTRIBUTE_NODE) {
3845 if (parent->properties == (xmlAttrPtr) cur)
3846 parent->properties = ((xmlAttrPtr) cur)->next;
3848 if (parent->children == cur)
3849 parent->children = cur->next;
3850 if (parent->last == cur)
3851 parent->last = cur->prev;
3855 if (cur->next != NULL)
3856 cur->next->prev = cur->prev;
3857 if (cur->prev != NULL)
3858 cur->prev->next = cur->next;
3859 cur->next = cur->prev = NULL;
3862 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
3865 * @old: the old node
3868 * Unlink the old node from its current context, prune the new one
3869 * at the same place. If @cur was already inserted in a document it is
3870 * first unlinked from its existing context.
3872 * Returns the @old node
3875 xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
3876 if (old == cur) return(NULL);
3877 if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) ||
3878 (old->parent == NULL)) {
3880 xmlGenericError(xmlGenericErrorContext,
3881 "xmlReplaceNode : old == NULL or without parent\n");
3885 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3892 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3894 xmlGenericError(xmlGenericErrorContext,
3895 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3899 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3901 xmlGenericError(xmlGenericErrorContext,
3902 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3907 xmlSetTreeDoc(cur, old->doc);
3908 cur->parent = old->parent;
3909 cur->next = old->next;
3910 if (cur->next != NULL)
3911 cur->next->prev = cur;
3912 cur->prev = old->prev;
3913 if (cur->prev != NULL)
3914 cur->prev->next = cur;
3915 if (cur->parent != NULL) {
3916 if (cur->type == XML_ATTRIBUTE_NODE) {
3917 if (cur->parent->properties == (xmlAttrPtr)old)
3918 cur->parent->properties = ((xmlAttrPtr) cur);
3920 if (cur->parent->children == old)
3921 cur->parent->children = cur;
3922 if (cur->parent->last == old)
3923 cur->parent->last = cur;
3926 old->next = old->prev = NULL;
3930 #endif /* LIBXML_TREE_ENABLED */
3932 /************************************************************************
3936 ************************************************************************/
3940 * @cur: the namespace
3942 * Do a copy of the namespace.
3944 * Returns: a new #xmlNsPtr, or NULL in case of error.
3947 xmlCopyNamespace(xmlNsPtr cur) {
3950 if (cur == NULL) return(NULL);
3951 switch (cur->type) {
3952 case XML_LOCAL_NAMESPACE:
3953 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3957 xmlGenericError(xmlGenericErrorContext,
3958 "xmlCopyNamespace: invalid type %d\n", cur->type);
3966 * xmlCopyNamespaceList:
3967 * @cur: the first namespace
3969 * Do a copy of an namespace list.
3971 * Returns: a new #xmlNsPtr, or NULL in case of error.
3974 xmlCopyNamespaceList(xmlNsPtr cur) {
3975 xmlNsPtr ret = NULL;
3976 xmlNsPtr p = NULL,q;
3978 while (cur != NULL) {
3979 q = xmlCopyNamespace(cur);
3992 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
3995 xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
3998 if (cur == NULL) return(NULL);
3999 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4002 ret = xmlNewDocProp(target->doc, cur->name, NULL);
4003 else if (doc != NULL)
4004 ret = xmlNewDocProp(doc, cur->name, NULL);
4005 else if (cur->parent != NULL)
4006 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
4007 else if (cur->children != NULL)
4008 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
4010 ret = xmlNewDocProp(NULL, cur->name, NULL);
4011 if (ret == NULL) return(NULL);
4012 ret->parent = target;
4014 if ((cur->ns != NULL) && (target != NULL)) {
4017 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
4020 * Humm, we are copying an element whose namespace is defined
4021 * out of the new tree scope. Search it in the original tree
4022 * and add it at the top of the new tree
4024 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
4026 xmlNodePtr root = target;
4027 xmlNodePtr pred = NULL;
4029 while (root->parent != NULL) {
4031 root = root->parent;
4033 if (root == (xmlNodePtr) target->doc) {
4034 /* correct possibly cycling above the document elt */
4037 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4041 * we have to find something appropriate here since
4042 * we cant be sure, that the namespce we found is identified
4045 if (xmlStrEqual(ns->href, cur->ns->href)) {
4046 /* this is the nice case */
4050 * we are in trouble: we need a new reconcilied namespace.
4053 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
4060 if (cur->children != NULL) {
4063 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
4065 tmp = ret->children;
4066 while (tmp != NULL) {
4067 /* tmp->parent = (xmlNodePtr)ret; */
4068 if (tmp->next == NULL)
4076 if ((target!= NULL) && (cur!= NULL) &&
4077 (target->doc != NULL) && (cur->doc != NULL) &&
4078 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
4079 if (xmlIsID(cur->doc, cur->parent, cur)) {
4082 id = xmlNodeListGetString(cur->doc, cur->children, 1);
4084 xmlAddID(NULL, target->doc, id, ret);
4094 * @target: the element where the attribute will be grafted
4095 * @cur: the attribute
4097 * Do a copy of the attribute.
4099 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4102 xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
4103 return xmlCopyPropInternal(NULL, target, cur);
4108 * @target: the element where the attributes will be grafted
4109 * @cur: the first attribute
4111 * Do a copy of an attribute list.
4113 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4116 xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
4117 xmlAttrPtr ret = NULL;
4118 xmlAttrPtr p = NULL,q;
4120 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4122 while (cur != NULL) {
4123 q = xmlCopyProp(target, cur);
4139 * NOTE about the CopyNode operations !
4141 * They are split into external and internal parts for one
4142 * tricky reason: namespaces. Doing a direct copy of a node
4143 * say RPM:Copyright without changing the namespace pointer to
4144 * something else can produce stale links. One way to do it is
4145 * to keep a reference counter but this doesn't work as soon
4146 * as one move the element or the subtree out of the scope of
4147 * the existing namespace. The actual solution seems to add
4148 * a copy of the namespace at the top of the copied tree if
4149 * not available in the subtree.
4150 * Hence two functions, the public front-end call the inner ones
4151 * The argument "recursive" normally indicates a recursive copy
4152 * of the node with values 0 (no) and 1 (yes). For XInclude,
4153 * however, we allow a value of 2 to indicate copy properties and
4154 * namespace info, but don't recurse on children.
4158 xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
4162 if (node == NULL) return(NULL);
4163 switch (node->type) {
4165 case XML_CDATA_SECTION_NODE:
4166 case XML_ELEMENT_NODE:
4167 case XML_DOCUMENT_FRAG_NODE:
4168 case XML_ENTITY_REF_NODE:
4169 case XML_ENTITY_NODE:
4171 case XML_COMMENT_NODE:
4172 case XML_XINCLUDE_START:
4173 case XML_XINCLUDE_END:
4175 case XML_ATTRIBUTE_NODE:
4176 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
4177 case XML_NAMESPACE_DECL:
4178 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
4180 case XML_DOCUMENT_NODE:
4181 case XML_HTML_DOCUMENT_NODE:
4182 #ifdef LIBXML_DOCB_ENABLED
4183 case XML_DOCB_DOCUMENT_NODE:
4185 #ifdef LIBXML_TREE_ENABLED
4186 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
4187 #endif /* LIBXML_TREE_ENABLED */
4188 case XML_DOCUMENT_TYPE_NODE:
4189 case XML_NOTATION_NODE:
4191 case XML_ELEMENT_DECL:
4192 case XML_ATTRIBUTE_DECL:
4193 case XML_ENTITY_DECL:
4198 * Allocate a new node and fill the fields.
4200 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4202 xmlTreeErrMemory("copying node");
4205 memset(ret, 0, sizeof(xmlNode));
4206 ret->type = node->type;
4209 ret->parent = parent;
4210 if (node->name == xmlStringText)
4211 ret->name = xmlStringText;
4212 else if (node->name == xmlStringTextNoenc)
4213 ret->name = xmlStringTextNoenc;
4214 else if (node->name == xmlStringComment)
4215 ret->name = xmlStringComment;
4216 else if (node->name != NULL) {
4217 if ((doc != NULL) && (doc->dict != NULL))
4218 ret->name = xmlDictLookup(doc->dict, node->name, -1);
4220 ret->name = xmlStrdup(node->name);
4222 if ((node->type != XML_ELEMENT_NODE) &&
4223 (node->content != NULL) &&
4224 (node->type != XML_ENTITY_REF_NODE) &&
4225 (node->type != XML_XINCLUDE_END) &&
4226 (node->type != XML_XINCLUDE_START)) {
4227 ret->content = xmlStrdup(node->content);
4229 if (node->type == XML_ELEMENT_NODE)
4230 ret->line = node->line;
4232 if (parent != NULL) {
4236 * this is a tricky part for the node register thing:
4237 * in case ret does get coalesced in xmlAddChild
4238 * the deregister-node callback is called; so we register ret now already
4240 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
4241 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4243 tmp = xmlAddChild(parent, ret);
4244 /* node could have coalesced */
4251 if (((node->type == XML_ELEMENT_NODE) ||
4252 (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
4253 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
4255 if (node->ns != NULL) {
4258 ns = xmlSearchNs(doc, ret, node->ns->prefix);
4261 * Humm, we are copying an element whose namespace is defined
4262 * out of the new tree scope. Search it in the original tree
4263 * and add it at the top of the new tree
4265 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
4267 xmlNodePtr root = ret;
4269 while (root->parent != NULL) root = root->parent;
4270 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4272 ret->ns = xmlNewReconciliedNs(doc, ret, node->ns);
4276 * reference the existing namespace definition in our own tree.
4281 if (((node->type == XML_ELEMENT_NODE) ||
4282 (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
4283 ret->properties = xmlCopyPropList(ret, node->properties);
4284 if (node->type == XML_ENTITY_REF_NODE) {
4285 if ((doc == NULL) || (node->doc != doc)) {
4287 * The copied node will go into a separate document, so
4288 * to avoid dangling references to the ENTITY_DECL node
4289 * we cannot keep the reference. Try to find it in the
4292 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
4294 ret->children = node->children;
4296 ret->last = ret->children;
4297 } else if ((node->children != NULL) && (extended != 2)) {
4298 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
4299 UPDATE_LAST_CHILD_AND_PARENT(ret)
4303 /* if parent != NULL we already registered the node above */
4304 if ((parent == NULL) &&
4305 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
4306 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4311 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
4312 xmlNodePtr ret = NULL;
4313 xmlNodePtr p = NULL,q;
4315 while (node != NULL) {
4316 #ifdef LIBXML_TREE_ENABLED
4317 if (node->type == XML_DTD_NODE ) {
4322 if (doc->intSubset == NULL) {
4323 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
4324 if (q == NULL) return(NULL);
4327 doc->intSubset = (xmlDtdPtr) q;
4328 xmlAddChild(parent, q);
4330 q = (xmlNodePtr) doc->intSubset;
4331 xmlAddChild(parent, q);
4334 #endif /* LIBXML_TREE_ENABLED */
4335 q = xmlStaticCopyNode(node, doc, parent, 1);
4336 if (q == NULL) return(NULL);
4340 } else if (p != q) {
4341 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
4354 * @extended: if 1 do a recursive copy (properties, namespaces and children
4356 * if 2 copy properties and namespaces (when applicable)
4358 * Do a copy of the node.
4360 * Returns: a new #xmlNodePtr, or NULL in case of error.
4363 xmlCopyNode(xmlNodePtr node, int extended) {
4366 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
4373 * @doc: the document
4374 * @extended: if 1 do a recursive copy (properties, namespaces and children
4376 * if 2 copy properties and namespaces (when applicable)
4378 * Do a copy of the node to a given document.
4380 * Returns: a new #xmlNodePtr, or NULL in case of error.
4383 xmlDocCopyNode(xmlNodePtr node, xmlDocPtr doc, int extended) {
4386 ret = xmlStaticCopyNode(node, doc, NULL, extended);
4391 * xmlDocCopyNodeList:
4392 * @doc: the target document
4393 * @node: the first node in the list.
4395 * Do a recursive copy of the node list.
4397 * Returns: a new #xmlNodePtr, or NULL in case of error.
4399 xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, xmlNodePtr node) {
4400 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4406 * @node: the first node in the list.
4408 * Do a recursive copy of the node list.
4409 * Use xmlDocCopyNodeList() if possible to ensure string interning.
4411 * Returns: a new #xmlNodePtr, or NULL in case of error.
4413 xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
4414 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4418 #if defined(LIBXML_TREE_ENABLED)
4423 * Do a copy of the dtd.
4425 * Returns: a new #xmlDtdPtr, or NULL in case of error.
4428 xmlCopyDtd(xmlDtdPtr dtd) {
4430 xmlNodePtr cur, p = NULL, q;
4432 if (dtd == NULL) return(NULL);
4433 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4434 if (ret == NULL) return(NULL);
4435 if (dtd->entities != NULL)
4436 ret->entities = (void *) xmlCopyEntitiesTable(
4437 (xmlEntitiesTablePtr) dtd->entities);
4438 if (dtd->notations != NULL)
4439 ret->notations = (void *) xmlCopyNotationTable(
4440 (xmlNotationTablePtr) dtd->notations);
4441 if (dtd->elements != NULL)
4442 ret->elements = (void *) xmlCopyElementTable(
4443 (xmlElementTablePtr) dtd->elements);
4444 if (dtd->attributes != NULL)
4445 ret->attributes = (void *) xmlCopyAttributeTable(
4446 (xmlAttributeTablePtr) dtd->attributes);
4447 if (dtd->pentities != NULL)
4448 ret->pentities = (void *) xmlCopyEntitiesTable(
4449 (xmlEntitiesTablePtr) dtd->pentities);
4451 cur = dtd->children;
4452 while (cur != NULL) {
4455 if (cur->type == XML_ENTITY_DECL) {
4456 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4457 switch (tmp->etype) {
4458 case XML_INTERNAL_GENERAL_ENTITY:
4459 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4460 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4461 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4463 case XML_INTERNAL_PARAMETER_ENTITY:
4464 case XML_EXTERNAL_PARAMETER_ENTITY:
4466 xmlGetParameterEntityFromDtd(ret, tmp->name);
4468 case XML_INTERNAL_PREDEFINED_ENTITY:
4471 } else if (cur->type == XML_ELEMENT_DECL) {
4472 xmlElementPtr tmp = (xmlElementPtr) cur;
4474 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4475 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4476 xmlAttributePtr tmp = (xmlAttributePtr) cur;
4478 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4479 } else if (cur->type == XML_COMMENT_NODE) {
4480 q = xmlCopyNode(cur, 0);
4494 q->parent = (xmlNodePtr) ret;
4505 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
4508 * @doc: the document
4509 * @recursive: if not zero do a recursive copy.
4511 * Do a copy of the document info. If recursive, the content tree will
4512 * be copied too as well as DTD, namespaces and entities.
4514 * Returns: a new #xmlDocPtr, or NULL in case of error.
4517 xmlCopyDoc(xmlDocPtr doc, int recursive) {
4520 if (doc == NULL) return(NULL);
4521 ret = xmlNewDoc(doc->version);
4522 if (ret == NULL) return(NULL);
4523 if (doc->name != NULL)
4524 ret->name = xmlMemStrdup(doc->name);
4525 if (doc->encoding != NULL)
4526 ret->encoding = xmlStrdup(doc->encoding);
4527 if (doc->URL != NULL)
4528 ret->URL = xmlStrdup(doc->URL);
4529 ret->charset = doc->charset;
4530 ret->compression = doc->compression;
4531 ret->standalone = doc->standalone;
4532 if (!recursive) return(ret);
4535 ret->children = NULL;
4536 #ifdef LIBXML_TREE_ENABLED
4537 if (doc->intSubset != NULL) {
4538 ret->intSubset = xmlCopyDtd(doc->intSubset);
4539 if (ret->intSubset == NULL) {
4543 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
4544 ret->intSubset->parent = ret;
4547 if (doc->oldNs != NULL)
4548 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4549 if (doc->children != NULL) {
4552 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4555 tmp = ret->children;
4556 while (tmp != NULL) {
4557 if (tmp->next == NULL)
4564 #endif /* LIBXML_TREE_ENABLED */
4566 /************************************************************************
4568 * Content access functions *
4570 ************************************************************************/
4573 * xmlGetLineNoInternal:
4575 * @depth: used to limit any risk of recursion
4577 * Get line number of @node.
4578 * Try to override the limitation of lines being store in 16 bits ints
4580 * Returns the line number if successful, -1 otherwise
4583 xmlGetLineNoInternal(const xmlNode *node, int depth)
4592 if ((node->type == XML_ELEMENT_NODE) ||
4593 (node->type == XML_TEXT_NODE) ||
4594 (node->type == XML_COMMENT_NODE) ||
4595 (node->type == XML_PI_NODE)) {
4596 if (node->line == 65535) {
4597 if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL))
4598 result = (long) node->psvi;
4599 else if ((node->type == XML_ELEMENT_NODE) &&
4600 (node->children != NULL))
4601 result = xmlGetLineNoInternal(node->children, depth + 1);
4602 else if (node->next != NULL)
4603 result = xmlGetLineNoInternal(node->next, depth + 1);
4604 else if (node->prev != NULL)
4605 result = xmlGetLineNoInternal(node->prev, depth + 1);
4607 if ((result == -1) || (result == 65535))
4608 result = (long) node->line;
4609 } else if ((node->prev != NULL) &&
4610 ((node->prev->type == XML_ELEMENT_NODE) ||
4611 (node->prev->type == XML_TEXT_NODE) ||
4612 (node->prev->type == XML_COMMENT_NODE) ||
4613 (node->prev->type == XML_PI_NODE)))
4614 result = xmlGetLineNoInternal(node->prev, depth + 1);
4615 else if ((node->parent != NULL) &&
4616 (node->parent->type == XML_ELEMENT_NODE))
4617 result = xmlGetLineNoInternal(node->parent, depth + 1);
4626 * Get line number of @node.
4627 * Try to override the limitation of lines being store in 16 bits ints
4628 * if XML_PARSE_BIG_LINES parser option was used
4630 * Returns the line number if successful, -1 otherwise
4633 xmlGetLineNo(const xmlNode *node)
4635 return(xmlGetLineNoInternal(node, 0));
4638 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
4643 * Build a structure based Path for the given node
4645 * Returns the new path or NULL in case of error. The caller must free
4646 * the returned string
4649 xmlGetNodePath(const xmlNode *node)
4651 const xmlNode *cur, *tmp, *next;
4652 xmlChar *buffer = NULL, *temp;
4658 int occur = 0, generic;
4660 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
4664 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4665 if (buffer == NULL) {
4666 xmlTreeErrMemory("getting node path");
4669 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4671 xmlTreeErrMemory("getting node path");
4682 if ((cur->type == XML_DOCUMENT_NODE) ||
4683 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4684 if (buffer[0] == '/')
4688 } else if (cur->type == XML_ELEMENT_NODE) {
4691 name = (const char *) cur->name;
4693 if (cur->ns->prefix != NULL) {
4694 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4695 (char *)cur->ns->prefix, (char *)cur->name);
4696 nametemp[sizeof(nametemp) - 1] = 0;
4700 * We cannot express named elements in the default
4701 * namespace, so use "*".
4710 * Thumbler index computation
4711 * TODO: the ocurence test seems bogus for namespaced names
4714 while (tmp != NULL) {
4715 if ((tmp->type == XML_ELEMENT_NODE) &&
4717 (xmlStrEqual(cur->name, tmp->name) &&
4718 ((tmp->ns == cur->ns) ||
4719 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4720 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4726 while (tmp != NULL && occur == 0) {
4727 if ((tmp->type == XML_ELEMENT_NODE) &&
4729 (xmlStrEqual(cur->name, tmp->name) &&
4730 ((tmp->ns == cur->ns) ||
4731 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4732 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4740 } else if (cur->type == XML_COMMENT_NODE) {
4746 * Thumbler index computation
4749 while (tmp != NULL) {
4750 if (tmp->type == XML_COMMENT_NODE)
4756 while (tmp != NULL && occur == 0) {
4757 if (tmp->type == XML_COMMENT_NODE)
4765 } else if ((cur->type == XML_TEXT_NODE) ||
4766 (cur->type == XML_CDATA_SECTION_NODE)) {
4772 * Thumbler index computation
4775 while (tmp != NULL) {
4776 if ((tmp->type == XML_TEXT_NODE) ||
4777 (tmp->type == XML_CDATA_SECTION_NODE))
4782 * Evaluate if this is the only text- or CDATA-section-node;
4783 * if yes, then we'll get "text()", otherwise "text()[1]".
4787 while (tmp != NULL) {
4788 if ((tmp->type == XML_TEXT_NODE) ||
4789 (tmp->type == XML_CDATA_SECTION_NODE))
4798 } else if (cur->type == XML_PI_NODE) {
4800 snprintf(nametemp, sizeof(nametemp) - 1,
4801 "processing-instruction('%s')", (char *)cur->name);
4802 nametemp[sizeof(nametemp) - 1] = 0;
4808 * Thumbler index computation
4811 while (tmp != NULL) {
4812 if ((tmp->type == XML_PI_NODE) &&
4813 (xmlStrEqual(cur->name, tmp->name)))
4819 while (tmp != NULL && occur == 0) {
4820 if ((tmp->type == XML_PI_NODE) &&
4821 (xmlStrEqual(cur->name, tmp->name)))
4830 } else if (cur->type == XML_ATTRIBUTE_NODE) {
4832 name = (const char *) (((xmlAttrPtr) cur)->name);
4834 if (cur->ns->prefix != NULL)
4835 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4836 (char *)cur->ns->prefix, (char *)cur->name);
4838 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4840 nametemp[sizeof(nametemp) - 1] = 0;
4843 next = ((xmlAttrPtr) cur)->parent;
4849 * Make sure there is enough room
4851 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4853 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4854 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4856 xmlTreeErrMemory("getting node path");
4862 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4864 xmlTreeErrMemory("getting node path");
4872 snprintf((char *) buf, buf_len, "%s%s%s",
4873 sep, name, (char *) buffer);
4875 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
4876 sep, name, occur, (char *) buffer);
4877 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
4879 } while (cur != NULL);
4883 #endif /* LIBXML_TREE_ENABLED */
4886 * xmlDocGetRootElement:
4887 * @doc: the document
4889 * Get the root element of the document (doc->children is a list
4890 * containing possibly comments, PIs, etc ...).
4892 * Returns the #xmlNodePtr for the root or NULL
4895 xmlDocGetRootElement(const xmlDoc *doc) {
4898 if (doc == NULL) return(NULL);
4899 ret = doc->children;
4900 while (ret != NULL) {
4901 if (ret->type == XML_ELEMENT_NODE)
4908 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
4910 * xmlDocSetRootElement:
4911 * @doc: the document
4912 * @root: the new document root element, if root is NULL no action is taken,
4913 * to remove a node from a document use xmlUnlinkNode(root) instead.
4915 * Set the root element of the document (doc->children is a list
4916 * containing possibly comments, PIs, etc ...).
4918 * Returns the old root element if any was found, NULL if root was NULL
4921 xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4922 xmlNodePtr old = NULL;
4924 if (doc == NULL) return(NULL);
4925 if ((root == NULL) || (root->type == XML_NAMESPACE_DECL))
4927 xmlUnlinkNode(root);
4928 xmlSetTreeDoc(root, doc);
4929 root->parent = (xmlNodePtr) doc;
4930 old = doc->children;
4931 while (old != NULL) {
4932 if (old->type == XML_ELEMENT_NODE)
4937 if (doc->children == NULL) {
4938 doc->children = root;
4941 xmlAddSibling(doc->children, root);
4944 xmlReplaceNode(old, root);
4950 #if defined(LIBXML_TREE_ENABLED)
4953 * @cur: the node being changed
4954 * @lang: the language description
4956 * Set the language of a node, i.e. the values of the xml:lang
4960 xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
4963 if (cur == NULL) return;
4966 case XML_CDATA_SECTION_NODE:
4967 case XML_COMMENT_NODE:
4968 case XML_DOCUMENT_NODE:
4969 case XML_DOCUMENT_TYPE_NODE:
4970 case XML_DOCUMENT_FRAG_NODE:
4971 case XML_NOTATION_NODE:
4972 case XML_HTML_DOCUMENT_NODE:
4974 case XML_ELEMENT_DECL:
4975 case XML_ATTRIBUTE_DECL:
4976 case XML_ENTITY_DECL:
4978 case XML_ENTITY_REF_NODE:
4979 case XML_ENTITY_NODE:
4980 case XML_NAMESPACE_DECL:
4981 #ifdef LIBXML_DOCB_ENABLED
4982 case XML_DOCB_DOCUMENT_NODE:
4984 case XML_XINCLUDE_START:
4985 case XML_XINCLUDE_END:
4987 case XML_ELEMENT_NODE:
4988 case XML_ATTRIBUTE_NODE:
4991 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4994 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
4996 #endif /* LIBXML_TREE_ENABLED */
5000 * @cur: the node being checked
5002 * Searches the language of a node, i.e. the values of the xml:lang
5003 * attribute or the one carried by the nearest ancestor.
5005 * Returns a pointer to the lang value, or NULL if not found
5006 * It's up to the caller to free the memory with xmlFree().
5009 xmlNodeGetLang(const xmlNode *cur) {
5012 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
5014 while (cur != NULL) {
5015 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
5024 #ifdef LIBXML_TREE_ENABLED
5026 * xmlNodeSetSpacePreserve:
5027 * @cur: the node being changed
5028 * @val: the xml:space value ("0": default, 1: "preserve")
5030 * Set (or reset) the space preserving behaviour of a node, i.e. the
5031 * value of the xml:space attribute.
5034 xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
5037 if (cur == NULL) return;
5040 case XML_CDATA_SECTION_NODE:
5041 case XML_COMMENT_NODE:
5042 case XML_DOCUMENT_NODE:
5043 case XML_DOCUMENT_TYPE_NODE:
5044 case XML_DOCUMENT_FRAG_NODE:
5045 case XML_NOTATION_NODE:
5046 case XML_HTML_DOCUMENT_NODE:
5048 case XML_ELEMENT_DECL:
5049 case XML_ATTRIBUTE_DECL:
5050 case XML_ENTITY_DECL:
5052 case XML_ENTITY_REF_NODE:
5053 case XML_ENTITY_NODE:
5054 case XML_NAMESPACE_DECL:
5055 case XML_XINCLUDE_START:
5056 case XML_XINCLUDE_END:
5057 #ifdef LIBXML_DOCB_ENABLED
5058 case XML_DOCB_DOCUMENT_NODE:
5061 case XML_ELEMENT_NODE:
5062 case XML_ATTRIBUTE_NODE:
5065 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5070 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
5073 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
5077 #endif /* LIBXML_TREE_ENABLED */
5080 * xmlNodeGetSpacePreserve:
5081 * @cur: the node being checked
5083 * Searches the space preserving behaviour of a node, i.e. the values
5084 * of the xml:space attribute or the one carried by the nearest
5087 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
5090 xmlNodeGetSpacePreserve(const xmlNode *cur) {
5093 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
5095 while (cur != NULL) {
5096 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
5097 if (space != NULL) {
5098 if (xmlStrEqual(space, BAD_CAST "preserve")) {
5102 if (xmlStrEqual(space, BAD_CAST "default")) {
5113 #ifdef LIBXML_TREE_ENABLED
5116 * @cur: the node being changed
5117 * @name: the new tag name
5119 * Set (or reset) the name of a node.
5122 xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
5125 const xmlChar *freeme = NULL;
5127 if (cur == NULL) return;
5128 if (name == NULL) return;
5131 case XML_CDATA_SECTION_NODE:
5132 case XML_COMMENT_NODE:
5133 case XML_DOCUMENT_TYPE_NODE:
5134 case XML_DOCUMENT_FRAG_NODE:
5135 case XML_NOTATION_NODE:
5136 case XML_HTML_DOCUMENT_NODE:
5137 case XML_NAMESPACE_DECL:
5138 case XML_XINCLUDE_START:
5139 case XML_XINCLUDE_END:
5140 #ifdef LIBXML_DOCB_ENABLED
5141 case XML_DOCB_DOCUMENT_NODE:
5144 case XML_ELEMENT_NODE:
5145 case XML_ATTRIBUTE_NODE:
5147 case XML_ENTITY_REF_NODE:
5148 case XML_ENTITY_NODE:
5150 case XML_DOCUMENT_NODE:
5151 case XML_ELEMENT_DECL:
5152 case XML_ATTRIBUTE_DECL:
5153 case XML_ENTITY_DECL:
5162 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
5164 cur->name = xmlDictLookup(dict, name, -1);
5166 if (cur->name != NULL)
5168 cur->name = xmlStrdup(name);
5172 xmlFree((xmlChar *) freeme);
5176 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
5179 * @cur: the node being changed
5180 * @uri: the new base URI
5182 * Set (or reset) the base URI of a node, i.e. the value of the
5183 * xml:base attribute.
5186 xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
5190 if (cur == NULL) return;
5193 case XML_CDATA_SECTION_NODE:
5194 case XML_COMMENT_NODE:
5195 case XML_DOCUMENT_TYPE_NODE:
5196 case XML_DOCUMENT_FRAG_NODE:
5197 case XML_NOTATION_NODE:
5199 case XML_ELEMENT_DECL:
5200 case XML_ATTRIBUTE_DECL:
5201 case XML_ENTITY_DECL:
5203 case XML_ENTITY_REF_NODE:
5204 case XML_ENTITY_NODE:
5205 case XML_NAMESPACE_DECL:
5206 case XML_XINCLUDE_START:
5207 case XML_XINCLUDE_END:
5209 case XML_ELEMENT_NODE:
5210 case XML_ATTRIBUTE_NODE:
5212 case XML_DOCUMENT_NODE:
5213 #ifdef LIBXML_DOCB_ENABLED
5214 case XML_DOCB_DOCUMENT_NODE:
5216 case XML_HTML_DOCUMENT_NODE: {
5217 xmlDocPtr doc = (xmlDocPtr) cur;
5219 if (doc->URL != NULL)
5220 xmlFree((xmlChar *) doc->URL);
5224 doc->URL = xmlPathToURI(uri);
5229 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5232 fixed = xmlPathToURI(uri);
5233 if (fixed != NULL) {
5234 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
5237 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
5240 #endif /* LIBXML_TREE_ENABLED */
5244 * @doc: the document the node pertains to
5245 * @cur: the node being checked
5247 * Searches for the BASE URL. The code should work on both XML
5248 * and HTML document even if base mechanisms are completely different.
5249 * It returns the base as defined in RFC 2396 sections
5250 * 5.1.1. Base URI within Document Content
5252 * 5.1.2. Base URI from the Encapsulating Entity
5253 * However it does not return the document base (5.1.3), use
5254 * doc->URL in this case
5256 * Returns a pointer to the base URL, or NULL if not found
5257 * It's up to the caller to free the memory with xmlFree().
5260 xmlNodeGetBase(const xmlDoc *doc, const xmlNode *cur) {
5261 xmlChar *oldbase = NULL;
5262 xmlChar *base, *newbase;
5264 if ((cur == NULL) && (doc == NULL))
5266 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
5268 if (doc == NULL) doc = cur->doc;
5269 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
5270 cur = doc->children;
5271 while ((cur != NULL) && (cur->name != NULL)) {
5272 if (cur->type != XML_ELEMENT_NODE) {
5276 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5277 cur = cur->children;
5280 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5281 cur = cur->children;
5284 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5285 return(xmlGetProp(cur, BAD_CAST "href"));
5291 while (cur != NULL) {
5292 if (cur->type == XML_ENTITY_DECL) {
5293 xmlEntityPtr ent = (xmlEntityPtr) cur;
5294 return(xmlStrdup(ent->URI));
5296 if (cur->type == XML_ELEMENT_NODE) {
5297 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
5299 if (oldbase != NULL) {
5300 newbase = xmlBuildURI(oldbase, base);
5301 if (newbase != NULL) {
5313 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
5314 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
5315 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
5321 if ((doc != NULL) && (doc->URL != NULL)) {
5322 if (oldbase == NULL)
5323 return(xmlStrdup(doc->URL));
5324 newbase = xmlBuildURI(oldbase, doc->URL);
5332 * xmlNodeBufGetContent:
5334 * @cur: the node being read
5336 * Read the value of a node @cur, this can be either the text carried
5337 * directly by this node if it's a TEXT node or the aggregate string
5338 * of the values carried by this node child's (TEXT and ENTITY_REF).
5339 * Entity references are substituted.
5340 * Fills up the buffer @buffer with this value
5342 * Returns 0 in case of success and -1 in case of error.
5345 xmlNodeBufGetContent(xmlBufferPtr buffer, const xmlNode *cur)
5350 if ((cur == NULL) || (buffer == NULL)) return(-1);
5351 buf = xmlBufFromBuffer(buffer);
5352 ret = xmlBufGetNodeContent(buf, cur);
5353 buffer = xmlBufBackToBuffer(buf);
5354 if ((ret < 0) || (buffer == NULL))
5360 * xmlBufGetNodeContent:
5361 * @buf: a buffer xmlBufPtr
5362 * @cur: the node being read
5364 * Read the value of a node @cur, this can be either the text carried
5365 * directly by this node if it's a TEXT node or the aggregate string
5366 * of the values carried by this node child's (TEXT and ENTITY_REF).
5367 * Entity references are substituted.
5368 * Fills up the buffer @buf with this value
5370 * Returns 0 in case of success and -1 in case of error.
5373 xmlBufGetNodeContent(xmlBufPtr buf, const xmlNode *cur)
5375 if ((cur == NULL) || (buf == NULL)) return(-1);
5376 switch (cur->type) {
5377 case XML_CDATA_SECTION_NODE:
5379 xmlBufCat(buf, cur->content);
5381 case XML_DOCUMENT_FRAG_NODE:
5382 case XML_ELEMENT_NODE:{
5383 const xmlNode *tmp = cur;
5385 while (tmp != NULL) {
5386 switch (tmp->type) {
5387 case XML_CDATA_SECTION_NODE:
5389 if (tmp->content != NULL)
5390 xmlBufCat(buf, tmp->content);
5392 case XML_ENTITY_REF_NODE:
5393 xmlBufGetNodeContent(buf, tmp);
5401 if (tmp->children != NULL) {
5402 if (tmp->children->type != XML_ENTITY_DECL) {
5403 tmp = tmp->children;
5410 if (tmp->next != NULL) {
5423 if (tmp->next != NULL) {
5427 } while (tmp != NULL);
5431 case XML_ATTRIBUTE_NODE:{
5432 xmlAttrPtr attr = (xmlAttrPtr) cur;
5433 xmlNodePtr tmp = attr->children;
5435 while (tmp != NULL) {
5436 if (tmp->type == XML_TEXT_NODE)
5437 xmlBufCat(buf, tmp->content);
5439 xmlBufGetNodeContent(buf, tmp);
5444 case XML_COMMENT_NODE:
5446 xmlBufCat(buf, cur->content);
5448 case XML_ENTITY_REF_NODE:{
5452 /* lookup entity declaration */
5453 ent = xmlGetDocEntity(cur->doc, cur->name);
5457 /* an entity content can be any "well balanced chunk",
5458 * i.e. the result of the content [43] production:
5459 * http://www.w3.org/TR/REC-xml#NT-content
5460 * -> we iterate through child nodes and recursive call
5461 * xmlNodeGetContent() which handles all possible node types */
5462 tmp = ent->children;
5464 xmlBufGetNodeContent(buf, tmp);
5469 case XML_ENTITY_NODE:
5470 case XML_DOCUMENT_TYPE_NODE:
5471 case XML_NOTATION_NODE:
5473 case XML_XINCLUDE_START:
5474 case XML_XINCLUDE_END:
5476 case XML_DOCUMENT_NODE:
5477 #ifdef LIBXML_DOCB_ENABLED
5478 case XML_DOCB_DOCUMENT_NODE:
5480 case XML_HTML_DOCUMENT_NODE:
5481 cur = cur->children;
5482 while (cur!= NULL) {
5483 if ((cur->type == XML_ELEMENT_NODE) ||
5484 (cur->type == XML_TEXT_NODE) ||
5485 (cur->type == XML_CDATA_SECTION_NODE)) {
5486 xmlBufGetNodeContent(buf, cur);
5491 case XML_NAMESPACE_DECL:
5492 xmlBufCat(buf, ((xmlNsPtr) cur)->href);
5494 case XML_ELEMENT_DECL:
5495 case XML_ATTRIBUTE_DECL:
5496 case XML_ENTITY_DECL:
5503 * xmlNodeGetContent:
5504 * @cur: the node being read
5506 * Read the value of a node, this can be either the text carried
5507 * directly by this node if it's a TEXT node or the aggregate string
5508 * of the values carried by this node child's (TEXT and ENTITY_REF).
5509 * Entity references are substituted.
5510 * Returns a new #xmlChar * or NULL if no content is available.
5511 * It's up to the caller to free the memory with xmlFree().
5514 xmlNodeGetContent(const xmlNode *cur)
5518 switch (cur->type) {
5519 case XML_DOCUMENT_FRAG_NODE:
5520 case XML_ELEMENT_NODE:{
5524 buf = xmlBufCreateSize(64);
5527 xmlBufGetNodeContent(buf, cur);
5528 ret = xmlBufDetach(buf);
5532 case XML_ATTRIBUTE_NODE:
5533 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
5534 case XML_COMMENT_NODE:
5536 if (cur->content != NULL)
5537 return (xmlStrdup(cur->content));
5539 case XML_ENTITY_REF_NODE:{
5544 /* lookup entity declaration */
5545 ent = xmlGetDocEntity(cur->doc, cur->name);
5549 buf = xmlBufCreate();
5553 xmlBufGetNodeContent(buf, cur);
5555 ret = xmlBufDetach(buf);
5559 case XML_ENTITY_NODE:
5560 case XML_DOCUMENT_TYPE_NODE:
5561 case XML_NOTATION_NODE:
5563 case XML_XINCLUDE_START:
5564 case XML_XINCLUDE_END:
5566 case XML_DOCUMENT_NODE:
5567 #ifdef LIBXML_DOCB_ENABLED
5568 case XML_DOCB_DOCUMENT_NODE:
5570 case XML_HTML_DOCUMENT_NODE: {
5574 buf = xmlBufCreate();
5578 xmlBufGetNodeContent(buf, (xmlNodePtr) cur);
5580 ret = xmlBufDetach(buf);
5584 case XML_NAMESPACE_DECL: {
5587 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5590 case XML_ELEMENT_DECL:
5593 case XML_ATTRIBUTE_DECL:
5596 case XML_ENTITY_DECL:
5599 case XML_CDATA_SECTION_NODE:
5601 if (cur->content != NULL)
5602 return (xmlStrdup(cur->content));
5609 * xmlNodeSetContent:
5610 * @cur: the node being modified
5611 * @content: the new value of the content
5613 * Replace the content of a node.
5614 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5615 * references, but XML special chars need to be escaped first by using
5616 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5619 xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5622 xmlGenericError(xmlGenericErrorContext,
5623 "xmlNodeSetContent : node == NULL\n");
5627 switch (cur->type) {
5628 case XML_DOCUMENT_FRAG_NODE:
5629 case XML_ELEMENT_NODE:
5630 case XML_ATTRIBUTE_NODE:
5631 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5632 cur->children = xmlStringGetNodeList(cur->doc, content);
5633 UPDATE_LAST_CHILD_AND_PARENT(cur)
5636 case XML_CDATA_SECTION_NODE:
5637 case XML_ENTITY_REF_NODE:
5638 case XML_ENTITY_NODE:
5640 case XML_COMMENT_NODE:
5641 if ((cur->content != NULL) &&
5642 (cur->content != (xmlChar *) &(cur->properties))) {
5643 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5644 (xmlDictOwns(cur->doc->dict, cur->content))))
5645 xmlFree(cur->content);
5647 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5648 cur->last = cur->children = NULL;
5649 if (content != NULL) {
5650 cur->content = xmlStrdup(content);
5652 cur->content = NULL;
5653 cur->properties = NULL;
5656 case XML_DOCUMENT_NODE:
5657 case XML_HTML_DOCUMENT_NODE:
5658 case XML_DOCUMENT_TYPE_NODE:
5659 case XML_XINCLUDE_START:
5660 case XML_XINCLUDE_END:
5661 #ifdef LIBXML_DOCB_ENABLED
5662 case XML_DOCB_DOCUMENT_NODE:
5665 case XML_NOTATION_NODE:
5669 case XML_NAMESPACE_DECL:
5671 case XML_ELEMENT_DECL:
5674 case XML_ATTRIBUTE_DECL:
5677 case XML_ENTITY_DECL:
5683 #ifdef LIBXML_TREE_ENABLED
5685 * xmlNodeSetContentLen:
5686 * @cur: the node being modified
5687 * @content: the new value of the content
5688 * @len: the size of @content
5690 * Replace the content of a node.
5691 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5692 * references, but XML special chars need to be escaped first by using
5693 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5696 xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5699 xmlGenericError(xmlGenericErrorContext,
5700 "xmlNodeSetContentLen : node == NULL\n");
5704 switch (cur->type) {
5705 case XML_DOCUMENT_FRAG_NODE:
5706 case XML_ELEMENT_NODE:
5707 case XML_ATTRIBUTE_NODE:
5708 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5709 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5710 UPDATE_LAST_CHILD_AND_PARENT(cur)
5713 case XML_CDATA_SECTION_NODE:
5714 case XML_ENTITY_REF_NODE:
5715 case XML_ENTITY_NODE:
5717 case XML_COMMENT_NODE:
5718 case XML_NOTATION_NODE:
5719 if ((cur->content != NULL) &&
5720 (cur->content != (xmlChar *) &(cur->properties))) {
5721 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5722 (xmlDictOwns(cur->doc->dict, cur->content))))
5723 xmlFree(cur->content);
5725 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5726 cur->children = cur->last = NULL;
5727 if (content != NULL) {
5728 cur->content = xmlStrndup(content, len);
5730 cur->content = NULL;
5731 cur->properties = NULL;
5734 case XML_DOCUMENT_NODE:
5736 case XML_HTML_DOCUMENT_NODE:
5737 case XML_DOCUMENT_TYPE_NODE:
5738 case XML_NAMESPACE_DECL:
5739 case XML_XINCLUDE_START:
5740 case XML_XINCLUDE_END:
5741 #ifdef LIBXML_DOCB_ENABLED
5742 case XML_DOCB_DOCUMENT_NODE:
5745 case XML_ELEMENT_DECL:
5748 case XML_ATTRIBUTE_DECL:
5751 case XML_ENTITY_DECL:
5756 #endif /* LIBXML_TREE_ENABLED */
5759 * xmlNodeAddContentLen:
5760 * @cur: the node being modified
5761 * @content: extra content
5762 * @len: the size of @content
5764 * Append the extra substring to the node content.
5765 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5766 * raw text, so unescaped XML special chars are allowed, entity
5767 * references are not supported.
5770 xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5773 xmlGenericError(xmlGenericErrorContext,
5774 "xmlNodeAddContentLen : node == NULL\n");
5778 if (len <= 0) return;
5779 switch (cur->type) {
5780 case XML_DOCUMENT_FRAG_NODE:
5781 case XML_ELEMENT_NODE: {
5782 xmlNodePtr last, newNode, tmp;
5785 newNode = xmlNewTextLen(content, len);
5786 if (newNode != NULL) {
5787 tmp = xmlAddChild(cur, newNode);
5790 if ((last != NULL) && (last->next == newNode)) {
5791 xmlTextMerge(last, newNode);
5796 case XML_ATTRIBUTE_NODE:
5799 case XML_CDATA_SECTION_NODE:
5800 case XML_ENTITY_REF_NODE:
5801 case XML_ENTITY_NODE:
5803 case XML_COMMENT_NODE:
5804 case XML_NOTATION_NODE:
5805 if (content != NULL) {
5806 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5807 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5808 xmlDictOwns(cur->doc->dict, cur->content))) {
5809 cur->content = xmlStrncatNew(cur->content, content, len);
5810 cur->properties = NULL;
5814 cur->content = xmlStrncat(cur->content, content, len);
5816 case XML_DOCUMENT_NODE:
5818 case XML_HTML_DOCUMENT_NODE:
5819 case XML_DOCUMENT_TYPE_NODE:
5820 case XML_NAMESPACE_DECL:
5821 case XML_XINCLUDE_START:
5822 case XML_XINCLUDE_END:
5823 #ifdef LIBXML_DOCB_ENABLED
5824 case XML_DOCB_DOCUMENT_NODE:
5827 case XML_ELEMENT_DECL:
5828 case XML_ATTRIBUTE_DECL:
5829 case XML_ENTITY_DECL:
5835 * xmlNodeAddContent:
5836 * @cur: the node being modified
5837 * @content: extra content
5839 * Append the extra substring to the node content.
5840 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5841 * raw text, so unescaped XML special chars are allowed, entity
5842 * references are not supported.
5845 xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5850 xmlGenericError(xmlGenericErrorContext,
5851 "xmlNodeAddContent : node == NULL\n");
5855 if (content == NULL) return;
5856 len = xmlStrlen(content);
5857 xmlNodeAddContentLen(cur, content, len);
5862 * @first: the first text node
5863 * @second: the second text node being merged
5865 * Merge two text nodes into one
5866 * Returns the first text node augmented
5869 xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5870 if (first == NULL) return(second);
5871 if (second == NULL) return(first);
5872 if (first->type != XML_TEXT_NODE) return(first);
5873 if (second->type != XML_TEXT_NODE) return(first);
5874 if (second->name != first->name)
5876 xmlNodeAddContent(first, second->content);
5877 xmlUnlinkNode(second);
5878 xmlFreeNode(second);
5882 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
5885 * @doc: the document
5886 * @node: the current node
5888 * Search all the namespace applying to a given element.
5889 * Returns an NULL terminated array of all the #xmlNsPtr found
5890 * that need to be freed by the caller or NULL if no
5891 * namespace if defined
5894 xmlGetNsList(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node)
5897 xmlNsPtr *ret = NULL;
5902 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5905 while (node != NULL) {
5906 if (node->type == XML_ELEMENT_NODE) {
5908 while (cur != NULL) {
5911 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5914 xmlTreeErrMemory("getting namespace list");
5919 for (i = 0; i < nbns; i++) {
5920 if ((cur->prefix == ret[i]->prefix) ||
5921 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5925 if (nbns >= maxns) {
5927 ret = (xmlNsPtr *) xmlRealloc(ret,
5932 xmlTreeErrMemory("getting namespace list");
5943 node = node->parent;
5947 #endif /* LIBXML_TREE_ENABLED */
5950 * xmlTreeEnsureXMLDecl:
5953 * Ensures that there is an XML namespace declaration on the doc.
5955 * Returns the XML ns-struct or NULL on API and internal errors.
5958 xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5962 if (doc->oldNs != NULL)
5963 return (doc->oldNs);
5966 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5969 "allocating the XML namespace");
5972 memset(ns, 0, sizeof(xmlNs));
5973 ns->type = XML_LOCAL_NAMESPACE;
5974 ns->href = xmlStrdup(XML_XML_NAMESPACE);
5975 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5983 * @doc: the document
5984 * @node: the current node
5985 * @nameSpace: the namespace prefix
5987 * Search a Ns registered under a given name space for a document.
5988 * recurse on the parents until it finds the defined namespace
5989 * or return NULL otherwise.
5990 * @nameSpace can be NULL, this is a search for the default namespace.
5991 * We don't allow to cross entities boundaries. If you don't declare
5992 * the namespace within those you will be in troubles !!! A warning
5993 * is generated to cover this case.
5995 * Returns the namespace pointer or NULL.
5998 xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
6001 const xmlNode *orig = node;
6003 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL);
6004 if ((nameSpace != NULL) &&
6005 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
6006 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6008 * The XML-1.0 namespace is normally held on the root
6009 * element. In this case exceptionally create it on the
6012 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6014 xmlTreeErrMemory("searching namespace");
6017 memset(cur, 0, sizeof(xmlNs));
6018 cur->type = XML_LOCAL_NAMESPACE;
6019 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6020 cur->prefix = xmlStrdup((const xmlChar *)"xml");
6021 cur->next = node->nsDef;
6031 * Return the XML namespace declaration held by the doc.
6033 if (doc->oldNs == NULL)
6034 return(xmlTreeEnsureXMLDecl(doc));
6038 while (node != NULL) {
6039 if ((node->type == XML_ENTITY_REF_NODE) ||
6040 (node->type == XML_ENTITY_NODE) ||
6041 (node->type == XML_ENTITY_DECL))
6043 if (node->type == XML_ELEMENT_NODE) {
6045 while (cur != NULL) {
6046 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6047 (cur->href != NULL))
6049 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6050 (cur->href != NULL) &&
6051 (xmlStrEqual(cur->prefix, nameSpace)))
6058 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6059 (cur->href != NULL))
6061 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6062 (cur->href != NULL) &&
6063 (xmlStrEqual(cur->prefix, nameSpace)))
6068 node = node->parent;
6075 * @doc: the document
6076 * @node: the current node
6077 * @ancestor: the ancestor carrying the namespace
6078 * @prefix: the namespace prefix
6080 * Verify that the given namespace held on @ancestor is still in scope
6083 * Returns 1 if true, 0 if false and -1 in case of error.
6086 xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
6087 xmlNodePtr ancestor, const xmlChar * prefix)
6091 while ((node != NULL) && (node != ancestor)) {
6092 if ((node->type == XML_ENTITY_REF_NODE) ||
6093 (node->type == XML_ENTITY_NODE) ||
6094 (node->type == XML_ENTITY_DECL))
6096 if (node->type == XML_ELEMENT_NODE) {
6098 while (tst != NULL) {
6099 if ((tst->prefix == NULL)
6100 && (prefix == NULL))
6102 if ((tst->prefix != NULL)
6104 && (xmlStrEqual(tst->prefix, prefix)))
6109 node = node->parent;
6111 if (node != ancestor)
6117 * xmlSearchNsByHref:
6118 * @doc: the document
6119 * @node: the current node
6120 * @href: the namespace value
6122 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
6123 * the defined namespace or return NULL otherwise.
6124 * Returns the namespace pointer or NULL.
6127 xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
6130 xmlNodePtr orig = node;
6133 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL))
6135 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
6137 * Only the document can hold the XML spec namespace.
6139 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6141 * The XML-1.0 namespace is normally held on the root
6142 * element. In this case exceptionally create it on the
6145 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6147 xmlTreeErrMemory("searching namespace");
6150 memset(cur, 0, sizeof(xmlNs));
6151 cur->type = XML_LOCAL_NAMESPACE;
6152 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6153 cur->prefix = xmlStrdup((const xmlChar *) "xml");
6154 cur->next = node->nsDef;
6164 * Return the XML namespace declaration held by the doc.
6166 if (doc->oldNs == NULL)
6167 return(xmlTreeEnsureXMLDecl(doc));
6171 is_attr = (node->type == XML_ATTRIBUTE_NODE);
6172 while (node != NULL) {
6173 if ((node->type == XML_ENTITY_REF_NODE) ||
6174 (node->type == XML_ENTITY_NODE) ||
6175 (node->type == XML_ENTITY_DECL))
6177 if (node->type == XML_ELEMENT_NODE) {
6179 while (cur != NULL) {
6180 if ((cur->href != NULL) && (href != NULL) &&
6181 (xmlStrEqual(cur->href, href))) {
6182 if (((!is_attr) || (cur->prefix != NULL)) &&
6183 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6191 if ((cur->href != NULL) && (href != NULL) &&
6192 (xmlStrEqual(cur->href, href))) {
6193 if (((!is_attr) || (cur->prefix != NULL)) &&
6194 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6200 node = node->parent;
6206 * xmlNewReconciliedNs:
6207 * @doc: the document
6208 * @tree: a node expected to hold the new namespace
6209 * @ns: the original namespace
6211 * This function tries to locate a namespace definition in a tree
6212 * ancestors, or create a new namespace definition node similar to
6213 * @ns trying to reuse the same prefix. However if the given prefix is
6214 * null (default namespace) or reused within the subtree defined by
6215 * @tree or on one of its ancestors then a new prefix is generated.
6216 * Returns the (new) namespace definition or NULL in case of error
6219 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
6224 if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) {
6226 xmlGenericError(xmlGenericErrorContext,
6227 "xmlNewReconciliedNs : tree == NULL\n");
6231 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
6233 xmlGenericError(xmlGenericErrorContext,
6234 "xmlNewReconciliedNs : ns == NULL\n");
6239 * Search an existing namespace definition inherited.
6241 def = xmlSearchNsByHref(doc, tree, ns->href);
6246 * Find a close prefix which is not already in use.
6247 * Let's strip namespace prefixes longer than 20 chars !
6249 if (ns->prefix == NULL)
6250 snprintf((char *) prefix, sizeof(prefix), "default");
6252 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
6254 def = xmlSearchNs(doc, tree, prefix);
6255 while (def != NULL) {
6256 if (counter > 1000) return(NULL);
6257 if (ns->prefix == NULL)
6258 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
6260 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
6261 (char *)ns->prefix, counter++);
6262 def = xmlSearchNs(doc, tree, prefix);
6266 * OK, now we are ready to create a new one.
6268 def = xmlNewNs(tree, ns->href, prefix);
6272 #ifdef LIBXML_TREE_ENABLED
6274 * xmlReconciliateNs:
6275 * @doc: the document
6276 * @tree: a node defining the subtree to reconciliate
6278 * This function checks that all the namespaces declared within the given
6279 * tree are properly declared. This is needed for example after Copy or Cut
6280 * and then paste operations. The subtree may still hold pointers to
6281 * namespace declarations outside the subtree or invalid/masked. As much
6282 * as possible the function try to reuse the existing namespaces found in
6283 * the new environment. If not possible the new namespaces are redeclared
6284 * on @tree at the top of the given subtree.
6285 * Returns the number of namespace declarations created or -1 in case of error.
6288 xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
6289 xmlNsPtr *oldNs = NULL;
6290 xmlNsPtr *newNs = NULL;
6295 xmlNodePtr node = tree;
6299 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6300 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
6301 if (node->doc != doc) return(-1);
6302 while (node != NULL) {
6304 * Reconciliate the node namespace
6306 if (node->ns != NULL) {
6308 * initialize the cache if needed
6310 if (sizeCache == 0) {
6312 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6314 if (oldNs == NULL) {
6315 xmlTreeErrMemory("fixing namespaces");
6318 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6320 if (newNs == NULL) {
6321 xmlTreeErrMemory("fixing namespaces");
6326 for (i = 0;i < nbCache;i++) {
6327 if (oldNs[i] == node->ns) {
6328 node->ns = newNs[i];
6334 * OK we need to recreate a new namespace definition
6336 n = xmlNewReconciliedNs(doc, tree, node->ns);
6337 if (n != NULL) { /* :-( what if else ??? */
6339 * check if we need to grow the cache buffers.
6341 if (sizeCache <= nbCache) {
6343 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
6345 if (oldNs == NULL) {
6346 xmlTreeErrMemory("fixing namespaces");
6350 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
6352 if (newNs == NULL) {
6353 xmlTreeErrMemory("fixing namespaces");
6359 oldNs[nbCache++] = node->ns;
6365 * now check for namespace hold by attributes on the node.
6367 if (node->type == XML_ELEMENT_NODE) {
6368 attr = node->properties;
6369 while (attr != NULL) {
6370 if (attr->ns != NULL) {
6372 * initialize the cache if needed
6374 if (sizeCache == 0) {
6376 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6378 if (oldNs == NULL) {
6379 xmlTreeErrMemory("fixing namespaces");
6382 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6384 if (newNs == NULL) {
6385 xmlTreeErrMemory("fixing namespaces");
6390 for (i = 0;i < nbCache;i++) {
6391 if (oldNs[i] == attr->ns) {
6392 attr->ns = newNs[i];
6398 * OK we need to recreate a new namespace definition
6400 n = xmlNewReconciliedNs(doc, tree, attr->ns);
6401 if (n != NULL) { /* :-( what if else ??? */
6403 * check if we need to grow the cache buffers.
6405 if (sizeCache <= nbCache) {
6407 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
6408 sizeCache * sizeof(xmlNsPtr));
6409 if (oldNs == NULL) {
6410 xmlTreeErrMemory("fixing namespaces");
6414 newNs = (xmlNsPtr *) xmlRealloc(newNs,
6415 sizeCache * sizeof(xmlNsPtr));
6416 if (newNs == NULL) {
6417 xmlTreeErrMemory("fixing namespaces");
6423 oldNs[nbCache++] = attr->ns;
6433 * Browse the full subtree, deep first
6435 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
6437 node = node->children;
6438 } else if ((node != tree) && (node->next != NULL)) {
6441 } else if (node != tree) {
6442 /* go up to parents->next if needed */
6443 while (node != tree) {
6444 if (node->parent != NULL)
6445 node = node->parent;
6446 if ((node != tree) && (node->next != NULL)) {
6450 if (node->parent == NULL) {
6455 /* exit condition */
6467 #endif /* LIBXML_TREE_ENABLED */
6470 xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name,
6471 const xmlChar *nsName, int useDTD)
6475 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6478 if (node->properties != NULL) {
6479 prop = node->properties;
6480 if (nsName == NULL) {
6482 * We want the attr to be in no namespace.
6485 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6489 } while (prop != NULL);
6492 * We want the attr to be in the specified namespace.
6495 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6496 ((prop->ns->href == nsName) ||
6497 xmlStrEqual(prop->ns->href, nsName)))
6502 } while (prop != NULL);
6506 #ifdef LIBXML_TREE_ENABLED
6510 * Check if there is a default/fixed attribute declaration in
6511 * the internal or external subset.
6513 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6514 xmlDocPtr doc = node->doc;
6515 xmlAttributePtr attrDecl = NULL;
6516 xmlChar *elemQName, *tmpstr = NULL;
6519 * We need the QName of the element for the DTD-lookup.
6521 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6522 tmpstr = xmlStrdup(node->ns->prefix);
6523 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6524 tmpstr = xmlStrcat(tmpstr, node->name);
6529 elemQName = (xmlChar *) node->name;
6530 if (nsName == NULL) {
6532 * The common and nice case: Attr in no namespace.
6534 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6535 elemQName, name, NULL);
6536 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6537 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6538 elemQName, name, NULL);
6541 xmlNsPtr *nsList, *cur;
6544 * The ugly case: Search using the prefixes of in-scope
6545 * ns-decls corresponding to @nsName.
6547 nsList = xmlGetNsList(node->doc, node);
6548 if (nsList == NULL) {
6554 while (*cur != NULL) {
6555 if (xmlStrEqual((*cur)->href, nsName)) {
6556 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6557 name, (*cur)->prefix);
6560 if (doc->extSubset != NULL) {
6561 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6562 name, (*cur)->prefix);
6574 * Only default/fixed attrs are relevant.
6576 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6577 return((xmlAttrPtr) attrDecl);
6579 #endif /* LIBXML_TREE_ENABLED */
6584 xmlGetPropNodeValueInternal(const xmlAttr *prop)
6588 if (prop->type == XML_ATTRIBUTE_NODE) {
6590 * Note that we return at least the empty string.
6591 * TODO: Do we really always want that?
6593 if (prop->children != NULL) {
6594 if ((prop->children->next == NULL) &&
6595 ((prop->children->type == XML_TEXT_NODE) ||
6596 (prop->children->type == XML_CDATA_SECTION_NODE)))
6599 * Optimization for the common case: only 1 text node.
6601 return(xmlStrdup(prop->children->content));
6605 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6610 return(xmlStrdup((xmlChar *)""));
6611 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6612 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6620 * @name: the attribute name
6622 * Search an attribute associated to a node
6623 * This function also looks in DTD attribute declaration for #FIXED or
6624 * default declaration values unless DTD use has been turned off.
6626 * Returns the attribute or the attribute declaration or NULL if
6627 * neither was found.
6630 xmlHasProp(const xmlNode *node, const xmlChar *name) {
6634 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6637 * Check on the properties attached to the node
6639 prop = node->properties;
6640 while (prop != NULL) {
6641 if (xmlStrEqual(prop->name, name)) {
6646 if (!xmlCheckDTD) return(NULL);
6649 * Check if there is a default declaration in the internal
6650 * or external subsets
6654 xmlAttributePtr attrDecl;
6655 if (doc->intSubset != NULL) {
6656 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6657 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6658 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6659 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6660 /* return attribute declaration only if a default value is given
6661 (that includes #FIXED declarations) */
6662 return((xmlAttrPtr) attrDecl);
6671 * @name: the attribute name
6672 * @nameSpace: the URI of the namespace
6674 * Search for an attribute associated to a node
6675 * This attribute has to be anchored in the namespace specified.
6676 * This does the entity substitution.
6677 * This function looks in DTD attribute declaration for #FIXED or
6678 * default declaration values unless DTD use has been turned off.
6679 * Note that a namespace of NULL indicates to use the default namespace.
6681 * Returns the attribute or the attribute declaration or NULL
6682 * if neither was found.
6685 xmlHasNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
6687 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
6693 * @name: the attribute name
6695 * Search and get the value of an attribute associated to a node
6696 * This does the entity substitution.
6697 * This function looks in DTD attribute declaration for #FIXED or
6698 * default declaration values unless DTD use has been turned off.
6699 * NOTE: this function acts independently of namespaces associated
6700 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6701 * for namespace aware processing.
6703 * Returns the attribute value or NULL if not found.
6704 * It's up to the caller to free the memory with xmlFree().
6707 xmlGetProp(const xmlNode *node, const xmlChar *name) {
6710 prop = xmlHasProp(node, name);
6713 return(xmlGetPropNodeValueInternal(prop));
6719 * @name: the attribute name
6721 * Search and get the value of an attribute associated to a node
6722 * This does the entity substitution.
6723 * This function looks in DTD attribute declaration for #FIXED or
6724 * default declaration values unless DTD use has been turned off.
6725 * This function is similar to xmlGetProp except it will accept only
6726 * an attribute in no namespace.
6728 * Returns the attribute value or NULL if not found.
6729 * It's up to the caller to free the memory with xmlFree().
6732 xmlGetNoNsProp(const xmlNode *node, const xmlChar *name) {
6735 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6738 return(xmlGetPropNodeValueInternal(prop));
6744 * @name: the attribute name
6745 * @nameSpace: the URI of the namespace
6747 * Search and get the value of an attribute associated to a node
6748 * This attribute has to be anchored in the namespace specified.
6749 * This does the entity substitution.
6750 * This function looks in DTD attribute declaration for #FIXED or
6751 * default declaration values unless DTD use has been turned off.
6753 * Returns the attribute value or NULL if not found.
6754 * It's up to the caller to free the memory with xmlFree().
6757 xmlGetNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
6760 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6763 return(xmlGetPropNodeValueInternal(prop));
6766 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6770 * @name: the attribute name
6772 * Remove an attribute carried by a node.
6773 * This handles only attributes in no namespace.
6774 * Returns 0 if successful, -1 if not found
6777 xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
6780 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6783 xmlUnlinkNode((xmlNodePtr) prop);
6791 * @ns: the namespace definition
6792 * @name: the attribute name
6794 * Remove an attribute carried by a node.
6795 * Returns 0 if successful, -1 if not found
6798 xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
6801 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6804 xmlUnlinkNode((xmlNodePtr) prop);
6810 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
6814 * @name: the attribute name (a QName)
6815 * @value: the attribute value
6817 * Set (or reset) an attribute carried by a node.
6818 * If @name has a prefix, then the corresponding
6819 * namespace-binding will be used, if in scope; it is an
6820 * error it there's no such ns-binding for the prefix in
6822 * Returns the attribute pointer.
6826 xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
6828 const xmlChar *nqname;
6830 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
6836 nqname = xmlSplitQName3(name, &len);
6837 if (nqname != NULL) {
6839 xmlChar *prefix = xmlStrndup(name, len);
6840 ns = xmlSearchNs(node->doc, node, prefix);
6844 return(xmlSetNsProp(node, ns, nqname, value));
6846 return(xmlSetNsProp(node, NULL, name, value));
6852 * @ns: the namespace definition
6853 * @name: the attribute name
6854 * @value: the attribute value
6856 * Set (or reset) an attribute carried by a node.
6857 * The ns structure must be in scope, this is not checked
6859 * Returns the attribute pointer.
6862 xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6863 const xmlChar *value)
6867 if (ns && (ns->href == NULL))
6869 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6872 * Modify the attribute's value.
6874 if (prop->atype == XML_ATTRIBUTE_ID) {
6875 xmlRemoveID(node->doc, prop);
6876 prop->atype = XML_ATTRIBUTE_ID;
6878 if (prop->children != NULL)
6879 xmlFreeNodeList(prop->children);
6880 prop->children = NULL;
6883 if (value != NULL) {
6886 if(!xmlCheckUTF8(value)) {
6887 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc,
6889 if (node->doc != NULL)
6890 node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
6892 prop->children = xmlNewDocText(node->doc, value);
6894 tmp = prop->children;
6895 while (tmp != NULL) {
6896 tmp->parent = (xmlNodePtr) prop;
6897 if (tmp->next == NULL)
6902 if (prop->atype == XML_ATTRIBUTE_ID)
6903 xmlAddID(NULL, node->doc, value, prop);
6907 * No equal attr found; create a new one.
6909 return(xmlNewPropInternal(node, ns, name, value, 0));
6912 #endif /* LIBXML_TREE_ENABLED */
6918 * Is this node a Text node ?
6919 * Returns 1 yes, 0 no
6922 xmlNodeIsText(const xmlNode *node) {
6923 if (node == NULL) return(0);
6925 if (node->type == XML_TEXT_NODE) return(1);
6933 * Checks whether this node is an empty or whitespace only
6934 * (and possibly ignorable) text-node.
6936 * Returns 1 yes, 0 no
6939 xmlIsBlankNode(const xmlNode *node) {
6941 if (node == NULL) return(0);
6943 if ((node->type != XML_TEXT_NODE) &&
6944 (node->type != XML_CDATA_SECTION_NODE))
6946 if (node->content == NULL) return(1);
6947 cur = node->content;
6949 if (!IS_BLANK_CH(*cur)) return(0);
6959 * @content: the content
6960 * @len: @content length
6962 * Concat the given string at the end of the existing node content
6964 * Returns -1 in case of error, 0 otherwise
6968 xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
6969 if (node == NULL) return(-1);
6971 if ((node->type != XML_TEXT_NODE) &&
6972 (node->type != XML_CDATA_SECTION_NODE) &&
6973 (node->type != XML_COMMENT_NODE) &&
6974 (node->type != XML_PI_NODE)) {
6976 xmlGenericError(xmlGenericErrorContext,
6977 "xmlTextConcat: node is not text nor CDATA\n");
6981 /* need to check if content is currently in the dictionary */
6982 if ((node->content == (xmlChar *) &(node->properties)) ||
6983 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6984 xmlDictOwns(node->doc->dict, node->content))) {
6985 node->content = xmlStrncatNew(node->content, content, len);
6987 node->content = xmlStrncat(node->content, content, len);
6989 node->properties = NULL;
6990 if (node->content == NULL)
6995 /************************************************************************
6997 * Output : to a FILE or in memory *
6999 ************************************************************************/
7004 * routine to create an XML buffer.
7005 * returns the new structure.
7008 xmlBufferCreate(void) {
7011 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7013 xmlTreeErrMemory("creating buffer");
7017 ret->size = xmlDefaultBufferSize;
7018 ret->alloc = xmlBufferAllocScheme;
7019 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
7020 if (ret->content == NULL) {
7021 xmlTreeErrMemory("creating buffer");
7025 ret->content[0] = 0;
7026 ret->contentIO = NULL;
7031 * xmlBufferCreateSize:
7032 * @size: initial size of buffer
7034 * routine to create an XML buffer.
7035 * returns the new structure.
7038 xmlBufferCreateSize(size_t size) {
7041 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7043 xmlTreeErrMemory("creating buffer");
7047 ret->alloc = xmlBufferAllocScheme;
7048 ret->size = (size ? size+2 : 0); /* +1 for ending null */
7050 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
7051 if (ret->content == NULL) {
7052 xmlTreeErrMemory("creating buffer");
7056 ret->content[0] = 0;
7058 ret->content = NULL;
7059 ret->contentIO = NULL;
7067 * Remove the string contained in a buffer and gie it back to the
7068 * caller. The buffer is reset to an empty content.
7069 * This doesn't work with immutable buffers as they can't be reset.
7071 * Returns the previous string contained by the buffer.
7074 xmlBufferDetach(xmlBufferPtr buf) {
7079 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
7083 buf->content = NULL;
7092 * xmlBufferCreateStatic:
7093 * @mem: the memory area
7094 * @size: the size in byte
7096 * routine to create an XML buffer from an immutable memory area.
7097 * The area won't be modified nor copied, and is expected to be
7098 * present until the end of the buffer lifetime.
7100 * returns the new structure.
7103 xmlBufferCreateStatic(void *mem, size_t size) {
7106 if ((mem == NULL) || (size == 0))
7109 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7111 xmlTreeErrMemory("creating buffer");
7116 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
7117 ret->content = (xmlChar *) mem;
7122 * xmlBufferSetAllocationScheme:
7123 * @buf: the buffer to tune
7124 * @scheme: allocation scheme to use
7126 * Sets the allocation scheme for this buffer
7129 xmlBufferSetAllocationScheme(xmlBufferPtr buf,
7130 xmlBufferAllocationScheme scheme) {
7133 xmlGenericError(xmlGenericErrorContext,
7134 "xmlBufferSetAllocationScheme: buf == NULL\n");
7138 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7139 (buf->alloc == XML_BUFFER_ALLOC_IO)) return;
7140 if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
7141 (scheme == XML_BUFFER_ALLOC_EXACT) ||
7142 (scheme == XML_BUFFER_ALLOC_HYBRID) ||
7143 (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
7144 buf->alloc = scheme;
7149 * @buf: the buffer to free
7151 * Frees an XML buffer. It frees both the content and the structure which
7155 xmlBufferFree(xmlBufferPtr buf) {
7158 xmlGenericError(xmlGenericErrorContext,
7159 "xmlBufferFree: buf == NULL\n");
7164 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7165 (buf->contentIO != NULL)) {
7166 xmlFree(buf->contentIO);
7167 } else if ((buf->content != NULL) &&
7168 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
7169 xmlFree(buf->content);
7181 xmlBufferEmpty(xmlBufferPtr buf) {
7182 if (buf == NULL) return;
7183 if (buf->content == NULL) return;
7185 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
7186 buf->content = BAD_CAST "";
7187 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7188 (buf->contentIO != NULL)) {
7189 size_t start_buf = buf->content - buf->contentIO;
7191 buf->size += start_buf;
7192 buf->content = buf->contentIO;
7193 buf->content[0] = 0;
7195 buf->content[0] = 0;
7201 * @buf: the buffer to dump
7202 * @len: the number of xmlChar to remove
7204 * Remove the beginning of an XML buffer.
7206 * Returns the number of #xmlChar removed, or -1 in case of failure.
7209 xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
7210 if (buf == NULL) return(-1);
7211 if (len == 0) return(0);
7212 if (len > buf->use) return(-1);
7215 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7216 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
7218 * we just move the content pointer, but also make sure
7219 * the perceived buffer size has shrinked accordingly
7221 buf->content += len;
7225 * sometimes though it maybe be better to really shrink
7228 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7229 size_t start_buf = buf->content - buf->contentIO;
7230 if (start_buf >= buf->size) {
7231 memmove(buf->contentIO, &buf->content[0], buf->use);
7232 buf->content = buf->contentIO;
7233 buf->content[buf->use] = 0;
7234 buf->size += start_buf;
7238 memmove(buf->content, &buf->content[len], buf->use);
7239 buf->content[buf->use] = 0;
7247 * @len: the minimum free size to allocate
7249 * Grow the available space of an XML buffer.
7251 * Returns the new available space or -1 in case of error
7254 xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
7258 if (buf == NULL) return(-1);
7260 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7261 if (len + buf->use < buf->size) return(0);
7264 * Windows has a BIG problem on realloc timing, so we try to double
7265 * the buffer size (if that's enough) (bug 146697)
7266 * Apparently BSD too, and it's probably best for linux too
7267 * On an embedded system this may be something to change
7270 if (buf->size > len)
7271 size = buf->size * 2;
7273 size = buf->use + len + 100;
7275 size = buf->use + len + 100;
7278 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7279 size_t start_buf = buf->content - buf->contentIO;
7281 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
7282 if (newbuf == NULL) {
7283 xmlTreeErrMemory("growing buffer");
7286 buf->contentIO = newbuf;
7287 buf->content = newbuf + start_buf;
7289 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
7290 if (newbuf == NULL) {
7291 xmlTreeErrMemory("growing buffer");
7294 buf->content = newbuf;
7297 return(buf->size - buf->use);
7302 * @file: the file output
7303 * @buf: the buffer to dump
7305 * Dumps an XML buffer to a FILE *.
7306 * Returns the number of #xmlChar written
7309 xmlBufferDump(FILE *file, xmlBufferPtr buf) {
7314 xmlGenericError(xmlGenericErrorContext,
7315 "xmlBufferDump: buf == NULL\n");
7319 if (buf->content == NULL) {
7321 xmlGenericError(xmlGenericErrorContext,
7322 "xmlBufferDump: buf->content == NULL\n");
7328 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
7336 * Function to extract the content of a buffer
7338 * Returns the internal content
7342 xmlBufferContent(const xmlBuffer *buf)
7347 return buf->content;
7354 * Function to get the length of a buffer
7356 * Returns the length of data in the internal content
7360 xmlBufferLength(const xmlBuffer *buf)
7370 * @buf: the buffer to resize
7371 * @size: the desired size
7373 * Resize a buffer to accommodate minimum size of @size.
7375 * Returns 0 in case of problems, 1 otherwise
7378 xmlBufferResize(xmlBufferPtr buf, unsigned int size)
7380 unsigned int newSize;
7381 xmlChar* rebuf = NULL;
7387 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7389 /* Don't resize if we don't have to */
7390 if (size < buf->size)
7393 /* figure out new size */
7394 switch (buf->alloc){
7395 case XML_BUFFER_ALLOC_IO:
7396 case XML_BUFFER_ALLOC_DOUBLEIT:
7397 /*take care of empty case*/
7398 newSize = (buf->size ? buf->size*2 : size + 10);
7399 while (size > newSize) {
7400 if (newSize > UINT_MAX / 2) {
7401 xmlTreeErrMemory("growing buffer");
7407 case XML_BUFFER_ALLOC_EXACT:
7410 case XML_BUFFER_ALLOC_HYBRID:
7411 if (buf->use < BASE_BUFFER_SIZE)
7414 newSize = buf->size * 2;
7415 while (size > newSize) {
7416 if (newSize > UINT_MAX / 2) {
7417 xmlTreeErrMemory("growing buffer");
7430 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7431 start_buf = buf->content - buf->contentIO;
7433 if (start_buf > newSize) {
7434 /* move data back to start */
7435 memmove(buf->contentIO, buf->content, buf->use);
7436 buf->content = buf->contentIO;
7437 buf->content[buf->use] = 0;
7438 buf->size += start_buf;
7440 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
7441 if (rebuf == NULL) {
7442 xmlTreeErrMemory("growing buffer");
7445 buf->contentIO = rebuf;
7446 buf->content = rebuf + start_buf;
7449 if (buf->content == NULL) {
7450 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7451 } else if (buf->size - buf->use < 100) {
7452 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
7455 * if we are reallocating a buffer far from being full, it's
7456 * better to make a new allocation and copy only the used range
7457 * and free the old one.
7459 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7460 if (rebuf != NULL) {
7461 memcpy(rebuf, buf->content, buf->use);
7462 xmlFree(buf->content);
7463 rebuf[buf->use] = 0;
7466 if (rebuf == NULL) {
7467 xmlTreeErrMemory("growing buffer");
7470 buf->content = rebuf;
7472 buf->size = newSize;
7479 * @buf: the buffer to dump
7480 * @str: the #xmlChar string
7481 * @len: the number of #xmlChar to add
7483 * Add a string range to an XML buffer. if len == -1, the length of
7484 * str is recomputed.
7486 * Returns 0 successful, a positive error code number otherwise
7487 * and -1 in case of internal or API error.
7490 xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
7491 unsigned int needSize;
7493 if ((str == NULL) || (buf == NULL)) {
7496 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7499 xmlGenericError(xmlGenericErrorContext,
7500 "xmlBufferAdd: len < 0\n");
7504 if (len == 0) return 0;
7507 len = xmlStrlen(str);
7509 if (len < 0) return -1;
7510 if (len == 0) return 0;
7512 needSize = buf->use + len + 2;
7513 if (needSize > buf->size){
7514 if (!xmlBufferResize(buf, needSize)){
7515 xmlTreeErrMemory("growing buffer");
7516 return XML_ERR_NO_MEMORY;
7520 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
7522 buf->content[buf->use] = 0;
7529 * @str: the #xmlChar string
7530 * @len: the number of #xmlChar to add
7532 * Add a string range to the beginning of an XML buffer.
7533 * if len == -1, the length of @str is recomputed.
7535 * Returns 0 successful, a positive error code number otherwise
7536 * and -1 in case of internal or API error.
7539 xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7540 unsigned int needSize;
7544 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7547 xmlGenericError(xmlGenericErrorContext,
7548 "xmlBufferAddHead: str == NULL\n");
7554 xmlGenericError(xmlGenericErrorContext,
7555 "xmlBufferAddHead: len < 0\n");
7559 if (len == 0) return 0;
7562 len = xmlStrlen(str);
7564 if (len <= 0) return -1;
7566 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7567 size_t start_buf = buf->content - buf->contentIO;
7569 if (start_buf > (unsigned int) len) {
7571 * We can add it in the space previously shrinked
7573 buf->content -= len;
7574 memmove(&buf->content[0], str, len);
7580 needSize = buf->use + len + 2;
7581 if (needSize > buf->size){
7582 if (!xmlBufferResize(buf, needSize)){
7583 xmlTreeErrMemory("growing buffer");
7584 return XML_ERR_NO_MEMORY;
7588 memmove(&buf->content[len], &buf->content[0], buf->use);
7589 memmove(&buf->content[0], str, len);
7591 buf->content[buf->use] = 0;
7597 * @buf: the buffer to add to
7598 * @str: the #xmlChar string
7600 * Append a zero terminated string to an XML buffer.
7602 * Returns 0 successful, a positive error code number otherwise
7603 * and -1 in case of internal or API error.
7606 xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
7609 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7610 if (str == NULL) return -1;
7611 return xmlBufferAdd(buf, str, -1);
7616 * @buf: the buffer to dump
7617 * @str: the C char string
7619 * Append a zero terminated C string to an XML buffer.
7621 * Returns 0 successful, a positive error code number otherwise
7622 * and -1 in case of internal or API error.
7625 xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7630 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7633 xmlGenericError(xmlGenericErrorContext,
7634 "xmlBufferCCat: str == NULL\n");
7638 for (cur = str;*cur != 0;cur++) {
7639 if (buf->use + 10 >= buf->size) {
7640 if (!xmlBufferResize(buf, buf->use+10)){
7641 xmlTreeErrMemory("growing buffer");
7642 return XML_ERR_NO_MEMORY;
7645 buf->content[buf->use++] = *cur;
7647 buf->content[buf->use] = 0;
7652 * xmlBufferWriteCHAR:
7653 * @buf: the XML buffer
7654 * @string: the string to add
7656 * routine which manages and grows an output buffer. This one adds
7657 * xmlChars at the end of the buffer.
7660 xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
7663 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7664 xmlBufferCat(buf, string);
7668 * xmlBufferWriteChar:
7669 * @buf: the XML buffer output
7670 * @string: the string to add
7672 * routine which manage and grows an output buffer. This one add
7673 * C chars at the end of the array.
7676 xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
7679 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7680 xmlBufferCCat(buf, string);
7685 * xmlBufferWriteQuotedString:
7686 * @buf: the XML buffer output
7687 * @string: the string to add
7689 * routine which manage and grows an output buffer. This one writes
7690 * a quoted or double quoted #xmlChar string, checking first if it holds
7691 * quote or double-quotes internally
7694 xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
7695 const xmlChar *cur, *base;
7698 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7699 if (xmlStrchr(string, '\"')) {
7700 if (xmlStrchr(string, '\'')) {
7702 xmlGenericError(xmlGenericErrorContext,
7703 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7705 xmlBufferCCat(buf, "\"");
7706 base = cur = string;
7710 xmlBufferAdd(buf, base, cur - base);
7711 xmlBufferAdd(buf, BAD_CAST """, 6);
7720 xmlBufferAdd(buf, base, cur - base);
7721 xmlBufferCCat(buf, "\"");
7724 xmlBufferCCat(buf, "\'");
7725 xmlBufferCat(buf, string);
7726 xmlBufferCCat(buf, "\'");
7729 xmlBufferCCat(buf, "\"");
7730 xmlBufferCat(buf, string);
7731 xmlBufferCCat(buf, "\"");
7737 * xmlGetDocCompressMode:
7738 * @doc: the document
7740 * get the compression ratio for a document, ZLIB based
7741 * Returns 0 (uncompressed) to 9 (max compression)
7744 xmlGetDocCompressMode (const xmlDoc *doc) {
7745 if (doc == NULL) return(-1);
7746 return(doc->compression);
7750 * xmlSetDocCompressMode:
7751 * @doc: the document
7752 * @mode: the compression ratio
7754 * set the compression ratio for a document, ZLIB based
7755 * Correct values: 0 (uncompressed) to 9 (max compression)
7758 xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7759 if (doc == NULL) return;
7760 if (mode < 0) doc->compression = 0;
7761 else if (mode > 9) doc->compression = 9;
7762 else doc->compression = mode;
7766 * xmlGetCompressMode:
7768 * get the default compression mode used, ZLIB based.
7769 * Returns 0 (uncompressed) to 9 (max compression)
7772 xmlGetCompressMode(void)
7774 return (xmlCompressMode);
7778 * xmlSetCompressMode:
7779 * @mode: the compression ratio
7781 * set the default compression mode used, ZLIB based
7782 * Correct values: 0 (uncompressed) to 9 (max compression)
7785 xmlSetCompressMode(int mode) {
7786 if (mode < 0) xmlCompressMode = 0;
7787 else if (mode > 9) xmlCompressMode = 9;
7788 else xmlCompressMode = mode;
7791 #define XML_TREE_NSMAP_PARENT -1
7792 #define XML_TREE_NSMAP_XML -2
7793 #define XML_TREE_NSMAP_DOC -3
7794 #define XML_TREE_NSMAP_CUSTOM -4
7796 typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7797 struct xmlNsMapItem {
7798 xmlNsMapItemPtr next;
7799 xmlNsMapItemPtr prev;
7800 xmlNsPtr oldNs; /* old ns decl reference */
7801 xmlNsPtr newNs; /* new ns decl reference */
7802 int shadowDepth; /* Shadowed at this depth */
7805 * >= 0 == @node's ns-decls
7806 * -1 == @parent's ns-decls
7807 * -2 == the doc->oldNs XML ns-decl
7808 * -3 == the doc->oldNs storage ns-decls
7809 * -4 == ns-decls provided via custom ns-handling
7814 typedef struct xmlNsMap *xmlNsMapPtr;
7816 xmlNsMapItemPtr first;
7817 xmlNsMapItemPtr last;
7818 xmlNsMapItemPtr pool;
7821 #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7822 #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7823 #define XML_NSMAP_POP(m, i) \
7825 (m)->last = (i)->prev; \
7826 if ((m)->last == NULL) \
7827 (m)->first = NULL; \
7829 (m)->last->next = NULL; \
7830 (i)->next = (m)->pool; \
7834 * xmlDOMWrapNsMapFree:
7840 xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7842 xmlNsMapItemPtr cur, tmp;
7847 while (cur != NULL) {
7853 while (cur != NULL) {
7862 * xmlDOMWrapNsMapAddItem:
7864 * @oldNs: the old ns-struct
7865 * @newNs: the new ns-struct
7866 * @depth: depth and ns-kind information
7868 * Adds an ns-mapping item.
7870 static xmlNsMapItemPtr
7871 xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
7872 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
7874 xmlNsMapItemPtr ret;
7879 if ((position != -1) && (position != 0))
7885 * Create the ns-map.
7887 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7889 xmlTreeErrMemory("allocating namespace map");
7892 memset(map, 0, sizeof(struct xmlNsMap));
7896 if (map->pool != NULL) {
7898 * Reuse an item from the pool.
7901 map->pool = ret->next;
7902 memset(ret, 0, sizeof(struct xmlNsMapItem));
7905 * Create a new item.
7907 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7909 xmlTreeErrMemory("allocating namespace map item");
7912 memset(ret, 0, sizeof(struct xmlNsMapItem));
7915 if (map->first == NULL) {
7921 } else if (position == -1) {
7925 ret->prev = map->last;
7926 map->last->next = ret;
7928 } else if (position == 0) {
7930 * Set on first position.
7932 map->first->prev = ret;
7933 ret->next = map->first;
7939 ret->shadowDepth = -1;
7945 * xmlDOMWrapStoreNs:
7947 * @nsName: the namespace name
7948 * @prefix: the prefix
7950 * Creates or reuses an xmlNs struct on doc->oldNs with
7951 * the given prefix and namespace name.
7953 * Returns the aquired ns struct or NULL in case of an API
7954 * or internal error.
7957 xmlDOMWrapStoreNs(xmlDocPtr doc,
7958 const xmlChar *nsName,
7959 const xmlChar *prefix)
7965 ns = xmlTreeEnsureXMLDecl(doc);
7968 if (ns->next != NULL) {
7971 while (ns != NULL) {
7972 if (((ns->prefix == prefix) ||
7973 xmlStrEqual(ns->prefix, prefix)) &&
7974 xmlStrEqual(ns->href, nsName)) {
7977 if (ns->next == NULL)
7984 ns->next = xmlNewNs(NULL, nsName, prefix);
7991 * xmlDOMWrapNewCtxt:
7993 * Allocates and initializes a new DOM-wrapper context.
7995 * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal error.
7998 xmlDOMWrapNewCtxt(void)
8000 xmlDOMWrapCtxtPtr ret;
8002 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
8004 xmlTreeErrMemory("allocating DOM-wrapper context");
8007 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
8012 * xmlDOMWrapFreeCtxt:
8013 * @ctxt: the DOM-wrapper context
8015 * Frees the DOM-wrapper context.
8018 xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
8022 if (ctxt->namespaceMap != NULL)
8023 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
8025 * TODO: Store the namespace map in the context.
8031 * xmlTreeLookupNsListByPrefix:
8032 * @nsList: a list of ns-structs
8033 * @prefix: the searched prefix
8035 * Searches for a ns-decl with the given prefix in @nsList.
8037 * Returns the ns-decl if found, NULL if not found and on
8041 xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
8049 if ((prefix == ns->prefix) ||
8050 xmlStrEqual(prefix, ns->prefix)) {
8054 } while (ns != NULL);
8061 * xmlDOMWrapNSNormGatherInScopeNs:
8062 * @map: the namespace map
8063 * @node: the node to start with
8065 * Puts in-scope namespaces into the ns-map.
8067 * Returns 0 on success, -1 on API or internal errors.
8070 xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
8078 if ((map == NULL) || (*map != NULL))
8080 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8083 * Get in-scope ns-decls of @parent.
8086 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
8087 if (cur->type == XML_ELEMENT_NODE) {
8088 if (cur->nsDef != NULL) {
8092 if (XML_NSMAP_NOTEMPTY(*map)) {
8094 * Skip shadowed prefixes.
8096 XML_NSMAP_FOREACH(*map, mi) {
8097 if ((ns->prefix == mi->newNs->prefix) ||
8098 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
8107 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
8108 ns, XML_TREE_NSMAP_PARENT);
8112 mi->shadowDepth = 0;
8114 } while (ns != NULL);
8123 * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
8124 * otherwise copy it, when it was in the source-dict.
8126 #define XML_TREE_ADOPT_STR(str) \
8127 if (adoptStr && (str != NULL)) { \
8128 if (destDoc->dict) { \
8129 const xmlChar *old = str; \
8130 str = xmlDictLookup(destDoc->dict, str, -1); \
8131 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
8132 (!xmlDictOwns(sourceDoc->dict, old))) \
8133 xmlFree((char *)old); \
8134 } else if ((sourceDoc) && (sourceDoc->dict) && \
8135 xmlDictOwns(sourceDoc->dict, str)) { \
8136 str = BAD_CAST xmlStrdup(str); \
8141 * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
8142 * put it in dest-dict or copy it.
8144 #define XML_TREE_ADOPT_STR_2(str) \
8145 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
8146 (sourceDoc->dict != NULL) && \
8147 xmlDictOwns(sourceDoc->dict, cur->content)) { \
8148 if (destDoc->dict) \
8149 cur->content = (xmlChar *) \
8150 xmlDictLookup(destDoc->dict, cur->content, -1); \
8152 cur->content = xmlStrdup(BAD_CAST cur->content); \
8156 * xmlDOMWrapNSNormAddNsMapItem2:
8158 * For internal use. Adds a ns-decl mapping.
8160 * Returns 0 on success, -1 on internal errors.
8163 xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
8164 xmlNsPtr oldNs, xmlNsPtr newNs)
8166 if (*list == NULL) {
8167 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
8168 if (*list == NULL) {
8169 xmlTreeErrMemory("alloc ns map item");
8174 } else if ((*number) >= (*size)) {
8176 *list = (xmlNsPtr *) xmlRealloc(*list,
8177 (*size) * 2 * sizeof(xmlNsPtr));
8178 if (*list == NULL) {
8179 xmlTreeErrMemory("realloc ns map item");
8183 (*list)[2 * (*number)] = oldNs;
8184 (*list)[2 * (*number) +1] = newNs;
8190 * xmlDOMWrapRemoveNode:
8191 * @ctxt: a DOM wrapper context
8193 * @node: the node to be removed.
8194 * @options: set of options, unused at the moment
8196 * Unlinks the given node from its owner.
8197 * This will substitute ns-references to node->nsDef for
8198 * ns-references to doc->oldNs, thus ensuring the removed
8199 * branch to be autark wrt ns-references.
8201 * NOTE: This function was not intensively tested.
8203 * Returns 0 on success, 1 if the node is not supported,
8204 * -1 on API and internal errors.
8207 xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
8208 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
8210 xmlNsPtr *list = NULL;
8211 int sizeList, nbList, i, j;
8214 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
8217 /* TODO: 0 or -1 ? */
8218 if (node->parent == NULL)
8221 switch (node->type) {
8223 case XML_CDATA_SECTION_NODE:
8224 case XML_ENTITY_REF_NODE:
8226 case XML_COMMENT_NODE:
8227 xmlUnlinkNode(node);
8229 case XML_ELEMENT_NODE:
8230 case XML_ATTRIBUTE_NODE:
8235 xmlUnlinkNode(node);
8237 * Save out-of-scope ns-references in doc->oldNs.
8240 switch (node->type) {
8241 case XML_ELEMENT_NODE:
8242 if ((ctxt == NULL) && (node->nsDef != NULL)) {
8245 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8246 &nbList, ns, ns) == -1)
8247 goto internal_error;
8249 } while (ns != NULL);
8251 /* No break on purpose. */
8252 case XML_ATTRIBUTE_NODE:
8253 if (node->ns != NULL) {
8258 for (i = 0, j = 0; i < nbList; i++, j += 2) {
8259 if (node->ns == list[j]) {
8260 node->ns = list[++j];
8272 * Add to doc's oldNs.
8274 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
8277 goto internal_error;
8283 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8284 &nbList, node->ns, ns) == -1)
8285 goto internal_error;
8289 if ((node->type == XML_ELEMENT_NODE) &&
8290 (node->properties != NULL)) {
8291 node = (xmlNodePtr) node->properties;
8299 if ((node->type == XML_ELEMENT_NODE) &&
8300 (node->children != NULL)) {
8301 node = node->children;
8307 if (node->next != NULL)
8310 node = node->parent;
8313 } while (node != NULL);
8326 * xmlSearchNsByNamespaceStrict:
8327 * @doc: the document
8328 * @node: the start node
8329 * @nsName: the searched namespace name
8330 * @retNs: the resulting ns-decl
8331 * @prefixed: if the found ns-decl must have a prefix (for attributes)
8333 * Dynamically searches for a ns-declaration which matches
8334 * the given @nsName in the ancestor-or-self axis of @node.
8336 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8337 * and internal errors.
8340 xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
8341 const xmlChar* nsName,
8342 xmlNsPtr *retNs, int prefixed)
8344 xmlNodePtr cur, prev = NULL, out = NULL;
8345 xmlNsPtr ns, prevns;
8347 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
8349 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8353 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
8354 *retNs = xmlTreeEnsureXMLDecl(doc);
8361 if (cur->type == XML_ELEMENT_NODE) {
8362 if (cur->nsDef != NULL) {
8363 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8364 if (prefixed && (ns->prefix == NULL))
8368 * Check the last level of ns-decls for a
8371 prevns = prev->nsDef;
8373 if ((prevns->prefix == ns->prefix) ||
8374 ((prevns->prefix != NULL) &&
8375 (ns->prefix != NULL) &&
8376 xmlStrEqual(prevns->prefix, ns->prefix))) {
8382 prevns = prevns->next;
8383 } while (prevns != NULL);
8388 * Ns-name comparison.
8390 if ((nsName == ns->href) ||
8391 xmlStrEqual(nsName, ns->href)) {
8393 * At this point the prefix can only be shadowed,
8394 * if we are the the (at least) 3rd level of
8400 ret = xmlNsInScope(doc, node, prev, ns->prefix);
8404 * TODO: Should we try to find a matching ns-name
8405 * only once? This here keeps on searching.
8406 * I think we should try further since, there might
8407 * be an other matching ns-decl with an unshadowed
8420 } else if ((cur->type == XML_ENTITY_NODE) ||
8421 (cur->type == XML_ENTITY_DECL))
8424 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8429 * xmlSearchNsByPrefixStrict:
8430 * @doc: the document
8431 * @node: the start node
8432 * @prefix: the searched namespace prefix
8433 * @retNs: the resulting ns-decl
8435 * Dynamically searches for a ns-declaration which matches
8436 * the given @nsName in the ancestor-or-self axis of @node.
8438 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8439 * and internal errors.
8442 xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
8443 const xmlChar* prefix,
8449 if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL))
8454 if (IS_STR_XML(prefix)) {
8456 *retNs = xmlTreeEnsureXMLDecl(doc);
8464 if (cur->type == XML_ELEMENT_NODE) {
8465 if (cur->nsDef != NULL) {
8468 if ((prefix == ns->prefix) ||
8469 xmlStrEqual(prefix, ns->prefix))
8472 * Disabled namespaces, e.g. xmlns:abc="".
8474 if (ns->href == NULL)
8481 } while (ns != NULL);
8483 } else if ((cur->type == XML_ENTITY_NODE) ||
8484 (cur->type == XML_ENTITY_DECL))
8487 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8492 * xmlDOMWrapNSNormDeclareNsForced:
8494 * @elem: the element-node to declare on
8495 * @nsName: the namespace-name of the ns-decl
8496 * @prefix: the preferred prefix of the ns-decl
8497 * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
8499 * Declares a new namespace on @elem. It tries to use the
8500 * given @prefix; if a ns-decl with the given prefix is already existent
8501 * on @elem, it will generate an other prefix.
8503 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8504 * and internal errors.
8507 xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
8509 const xmlChar *nsName,
8510 const xmlChar *prefix,
8516 const xmlChar *pref;
8519 if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
8522 * Create a ns-decl on @anchor.
8527 * Lookup whether the prefix is unused in elem's ns-decls.
8529 if ((elem->nsDef != NULL) &&
8530 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
8531 goto ns_next_prefix;
8532 if (checkShadow && elem->parent &&
8533 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8535 * Does it shadow ancestor ns-decls?
8537 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
8538 goto ns_next_prefix;
8540 ret = xmlNewNs(NULL, nsName, pref);
8543 if (elem->nsDef == NULL)
8546 xmlNsPtr ns2 = elem->nsDef;
8547 while (ns2->next != NULL)
8556 if (prefix == NULL) {
8557 snprintf((char *) buf, sizeof(buf),
8560 snprintf((char *) buf, sizeof(buf),
8561 "%.30s_%d", (char *)prefix, counter);
8562 pref = BAD_CAST buf;
8567 * xmlDOMWrapNSNormAquireNormalizedNs:
8569 * @elem: the element-node to declare namespaces on
8570 * @ns: the ns-struct to use for the search
8571 * @retNs: the found/created ns-struct
8572 * @nsMap: the ns-map
8573 * @depth: the current tree depth
8574 * @ancestorsOnly: search in ancestor ns-decls only
8575 * @prefixed: if the searched ns-decl must have a prefix (for attributes)
8577 * Searches for a matching ns-name in the ns-decls of @nsMap, if not
8578 * found it will either declare it on @elem, or store it in doc->oldNs.
8579 * If a new ns-decl needs to be declared on @elem, it tries to use the
8580 * @ns->prefix for it, if this prefix is already in use on @elem, it will
8581 * change the prefix or the new ns-decl.
8583 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8586 xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8598 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
8604 * Handle XML namespace.
8606 if (IS_STR_XML(ns->prefix)) {
8608 * Insert XML namespace mapping.
8610 *retNs = xmlTreeEnsureXMLDecl(doc);
8616 * If the search should be done in ancestors only and no
8617 * @elem (the first ancestor) was specified, then skip the search.
8619 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8620 (! (ancestorsOnly && (elem == NULL))))
8623 * Try to find an equal ns-name in in-scope ns-decls.
8625 XML_NSMAP_FOREACH(*nsMap, mi) {
8626 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8628 * ancestorsOnly: This should be turned on to gain speed,
8629 * if one knows that the branch itself was already
8630 * ns-wellformed and no stale references existed.
8631 * I.e. it searches in the ancestor axis only.
8633 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8634 /* Skip shadowed prefixes. */
8635 (mi->shadowDepth == -1) &&
8636 /* Skip xmlns="" or xmlns:foo="". */
8637 ((mi->newNs->href != NULL) &&
8638 (mi->newNs->href[0] != 0)) &&
8639 /* Ensure a prefix if wanted. */
8640 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8642 ((mi->newNs->href == ns->href) ||
8643 xmlStrEqual(mi->newNs->href, ns->href))) {
8644 /* Set the mapping. */
8652 * No luck, the namespace is out of scope or shadowed.
8658 * Store ns-decls in "oldNs" of the document-node.
8660 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8666 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
8667 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8675 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8680 if (*nsMap != NULL) {
8682 * Does it shadow ancestor ns-decls?
8684 XML_NSMAP_FOREACH(*nsMap, mi) {
8685 if ((mi->depth < depth) &&
8686 (mi->shadowDepth == -1) &&
8687 ((ns->prefix == mi->newNs->prefix) ||
8688 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8692 mi->shadowDepth = depth;
8697 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
8707 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
8708 } xmlDOMReconcileNSOptions;
8711 * xmlDOMWrapReconcileNamespaces:
8712 * @ctxt: DOM wrapper context, unused at the moment
8713 * @elem: the element-node
8714 * @options: option flags
8716 * Ensures that ns-references point to ns-decls hold on element-nodes.
8717 * Ensures that the tree is namespace wellformed by creating additional
8718 * ns-decls where needed. Note that, since prefixes of already existent
8719 * ns-decls can be shadowed by this process, it could break QNames in
8720 * attribute values or element content.
8722 * NOTE: This function was not intensively tested.
8724 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8728 xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8732 int depth = -1, adoptns = 0, parnsdone = 0;
8733 xmlNsPtr ns, prevns;
8735 xmlNodePtr cur, curElem = NULL;
8736 xmlNsMapPtr nsMap = NULL;
8737 xmlNsMapItemPtr /* topmi = NULL, */ mi;
8738 /* @ancestorsOnly should be set by an option flag. */
8739 int ancestorsOnly = 0;
8740 int optRemoveRedundantNS =
8741 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8742 xmlNsPtr *listRedund = NULL;
8743 int sizeRedund = 0, nbRedund = 0, ret, i, j;
8745 if ((elem == NULL) || (elem->doc == NULL) ||
8746 (elem->type != XML_ELEMENT_NODE))
8752 switch (cur->type) {
8753 case XML_ELEMENT_NODE:
8758 * Namespace declarations.
8760 if (cur->nsDef != NULL) {
8763 while (ns != NULL) {
8765 if ((elem->parent) &&
8766 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8768 * Gather ancestor in-scope ns-decls.
8770 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8771 elem->parent) == -1)
8772 goto internal_error;
8778 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8780 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
8781 XML_NSMAP_FOREACH(nsMap, mi) {
8782 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8783 (mi->shadowDepth == -1) &&
8784 ((ns->prefix == mi->newNs->prefix) ||
8785 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8786 ((ns->href == mi->newNs->href) ||
8787 xmlStrEqual(ns->href, mi->newNs->href)))
8790 * A redundant ns-decl was found.
8791 * Add it to the list of redundant ns-decls.
8793 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8794 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8795 goto internal_error;
8797 * Remove the ns-decl from the element-node.
8800 prevns->next = ns->next;
8802 cur->nsDef = ns->next;
8809 * Skip ns-references handling if the referenced
8810 * ns-decl is declared on the same element.
8812 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
8815 * Does it shadow any ns-decl?
8817 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8818 XML_NSMAP_FOREACH(nsMap, mi) {
8819 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8820 (mi->shadowDepth == -1) &&
8821 ((ns->prefix == mi->newNs->prefix) ||
8822 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8824 mi->shadowDepth = depth;
8831 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
8833 goto internal_error;
8842 /* No break on purpose. */
8843 case XML_ATTRIBUTE_NODE:
8844 /* No ns, no fun. */
8845 if (cur->ns == NULL)
8849 if ((elem->parent) &&
8850 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8851 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8852 elem->parent) == -1)
8853 goto internal_error;
8858 * Adjust the reference if this was a redundant ns-decl.
8861 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8862 if (cur->ns == listRedund[j]) {
8863 cur->ns = listRedund[++j];
8869 * Adopt ns-references.
8871 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8873 * Search for a mapping.
8875 XML_NSMAP_FOREACH(nsMap, mi) {
8876 if ((mi->shadowDepth == -1) &&
8877 (cur->ns == mi->oldNs)) {
8879 cur->ns = mi->newNs;
8885 * Aquire a normalized ns-decl and add it to the map.
8887 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8891 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8892 goto internal_error;
8896 if ((cur->type == XML_ELEMENT_NODE) &&
8897 (cur->properties != NULL)) {
8899 * Process attributes.
8901 cur = (xmlNodePtr) cur->properties;
8909 if ((cur->type == XML_ELEMENT_NODE) &&
8910 (cur->children != NULL)) {
8912 * Process content of element-nodes only.
8914 cur = cur->children;
8920 if (cur->type == XML_ELEMENT_NODE) {
8921 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8925 while ((nsMap->last != NULL) &&
8926 (nsMap->last->depth >= depth))
8928 XML_NSMAP_POP(nsMap, mi)
8933 XML_NSMAP_FOREACH(nsMap, mi) {
8934 if (mi->shadowDepth >= depth)
8935 mi->shadowDepth = -1;
8940 if (cur->next != NULL)
8943 if (cur->type == XML_ATTRIBUTE_NODE) {
8950 } while (cur != NULL);
8958 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8959 xmlFreeNs(listRedund[j]);
8961 xmlFree(listRedund);
8964 xmlDOMWrapNsMapFree(nsMap);
8969 * xmlDOMWrapAdoptBranch:
8970 * @ctxt: the optional context for custom processing
8971 * @sourceDoc: the optional sourceDoc
8972 * @node: the element-node to start with
8973 * @destDoc: the destination doc for adoption
8974 * @destParent: the optional new parent of @node in @destDoc
8975 * @options: option flags
8977 * Ensures that ns-references point to @destDoc: either to
8978 * elements->nsDef entries if @destParent is given, or to
8979 * @destDoc->oldNs otherwise.
8980 * If @destParent is given, it ensures that the tree is namespace
8981 * wellformed by creating additional ns-decls where needed.
8982 * Note that, since prefixes of already existent ns-decls can be
8983 * shadowed by this process, it could break QNames in attribute
8984 * values or element content.
8986 * NOTE: This function was not intensively tested.
8988 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8991 xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8992 xmlDocPtr sourceDoc,
8995 xmlNodePtr destParent,
8996 int options ATTRIBUTE_UNUSED)
8999 xmlNodePtr cur, curElem = NULL;
9000 xmlNsMapPtr nsMap = NULL;
9003 int depth = -1, adoptStr = 1;
9004 /* gather @parent's ns-decls. */
9006 /* @ancestorsOnly should be set per option. */
9007 int ancestorsOnly = 0;
9010 * Optimize string adoption for equal or none dicts.
9012 if ((sourceDoc != NULL) &&
9013 (sourceDoc->dict == destDoc->dict))
9019 * Get the ns-map from the context if available.
9022 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9024 * Disable search for ns-decls in the parent-axis of the
9025 * desination element, if:
9026 * 1) there's no destination parent
9027 * 2) custom ns-reference handling is used
9029 if ((destParent == NULL) ||
9030 (ctxt && ctxt->getNsForNodeFunc))
9037 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9038 goto internal_error;
9040 while (cur != NULL) {
9042 * Paranoid source-doc sanity check.
9044 if (cur->doc != sourceDoc) {
9046 * We'll assume XIncluded nodes if the doc differs.
9047 * TODO: Do we need to reconciliate XIncluded nodes?
9048 * This here skips XIncluded nodes and tries to handle
9051 if (cur->next == NULL)
9055 if ((cur->type == XML_XINCLUDE_END) ||
9056 (cur->doc == node->doc))
9058 } while (cur->next != NULL);
9060 if (cur->doc != node->doc)
9064 switch (cur->type) {
9065 case XML_XINCLUDE_START:
9066 case XML_XINCLUDE_END:
9071 case XML_ELEMENT_NODE:
9075 * Namespace declarations.
9076 * - ns->href and ns->prefix are never in the dict, so
9077 * we need not move the values over to the destination dict.
9078 * - Note that for custom handling of ns-references,
9079 * the ns-decls need not be stored in the ns-map,
9080 * since they won't be referenced by node->ns.
9083 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
9087 * Gather @parent's in-scope ns-decls.
9089 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9091 goto internal_error;
9094 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9096 * NOTE: ns->prefix and ns->href are never in the dict.
9097 * XML_TREE_ADOPT_STR(ns->prefix)
9098 * XML_TREE_ADOPT_STR(ns->href)
9101 * Does it shadow any ns-decl?
9103 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9104 XML_NSMAP_FOREACH(nsMap, mi) {
9105 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9106 (mi->shadowDepth == -1) &&
9107 ((ns->prefix == mi->newNs->prefix) ||
9108 xmlStrEqual(ns->prefix,
9109 mi->newNs->prefix))) {
9111 mi->shadowDepth = depth;
9118 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9119 ns, ns, depth) == NULL)
9120 goto internal_error;
9123 /* No break on purpose. */
9124 case XML_ATTRIBUTE_NODE:
9125 /* No namespace, no fun. */
9126 if (cur->ns == NULL)
9130 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9132 goto internal_error;
9136 * Adopt ns-references.
9138 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9140 * Search for a mapping.
9142 XML_NSMAP_FOREACH(nsMap, mi) {
9143 if ((mi->shadowDepth == -1) &&
9144 (cur->ns == mi->oldNs)) {
9146 cur->ns = mi->newNs;
9152 * No matching namespace in scope. We need a new one.
9154 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
9156 * User-defined behaviour.
9158 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9159 cur->ns->href, cur->ns->prefix);
9161 * Insert mapping if ns is available; it's the users fault
9164 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9165 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9166 goto internal_error;
9170 * Aquire a normalized ns-decl and add it to the map.
9172 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9173 /* ns-decls on curElem or on destDoc->oldNs */
9174 destParent ? curElem : NULL,
9178 /* ns-decls must be prefixed for attributes. */
9179 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9180 goto internal_error;
9185 * Further node properties.
9186 * TODO: Is this all?
9188 XML_TREE_ADOPT_STR(cur->name)
9189 if (cur->type == XML_ELEMENT_NODE) {
9196 if (cur->properties != NULL) {
9198 * Process first attribute node.
9200 cur = (xmlNodePtr) cur->properties;
9207 if ((sourceDoc != NULL) &&
9208 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
9210 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
9212 ((xmlAttrPtr) cur)->atype = 0;
9213 ((xmlAttrPtr) cur)->psvi = NULL;
9217 case XML_CDATA_SECTION_NODE:
9219 * This puts the content in the dest dict, only if
9220 * it was previously in the source dict.
9222 XML_TREE_ADOPT_STR_2(cur->content)
9224 case XML_ENTITY_REF_NODE:
9226 * Remove reference to the entitity-node.
9228 cur->content = NULL;
9229 cur->children = NULL;
9231 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9234 * Assign new entity-node if available.
9236 ent = xmlGetDocEntity(destDoc, cur->name);
9238 cur->content = ent->content;
9239 cur->children = (xmlNodePtr) ent;
9240 cur->last = (xmlNodePtr) ent;
9245 XML_TREE_ADOPT_STR(cur->name)
9246 XML_TREE_ADOPT_STR_2(cur->content)
9248 case XML_COMMENT_NODE:
9251 goto internal_error;
9256 if (cur->children != NULL) {
9257 cur = cur->children;
9264 if ((cur->type == XML_ELEMENT_NODE) ||
9265 (cur->type == XML_XINCLUDE_START) ||
9266 (cur->type == XML_XINCLUDE_END))
9269 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9271 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9275 while ((nsMap->last != NULL) &&
9276 (nsMap->last->depth >= depth))
9278 XML_NSMAP_POP(nsMap, mi)
9283 XML_NSMAP_FOREACH(nsMap, mi) {
9284 if (mi->shadowDepth >= depth)
9285 mi->shadowDepth = -1;
9290 if (cur->next != NULL)
9292 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
9293 (cur->parent->children != NULL))
9295 cur = cur->parent->children;
9311 if (nsMap != NULL) {
9312 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9314 * Just cleanup the map but don't free.
9318 nsMap->last->next = nsMap->pool;
9319 nsMap->pool = nsMap->first;
9320 nsMap->first = NULL;
9323 xmlDOMWrapNsMapFree(nsMap);
9329 * xmlDOMWrapCloneNode:
9330 * @ctxt: the optional context for custom processing
9331 * @sourceDoc: the optional sourceDoc
9332 * @node: the node to start with
9333 * @resNode: the clone of the given @node
9334 * @destDoc: the destination doc
9335 * @destParent: the optional new parent of @node in @destDoc
9336 * @deep: descend into child if set
9337 * @options: option flags
9339 * References of out-of scope ns-decls are remapped to point to @destDoc:
9340 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9341 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
9342 * This is the case when you don't know already where the cloned branch
9345 * If @destParent is given, it ensures that the tree is namespace
9346 * wellformed by creating additional ns-decls where needed.
9347 * Note that, since prefixes of already existent ns-decls can be
9348 * shadowed by this process, it could break QNames in attribute
9349 * values or element content.
9351 * 1) What to do with XInclude? Currently this returns an error for XInclude.
9353 * Returns 0 if the operation succeeded,
9354 * 1 if a node of unsupported (or not yet supported) type was given,
9355 * -1 on API/internal errors.
9359 xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
9360 xmlDocPtr sourceDoc,
9362 xmlNodePtr *resNode,
9364 xmlNodePtr destParent,
9366 int options ATTRIBUTE_UNUSED)
9369 xmlNodePtr cur, curElem = NULL;
9370 xmlNsMapPtr nsMap = NULL;
9374 /* int adoptStr = 1; */
9375 /* gather @parent's ns-decls. */
9379 * TODO: @ancestorsOnly should be set per option.
9382 int ancestorsOnly = 0;
9383 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
9384 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
9385 xmlDictPtr dict; /* The destination dict */
9387 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
9390 * TODO: Initially we support only element-nodes.
9392 if (node->type != XML_ELEMENT_NODE)
9395 * Check node->doc sanity.
9397 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9398 (node->doc != sourceDoc)) {
9400 * Might be an XIncluded node.
9404 if (sourceDoc == NULL)
9405 sourceDoc = node->doc;
9406 if (sourceDoc == NULL)
9409 dict = destDoc->dict;
9411 * Reuse the namespace map of the context.
9414 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9419 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9422 while (cur != NULL) {
9423 if (cur->doc != sourceDoc) {
9425 * We'll assume XIncluded nodes if the doc differs.
9426 * TODO: Do we need to reconciliate XIncluded nodes?
9427 * TODO: This here returns -1 in this case.
9429 goto internal_error;
9432 * Create a new node.
9434 switch (cur->type) {
9435 case XML_XINCLUDE_START:
9436 case XML_XINCLUDE_END:
9438 * TODO: What to do with XInclude?
9440 goto internal_error;
9442 case XML_ELEMENT_NODE:
9444 case XML_CDATA_SECTION_NODE:
9445 case XML_COMMENT_NODE:
9447 case XML_DOCUMENT_FRAG_NODE:
9448 case XML_ENTITY_REF_NODE:
9449 case XML_ENTITY_NODE:
9451 * Nodes of xmlNode structure.
9453 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
9454 if (clone == NULL) {
9455 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
9456 goto internal_error;
9458 memset(clone, 0, sizeof(xmlNode));
9460 * Set hierachical links.
9462 if (resultClone != NULL) {
9463 clone->parent = parentClone;
9465 prevClone->next = clone;
9466 clone->prev = prevClone;
9468 parentClone->children = clone;
9470 resultClone = clone;
9473 case XML_ATTRIBUTE_NODE:
9475 * Attributes (xmlAttr).
9477 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
9478 if (clone == NULL) {
9479 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
9480 goto internal_error;
9482 memset(clone, 0, sizeof(xmlAttr));
9484 * Set hierachical links.
9485 * TODO: Change this to add to the end of attributes.
9487 if (resultClone != NULL) {
9488 clone->parent = parentClone;
9490 prevClone->next = clone;
9491 clone->prev = prevClone;
9493 parentClone->properties = (xmlAttrPtr) clone;
9495 resultClone = clone;
9499 * TODO QUESTION: Any other nodes expected?
9501 goto internal_error;
9504 clone->type = cur->type;
9505 clone->doc = destDoc;
9508 * Clone the name of the node if any.
9510 if (cur->name == xmlStringText)
9511 clone->name = xmlStringText;
9512 else if (cur->name == xmlStringTextNoenc)
9514 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9515 * in tree.c, it might be set in Libxslt via
9516 * "xsl:disable-output-escaping".
9518 clone->name = xmlStringTextNoenc;
9519 else if (cur->name == xmlStringComment)
9520 clone->name = xmlStringComment;
9521 else if (cur->name != NULL) {
9522 DICT_CONST_COPY(cur->name, clone->name);
9525 switch (cur->type) {
9526 case XML_XINCLUDE_START:
9527 case XML_XINCLUDE_END:
9532 case XML_ELEMENT_NODE:
9536 * Namespace declarations.
9538 if (cur->nsDef != NULL) {
9540 if (destParent && (ctxt == NULL)) {
9542 * Gather @parent's in-scope ns-decls.
9544 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9546 goto internal_error;
9551 * Clone namespace declarations.
9553 cloneNsDefSlot = &(clone->nsDef);
9554 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9556 * Create a new xmlNs.
9558 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
9559 if (cloneNs == NULL) {
9560 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
9561 "allocating namespace");
9564 memset(cloneNs, 0, sizeof(xmlNs));
9565 cloneNs->type = XML_LOCAL_NAMESPACE;
9567 if (ns->href != NULL)
9568 cloneNs->href = xmlStrdup(ns->href);
9569 if (ns->prefix != NULL)
9570 cloneNs->prefix = xmlStrdup(ns->prefix);
9572 *cloneNsDefSlot = cloneNs;
9573 cloneNsDefSlot = &(cloneNs->next);
9576 * Note that for custom handling of ns-references,
9577 * the ns-decls need not be stored in the ns-map,
9578 * since they won't be referenced by node->ns.
9580 if ((ctxt == NULL) ||
9581 (ctxt->getNsForNodeFunc == NULL))
9584 * Does it shadow any ns-decl?
9586 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9587 XML_NSMAP_FOREACH(nsMap, mi) {
9588 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9589 (mi->shadowDepth == -1) &&
9590 ((ns->prefix == mi->newNs->prefix) ||
9591 xmlStrEqual(ns->prefix,
9592 mi->newNs->prefix))) {
9594 * Mark as shadowed at the current
9597 mi->shadowDepth = depth;
9604 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9605 ns, cloneNs, depth) == NULL)
9606 goto internal_error;
9610 /* cur->ns will be processed further down. */
9612 case XML_ATTRIBUTE_NODE:
9613 /* IDs will be processed further down. */
9614 /* cur->ns will be processed further down. */
9617 case XML_CDATA_SECTION_NODE:
9619 * Note that this will also cover the values of attributes.
9621 DICT_COPY(cur->content, clone->content);
9623 case XML_ENTITY_NODE:
9624 /* TODO: What to do here? */
9626 case XML_ENTITY_REF_NODE:
9627 if (sourceDoc != destDoc) {
9628 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9631 * Different doc: Assign new entity-node if available.
9633 ent = xmlGetDocEntity(destDoc, cur->name);
9635 clone->content = ent->content;
9636 clone->children = (xmlNodePtr) ent;
9637 clone->last = (xmlNodePtr) ent;
9642 * Same doc: Use the current node's entity declaration
9645 clone->content = cur->content;
9646 clone->children = cur->children;
9647 clone->last = cur->last;
9651 DICT_COPY(cur->content, clone->content);
9653 case XML_COMMENT_NODE:
9654 DICT_COPY(cur->content, clone->content);
9657 goto internal_error;
9660 if (cur->ns == NULL)
9661 goto end_ns_reference;
9663 /* handle_ns_reference: */
9665 ** The following will take care of references to ns-decls ********
9666 ** and is intended only for element- and attribute-nodes.
9670 if (destParent && (ctxt == NULL)) {
9671 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
9672 goto internal_error;
9677 * Adopt ns-references.
9679 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9681 * Search for a mapping.
9683 XML_NSMAP_FOREACH(nsMap, mi) {
9684 if ((mi->shadowDepth == -1) &&
9685 (cur->ns == mi->oldNs)) {
9687 * This is the nice case: a mapping was found.
9689 clone->ns = mi->newNs;
9690 goto end_ns_reference;
9695 * No matching namespace in scope. We need a new one.
9697 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
9699 * User-defined behaviour.
9701 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9702 cur->ns->href, cur->ns->prefix);
9704 * Add user's mapping.
9706 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9707 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9708 goto internal_error;
9712 * Aquire a normalized ns-decl and add it to the map.
9714 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9715 /* ns-decls on curElem or on destDoc->oldNs */
9716 destParent ? curElem : NULL,
9719 /* if we need to search only in the ancestor-axis */
9721 /* ns-decls must be prefixed for attributes. */
9722 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9723 goto internal_error;
9730 * Some post-processing.
9732 * Handle ID attributes.
9734 if ((clone->type == XML_ATTRIBUTE_NODE) &&
9735 (clone->parent != NULL))
9737 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
9741 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9742 if (idVal != NULL) {
9743 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9744 /* TODO: error message. */
9746 goto internal_error;
9754 ** The following will traverse the tree **************************
9757 * Walk the element's attributes before descending into child-nodes.
9759 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9761 parentClone = clone;
9762 cur = (xmlNodePtr) cur->properties;
9767 * Descend into child-nodes.
9769 if (cur->children != NULL) {
9770 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9772 parentClone = clone;
9773 cur = cur->children;
9780 * At this point we are done with the node, its content
9781 * and an element-nodes's attribute-nodes.
9785 if ((cur->type == XML_ELEMENT_NODE) ||
9786 (cur->type == XML_XINCLUDE_START) ||
9787 (cur->type == XML_XINCLUDE_END)) {
9789 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9791 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9795 while ((nsMap->last != NULL) &&
9796 (nsMap->last->depth >= depth))
9798 XML_NSMAP_POP(nsMap, mi)
9803 XML_NSMAP_FOREACH(nsMap, mi) {
9804 if (mi->shadowDepth >= depth)
9805 mi->shadowDepth = -1;
9810 if (cur->next != NULL) {
9813 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9817 if (clone->parent != NULL)
9818 clone->parent->last = clone;
9819 clone = clone->parent;
9821 parentClone = clone->parent;
9823 * Process parent --> next;
9828 /* This is for attributes only. */
9829 clone = clone->parent;
9830 parentClone = clone->parent;
9832 * Process parent-element --> children.
9847 if (nsMap != NULL) {
9848 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9850 * Just cleanup the map but don't free.
9854 nsMap->last->next = nsMap->pool;
9855 nsMap->pool = nsMap->first;
9856 nsMap->first = NULL;
9859 xmlDOMWrapNsMapFree(nsMap);
9862 * TODO: Should we try a cleanup of the cloned node in case of a
9865 *resNode = resultClone;
9870 * xmlDOMWrapAdoptAttr:
9871 * @ctxt: the optional context for custom processing
9872 * @sourceDoc: the optional source document of attr
9873 * @attr: the attribute-node to be adopted
9874 * @destDoc: the destination doc for adoption
9875 * @destParent: the optional new parent of @attr in @destDoc
9876 * @options: option flags
9878 * @attr is adopted by @destDoc.
9879 * Ensures that ns-references point to @destDoc: either to
9880 * elements->nsDef entries if @destParent is given, or to
9881 * @destDoc->oldNs otherwise.
9883 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9886 xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9887 xmlDocPtr sourceDoc,
9890 xmlNodePtr destParent,
9891 int options ATTRIBUTE_UNUSED)
9896 if ((attr == NULL) || (destDoc == NULL))
9899 attr->doc = destDoc;
9900 if (attr->ns != NULL) {
9904 /* TODO: User defined. */
9906 /* XML Namespace. */
9907 if (IS_STR_XML(attr->ns->prefix)) {
9908 ns = xmlTreeEnsureXMLDecl(destDoc);
9909 } else if (destParent == NULL) {
9911 * Store in @destDoc->oldNs.
9913 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9916 * Declare on @destParent.
9918 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
9920 goto internal_error;
9922 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9923 attr->ns->href, attr->ns->prefix, 1);
9927 goto internal_error;
9931 XML_TREE_ADOPT_STR(attr->name);
9937 if (attr->children == NULL)
9939 cur = attr->children;
9940 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9941 goto internal_error;
9942 while (cur != NULL) {
9944 switch (cur->type) {
9946 case XML_CDATA_SECTION_NODE:
9947 XML_TREE_ADOPT_STR_2(cur->content)
9949 case XML_ENTITY_REF_NODE:
9951 * Remove reference to the entitity-node.
9953 cur->content = NULL;
9954 cur->children = NULL;
9956 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9959 * Assign new entity-node if available.
9961 ent = xmlGetDocEntity(destDoc, cur->name);
9963 cur->content = ent->content;
9964 cur->children = (xmlNodePtr) ent;
9965 cur->last = (xmlNodePtr) ent;
9972 if (cur->children != NULL) {
9973 cur = cur->children;
9977 if (cur == (xmlNodePtr) attr)
9979 if (cur->next != NULL)
9992 * xmlDOMWrapAdoptNode:
9993 * @ctxt: the optional context for custom processing
9994 * @sourceDoc: the optional sourceDoc
9995 * @node: the node to start with
9996 * @destDoc: the destination doc
9997 * @destParent: the optional new parent of @node in @destDoc
9998 * @options: option flags
10000 * References of out-of scope ns-decls are remapped to point to @destDoc:
10001 * 1) If @destParent is given, then nsDef entries on element-nodes are used
10002 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
10003 * This is the case when you have an unlinked node and just want to move it
10004 * to the context of
10006 * If @destParent is given, it ensures that the tree is namespace
10007 * wellformed by creating additional ns-decls where needed.
10008 * Note that, since prefixes of already existent ns-decls can be
10009 * shadowed by this process, it could break QNames in attribute
10010 * values or element content.
10011 * NOTE: This function was not intensively tested.
10013 * Returns 0 if the operation succeeded,
10014 * 1 if a node of unsupported type was given,
10015 * 2 if a node of not yet supported type was given and
10016 * -1 on API/internal errors.
10019 xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
10020 xmlDocPtr sourceDoc,
10023 xmlNodePtr destParent,
10026 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
10027 (destDoc == NULL) ||
10028 ((destParent != NULL) && (destParent->doc != destDoc)))
10031 * Check node->doc sanity.
10033 if ((node->doc != NULL) && (sourceDoc != NULL) &&
10034 (node->doc != sourceDoc)) {
10036 * Might be an XIncluded node.
10040 if (sourceDoc == NULL)
10041 sourceDoc = node->doc;
10042 if (sourceDoc == destDoc)
10044 switch (node->type) {
10045 case XML_ELEMENT_NODE:
10046 case XML_ATTRIBUTE_NODE:
10047 case XML_TEXT_NODE:
10048 case XML_CDATA_SECTION_NODE:
10049 case XML_ENTITY_REF_NODE:
10051 case XML_COMMENT_NODE:
10053 case XML_DOCUMENT_FRAG_NODE:
10054 /* TODO: Support document-fragment-nodes. */
10060 * Unlink only if @node was not already added to @destParent.
10062 if ((node->parent != NULL) && (destParent != node->parent))
10063 xmlUnlinkNode(node);
10065 if (node->type == XML_ELEMENT_NODE) {
10066 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
10067 destDoc, destParent, options));
10068 } else if (node->type == XML_ATTRIBUTE_NODE) {
10069 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
10070 (xmlAttrPtr) node, destDoc, destParent, options));
10072 xmlNodePtr cur = node;
10075 cur->doc = destDoc;
10077 * Optimize string adoption.
10079 if ((sourceDoc != NULL) &&
10080 (sourceDoc->dict == destDoc->dict))
10082 switch (node->type) {
10083 case XML_TEXT_NODE:
10084 case XML_CDATA_SECTION_NODE:
10085 XML_TREE_ADOPT_STR_2(node->content)
10087 case XML_ENTITY_REF_NODE:
10089 * Remove reference to the entitity-node.
10091 node->content = NULL;
10092 node->children = NULL;
10094 if ((destDoc->intSubset) || (destDoc->extSubset)) {
10097 * Assign new entity-node if available.
10099 ent = xmlGetDocEntity(destDoc, node->name);
10101 node->content = ent->content;
10102 node->children = (xmlNodePtr) ent;
10103 node->last = (xmlNodePtr) ent;
10106 XML_TREE_ADOPT_STR(node->name)
10108 case XML_PI_NODE: {
10109 XML_TREE_ADOPT_STR(node->name)
10110 XML_TREE_ADOPT_STR_2(node->content)
10120 #define bottom_tree
10121 #include "elfgcchack.h"