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.
13 /* To avoid EBCDIC trouble when parsing on zOS */
15 #pragma convert("ISO8859-1")
21 #include <string.h> /* for memset() only ! */
33 #include <libxml/xmlmemory.h>
34 #include <libxml/tree.h>
35 #include <libxml/parser.h>
36 #include <libxml/uri.h>
37 #include <libxml/entities.h>
38 #include <libxml/valid.h>
39 #include <libxml/xmlerror.h>
40 #include <libxml/parserInternals.h>
41 #include <libxml/globals.h>
42 #ifdef LIBXML_HTML_ENABLED
43 #include <libxml/HTMLtree.h>
45 #ifdef LIBXML_DEBUG_ENABLED
46 #include <libxml/debugXML.h>
52 int __xmlRegisterCallbacks = 0;
54 /************************************************************************
56 * Forward declarations *
58 ************************************************************************/
61 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
63 static xmlChar* xmlGetPropNodeValueInternal(const xmlAttr *prop);
65 /************************************************************************
67 * Tree memory error handler *
69 ************************************************************************/
72 * @extra: extra informations
74 * Handle an out of memory condition
77 xmlTreeErrMemory(const char *extra)
79 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
84 * @code: the error number
85 * @extra: extra informations
87 * Handle an out of memory condition
90 xmlTreeErr(int code, xmlNodePtr node, const char *extra)
92 const char *msg = NULL;
95 case XML_TREE_INVALID_HEX:
96 msg = "invalid hexadecimal character value\n";
98 case XML_TREE_INVALID_DEC:
99 msg = "invalid decimal character value\n";
101 case XML_TREE_UNTERMINATED_ENTITY:
102 msg = "unterminated entity reference %15s\n";
104 case XML_TREE_NOT_UTF8:
105 msg = "string is not in UTF-8\n";
108 msg = "unexpected error number\n";
110 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
113 /************************************************************************
115 * A few static variables and macros *
117 ************************************************************************/
118 /* #undef xmlStringText */
119 const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
120 /* #undef xmlStringTextNoenc */
121 const xmlChar xmlStringTextNoenc[] =
122 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
123 /* #undef xmlStringComment */
124 const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
126 static int xmlCompressMode = 0;
127 static int xmlCheckDTD = 1;
129 #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
130 xmlNodePtr ulccur = (n)->children; \
131 if (ulccur == NULL) { \
134 while (ulccur->next != NULL) { \
135 ulccur->parent = (n); \
136 ulccur = ulccur->next; \
138 ulccur->parent = (n); \
139 (n)->last = ulccur; \
142 #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
143 (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
145 /* #define DEBUG_BUFFER */
146 /* #define DEBUG_TREE */
148 /************************************************************************
150 * Functions to move to entities.c once the *
151 * API freeze is smoothen and they can be made public. *
153 ************************************************************************/
154 #include <libxml/hash.h>
156 #ifdef LIBXML_TREE_ENABLED
158 * xmlGetEntityFromDtd:
159 * @dtd: A pointer to the DTD to search
160 * @name: The entity name
162 * Do an entity lookup in the DTD entity hash table and
163 * return the corresponding entity, if found.
165 * Returns A pointer to the entity structure or NULL if not found.
168 xmlGetEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
169 xmlEntitiesTablePtr table;
171 if((dtd != NULL) && (dtd->entities != NULL)) {
172 table = (xmlEntitiesTablePtr) dtd->entities;
173 return((xmlEntityPtr) xmlHashLookup(table, name));
174 /* return(xmlGetEntityFromTable(table, name)); */
179 * xmlGetParameterEntityFromDtd:
180 * @dtd: A pointer to the DTD to search
181 * @name: The entity name
183 * Do an entity lookup in the DTD pararmeter entity hash table and
184 * return the corresponding entity, if found.
186 * Returns A pointer to the entity structure or NULL if not found.
189 xmlGetParameterEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
190 xmlEntitiesTablePtr table;
192 if ((dtd != NULL) && (dtd->pentities != NULL)) {
193 table = (xmlEntitiesTablePtr) dtd->pentities;
194 return((xmlEntityPtr) xmlHashLookup(table, name));
195 /* return(xmlGetEntityFromTable(table, name)); */
199 #endif /* LIBXML_TREE_ENABLED */
201 /************************************************************************
203 * QName handling helper *
205 ************************************************************************/
210 * @prefix: the prefix
211 * @memory: preallocated memory
212 * @len: preallocated memory length
214 * Builds the QName @prefix:@ncname in @memory if there is enough space
215 * and prefix is not NULL nor empty, otherwise allocate a new string.
216 * If prefix is NULL or empty it returns ncname.
218 * Returns the new string which must be freed by the caller if different from
219 * @memory and @ncname or NULL in case of error
222 xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
223 xmlChar *memory, int len) {
227 if (ncname == NULL) return(NULL);
228 if (prefix == NULL) return((xmlChar *) ncname);
230 lenn = strlen((char *) ncname);
231 lenp = strlen((char *) prefix);
233 if ((memory == NULL) || (len < lenn + lenp + 2)) {
234 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
236 xmlTreeErrMemory("building QName");
242 memcpy(&ret[0], prefix, lenp);
244 memcpy(&ret[lenp + 1], ncname, lenn);
245 ret[lenn + lenp + 1] = 0;
251 * @name: the full QName
252 * @prefix: a xmlChar **
254 * parse an XML qualified name string
256 * [NS 5] QName ::= (Prefix ':')? LocalPart
258 * [NS 6] Prefix ::= NCName
260 * [NS 7] LocalPart ::= NCName
262 * Returns NULL if the name doesn't have a prefix. Otherwise, returns the
263 * local part, and prefix is updated to get the Prefix. Both the return value
264 * and the prefix must be freed by the caller.
267 xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
271 if (prefix == NULL) return(NULL);
273 if (name == NULL) return(NULL);
275 #ifndef XML_XML_NAMESPACE
276 /* xml: prefix is not really a namespace */
277 if ((name[0] == 'x') && (name[1] == 'm') &&
278 (name[2] == 'l') && (name[3] == ':'))
282 /* nasty but valid */
287 * we are not trying to validate but just to cut, and yes it will
288 * work even if this is as set of UTF-8 encoded chars
290 while ((name[len] != 0) && (name[len] != ':'))
296 *prefix = xmlStrndup(name, len);
297 if (*prefix == NULL) {
298 xmlTreeErrMemory("QName split");
301 ret = xmlStrdup(&name[len + 1]);
303 xmlTreeErrMemory("QName split");
304 if (*prefix != NULL) {
316 * @name: the full QName
319 * parse an XML qualified name string,i
321 * returns NULL if it is not a Qualified Name, otherwise, update len
322 * with the length in byte of the prefix and return a pointer
323 * to the start of the name without the prefix
327 xmlSplitQName3(const xmlChar *name, int *len) {
330 if (name == NULL) return(NULL);
331 if (len == NULL) return(NULL);
333 /* nasty but valid */
338 * we are not trying to validate but just to cut, and yes it will
339 * work even if this is as set of UTF-8 encoded chars
341 while ((name[l] != 0) && (name[l] != ':'))
352 /************************************************************************
354 * Check Name, NCName and QName strings *
356 ************************************************************************/
358 #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
360 #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)
363 * @value: the value to check
364 * @space: allow spaces in front and end of the string
366 * Check that a value conforms to the lexical space of NCName
368 * Returns 0 if this validates, a positive error code number otherwise
369 * and -1 in case of internal or API error.
372 xmlValidateNCName(const xmlChar *value, int space) {
373 const xmlChar *cur = value;
380 * First quick algorithm for ASCII range
383 while (IS_BLANK_CH(*cur)) cur++;
384 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
389 while (((*cur >= 'a') && (*cur <= 'z')) ||
390 ((*cur >= 'A') && (*cur <= 'Z')) ||
391 ((*cur >= '0') && (*cur <= '9')) ||
392 (*cur == '_') || (*cur == '-') || (*cur == '.'))
395 while (IS_BLANK_CH(*cur)) cur++;
401 * Second check for chars outside the ASCII range
404 c = CUR_SCHAR(cur, l);
406 while (IS_BLANK(c)) {
408 c = CUR_SCHAR(cur, l);
411 if ((!IS_LETTER(c)) && (c != '_'))
414 c = CUR_SCHAR(cur, l);
415 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
416 (c == '-') || (c == '_') || IS_COMBINING(c) ||
419 c = CUR_SCHAR(cur, l);
422 while (IS_BLANK(c)) {
424 c = CUR_SCHAR(cur, l);
434 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
437 * @value: the value to check
438 * @space: allow spaces in front and end of the string
440 * Check that a value conforms to the lexical space of QName
442 * Returns 0 if this validates, a positive error code number otherwise
443 * and -1 in case of internal or API error.
446 xmlValidateQName(const xmlChar *value, int space) {
447 const xmlChar *cur = value;
453 * First quick algorithm for ASCII range
456 while (IS_BLANK_CH(*cur)) cur++;
457 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
462 while (((*cur >= 'a') && (*cur <= 'z')) ||
463 ((*cur >= 'A') && (*cur <= 'Z')) ||
464 ((*cur >= '0') && (*cur <= '9')) ||
465 (*cur == '_') || (*cur == '-') || (*cur == '.'))
469 if (((*cur >= 'a') && (*cur <= 'z')) ||
470 ((*cur >= 'A') && (*cur <= 'Z')) ||
475 while (((*cur >= 'a') && (*cur <= 'z')) ||
476 ((*cur >= 'A') && (*cur <= 'Z')) ||
477 ((*cur >= '0') && (*cur <= '9')) ||
478 (*cur == '_') || (*cur == '-') || (*cur == '.'))
482 while (IS_BLANK_CH(*cur)) cur++;
488 * Second check for chars outside the ASCII range
491 c = CUR_SCHAR(cur, l);
493 while (IS_BLANK(c)) {
495 c = CUR_SCHAR(cur, l);
498 if ((!IS_LETTER(c)) && (c != '_'))
501 c = CUR_SCHAR(cur, l);
502 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
503 (c == '-') || (c == '_') || IS_COMBINING(c) ||
506 c = CUR_SCHAR(cur, l);
510 c = CUR_SCHAR(cur, l);
511 if ((!IS_LETTER(c)) && (c != '_'))
514 c = CUR_SCHAR(cur, l);
515 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
516 (c == '-') || (c == '_') || IS_COMBINING(c) ||
519 c = CUR_SCHAR(cur, l);
523 while (IS_BLANK(c)) {
525 c = CUR_SCHAR(cur, l);
535 * @value: the value to check
536 * @space: allow spaces in front and end of the string
538 * Check that a value conforms to the lexical space of Name
540 * Returns 0 if this validates, a positive error code number otherwise
541 * and -1 in case of internal or API error.
544 xmlValidateName(const xmlChar *value, int space) {
545 const xmlChar *cur = value;
551 * First quick algorithm for ASCII range
554 while (IS_BLANK_CH(*cur)) cur++;
555 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
556 (*cur == '_') || (*cur == ':'))
560 while (((*cur >= 'a') && (*cur <= 'z')) ||
561 ((*cur >= 'A') && (*cur <= 'Z')) ||
562 ((*cur >= '0') && (*cur <= '9')) ||
563 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
566 while (IS_BLANK_CH(*cur)) cur++;
572 * Second check for chars outside the ASCII range
575 c = CUR_SCHAR(cur, l);
577 while (IS_BLANK(c)) {
579 c = CUR_SCHAR(cur, l);
582 if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
585 c = CUR_SCHAR(cur, l);
586 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
587 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
589 c = CUR_SCHAR(cur, l);
592 while (IS_BLANK(c)) {
594 c = CUR_SCHAR(cur, l);
603 * xmlValidateNMToken:
604 * @value: the value to check
605 * @space: allow spaces in front and end of the string
607 * Check that a value conforms to the lexical space of NMToken
609 * Returns 0 if this validates, a positive error code number otherwise
610 * and -1 in case of internal or API error.
613 xmlValidateNMToken(const xmlChar *value, int space) {
614 const xmlChar *cur = value;
620 * First quick algorithm for ASCII range
623 while (IS_BLANK_CH(*cur)) cur++;
624 if (((*cur >= 'a') && (*cur <= 'z')) ||
625 ((*cur >= 'A') && (*cur <= 'Z')) ||
626 ((*cur >= '0') && (*cur <= '9')) ||
627 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
631 while (((*cur >= 'a') && (*cur <= 'z')) ||
632 ((*cur >= 'A') && (*cur <= 'Z')) ||
633 ((*cur >= '0') && (*cur <= '9')) ||
634 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
637 while (IS_BLANK_CH(*cur)) cur++;
643 * Second check for chars outside the ASCII range
646 c = CUR_SCHAR(cur, l);
648 while (IS_BLANK(c)) {
650 c = CUR_SCHAR(cur, l);
653 if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
654 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
657 c = CUR_SCHAR(cur, l);
658 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
659 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
661 c = CUR_SCHAR(cur, l);
664 while (IS_BLANK(c)) {
666 c = CUR_SCHAR(cur, l);
673 #endif /* LIBXML_TREE_ENABLED */
675 /************************************************************************
677 * Allocation and deallocation of basic structures *
679 ************************************************************************/
682 * xmlSetBufferAllocationScheme:
683 * @scheme: allocation method to use
685 * Set the buffer allocation method. Types are
686 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
687 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
688 * improves performance
691 xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
692 if ((scheme == XML_BUFFER_ALLOC_EXACT) ||
693 (scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
694 (scheme == XML_BUFFER_ALLOC_HYBRID))
695 xmlBufferAllocScheme = scheme;
699 * xmlGetBufferAllocationScheme:
702 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
703 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
704 * improves performance
705 * XML_BUFFER_ALLOC_HYBRID - use exact sizes on small strings to keep memory usage tight
706 * in normal usage, and doubleit on large strings to avoid
707 * pathological performance.
709 * Returns the current allocation scheme
711 xmlBufferAllocationScheme
712 xmlGetBufferAllocationScheme(void) {
713 return(xmlBufferAllocScheme);
718 * @node: the element carrying the namespace
719 * @href: the URI associated
720 * @prefix: the prefix for the namespace
722 * Creation of a new Namespace. This function will refuse to create
723 * a namespace with a similar prefix than an existing one present on this
725 * Note that for a default namespace, @prefix should be NULL.
727 * We use href==NULL in the case of an element creation where the namespace
730 * Returns a new namespace pointer or NULL
733 xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
736 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
739 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
740 /* xml namespace is predefined, no need to add it */
741 if (xmlStrEqual(href, XML_XML_NAMESPACE))
745 * Problem, this is an attempt to bind xml prefix to a wrong
746 * namespace, which breaks
747 * Namespace constraint: Reserved Prefixes and Namespace Names
748 * from XML namespace. But documents authors may not care in
749 * their context so let's proceed.
754 * Allocate a new Namespace and fill the fields.
756 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
758 xmlTreeErrMemory("building namespace");
761 memset(cur, 0, sizeof(xmlNs));
762 cur->type = XML_LOCAL_NAMESPACE;
765 cur->href = xmlStrdup(href);
767 cur->prefix = xmlStrdup(prefix);
770 * Add it at the end to preserve parsing order ...
771 * and checks for existing use of the prefix
774 if (node->nsDef == NULL) {
777 xmlNsPtr prev = node->nsDef;
779 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
780 (xmlStrEqual(prev->prefix, cur->prefix))) {
784 while (prev->next != NULL) {
786 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
787 (xmlStrEqual(prev->prefix, cur->prefix))) {
800 * @node: a node in the document
801 * @ns: a namespace pointer
803 * Associate a namespace to a node, a posteriori.
806 xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
809 xmlGenericError(xmlGenericErrorContext,
810 "xmlSetNs: node == NULL\n");
814 if ((node->type == XML_ELEMENT_NODE) ||
815 (node->type == XML_ATTRIBUTE_NODE))
821 * @cur: the namespace pointer
823 * Free up the structures associated to a namespace
826 xmlFreeNs(xmlNsPtr cur) {
829 xmlGenericError(xmlGenericErrorContext,
830 "xmlFreeNs : ns == NULL\n");
834 if (cur->href != NULL) xmlFree((char *) cur->href);
835 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
841 * @cur: the first namespace pointer
843 * Free up all the structures associated to the chained namespaces.
846 xmlFreeNsList(xmlNsPtr cur) {
850 xmlGenericError(xmlGenericErrorContext,
851 "xmlFreeNsList : ns == NULL\n");
855 while (cur != NULL) {
864 * @doc: the document pointer
865 * @name: the DTD name
866 * @ExternalID: the external ID
867 * @SystemID: the system ID
869 * Creation of a new DTD for the external subset. To create an
870 * internal subset, use xmlCreateIntSubset().
872 * Returns a pointer to the new DTD structure
875 xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
876 const xmlChar *ExternalID, const xmlChar *SystemID) {
879 if ((doc != NULL) && (doc->extSubset != NULL)) {
881 xmlGenericError(xmlGenericErrorContext,
882 "xmlNewDtd(%s): document %s already have a DTD %s\n",
883 /* !!! */ (char *) name, doc->name,
884 /* !!! */ (char *)doc->extSubset->name);
890 * Allocate a new DTD and fill the fields.
892 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
894 xmlTreeErrMemory("building DTD");
897 memset(cur, 0 , sizeof(xmlDtd));
898 cur->type = XML_DTD_NODE;
901 cur->name = xmlStrdup(name);
902 if (ExternalID != NULL)
903 cur->ExternalID = xmlStrdup(ExternalID);
904 if (SystemID != NULL)
905 cur->SystemID = xmlStrdup(SystemID);
907 doc->extSubset = cur;
910 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
911 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
917 * @doc: the document pointer
919 * Get the internal subset of a document
920 * Returns a pointer to the DTD structure or NULL if not found
924 xmlGetIntSubset(const xmlDoc *doc) {
930 while (cur != NULL) {
931 if (cur->type == XML_DTD_NODE)
932 return((xmlDtdPtr) cur);
935 return((xmlDtdPtr) doc->intSubset);
939 * xmlCreateIntSubset:
940 * @doc: the document pointer
941 * @name: the DTD name
942 * @ExternalID: the external (PUBLIC) ID
943 * @SystemID: the system ID
945 * Create the internal subset of a document
946 * Returns a pointer to the new DTD structure
949 xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
950 const xmlChar *ExternalID, const xmlChar *SystemID) {
953 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
955 xmlGenericError(xmlGenericErrorContext,
957 "xmlCreateIntSubset(): document %s already have an internal subset\n",
964 * Allocate a new DTD and fill the fields.
966 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
968 xmlTreeErrMemory("building internal subset");
971 memset(cur, 0, sizeof(xmlDtd));
972 cur->type = XML_DTD_NODE;
975 cur->name = xmlStrdup(name);
976 if (cur->name == NULL) {
977 xmlTreeErrMemory("building internal subset");
982 if (ExternalID != NULL) {
983 cur->ExternalID = xmlStrdup(ExternalID);
984 if (cur->ExternalID == NULL) {
985 xmlTreeErrMemory("building internal subset");
986 if (cur->name != NULL)
987 xmlFree((char *)cur->name);
992 if (SystemID != NULL) {
993 cur->SystemID = xmlStrdup(SystemID);
994 if (cur->SystemID == NULL) {
995 xmlTreeErrMemory("building internal subset");
996 if (cur->name != NULL)
997 xmlFree((char *)cur->name);
998 if (cur->ExternalID != NULL)
999 xmlFree((char *)cur->ExternalID);
1005 doc->intSubset = cur;
1008 if (doc->children == NULL) {
1009 doc->children = (xmlNodePtr) cur;
1010 doc->last = (xmlNodePtr) cur;
1012 if (doc->type == XML_HTML_DOCUMENT_NODE) {
1015 prev = doc->children;
1016 prev->prev = (xmlNodePtr) cur;
1018 doc->children = (xmlNodePtr) cur;
1022 next = doc->children;
1023 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
1026 cur->prev = doc->last;
1027 cur->prev->next = (xmlNodePtr) cur;
1029 doc->last = (xmlNodePtr) cur;
1032 cur->prev = next->prev;
1033 if (cur->prev == NULL)
1034 doc->children = (xmlNodePtr) cur;
1036 cur->prev->next = (xmlNodePtr) cur;
1037 next->prev = (xmlNodePtr) cur;
1043 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1044 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1052 * Free a string if it is not owned by the "dict" dictionary in the
1055 #define DICT_FREE(str) \
1056 if ((str) && ((!dict) || \
1057 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1058 xmlFree((char *)(str));
1065 * Copy a string using a "dict" dictionary in the current scope,
1068 #define DICT_COPY(str, cpy) \
1071 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1072 cpy = (xmlChar *) (str); \
1074 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1076 cpy = xmlStrdup((const xmlChar *)(str)); }
1082 * Copy a string using a "dict" dictionary in the current scope,
1085 #define DICT_CONST_COPY(str, cpy) \
1088 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1089 cpy = (const xmlChar *) (str); \
1091 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1093 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1098 * @cur: the DTD structure to free up
1100 * Free a DTD structure.
1103 xmlFreeDtd(xmlDtdPtr cur) {
1104 xmlDictPtr dict = NULL;
1109 if (cur->doc != NULL) dict = cur->doc->dict;
1111 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1112 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1114 if (cur->children != NULL) {
1115 xmlNodePtr next, c = cur->children;
1118 * Cleanup all nodes which are not part of the specific lists
1119 * of notations, elements, attributes and entities.
1123 if ((c->type != XML_NOTATION_NODE) &&
1124 (c->type != XML_ELEMENT_DECL) &&
1125 (c->type != XML_ATTRIBUTE_DECL) &&
1126 (c->type != XML_ENTITY_DECL)) {
1133 DICT_FREE(cur->name)
1134 DICT_FREE(cur->SystemID)
1135 DICT_FREE(cur->ExternalID)
1137 if (cur->notations != NULL)
1138 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
1140 if (cur->elements != NULL)
1141 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1142 if (cur->attributes != NULL)
1143 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1144 if (cur->entities != NULL)
1145 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1146 if (cur->pentities != NULL)
1147 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1154 * @version: xmlChar string giving the version of XML "1.0"
1156 * Creates a new XML document
1158 * Returns a new document
1161 xmlNewDoc(const xmlChar *version) {
1164 if (version == NULL)
1165 version = (const xmlChar *) "1.0";
1168 * Allocate a new document and fill the fields.
1170 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1172 xmlTreeErrMemory("building doc");
1175 memset(cur, 0, sizeof(xmlDoc));
1176 cur->type = XML_DOCUMENT_NODE;
1178 cur->version = xmlStrdup(version);
1179 if (cur->version == NULL) {
1180 xmlTreeErrMemory("building doc");
1184 cur->standalone = -1;
1185 cur->compression = -1; /* not initialized */
1187 cur->parseFlags = 0;
1188 cur->properties = XML_DOC_USERBUILT;
1190 * The in memory encoding is always UTF8
1191 * This field will never change and would
1192 * be obsolete if not for binary compatibility.
1194 cur->charset = XML_CHAR_ENCODING_UTF8;
1196 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1197 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1203 * @cur: pointer to the document
1205 * Free up all the structures used by a document, tree included.
1208 xmlFreeDoc(xmlDocPtr cur) {
1209 xmlDtdPtr extSubset, intSubset;
1210 xmlDictPtr dict = NULL;
1214 xmlGenericError(xmlGenericErrorContext,
1215 "xmlFreeDoc : document == NULL\n");
1219 #ifdef LIBXML_DEBUG_RUNTIME
1220 #ifdef LIBXML_DEBUG_ENABLED
1221 xmlDebugCheckDocument(stderr, cur);
1225 if (cur != NULL) dict = cur->dict;
1227 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1228 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1231 * Do this before freeing the children list to avoid ID lookups
1233 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1235 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1237 extSubset = cur->extSubset;
1238 intSubset = cur->intSubset;
1239 if (intSubset == extSubset)
1241 if (extSubset != NULL) {
1242 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
1243 cur->extSubset = NULL;
1244 xmlFreeDtd(extSubset);
1246 if (intSubset != NULL) {
1247 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
1248 cur->intSubset = NULL;
1249 xmlFreeDtd(intSubset);
1252 if (cur->children != NULL) xmlFreeNodeList(cur->children);
1253 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
1255 DICT_FREE(cur->version)
1256 DICT_FREE(cur->name)
1257 DICT_FREE(cur->encoding)
1260 if (dict) xmlDictFree(dict);
1264 * xmlStringLenGetNodeList:
1265 * @doc: the document
1266 * @value: the value of the text
1267 * @len: the length of the string value
1269 * Parse the value string and build the node list associated. Should
1270 * produce a flat tree with only TEXTs and ENTITY_REFs.
1271 * Returns a pointer to the first child
1274 xmlStringLenGetNodeList(const xmlDoc *doc, const xmlChar *value, int len) {
1275 xmlNodePtr ret = NULL, last = NULL;
1278 const xmlChar *cur = value, *end = cur + len;
1283 if (value == NULL) return(NULL);
1285 buf = xmlBufCreateSize(0);
1286 if (buf == NULL) return(NULL);
1287 xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID);
1290 while ((cur < end) && (*cur != 0)) {
1291 if (cur[0] == '&') {
1296 * Save the current text.
1299 if (xmlBufAdd(buf, q, cur - q))
1303 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1309 while (tmp != ';') { /* Non input consuming loop */
1310 if ((tmp >= '0') && (tmp <= '9'))
1311 charval = charval * 16 + (tmp - '0');
1312 else if ((tmp >= 'a') && (tmp <= 'f'))
1313 charval = charval * 16 + (tmp - 'a') + 10;
1314 else if ((tmp >= 'A') && (tmp <= 'F'))
1315 charval = charval * 16 + (tmp - 'A') + 10;
1317 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1331 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1337 while (tmp != ';') { /* Non input consuming loops */
1338 if ((tmp >= '0') && (tmp <= '9'))
1339 charval = charval * 10 + (tmp - '0');
1341 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1357 * Read the entity string
1361 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1362 if ((cur >= end) || (*cur == 0)) {
1363 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1369 * Predefined entities don't generate nodes
1371 val = xmlStrndup(q, cur - q);
1372 ent = xmlGetDocEntity(doc, val);
1373 if ((ent != NULL) &&
1374 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1376 if (xmlBufCat(buf, ent->content))
1381 * Flush buffer so far
1383 if (!xmlBufIsEmpty(buf)) {
1384 node = xmlNewDocText(doc, NULL);
1386 if (val != NULL) xmlFree(val);
1389 node->content = xmlBufDetach(buf);
1394 last = xmlAddNextSibling(last, node);
1399 * Create a new REFERENCE_REF node
1401 node = xmlNewReference(doc, val);
1403 if (val != NULL) xmlFree(val);
1406 else if ((ent != NULL) && (ent->children == NULL)) {
1409 /* Set to non-NULL value to avoid recursion. */
1410 ent->children = (xmlNodePtr) -1;
1411 ent->children = xmlStringGetNodeList(doc,
1412 (const xmlChar*)node->content);
1414 temp = ent->children;
1416 temp->parent = (xmlNodePtr)ent;
1424 last = xmlAddNextSibling(last, node);
1436 l = xmlCopyCharMultiByte(buffer, charval);
1439 if (xmlBufCat(buf, buffer))
1449 * Handle the last piece of text.
1451 if (xmlBufAdd(buf, q, cur - q))
1455 if (!xmlBufIsEmpty(buf)) {
1456 node = xmlNewDocText(doc, NULL);
1457 if (node == NULL) goto out;
1458 node->content = xmlBufDetach(buf);
1463 xmlAddNextSibling(last, node);
1465 } else if (ret == NULL) {
1466 ret = xmlNewDocText(doc, BAD_CAST "");
1475 * xmlStringGetNodeList:
1476 * @doc: the document
1477 * @value: the value of the attribute
1479 * Parse the value string and build the node list associated. Should
1480 * produce a flat tree with only TEXTs and ENTITY_REFs.
1481 * Returns a pointer to the first child
1484 xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) {
1485 xmlNodePtr ret = NULL, last = NULL;
1488 const xmlChar *cur = value;
1493 if (value == NULL) return(NULL);
1495 buf = xmlBufCreateSize(0);
1496 if (buf == NULL) return(NULL);
1497 xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID);
1501 if (cur[0] == '&') {
1506 * Save the current text.
1509 if (xmlBufAdd(buf, q, cur - q))
1513 if ((cur[1] == '#') && (cur[2] == 'x')) {
1516 while (tmp != ';') { /* Non input consuming loop */
1517 if ((tmp >= '0') && (tmp <= '9'))
1518 charval = charval * 16 + (tmp - '0');
1519 else if ((tmp >= 'a') && (tmp <= 'f'))
1520 charval = charval * 16 + (tmp - 'a') + 10;
1521 else if ((tmp >= 'A') && (tmp <= 'F'))
1522 charval = charval * 16 + (tmp - 'A') + 10;
1524 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1535 } else if (cur[1] == '#') {
1538 while (tmp != ';') { /* Non input consuming loops */
1539 if ((tmp >= '0') && (tmp <= '9'))
1540 charval = charval * 10 + (tmp - '0');
1542 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1555 * Read the entity string
1559 while ((*cur != 0) && (*cur != ';')) cur++;
1561 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1562 (xmlNodePtr) doc, (const char *) q);
1567 * Predefined entities don't generate nodes
1569 val = xmlStrndup(q, cur - q);
1570 ent = xmlGetDocEntity(doc, val);
1571 if ((ent != NULL) &&
1572 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1574 if (xmlBufCat(buf, ent->content))
1579 * Flush buffer so far
1581 if (!xmlBufIsEmpty(buf)) {
1582 node = xmlNewDocText(doc, NULL);
1583 node->content = xmlBufDetach(buf);
1588 last = xmlAddNextSibling(last, node);
1593 * Create a new REFERENCE_REF node
1595 node = xmlNewReference(doc, val);
1597 if (val != NULL) xmlFree(val);
1600 else if ((ent != NULL) && (ent->children == NULL)) {
1603 /* Set to non-NULL value to avoid recursion. */
1604 ent->children = (xmlNodePtr) -1;
1605 ent->children = xmlStringGetNodeList(doc,
1606 (const xmlChar*)node->content);
1608 temp = ent->children;
1610 temp->parent = (xmlNodePtr)ent;
1618 last = xmlAddNextSibling(last, node);
1630 len = xmlCopyCharMultiByte(buffer, charval);
1633 if (xmlBufCat(buf, buffer))
1640 if ((cur != q) || (ret == NULL)) {
1642 * Handle the last piece of text.
1644 xmlBufAdd(buf, q, cur - q);
1647 if (!xmlBufIsEmpty(buf)) {
1648 node = xmlNewDocText(doc, NULL);
1649 node->content = xmlBufDetach(buf);
1654 xmlAddNextSibling(last, node);
1664 * xmlNodeListGetString:
1665 * @doc: the document
1666 * @list: a Node list
1667 * @inLine: should we replace entity contents or show their external form
1669 * Build the string equivalent to the text contained in the Node list
1670 * made of TEXTs and ENTITY_REFs
1672 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1675 xmlNodeListGetString(xmlDocPtr doc, const xmlNode *list, int inLine)
1677 const xmlNode *node = list;
1678 xmlChar *ret = NULL;
1684 if ((list->parent != NULL) && (list->parent->type == XML_ATTRIBUTE_NODE))
1689 while (node != NULL) {
1690 if ((node->type == XML_TEXT_NODE) ||
1691 (node->type == XML_CDATA_SECTION_NODE)) {
1693 ret = xmlStrcat(ret, node->content);
1698 buffer = xmlEncodeAttributeEntities(doc, node->content);
1700 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1701 if (buffer != NULL) {
1702 ret = xmlStrcat(ret, buffer);
1706 } else if (node->type == XML_ENTITY_REF_NODE) {
1708 ent = xmlGetDocEntity(doc, node->name);
1712 /* an entity content can be any "well balanced chunk",
1713 * i.e. the result of the content [43] production:
1714 * http://www.w3.org/TR/REC-xml#NT-content.
1715 * So it can contain text, CDATA section or nested
1716 * entity reference nodes (among others).
1717 * -> we recursive call xmlNodeListGetString()
1718 * which handles these types */
1719 buffer = xmlNodeListGetString(doc, ent->children, 1);
1720 if (buffer != NULL) {
1721 ret = xmlStrcat(ret, buffer);
1725 ret = xmlStrcat(ret, node->content);
1732 ret = xmlStrncat(ret, buf, 1);
1733 ret = xmlStrcat(ret, node->name);
1736 ret = xmlStrncat(ret, buf, 1);
1741 xmlGenericError(xmlGenericErrorContext,
1742 "xmlGetNodeListString : invalid node type %d\n",
1751 #ifdef LIBXML_TREE_ENABLED
1753 * xmlNodeListGetRawString:
1754 * @doc: the document
1755 * @list: a Node list
1756 * @inLine: should we replace entity contents or show their external form
1758 * Builds the string equivalent to the text contained in the Node list
1759 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1760 * this function doesn't do any character encoding handling.
1762 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1765 xmlNodeListGetRawString(const xmlDoc *doc, const xmlNode *list, int inLine)
1767 const xmlNode *node = list;
1768 xmlChar *ret = NULL;
1774 while (node != NULL) {
1775 if ((node->type == XML_TEXT_NODE) ||
1776 (node->type == XML_CDATA_SECTION_NODE)) {
1778 ret = xmlStrcat(ret, node->content);
1782 buffer = xmlEncodeSpecialChars(doc, node->content);
1783 if (buffer != NULL) {
1784 ret = xmlStrcat(ret, buffer);
1788 } else if (node->type == XML_ENTITY_REF_NODE) {
1790 ent = xmlGetDocEntity(doc, node->name);
1794 /* an entity content can be any "well balanced chunk",
1795 * i.e. the result of the content [43] production:
1796 * http://www.w3.org/TR/REC-xml#NT-content.
1797 * So it can contain text, CDATA section or nested
1798 * entity reference nodes (among others).
1799 * -> we recursive call xmlNodeListGetRawString()
1800 * which handles these types */
1802 xmlNodeListGetRawString(doc, ent->children, 1);
1803 if (buffer != NULL) {
1804 ret = xmlStrcat(ret, buffer);
1808 ret = xmlStrcat(ret, node->content);
1815 ret = xmlStrncat(ret, buf, 1);
1816 ret = xmlStrcat(ret, node->name);
1819 ret = xmlStrncat(ret, buf, 1);
1824 xmlGenericError(xmlGenericErrorContext,
1825 "xmlGetNodeListString : invalid node type %d\n",
1833 #endif /* LIBXML_TREE_ENABLED */
1836 xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1837 const xmlChar * name, const xmlChar * value,
1841 xmlDocPtr doc = NULL;
1843 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
1844 if ((eatname == 1) &&
1845 ((node->doc == NULL) ||
1846 (!(xmlDictOwns(node->doc->dict, name)))))
1847 xmlFree((xmlChar *) name);
1852 * Allocate a new property and fill the fields.
1854 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1856 if ((eatname == 1) &&
1857 ((node == NULL) || (node->doc == NULL) ||
1858 (!(xmlDictOwns(node->doc->dict, name)))))
1859 xmlFree((xmlChar *) name);
1860 xmlTreeErrMemory("building attribute");
1863 memset(cur, 0, sizeof(xmlAttr));
1864 cur->type = XML_ATTRIBUTE_NODE;
1874 if ((doc != NULL) && (doc->dict != NULL))
1875 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1877 cur->name = xmlStrdup(name);
1881 if (value != NULL) {
1884 if(!xmlCheckUTF8(value)) {
1885 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc,
1888 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1890 cur->children = xmlNewDocText(doc, value);
1892 tmp = cur->children;
1893 while (tmp != NULL) {
1894 tmp->parent = (xmlNodePtr) cur;
1895 if (tmp->next == NULL)
1902 * Add it at the end to preserve parsing order ...
1905 if (node->properties == NULL) {
1906 node->properties = cur;
1908 xmlAttrPtr prev = node->properties;
1910 while (prev->next != NULL)
1917 if ((value != NULL) && (node != NULL) &&
1918 (xmlIsID(node->doc, node, cur) == 1))
1919 xmlAddID(NULL, node->doc, value, cur);
1921 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1922 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1926 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1927 defined(LIBXML_SCHEMAS_ENABLED)
1930 * @node: the holding node
1931 * @name: the name of the attribute
1932 * @value: the value of the attribute
1934 * Create a new property carried by a node.
1935 * Returns a pointer to the attribute
1938 xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1942 xmlGenericError(xmlGenericErrorContext,
1943 "xmlNewProp : name == NULL\n");
1948 return xmlNewPropInternal(node, NULL, name, value, 0);
1950 #endif /* LIBXML_TREE_ENABLED */
1954 * @node: the holding node
1955 * @ns: the namespace
1956 * @name: the name of the attribute
1957 * @value: the value of the attribute
1959 * Create a new property tagged with a namespace and carried by a node.
1960 * Returns a pointer to the attribute
1963 xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1964 const xmlChar *value) {
1968 xmlGenericError(xmlGenericErrorContext,
1969 "xmlNewNsProp : name == NULL\n");
1974 return xmlNewPropInternal(node, ns, name, value, 0);
1978 * xmlNewNsPropEatName:
1979 * @node: the holding node
1980 * @ns: the namespace
1981 * @name: the name of the attribute
1982 * @value: the value of the attribute
1984 * Create a new property tagged with a namespace and carried by a node.
1985 * Returns a pointer to the attribute
1988 xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1989 const xmlChar *value) {
1993 xmlGenericError(xmlGenericErrorContext,
1994 "xmlNewNsPropEatName : name == NULL\n");
1999 return xmlNewPropInternal(node, ns, name, value, 1);
2004 * @doc: the document
2005 * @name: the name of the attribute
2006 * @value: the value of the attribute
2008 * Create a new property carried by a document.
2009 * Returns a pointer to the attribute
2012 xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
2017 xmlGenericError(xmlGenericErrorContext,
2018 "xmlNewDocProp : name == NULL\n");
2024 * Allocate a new property and fill the fields.
2026 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
2028 xmlTreeErrMemory("building attribute");
2031 memset(cur, 0, sizeof(xmlAttr));
2032 cur->type = XML_ATTRIBUTE_NODE;
2034 if ((doc != NULL) && (doc->dict != NULL))
2035 cur->name = xmlDictLookup(doc->dict, name, -1);
2037 cur->name = xmlStrdup(name);
2039 if (value != NULL) {
2042 cur->children = xmlStringGetNodeList(doc, value);
2045 tmp = cur->children;
2046 while (tmp != NULL) {
2047 tmp->parent = (xmlNodePtr) cur;
2048 if (tmp->next == NULL)
2054 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2055 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2061 * @cur: the first property in the list
2063 * Free a property and all its siblings, all the children are freed too.
2066 xmlFreePropList(xmlAttrPtr cur) {
2068 if (cur == NULL) return;
2069 while (cur != NULL) {
2078 * @cur: an attribute
2080 * Free one attribute, all the content is freed too
2083 xmlFreeProp(xmlAttrPtr cur) {
2084 xmlDictPtr dict = NULL;
2085 if (cur == NULL) return;
2087 if (cur->doc != NULL) dict = cur->doc->dict;
2089 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
2090 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2092 /* Check for ID removal -> leading to invalid references ! */
2093 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2094 xmlRemoveID(cur->doc, cur);
2096 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2097 DICT_FREE(cur->name)
2103 * @cur: an attribute
2105 * Unlink and free one attribute, all the content is freed too
2106 * Note this doesn't work for namespace definition attributes
2108 * Returns 0 if success and -1 in case of error.
2111 xmlRemoveProp(xmlAttrPtr cur) {
2115 xmlGenericError(xmlGenericErrorContext,
2116 "xmlRemoveProp : cur == NULL\n");
2120 if (cur->parent == NULL) {
2122 xmlGenericError(xmlGenericErrorContext,
2123 "xmlRemoveProp : cur->parent == NULL\n");
2127 tmp = cur->parent->properties;
2129 cur->parent->properties = cur->next;
2130 if (cur->next != NULL)
2131 cur->next->prev = NULL;
2135 while (tmp != NULL) {
2136 if (tmp->next == cur) {
2137 tmp->next = cur->next;
2138 if (tmp->next != NULL)
2139 tmp->next->prev = tmp;
2146 xmlGenericError(xmlGenericErrorContext,
2147 "xmlRemoveProp : attribute not owned by its node\n");
2154 * @doc: the target document
2155 * @name: the processing instruction name
2156 * @content: the PI content
2158 * Creation of a processing instruction element.
2159 * Returns a pointer to the new node object.
2162 xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
2167 xmlGenericError(xmlGenericErrorContext,
2168 "xmlNewPI : name == NULL\n");
2174 * Allocate a new node and fill the fields.
2176 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2178 xmlTreeErrMemory("building PI");
2181 memset(cur, 0, sizeof(xmlNode));
2182 cur->type = XML_PI_NODE;
2184 if ((doc != NULL) && (doc->dict != NULL))
2185 cur->name = xmlDictLookup(doc->dict, name, -1);
2187 cur->name = xmlStrdup(name);
2188 if (content != NULL) {
2189 cur->content = xmlStrdup(content);
2193 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2194 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2200 * @name: the processing instruction name
2201 * @content: the PI content
2203 * Creation of a processing instruction element.
2204 * Use xmlDocNewPI preferably to get string interning
2206 * Returns a pointer to the new node object.
2209 xmlNewPI(const xmlChar *name, const xmlChar *content) {
2210 return(xmlNewDocPI(NULL, name, content));
2215 * @ns: namespace if any
2216 * @name: the node name
2218 * Creation of a new node element. @ns is optional (NULL).
2220 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2224 xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2229 xmlGenericError(xmlGenericErrorContext,
2230 "xmlNewNode : name == NULL\n");
2236 * Allocate a new node and fill the fields.
2238 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2240 xmlTreeErrMemory("building node");
2243 memset(cur, 0, sizeof(xmlNode));
2244 cur->type = XML_ELEMENT_NODE;
2246 cur->name = xmlStrdup(name);
2249 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2250 xmlRegisterNodeDefaultValue(cur);
2255 * xmlNewNodeEatName:
2256 * @ns: namespace if any
2257 * @name: the node name
2259 * Creation of a new node element. @ns is optional (NULL).
2261 * Returns a pointer to the new node object, with pointer @name as
2262 * new node's name. Use xmlNewNode() if a copy of @name string is
2263 * is needed as new node's name.
2266 xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2271 xmlGenericError(xmlGenericErrorContext,
2272 "xmlNewNode : name == NULL\n");
2278 * Allocate a new node and fill the fields.
2280 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2282 xmlTreeErrMemory("building node");
2283 /* we can't check here that name comes from the doc dictionary */
2286 memset(cur, 0, sizeof(xmlNode));
2287 cur->type = XML_ELEMENT_NODE;
2292 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2293 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2299 * @doc: the document
2300 * @ns: namespace if any
2301 * @name: the node name
2302 * @content: the XML text content if any
2304 * Creation of a new node element within a document. @ns and @content
2305 * are optional (NULL).
2306 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2307 * references, but XML special chars need to be escaped first by using
2308 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2309 * need entities support.
2311 * Returns a pointer to the new node object.
2314 xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2315 const xmlChar *name, const xmlChar *content) {
2318 if ((doc != NULL) && (doc->dict != NULL))
2319 cur = xmlNewNodeEatName(ns, (xmlChar *)
2320 xmlDictLookup(doc->dict, name, -1));
2322 cur = xmlNewNode(ns, name);
2325 if (content != NULL) {
2326 cur->children = xmlStringGetNodeList(doc, content);
2327 UPDATE_LAST_CHILD_AND_PARENT(cur)
2335 * xmlNewDocNodeEatName:
2336 * @doc: the document
2337 * @ns: namespace if any
2338 * @name: the node name
2339 * @content: the XML text content if any
2341 * Creation of a new node element within a document. @ns and @content
2342 * are optional (NULL).
2343 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2344 * references, but XML special chars need to be escaped first by using
2345 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2346 * need entities support.
2348 * Returns a pointer to the new node object.
2351 xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2352 xmlChar *name, const xmlChar *content) {
2355 cur = xmlNewNodeEatName(ns, name);
2358 if (content != NULL) {
2359 cur->children = xmlStringGetNodeList(doc, content);
2360 UPDATE_LAST_CHILD_AND_PARENT(cur)
2363 /* if name don't come from the doc dictionary free it here */
2364 if ((name != NULL) && (doc != NULL) &&
2365 (!(xmlDictOwns(doc->dict, name))))
2371 #ifdef LIBXML_TREE_ENABLED
2374 * @doc: the document
2375 * @ns: namespace if any
2376 * @name: the node name
2377 * @content: the text content if any
2379 * Creation of a new node element within a document. @ns and @content
2380 * are optional (NULL).
2382 * Returns a pointer to the new node object.
2385 xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2386 const xmlChar *name, const xmlChar *content) {
2389 cur = xmlNewDocNode(doc, ns, name, NULL);
2392 if (content != NULL) {
2393 cur->children = xmlNewDocText(doc, content);
2394 UPDATE_LAST_CHILD_AND_PARENT(cur)
2401 * xmlNewDocFragment:
2402 * @doc: the document owning the fragment
2404 * Creation of a new Fragment node.
2405 * Returns a pointer to the new node object.
2408 xmlNewDocFragment(xmlDocPtr doc) {
2412 * Allocate a new DocumentFragment node and fill the fields.
2414 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2416 xmlTreeErrMemory("building fragment");
2419 memset(cur, 0, sizeof(xmlNode));
2420 cur->type = XML_DOCUMENT_FRAG_NODE;
2424 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2425 xmlRegisterNodeDefaultValue(cur);
2428 #endif /* LIBXML_TREE_ENABLED */
2432 * @content: the text content
2434 * Creation of a new text node.
2435 * Returns a pointer to the new node object.
2438 xmlNewText(const xmlChar *content) {
2442 * Allocate a new node and fill the fields.
2444 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2446 xmlTreeErrMemory("building text");
2449 memset(cur, 0, sizeof(xmlNode));
2450 cur->type = XML_TEXT_NODE;
2452 cur->name = xmlStringText;
2453 if (content != NULL) {
2454 cur->content = xmlStrdup(content);
2457 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2458 xmlRegisterNodeDefaultValue(cur);
2462 #ifdef LIBXML_TREE_ENABLED
2465 * @parent: the parent node
2466 * @ns: a namespace if any
2467 * @name: the name of the child
2468 * @content: the text content of the child if any.
2470 * Creation of a new child element, added at the end of @parent children list.
2471 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2472 * created element inherits the namespace of @parent. If @content is non NULL,
2473 * a child TEXT node will be created containing the string @content.
2474 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2475 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
2476 * reserved XML chars that might appear in @content, such as the ampersand,
2477 * greater-than or less-than signs, are automatically replaced by their XML
2478 * escaped entity representations.
2480 * Returns a pointer to the new node object.
2483 xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2484 const xmlChar *name, const xmlChar *content) {
2485 xmlNodePtr cur, prev;
2487 if (parent == NULL) {
2489 xmlGenericError(xmlGenericErrorContext,
2490 "xmlNewTextChild : parent == NULL\n");
2497 xmlGenericError(xmlGenericErrorContext,
2498 "xmlNewTextChild : name == NULL\n");
2504 * Allocate a new node
2506 if (parent->type == XML_ELEMENT_NODE) {
2508 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2510 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2511 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2512 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2514 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2516 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2517 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2518 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2522 if (cur == NULL) return(NULL);
2525 * add the new element at the end of the children list.
2527 cur->type = XML_ELEMENT_NODE;
2528 cur->parent = parent;
2529 cur->doc = parent->doc;
2530 if (parent->children == NULL) {
2531 parent->children = cur;
2534 prev = parent->last;
2542 #endif /* LIBXML_TREE_ENABLED */
2546 * @doc: the document
2547 * @name: the char ref string, starting with # or "&# ... ;"
2549 * Creation of a new character reference node.
2550 * Returns a pointer to the new node object.
2553 xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2560 * Allocate a new node and fill the fields.
2562 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2564 xmlTreeErrMemory("building character reference");
2567 memset(cur, 0, sizeof(xmlNode));
2568 cur->type = XML_ENTITY_REF_NODE;
2571 if (name[0] == '&') {
2574 len = xmlStrlen(name);
2575 if (name[len - 1] == ';')
2576 cur->name = xmlStrndup(name, len - 1);
2578 cur->name = xmlStrndup(name, len);
2580 cur->name = xmlStrdup(name);
2582 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2583 xmlRegisterNodeDefaultValue(cur);
2589 * @doc: the document
2590 * @name: the reference name, or the reference string with & and ;
2592 * Creation of a new reference node.
2593 * Returns a pointer to the new node object.
2596 xmlNewReference(const xmlDoc *doc, const xmlChar *name) {
2604 * Allocate a new node and fill the fields.
2606 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2608 xmlTreeErrMemory("building reference");
2611 memset(cur, 0, sizeof(xmlNode));
2612 cur->type = XML_ENTITY_REF_NODE;
2614 cur->doc = (xmlDoc *)doc;
2615 if (name[0] == '&') {
2618 len = xmlStrlen(name);
2619 if (name[len - 1] == ';')
2620 cur->name = xmlStrndup(name, len - 1);
2622 cur->name = xmlStrndup(name, len);
2624 cur->name = xmlStrdup(name);
2626 ent = xmlGetDocEntity(doc, cur->name);
2628 cur->content = ent->content;
2630 * The parent pointer in entity is a DTD pointer and thus is NOT
2631 * updated. Not sure if this is 100% correct.
2634 cur->children = (xmlNodePtr) ent;
2635 cur->last = (xmlNodePtr) ent;
2638 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2639 xmlRegisterNodeDefaultValue(cur);
2645 * @doc: the document
2646 * @content: the text content
2648 * Creation of a new text node within a document.
2649 * Returns a pointer to the new node object.
2652 xmlNewDocText(const xmlDoc *doc, const xmlChar *content) {
2655 cur = xmlNewText(content);
2656 if (cur != NULL) cur->doc = (xmlDoc *)doc;
2662 * @content: the text content
2663 * @len: the text len.
2665 * Creation of a new text node with an extra parameter for the content's length
2666 * Returns a pointer to the new node object.
2669 xmlNewTextLen(const xmlChar *content, int len) {
2673 * Allocate a new node and fill the fields.
2675 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2677 xmlTreeErrMemory("building text");
2680 memset(cur, 0, sizeof(xmlNode));
2681 cur->type = XML_TEXT_NODE;
2683 cur->name = xmlStringText;
2684 if (content != NULL) {
2685 cur->content = xmlStrndup(content, len);
2688 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2689 xmlRegisterNodeDefaultValue(cur);
2695 * @doc: the document
2696 * @content: the text content
2697 * @len: the text len.
2699 * Creation of a new text node with an extra content length parameter. The
2700 * text node pertain to a given document.
2701 * Returns a pointer to the new node object.
2704 xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2707 cur = xmlNewTextLen(content, len);
2708 if (cur != NULL) cur->doc = doc;
2714 * @content: the comment content
2716 * Creation of a new node containing a comment.
2717 * Returns a pointer to the new node object.
2720 xmlNewComment(const xmlChar *content) {
2724 * Allocate a new node and fill the fields.
2726 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2728 xmlTreeErrMemory("building comment");
2731 memset(cur, 0, sizeof(xmlNode));
2732 cur->type = XML_COMMENT_NODE;
2734 cur->name = xmlStringComment;
2735 if (content != NULL) {
2736 cur->content = xmlStrdup(content);
2739 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2740 xmlRegisterNodeDefaultValue(cur);
2746 * @doc: the document
2747 * @content: the CDATA block content content
2748 * @len: the length of the block
2750 * Creation of a new node containing a CDATA block.
2751 * Returns a pointer to the new node object.
2754 xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2758 * Allocate a new node and fill the fields.
2760 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2762 xmlTreeErrMemory("building CDATA");
2765 memset(cur, 0, sizeof(xmlNode));
2766 cur->type = XML_CDATA_SECTION_NODE;
2769 if (content != NULL) {
2770 cur->content = xmlStrndup(content, len);
2773 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2774 xmlRegisterNodeDefaultValue(cur);
2780 * @doc: the document
2781 * @content: the comment content
2783 * Creation of a new node containing a comment within a document.
2784 * Returns a pointer to the new node object.
2787 xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2790 cur = xmlNewComment(content);
2791 if (cur != NULL) cur->doc = doc;
2797 * @tree: the top element
2798 * @doc: the document
2800 * update all nodes under the tree to point to the right document
2803 xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
2806 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
2808 if (tree->doc != doc) {
2809 if(tree->type == XML_ELEMENT_NODE) {
2810 prop = tree->properties;
2811 while (prop != NULL) {
2812 if (prop->atype == XML_ATTRIBUTE_ID) {
2813 xmlRemoveID(tree->doc, prop);
2817 xmlSetListDoc(prop->children, doc);
2820 * TODO: ID attributes should be also added to the new
2821 * document, but this breaks things like xmlReplaceNode.
2822 * The underlying problem is that xmlRemoveID is only called
2823 * if a node is destroyed, not if it's unlinked.
2826 if (xmlIsID(doc, tree, prop)) {
2827 xmlChar *idVal = xmlNodeListGetString(doc, prop->children,
2829 xmlAddID(NULL, doc, idVal, prop);
2836 if (tree->children != NULL)
2837 xmlSetListDoc(tree->children, doc);
2844 * @list: the first element
2845 * @doc: the document
2847 * update all nodes in the list to point to the right document
2850 xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2853 if ((list == NULL) || (list->type == XML_NAMESPACE_DECL))
2856 while (cur != NULL) {
2857 if (cur->doc != doc)
2858 xmlSetTreeDoc(cur, doc);
2863 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
2866 * @parent: the parent node
2867 * @ns: a namespace if any
2868 * @name: the name of the child
2869 * @content: the XML content of the child if any.
2871 * Creation of a new child element, added at the end of @parent children list.
2872 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2873 * created element inherits the namespace of @parent. If @content is non NULL,
2874 * a child list containing the TEXTs and ENTITY_REFs node will be created.
2875 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2876 * references. XML special chars must be escaped first by using
2877 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
2879 * Returns a pointer to the new node object.
2882 xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2883 const xmlChar *name, const xmlChar *content) {
2884 xmlNodePtr cur, prev;
2886 if (parent == NULL) {
2888 xmlGenericError(xmlGenericErrorContext,
2889 "xmlNewChild : parent == NULL\n");
2896 xmlGenericError(xmlGenericErrorContext,
2897 "xmlNewChild : name == NULL\n");
2903 * Allocate a new node
2905 if (parent->type == XML_ELEMENT_NODE) {
2907 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2909 cur = xmlNewDocNode(parent->doc, ns, name, content);
2910 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2911 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2913 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2915 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
2916 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2917 cur = xmlNewDocNode( parent->doc, ns, name, content);
2921 if (cur == NULL) return(NULL);
2924 * add the new element at the end of the children list.
2926 cur->type = XML_ELEMENT_NODE;
2927 cur->parent = parent;
2928 cur->doc = parent->doc;
2929 if (parent->children == NULL) {
2930 parent->children = cur;
2933 prev = parent->last;
2941 #endif /* LIBXML_TREE_ENABLED */
2944 * xmlAddPropSibling:
2945 * @prev: the attribute to which @prop is added after
2946 * @cur: the base attribute passed to calling function
2947 * @prop: the new attribute
2949 * Add a new attribute after @prev using @cur as base attribute.
2950 * When inserting before @cur, @prev is passed as @cur->prev.
2951 * When inserting after @cur, @prev is passed as @cur.
2952 * If an existing attribute is found it is detroyed prior to adding @prop.
2954 * Returns the attribute being inserted or NULL in case of error.
2957 xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2960 if ((cur == NULL) || (cur->type != XML_ATTRIBUTE_NODE) ||
2961 (prop == NULL) || (prop->type != XML_ATTRIBUTE_NODE) ||
2962 ((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)))
2965 /* check if an attribute with the same name exists */
2966 if (prop->ns == NULL)
2967 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2969 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2971 if (prop->doc != cur->doc) {
2972 xmlSetTreeDoc(prop, cur->doc);
2974 prop->parent = cur->parent;
2977 prop->next = prev->next;
2980 prop->next->prev = prop;
2985 if (prop->prev == NULL && prop->parent != NULL)
2986 prop->parent->properties = (xmlAttrPtr) prop;
2987 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2988 /* different instance, destroy it (attributes must be unique) */
2989 xmlRemoveProp((xmlAttrPtr) attr);
2995 * xmlAddNextSibling:
2996 * @cur: the child node
2997 * @elem: the new node
2999 * Add a new node @elem as the next sibling of @cur
3000 * If the new node was already inserted in a document it is
3001 * first unlinked from its existing context.
3002 * As a result of text merging @elem may be freed.
3003 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3004 * If there is an attribute with equal name, it is first destroyed.
3006 * Returns the new node or NULL in case of error.
3009 xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
3010 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3012 xmlGenericError(xmlGenericErrorContext,
3013 "xmlAddNextSibling : cur == NULL\n");
3017 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
3019 xmlGenericError(xmlGenericErrorContext,
3020 "xmlAddNextSibling : elem == NULL\n");
3027 xmlGenericError(xmlGenericErrorContext,
3028 "xmlAddNextSibling : cur == elem\n");
3033 xmlUnlinkNode(elem);
3035 if (elem->type == XML_TEXT_NODE) {
3036 if (cur->type == XML_TEXT_NODE) {
3037 xmlNodeAddContent(cur, elem->content);
3041 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
3042 (cur->name == cur->next->name)) {
3045 tmp = xmlStrdup(elem->content);
3046 tmp = xmlStrcat(tmp, cur->next->content);
3047 xmlNodeSetContent(cur->next, tmp);
3052 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3053 return xmlAddPropSibling(cur, cur, elem);
3056 if (elem->doc != cur->doc) {
3057 xmlSetTreeDoc(elem, cur->doc);
3059 elem->parent = cur->parent;
3061 elem->next = cur->next;
3063 if (elem->next != NULL)
3064 elem->next->prev = elem;
3065 if ((elem->parent != NULL) && (elem->parent->last == cur))
3066 elem->parent->last = elem;
3070 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
3071 defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
3073 * xmlAddPrevSibling:
3074 * @cur: the child node
3075 * @elem: the new node
3077 * Add a new node @elem as the previous sibling of @cur
3078 * merging adjacent TEXT nodes (@elem may be freed)
3079 * If the new node was already inserted in a document it is
3080 * first unlinked from its existing context.
3081 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3082 * If there is an attribute with equal name, it is first destroyed.
3084 * Returns the new node or NULL in case of error.
3087 xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
3088 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3090 xmlGenericError(xmlGenericErrorContext,
3091 "xmlAddPrevSibling : cur == NULL\n");
3095 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
3097 xmlGenericError(xmlGenericErrorContext,
3098 "xmlAddPrevSibling : elem == NULL\n");
3105 xmlGenericError(xmlGenericErrorContext,
3106 "xmlAddPrevSibling : cur == elem\n");
3111 xmlUnlinkNode(elem);
3113 if (elem->type == XML_TEXT_NODE) {
3114 if (cur->type == XML_TEXT_NODE) {
3117 tmp = xmlStrdup(elem->content);
3118 tmp = xmlStrcat(tmp, cur->content);
3119 xmlNodeSetContent(cur, tmp);
3124 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3125 (cur->name == cur->prev->name)) {
3126 xmlNodeAddContent(cur->prev, elem->content);
3130 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3131 return xmlAddPropSibling(cur->prev, cur, elem);
3134 if (elem->doc != cur->doc) {
3135 xmlSetTreeDoc(elem, cur->doc);
3137 elem->parent = cur->parent;
3139 elem->prev = cur->prev;
3141 if (elem->prev != NULL)
3142 elem->prev->next = elem;
3143 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
3144 elem->parent->children = elem;
3148 #endif /* LIBXML_TREE_ENABLED */
3152 * @cur: the child node
3153 * @elem: the new node
3155 * Add a new element @elem to the list of siblings of @cur
3156 * merging adjacent TEXT nodes (@elem may be freed)
3157 * If the new element was already inserted in a document it is
3158 * first unlinked from its existing context.
3160 * Returns the new element or NULL in case of error.
3163 xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3166 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3168 xmlGenericError(xmlGenericErrorContext,
3169 "xmlAddSibling : cur == NULL\n");
3174 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
3176 xmlGenericError(xmlGenericErrorContext,
3177 "xmlAddSibling : elem == NULL\n");
3184 xmlGenericError(xmlGenericErrorContext,
3185 "xmlAddSibling : cur == elem\n");
3191 * Constant time is we can rely on the ->parent->last to find
3194 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
3195 (cur->parent->children != NULL) &&
3196 (cur->parent->last != NULL) &&
3197 (cur->parent->last->next == NULL)) {
3198 cur = cur->parent->last;
3200 while (cur->next != NULL) cur = cur->next;
3203 xmlUnlinkNode(elem);
3205 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3206 (cur->name == elem->name)) {
3207 xmlNodeAddContent(cur, elem->content);
3210 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3211 return xmlAddPropSibling(cur, cur, elem);
3214 if (elem->doc != cur->doc) {
3215 xmlSetTreeDoc(elem, cur->doc);
3217 parent = cur->parent;
3220 elem->parent = parent;
3223 parent->last = elem;
3230 * @parent: the parent node
3231 * @cur: the first node in the list
3233 * Add a list of node at the end of the child list of the parent
3234 * merging adjacent TEXT nodes (@cur may be freed)
3236 * Returns the last child or NULL in case of error.
3239 xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3242 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3244 xmlGenericError(xmlGenericErrorContext,
3245 "xmlAddChildList : parent == NULL\n");
3250 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3252 xmlGenericError(xmlGenericErrorContext,
3253 "xmlAddChildList : child == NULL\n");
3258 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3259 (cur->doc != parent->doc)) {
3261 xmlGenericError(xmlGenericErrorContext,
3262 "Elements moved to a different document\n");
3267 * add the first element at the end of the children list.
3270 if (parent->children == NULL) {
3271 parent->children = cur;
3274 * If cur and parent->last both are TEXT nodes, then merge them.
3276 if ((cur->type == XML_TEXT_NODE) &&
3277 (parent->last->type == XML_TEXT_NODE) &&
3278 (cur->name == parent->last->name)) {
3279 xmlNodeAddContent(parent->last, cur->content);
3281 * if it's the only child, nothing more to be done.
3283 if (cur->next == NULL) {
3285 return(parent->last);
3291 prev = parent->last;
3295 while (cur->next != NULL) {
3296 cur->parent = parent;
3297 if (cur->doc != parent->doc) {
3298 xmlSetTreeDoc(cur, parent->doc);
3302 cur->parent = parent;
3303 /* the parent may not be linked to a doc ! */
3304 if (cur->doc != parent->doc) {
3305 xmlSetTreeDoc(cur, parent->doc);
3314 * @parent: the parent node
3315 * @cur: the child node
3317 * Add a new node to @parent, at the end of the child (or property) list
3318 * merging adjacent TEXT nodes (in which case @cur is freed)
3319 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3320 * If there is an attribute with equal name, it is first destroyed.
3322 * Returns the child or NULL in case of error.
3325 xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3328 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3330 xmlGenericError(xmlGenericErrorContext,
3331 "xmlAddChild : parent == NULL\n");
3336 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3338 xmlGenericError(xmlGenericErrorContext,
3339 "xmlAddChild : child == NULL\n");
3344 if (parent == cur) {
3346 xmlGenericError(xmlGenericErrorContext,
3347 "xmlAddChild : parent == cur\n");
3352 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
3353 * cur is then freed.
3355 if (cur->type == XML_TEXT_NODE) {
3356 if ((parent->type == XML_TEXT_NODE) &&
3357 (parent->content != NULL) &&
3358 (parent->name == cur->name)) {
3359 xmlNodeAddContent(parent, cur->content);
3363 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
3364 (parent->last->name == cur->name) &&
3365 (parent->last != cur)) {
3366 xmlNodeAddContent(parent->last, cur->content);
3368 return(parent->last);
3373 * add the new element at the end of the children list.
3376 cur->parent = parent;
3377 if (cur->doc != parent->doc) {
3378 xmlSetTreeDoc(cur, parent->doc);
3380 /* this check prevents a loop on tree-traversions if a developer
3381 * tries to add a node to its parent multiple times
3389 if ((parent->type == XML_TEXT_NODE) &&
3390 (parent->content != NULL) &&
3392 xmlNodeAddContent(parent, cur->content);
3396 if (cur->type == XML_ATTRIBUTE_NODE) {
3397 if (parent->type != XML_ELEMENT_NODE)
3399 if (parent->properties != NULL) {
3400 /* check if an attribute with the same name exists */
3401 xmlAttrPtr lastattr;
3403 if (cur->ns == NULL)
3404 lastattr = xmlHasNsProp(parent, cur->name, NULL);
3406 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
3407 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
3408 /* different instance, destroy it (attributes must be unique) */
3409 xmlUnlinkNode((xmlNodePtr) lastattr);
3410 xmlFreeProp(lastattr);
3412 if (lastattr == (xmlAttrPtr) cur)
3416 if (parent->properties == NULL) {
3417 parent->properties = (xmlAttrPtr) cur;
3420 xmlAttrPtr lastattr = parent->properties;
3421 while (lastattr->next != NULL) {
3422 lastattr = lastattr->next;
3424 lastattr->next = (xmlAttrPtr) cur;
3425 ((xmlAttrPtr) cur)->prev = lastattr;
3428 if (parent->children == NULL) {
3429 parent->children = cur;
3432 prev = parent->last;
3443 * @parent: the parent node
3445 * Search the last child of a node.
3446 * Returns the last child or NULL if none.
3449 xmlGetLastChild(const xmlNode *parent) {
3450 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3452 xmlGenericError(xmlGenericErrorContext,
3453 "xmlGetLastChild : parent == NULL\n");
3457 return(parent->last);
3460 #ifdef LIBXML_TREE_ENABLED
3462 * 5 interfaces from DOM ElementTraversal
3466 * xmlChildElementCount:
3467 * @parent: the parent node
3469 * Finds the current number of child nodes of that element which are
3471 * Note the handling of entities references is different than in
3472 * the W3C DOM element traversal spec since we don't have back reference
3473 * from entities content to entities references.
3475 * Returns the count of element child or 0 if not available
3478 xmlChildElementCount(xmlNodePtr parent) {
3479 unsigned long ret = 0;
3480 xmlNodePtr cur = NULL;
3484 switch (parent->type) {
3485 case XML_ELEMENT_NODE:
3486 case XML_ENTITY_NODE:
3487 case XML_DOCUMENT_NODE:
3488 case XML_DOCUMENT_FRAG_NODE:
3489 case XML_HTML_DOCUMENT_NODE:
3490 cur = parent->children;
3495 while (cur != NULL) {
3496 if (cur->type == XML_ELEMENT_NODE)
3504 * xmlFirstElementChild:
3505 * @parent: the parent node
3507 * Finds the first child node of that element which is a Element node
3508 * Note the handling of entities references is different than in
3509 * the W3C DOM element traversal spec since we don't have back reference
3510 * from entities content to entities references.
3512 * Returns the first element child or NULL if not available
3515 xmlFirstElementChild(xmlNodePtr parent) {
3516 xmlNodePtr cur = NULL;
3520 switch (parent->type) {
3521 case XML_ELEMENT_NODE:
3522 case XML_ENTITY_NODE:
3523 case XML_DOCUMENT_NODE:
3524 case XML_DOCUMENT_FRAG_NODE:
3525 case XML_HTML_DOCUMENT_NODE:
3526 cur = parent->children;
3531 while (cur != NULL) {
3532 if (cur->type == XML_ELEMENT_NODE)
3540 * xmlLastElementChild:
3541 * @parent: the parent node
3543 * Finds the last child node of that element which is a Element node
3544 * Note the handling of entities references is different than in
3545 * the W3C DOM element traversal spec since we don't have back reference
3546 * from entities content to entities references.
3548 * Returns the last element child or NULL if not available
3551 xmlLastElementChild(xmlNodePtr parent) {
3552 xmlNodePtr cur = NULL;
3556 switch (parent->type) {
3557 case XML_ELEMENT_NODE:
3558 case XML_ENTITY_NODE:
3559 case XML_DOCUMENT_NODE:
3560 case XML_DOCUMENT_FRAG_NODE:
3561 case XML_HTML_DOCUMENT_NODE:
3567 while (cur != NULL) {
3568 if (cur->type == XML_ELEMENT_NODE)
3576 * xmlPreviousElementSibling:
3577 * @node: the current node
3579 * Finds the first closest previous sibling of the node which is an
3581 * Note the handling of entities references is different than in
3582 * the W3C DOM element traversal spec since we don't have back reference
3583 * from entities content to entities references.
3585 * Returns the previous element sibling or NULL if not available
3588 xmlPreviousElementSibling(xmlNodePtr node) {
3591 switch (node->type) {
3592 case XML_ELEMENT_NODE:
3594 case XML_CDATA_SECTION_NODE:
3595 case XML_ENTITY_REF_NODE:
3596 case XML_ENTITY_NODE:
3598 case XML_COMMENT_NODE:
3599 case XML_XINCLUDE_START:
3600 case XML_XINCLUDE_END:
3606 while (node != NULL) {
3607 if (node->type == XML_ELEMENT_NODE)
3615 * xmlNextElementSibling:
3616 * @node: the current node
3618 * Finds the first closest next sibling of the node which is an
3620 * Note the handling of entities references is different than in
3621 * the W3C DOM element traversal spec since we don't have back reference
3622 * from entities content to entities references.
3624 * Returns the next element sibling or NULL if not available
3627 xmlNextElementSibling(xmlNodePtr node) {
3630 switch (node->type) {
3631 case XML_ELEMENT_NODE:
3633 case XML_CDATA_SECTION_NODE:
3634 case XML_ENTITY_REF_NODE:
3635 case XML_ENTITY_NODE:
3637 case XML_COMMENT_NODE:
3639 case XML_XINCLUDE_START:
3640 case XML_XINCLUDE_END:
3646 while (node != NULL) {
3647 if (node->type == XML_ELEMENT_NODE)
3654 #endif /* LIBXML_TREE_ENABLED */
3658 * @cur: the first node in the list
3660 * Free a node and all its siblings, this is a recursive behaviour, all
3661 * the children are freed too.
3664 xmlFreeNodeList(xmlNodePtr cur) {
3666 xmlDictPtr dict = NULL;
3668 if (cur == NULL) return;
3669 if (cur->type == XML_NAMESPACE_DECL) {
3670 xmlFreeNsList((xmlNsPtr) cur);
3673 if ((cur->type == XML_DOCUMENT_NODE) ||
3674 #ifdef LIBXML_DOCB_ENABLED
3675 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
3677 (cur->type == XML_HTML_DOCUMENT_NODE)) {
3678 xmlFreeDoc((xmlDocPtr) cur);
3681 if (cur->doc != NULL) dict = cur->doc->dict;
3682 while (cur != NULL) {
3684 if (cur->type != XML_DTD_NODE) {
3686 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3687 xmlDeregisterNodeDefaultValue(cur);
3689 if ((cur->children != NULL) &&
3690 (cur->type != XML_ENTITY_REF_NODE))
3691 xmlFreeNodeList(cur->children);
3692 if (((cur->type == XML_ELEMENT_NODE) ||
3693 (cur->type == XML_XINCLUDE_START) ||
3694 (cur->type == XML_XINCLUDE_END)) &&
3695 (cur->properties != NULL))
3696 xmlFreePropList(cur->properties);
3697 if ((cur->type != XML_ELEMENT_NODE) &&
3698 (cur->type != XML_XINCLUDE_START) &&
3699 (cur->type != XML_XINCLUDE_END) &&
3700 (cur->type != XML_ENTITY_REF_NODE) &&
3701 (cur->content != (xmlChar *) &(cur->properties))) {
3702 DICT_FREE(cur->content)
3704 if (((cur->type == XML_ELEMENT_NODE) ||
3705 (cur->type == XML_XINCLUDE_START) ||
3706 (cur->type == XML_XINCLUDE_END)) &&
3707 (cur->nsDef != NULL))
3708 xmlFreeNsList(cur->nsDef);
3711 * When a node is a text node or a comment, it uses a global static
3712 * variable for the name of the node.
3713 * Otherwise the node name might come from the document's
3716 if ((cur->name != NULL) &&
3717 (cur->type != XML_TEXT_NODE) &&
3718 (cur->type != XML_COMMENT_NODE))
3719 DICT_FREE(cur->name)
3730 * Free a node, this is a recursive behaviour, all the children are freed too.
3731 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3734 xmlFreeNode(xmlNodePtr cur) {
3735 xmlDictPtr dict = NULL;
3737 if (cur == NULL) return;
3739 /* use xmlFreeDtd for DTD nodes */
3740 if (cur->type == XML_DTD_NODE) {
3741 xmlFreeDtd((xmlDtdPtr) cur);
3744 if (cur->type == XML_NAMESPACE_DECL) {
3745 xmlFreeNs((xmlNsPtr) cur);
3748 if (cur->type == XML_ATTRIBUTE_NODE) {
3749 xmlFreeProp((xmlAttrPtr) cur);
3753 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3754 xmlDeregisterNodeDefaultValue(cur);
3756 if (cur->doc != NULL) dict = cur->doc->dict;
3758 if (cur->type == XML_ENTITY_DECL) {
3759 xmlEntityPtr ent = (xmlEntityPtr) cur;
3760 DICT_FREE(ent->SystemID);
3761 DICT_FREE(ent->ExternalID);
3763 if ((cur->children != NULL) &&
3764 (cur->type != XML_ENTITY_REF_NODE))
3765 xmlFreeNodeList(cur->children);
3766 if (((cur->type == XML_ELEMENT_NODE) ||
3767 (cur->type == XML_XINCLUDE_START) ||
3768 (cur->type == XML_XINCLUDE_END)) &&
3769 (cur->properties != NULL))
3770 xmlFreePropList(cur->properties);
3771 if ((cur->type != XML_ELEMENT_NODE) &&
3772 (cur->content != NULL) &&
3773 (cur->type != XML_ENTITY_REF_NODE) &&
3774 (cur->type != XML_XINCLUDE_END) &&
3775 (cur->type != XML_XINCLUDE_START) &&
3776 (cur->content != (xmlChar *) &(cur->properties))) {
3777 DICT_FREE(cur->content)
3781 * When a node is a text node or a comment, it uses a global static
3782 * variable for the name of the node.
3783 * Otherwise the node name might come from the document's dictionary
3785 if ((cur->name != NULL) &&
3786 (cur->type != XML_TEXT_NODE) &&
3787 (cur->type != XML_COMMENT_NODE))
3788 DICT_FREE(cur->name)
3790 if (((cur->type == XML_ELEMENT_NODE) ||
3791 (cur->type == XML_XINCLUDE_START) ||
3792 (cur->type == XML_XINCLUDE_END)) &&
3793 (cur->nsDef != NULL))
3794 xmlFreeNsList(cur->nsDef);
3802 * Unlink a node from it's current context, the node is not freed
3803 * If one need to free the node, use xmlFreeNode() routine after the
3804 * unlink to discard it.
3805 * Note that namespace nodes can't be unlinked as they do not have
3806 * pointer to their parent.
3809 xmlUnlinkNode(xmlNodePtr cur) {
3812 xmlGenericError(xmlGenericErrorContext,
3813 "xmlUnlinkNode : node == NULL\n");
3817 if (cur->type == XML_NAMESPACE_DECL)
3819 if (cur->type == XML_DTD_NODE) {
3823 if (doc->intSubset == (xmlDtdPtr) cur)
3824 doc->intSubset = NULL;
3825 if (doc->extSubset == (xmlDtdPtr) cur)
3826 doc->extSubset = NULL;
3829 if (cur->type == XML_ENTITY_DECL) {
3833 if (doc->intSubset != NULL) {
3834 if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur)
3835 xmlHashRemoveEntry(doc->intSubset->entities, cur->name,
3837 if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur)
3838 xmlHashRemoveEntry(doc->intSubset->pentities, cur->name,
3841 if (doc->extSubset != NULL) {
3842 if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur)
3843 xmlHashRemoveEntry(doc->extSubset->entities, cur->name,
3845 if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur)
3846 xmlHashRemoveEntry(doc->extSubset->pentities, cur->name,
3851 if (cur->parent != NULL) {
3853 parent = cur->parent;
3854 if (cur->type == XML_ATTRIBUTE_NODE) {
3855 if (parent->properties == (xmlAttrPtr) cur)
3856 parent->properties = ((xmlAttrPtr) cur)->next;
3858 if (parent->children == cur)
3859 parent->children = cur->next;
3860 if (parent->last == cur)
3861 parent->last = cur->prev;
3865 if (cur->next != NULL)
3866 cur->next->prev = cur->prev;
3867 if (cur->prev != NULL)
3868 cur->prev->next = cur->next;
3869 cur->next = cur->prev = NULL;
3872 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
3875 * @old: the old node
3878 * Unlink the old node from its current context, prune the new one
3879 * at the same place. If @cur was already inserted in a document it is
3880 * first unlinked from its existing context.
3882 * Returns the @old node
3885 xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
3886 if (old == cur) return(NULL);
3887 if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) ||
3888 (old->parent == NULL)) {
3890 xmlGenericError(xmlGenericErrorContext,
3891 "xmlReplaceNode : old == NULL or without parent\n");
3895 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3902 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3904 xmlGenericError(xmlGenericErrorContext,
3905 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3909 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3911 xmlGenericError(xmlGenericErrorContext,
3912 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3917 xmlSetTreeDoc(cur, old->doc);
3918 cur->parent = old->parent;
3919 cur->next = old->next;
3920 if (cur->next != NULL)
3921 cur->next->prev = cur;
3922 cur->prev = old->prev;
3923 if (cur->prev != NULL)
3924 cur->prev->next = cur;
3925 if (cur->parent != NULL) {
3926 if (cur->type == XML_ATTRIBUTE_NODE) {
3927 if (cur->parent->properties == (xmlAttrPtr)old)
3928 cur->parent->properties = ((xmlAttrPtr) cur);
3930 if (cur->parent->children == old)
3931 cur->parent->children = cur;
3932 if (cur->parent->last == old)
3933 cur->parent->last = cur;
3936 old->next = old->prev = NULL;
3940 #endif /* LIBXML_TREE_ENABLED */
3942 /************************************************************************
3946 ************************************************************************/
3950 * @cur: the namespace
3952 * Do a copy of the namespace.
3954 * Returns: a new #xmlNsPtr, or NULL in case of error.
3957 xmlCopyNamespace(xmlNsPtr cur) {
3960 if (cur == NULL) return(NULL);
3961 switch (cur->type) {
3962 case XML_LOCAL_NAMESPACE:
3963 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3967 xmlGenericError(xmlGenericErrorContext,
3968 "xmlCopyNamespace: invalid type %d\n", cur->type);
3976 * xmlCopyNamespaceList:
3977 * @cur: the first namespace
3979 * Do a copy of an namespace list.
3981 * Returns: a new #xmlNsPtr, or NULL in case of error.
3984 xmlCopyNamespaceList(xmlNsPtr cur) {
3985 xmlNsPtr ret = NULL;
3986 xmlNsPtr p = NULL,q;
3988 while (cur != NULL) {
3989 q = xmlCopyNamespace(cur);
4002 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
4005 xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
4008 if (cur == NULL) return(NULL);
4009 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4012 ret = xmlNewDocProp(target->doc, cur->name, NULL);
4013 else if (doc != NULL)
4014 ret = xmlNewDocProp(doc, cur->name, NULL);
4015 else if (cur->parent != NULL)
4016 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
4017 else if (cur->children != NULL)
4018 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
4020 ret = xmlNewDocProp(NULL, cur->name, NULL);
4021 if (ret == NULL) return(NULL);
4022 ret->parent = target;
4024 if ((cur->ns != NULL) && (target != NULL)) {
4027 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
4030 * Humm, we are copying an element whose namespace is defined
4031 * out of the new tree scope. Search it in the original tree
4032 * and add it at the top of the new tree
4034 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
4036 xmlNodePtr root = target;
4037 xmlNodePtr pred = NULL;
4039 while (root->parent != NULL) {
4041 root = root->parent;
4043 if (root == (xmlNodePtr) target->doc) {
4044 /* correct possibly cycling above the document elt */
4047 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4051 * we have to find something appropriate here since
4052 * we cant be sure, that the namespce we found is identified
4055 if (xmlStrEqual(ns->href, cur->ns->href)) {
4056 /* this is the nice case */
4060 * we are in trouble: we need a new reconcilied namespace.
4063 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
4070 if (cur->children != NULL) {
4073 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
4075 tmp = ret->children;
4076 while (tmp != NULL) {
4077 /* tmp->parent = (xmlNodePtr)ret; */
4078 if (tmp->next == NULL)
4086 if ((target!= NULL) && (cur!= NULL) &&
4087 (target->doc != NULL) && (cur->doc != NULL) &&
4088 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
4089 if (xmlIsID(cur->doc, cur->parent, cur)) {
4092 id = xmlNodeListGetString(cur->doc, cur->children, 1);
4094 xmlAddID(NULL, target->doc, id, ret);
4104 * @target: the element where the attribute will be grafted
4105 * @cur: the attribute
4107 * Do a copy of the attribute.
4109 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4112 xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
4113 return xmlCopyPropInternal(NULL, target, cur);
4118 * @target: the element where the attributes will be grafted
4119 * @cur: the first attribute
4121 * Do a copy of an attribute list.
4123 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4126 xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
4127 xmlAttrPtr ret = NULL;
4128 xmlAttrPtr p = NULL,q;
4130 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4132 while (cur != NULL) {
4133 q = xmlCopyProp(target, cur);
4149 * NOTE about the CopyNode operations !
4151 * They are split into external and internal parts for one
4152 * tricky reason: namespaces. Doing a direct copy of a node
4153 * say RPM:Copyright without changing the namespace pointer to
4154 * something else can produce stale links. One way to do it is
4155 * to keep a reference counter but this doesn't work as soon
4156 * as one move the element or the subtree out of the scope of
4157 * the existing namespace. The actual solution seems to add
4158 * a copy of the namespace at the top of the copied tree if
4159 * not available in the subtree.
4160 * Hence two functions, the public front-end call the inner ones
4161 * The argument "recursive" normally indicates a recursive copy
4162 * of the node with values 0 (no) and 1 (yes). For XInclude,
4163 * however, we allow a value of 2 to indicate copy properties and
4164 * namespace info, but don't recurse on children.
4168 xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
4172 if (node == NULL) return(NULL);
4173 switch (node->type) {
4175 case XML_CDATA_SECTION_NODE:
4176 case XML_ELEMENT_NODE:
4177 case XML_DOCUMENT_FRAG_NODE:
4178 case XML_ENTITY_REF_NODE:
4179 case XML_ENTITY_NODE:
4181 case XML_COMMENT_NODE:
4182 case XML_XINCLUDE_START:
4183 case XML_XINCLUDE_END:
4185 case XML_ATTRIBUTE_NODE:
4186 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
4187 case XML_NAMESPACE_DECL:
4188 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
4190 case XML_DOCUMENT_NODE:
4191 case XML_HTML_DOCUMENT_NODE:
4192 #ifdef LIBXML_DOCB_ENABLED
4193 case XML_DOCB_DOCUMENT_NODE:
4195 #ifdef LIBXML_TREE_ENABLED
4196 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
4197 #endif /* LIBXML_TREE_ENABLED */
4198 case XML_DOCUMENT_TYPE_NODE:
4199 case XML_NOTATION_NODE:
4201 case XML_ELEMENT_DECL:
4202 case XML_ATTRIBUTE_DECL:
4203 case XML_ENTITY_DECL:
4208 * Allocate a new node and fill the fields.
4210 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4212 xmlTreeErrMemory("copying node");
4215 memset(ret, 0, sizeof(xmlNode));
4216 ret->type = node->type;
4219 ret->parent = parent;
4220 if (node->name == xmlStringText)
4221 ret->name = xmlStringText;
4222 else if (node->name == xmlStringTextNoenc)
4223 ret->name = xmlStringTextNoenc;
4224 else if (node->name == xmlStringComment)
4225 ret->name = xmlStringComment;
4226 else if (node->name != NULL) {
4227 if ((doc != NULL) && (doc->dict != NULL))
4228 ret->name = xmlDictLookup(doc->dict, node->name, -1);
4230 ret->name = xmlStrdup(node->name);
4232 if ((node->type != XML_ELEMENT_NODE) &&
4233 (node->content != NULL) &&
4234 (node->type != XML_ENTITY_REF_NODE) &&
4235 (node->type != XML_XINCLUDE_END) &&
4236 (node->type != XML_XINCLUDE_START)) {
4237 ret->content = xmlStrdup(node->content);
4239 if (node->type == XML_ELEMENT_NODE)
4240 ret->line = node->line;
4242 if (parent != NULL) {
4246 * this is a tricky part for the node register thing:
4247 * in case ret does get coalesced in xmlAddChild
4248 * the deregister-node callback is called; so we register ret now already
4250 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
4251 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4253 tmp = xmlAddChild(parent, ret);
4254 /* node could have coalesced */
4261 if (((node->type == XML_ELEMENT_NODE) ||
4262 (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
4263 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
4265 if (node->ns != NULL) {
4268 ns = xmlSearchNs(doc, ret, node->ns->prefix);
4271 * Humm, we are copying an element whose namespace is defined
4272 * out of the new tree scope. Search it in the original tree
4273 * and add it at the top of the new tree
4275 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
4277 xmlNodePtr root = ret;
4279 while (root->parent != NULL) root = root->parent;
4280 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4282 ret->ns = xmlNewReconciliedNs(doc, ret, node->ns);
4286 * reference the existing namespace definition in our own tree.
4291 if (((node->type == XML_ELEMENT_NODE) ||
4292 (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
4293 ret->properties = xmlCopyPropList(ret, node->properties);
4294 if (node->type == XML_ENTITY_REF_NODE) {
4295 if ((doc == NULL) || (node->doc != doc)) {
4297 * The copied node will go into a separate document, so
4298 * to avoid dangling references to the ENTITY_DECL node
4299 * we cannot keep the reference. Try to find it in the
4302 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
4304 ret->children = node->children;
4306 ret->last = ret->children;
4307 } else if ((node->children != NULL) && (extended != 2)) {
4308 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
4309 UPDATE_LAST_CHILD_AND_PARENT(ret)
4313 /* if parent != NULL we already registered the node above */
4314 if ((parent == NULL) &&
4315 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
4316 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4321 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
4322 xmlNodePtr ret = NULL;
4323 xmlNodePtr p = NULL,q;
4325 while (node != NULL) {
4326 #ifdef LIBXML_TREE_ENABLED
4327 if (node->type == XML_DTD_NODE ) {
4332 if (doc->intSubset == NULL) {
4333 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
4334 if (q == NULL) return(NULL);
4337 doc->intSubset = (xmlDtdPtr) q;
4338 xmlAddChild(parent, q);
4340 q = (xmlNodePtr) doc->intSubset;
4341 xmlAddChild(parent, q);
4344 #endif /* LIBXML_TREE_ENABLED */
4345 q = xmlStaticCopyNode(node, doc, parent, 1);
4346 if (q == NULL) return(NULL);
4350 } else if (p != q) {
4351 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
4364 * @extended: if 1 do a recursive copy (properties, namespaces and children
4366 * if 2 copy properties and namespaces (when applicable)
4368 * Do a copy of the node.
4370 * Returns: a new #xmlNodePtr, or NULL in case of error.
4373 xmlCopyNode(xmlNodePtr node, int extended) {
4376 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
4383 * @doc: the document
4384 * @extended: if 1 do a recursive copy (properties, namespaces and children
4386 * if 2 copy properties and namespaces (when applicable)
4388 * Do a copy of the node to a given document.
4390 * Returns: a new #xmlNodePtr, or NULL in case of error.
4393 xmlDocCopyNode(xmlNodePtr node, xmlDocPtr doc, int extended) {
4396 ret = xmlStaticCopyNode(node, doc, NULL, extended);
4401 * xmlDocCopyNodeList:
4402 * @doc: the target document
4403 * @node: the first node in the list.
4405 * Do a recursive copy of the node list.
4407 * Returns: a new #xmlNodePtr, or NULL in case of error.
4409 xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, xmlNodePtr node) {
4410 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4416 * @node: the first node in the list.
4418 * Do a recursive copy of the node list.
4419 * Use xmlDocCopyNodeList() if possible to ensure string interning.
4421 * Returns: a new #xmlNodePtr, or NULL in case of error.
4423 xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
4424 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4428 #if defined(LIBXML_TREE_ENABLED)
4433 * Do a copy of the dtd.
4435 * Returns: a new #xmlDtdPtr, or NULL in case of error.
4438 xmlCopyDtd(xmlDtdPtr dtd) {
4440 xmlNodePtr cur, p = NULL, q;
4442 if (dtd == NULL) return(NULL);
4443 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4444 if (ret == NULL) return(NULL);
4445 if (dtd->entities != NULL)
4446 ret->entities = (void *) xmlCopyEntitiesTable(
4447 (xmlEntitiesTablePtr) dtd->entities);
4448 if (dtd->notations != NULL)
4449 ret->notations = (void *) xmlCopyNotationTable(
4450 (xmlNotationTablePtr) dtd->notations);
4451 if (dtd->elements != NULL)
4452 ret->elements = (void *) xmlCopyElementTable(
4453 (xmlElementTablePtr) dtd->elements);
4454 if (dtd->attributes != NULL)
4455 ret->attributes = (void *) xmlCopyAttributeTable(
4456 (xmlAttributeTablePtr) dtd->attributes);
4457 if (dtd->pentities != NULL)
4458 ret->pentities = (void *) xmlCopyEntitiesTable(
4459 (xmlEntitiesTablePtr) dtd->pentities);
4461 cur = dtd->children;
4462 while (cur != NULL) {
4465 if (cur->type == XML_ENTITY_DECL) {
4466 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4467 switch (tmp->etype) {
4468 case XML_INTERNAL_GENERAL_ENTITY:
4469 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4470 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4471 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4473 case XML_INTERNAL_PARAMETER_ENTITY:
4474 case XML_EXTERNAL_PARAMETER_ENTITY:
4476 xmlGetParameterEntityFromDtd(ret, tmp->name);
4478 case XML_INTERNAL_PREDEFINED_ENTITY:
4481 } else if (cur->type == XML_ELEMENT_DECL) {
4482 xmlElementPtr tmp = (xmlElementPtr) cur;
4484 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4485 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4486 xmlAttributePtr tmp = (xmlAttributePtr) cur;
4488 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4489 } else if (cur->type == XML_COMMENT_NODE) {
4490 q = xmlCopyNode(cur, 0);
4504 q->parent = (xmlNodePtr) ret;
4515 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
4518 * @doc: the document
4519 * @recursive: if not zero do a recursive copy.
4521 * Do a copy of the document info. If recursive, the content tree will
4522 * be copied too as well as DTD, namespaces and entities.
4524 * Returns: a new #xmlDocPtr, or NULL in case of error.
4527 xmlCopyDoc(xmlDocPtr doc, int recursive) {
4530 if (doc == NULL) return(NULL);
4531 ret = xmlNewDoc(doc->version);
4532 if (ret == NULL) return(NULL);
4533 if (doc->name != NULL)
4534 ret->name = xmlMemStrdup(doc->name);
4535 if (doc->encoding != NULL)
4536 ret->encoding = xmlStrdup(doc->encoding);
4537 if (doc->URL != NULL)
4538 ret->URL = xmlStrdup(doc->URL);
4539 ret->charset = doc->charset;
4540 ret->compression = doc->compression;
4541 ret->standalone = doc->standalone;
4542 if (!recursive) return(ret);
4545 ret->children = NULL;
4546 #ifdef LIBXML_TREE_ENABLED
4547 if (doc->intSubset != NULL) {
4548 ret->intSubset = xmlCopyDtd(doc->intSubset);
4549 if (ret->intSubset == NULL) {
4553 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
4554 ret->intSubset->parent = ret;
4557 if (doc->oldNs != NULL)
4558 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4559 if (doc->children != NULL) {
4562 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4565 tmp = ret->children;
4566 while (tmp != NULL) {
4567 if (tmp->next == NULL)
4574 #endif /* LIBXML_TREE_ENABLED */
4576 /************************************************************************
4578 * Content access functions *
4580 ************************************************************************/
4583 * xmlGetLineNoInternal:
4585 * @depth: used to limit any risk of recursion
4587 * Get line number of @node.
4588 * Try to override the limitation of lines being store in 16 bits ints
4590 * Returns the line number if successful, -1 otherwise
4593 xmlGetLineNoInternal(const xmlNode *node, int depth)
4602 if ((node->type == XML_ELEMENT_NODE) ||
4603 (node->type == XML_TEXT_NODE) ||
4604 (node->type == XML_COMMENT_NODE) ||
4605 (node->type == XML_PI_NODE)) {
4606 if (node->line == 65535) {
4607 if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL))
4608 result = (long) node->psvi;
4609 else if ((node->type == XML_ELEMENT_NODE) &&
4610 (node->children != NULL))
4611 result = xmlGetLineNoInternal(node->children, depth + 1);
4612 else if (node->next != NULL)
4613 result = xmlGetLineNoInternal(node->next, depth + 1);
4614 else if (node->prev != NULL)
4615 result = xmlGetLineNoInternal(node->prev, depth + 1);
4617 if ((result == -1) || (result == 65535))
4618 result = (long) node->line;
4619 } else if ((node->prev != NULL) &&
4620 ((node->prev->type == XML_ELEMENT_NODE) ||
4621 (node->prev->type == XML_TEXT_NODE) ||
4622 (node->prev->type == XML_COMMENT_NODE) ||
4623 (node->prev->type == XML_PI_NODE)))
4624 result = xmlGetLineNoInternal(node->prev, depth + 1);
4625 else if ((node->parent != NULL) &&
4626 (node->parent->type == XML_ELEMENT_NODE))
4627 result = xmlGetLineNoInternal(node->parent, depth + 1);
4636 * Get line number of @node.
4637 * Try to override the limitation of lines being store in 16 bits ints
4638 * if XML_PARSE_BIG_LINES parser option was used
4640 * Returns the line number if successful, -1 otherwise
4643 xmlGetLineNo(const xmlNode *node)
4645 return(xmlGetLineNoInternal(node, 0));
4648 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
4653 * Build a structure based Path for the given node
4655 * Returns the new path or NULL in case of error. The caller must free
4656 * the returned string
4659 xmlGetNodePath(const xmlNode *node)
4661 const xmlNode *cur, *tmp, *next;
4662 xmlChar *buffer = NULL, *temp;
4668 int occur = 0, generic;
4670 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
4674 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4675 if (buffer == NULL) {
4676 xmlTreeErrMemory("getting node path");
4679 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4681 xmlTreeErrMemory("getting node path");
4692 if ((cur->type == XML_DOCUMENT_NODE) ||
4693 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4694 if (buffer[0] == '/')
4698 } else if (cur->type == XML_ELEMENT_NODE) {
4701 name = (const char *) cur->name;
4703 if (cur->ns->prefix != NULL) {
4704 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4705 (char *)cur->ns->prefix, (char *)cur->name);
4706 nametemp[sizeof(nametemp) - 1] = 0;
4710 * We cannot express named elements in the default
4711 * namespace, so use "*".
4720 * Thumbler index computation
4721 * TODO: the ocurence test seems bogus for namespaced names
4724 while (tmp != NULL) {
4725 if ((tmp->type == XML_ELEMENT_NODE) &&
4727 (xmlStrEqual(cur->name, tmp->name) &&
4728 ((tmp->ns == cur->ns) ||
4729 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4730 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4736 while (tmp != NULL && occur == 0) {
4737 if ((tmp->type == XML_ELEMENT_NODE) &&
4739 (xmlStrEqual(cur->name, tmp->name) &&
4740 ((tmp->ns == cur->ns) ||
4741 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4742 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4750 } else if (cur->type == XML_COMMENT_NODE) {
4756 * Thumbler index computation
4759 while (tmp != NULL) {
4760 if (tmp->type == XML_COMMENT_NODE)
4766 while (tmp != NULL && occur == 0) {
4767 if (tmp->type == XML_COMMENT_NODE)
4775 } else if ((cur->type == XML_TEXT_NODE) ||
4776 (cur->type == XML_CDATA_SECTION_NODE)) {
4782 * Thumbler index computation
4785 while (tmp != NULL) {
4786 if ((tmp->type == XML_TEXT_NODE) ||
4787 (tmp->type == XML_CDATA_SECTION_NODE))
4792 * Evaluate if this is the only text- or CDATA-section-node;
4793 * if yes, then we'll get "text()", otherwise "text()[1]".
4797 while (tmp != NULL) {
4798 if ((tmp->type == XML_TEXT_NODE) ||
4799 (tmp->type == XML_CDATA_SECTION_NODE))
4808 } else if (cur->type == XML_PI_NODE) {
4810 snprintf(nametemp, sizeof(nametemp) - 1,
4811 "processing-instruction('%s')", (char *)cur->name);
4812 nametemp[sizeof(nametemp) - 1] = 0;
4818 * Thumbler index computation
4821 while (tmp != NULL) {
4822 if ((tmp->type == XML_PI_NODE) &&
4823 (xmlStrEqual(cur->name, tmp->name)))
4829 while (tmp != NULL && occur == 0) {
4830 if ((tmp->type == XML_PI_NODE) &&
4831 (xmlStrEqual(cur->name, tmp->name)))
4840 } else if (cur->type == XML_ATTRIBUTE_NODE) {
4842 name = (const char *) (((xmlAttrPtr) cur)->name);
4844 if (cur->ns->prefix != NULL)
4845 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4846 (char *)cur->ns->prefix, (char *)cur->name);
4848 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4850 nametemp[sizeof(nametemp) - 1] = 0;
4853 next = ((xmlAttrPtr) cur)->parent;
4859 * Make sure there is enough room
4861 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4863 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4864 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4866 xmlTreeErrMemory("getting node path");
4872 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4874 xmlTreeErrMemory("getting node path");
4882 snprintf((char *) buf, buf_len, "%s%s%s",
4883 sep, name, (char *) buffer);
4885 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
4886 sep, name, occur, (char *) buffer);
4887 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
4889 } while (cur != NULL);
4893 #endif /* LIBXML_TREE_ENABLED */
4896 * xmlDocGetRootElement:
4897 * @doc: the document
4899 * Get the root element of the document (doc->children is a list
4900 * containing possibly comments, PIs, etc ...).
4902 * Returns the #xmlNodePtr for the root or NULL
4905 xmlDocGetRootElement(const xmlDoc *doc) {
4908 if (doc == NULL) return(NULL);
4909 ret = doc->children;
4910 while (ret != NULL) {
4911 if (ret->type == XML_ELEMENT_NODE)
4918 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
4920 * xmlDocSetRootElement:
4921 * @doc: the document
4922 * @root: the new document root element, if root is NULL no action is taken,
4923 * to remove a node from a document use xmlUnlinkNode(root) instead.
4925 * Set the root element of the document (doc->children is a list
4926 * containing possibly comments, PIs, etc ...).
4928 * Returns the old root element if any was found, NULL if root was NULL
4931 xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4932 xmlNodePtr old = NULL;
4934 if (doc == NULL) return(NULL);
4935 if ((root == NULL) || (root->type == XML_NAMESPACE_DECL))
4937 xmlUnlinkNode(root);
4938 xmlSetTreeDoc(root, doc);
4939 root->parent = (xmlNodePtr) doc;
4940 old = doc->children;
4941 while (old != NULL) {
4942 if (old->type == XML_ELEMENT_NODE)
4947 if (doc->children == NULL) {
4948 doc->children = root;
4951 xmlAddSibling(doc->children, root);
4954 xmlReplaceNode(old, root);
4960 #if defined(LIBXML_TREE_ENABLED)
4963 * @cur: the node being changed
4964 * @lang: the language description
4966 * Set the language of a node, i.e. the values of the xml:lang
4970 xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
4973 if (cur == NULL) return;
4976 case XML_CDATA_SECTION_NODE:
4977 case XML_COMMENT_NODE:
4978 case XML_DOCUMENT_NODE:
4979 case XML_DOCUMENT_TYPE_NODE:
4980 case XML_DOCUMENT_FRAG_NODE:
4981 case XML_NOTATION_NODE:
4982 case XML_HTML_DOCUMENT_NODE:
4984 case XML_ELEMENT_DECL:
4985 case XML_ATTRIBUTE_DECL:
4986 case XML_ENTITY_DECL:
4988 case XML_ENTITY_REF_NODE:
4989 case XML_ENTITY_NODE:
4990 case XML_NAMESPACE_DECL:
4991 #ifdef LIBXML_DOCB_ENABLED
4992 case XML_DOCB_DOCUMENT_NODE:
4994 case XML_XINCLUDE_START:
4995 case XML_XINCLUDE_END:
4997 case XML_ELEMENT_NODE:
4998 case XML_ATTRIBUTE_NODE:
5001 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5004 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
5006 #endif /* LIBXML_TREE_ENABLED */
5010 * @cur: the node being checked
5012 * Searches the language of a node, i.e. the values of the xml:lang
5013 * attribute or the one carried by the nearest ancestor.
5015 * Returns a pointer to the lang value, or NULL if not found
5016 * It's up to the caller to free the memory with xmlFree().
5019 xmlNodeGetLang(const xmlNode *cur) {
5022 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
5024 while (cur != NULL) {
5025 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
5034 #ifdef LIBXML_TREE_ENABLED
5036 * xmlNodeSetSpacePreserve:
5037 * @cur: the node being changed
5038 * @val: the xml:space value ("0": default, 1: "preserve")
5040 * Set (or reset) the space preserving behaviour of a node, i.e. the
5041 * value of the xml:space attribute.
5044 xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
5047 if (cur == NULL) return;
5050 case XML_CDATA_SECTION_NODE:
5051 case XML_COMMENT_NODE:
5052 case XML_DOCUMENT_NODE:
5053 case XML_DOCUMENT_TYPE_NODE:
5054 case XML_DOCUMENT_FRAG_NODE:
5055 case XML_NOTATION_NODE:
5056 case XML_HTML_DOCUMENT_NODE:
5058 case XML_ELEMENT_DECL:
5059 case XML_ATTRIBUTE_DECL:
5060 case XML_ENTITY_DECL:
5062 case XML_ENTITY_REF_NODE:
5063 case XML_ENTITY_NODE:
5064 case XML_NAMESPACE_DECL:
5065 case XML_XINCLUDE_START:
5066 case XML_XINCLUDE_END:
5067 #ifdef LIBXML_DOCB_ENABLED
5068 case XML_DOCB_DOCUMENT_NODE:
5071 case XML_ELEMENT_NODE:
5072 case XML_ATTRIBUTE_NODE:
5075 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5080 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
5083 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
5087 #endif /* LIBXML_TREE_ENABLED */
5090 * xmlNodeGetSpacePreserve:
5091 * @cur: the node being checked
5093 * Searches the space preserving behaviour of a node, i.e. the values
5094 * of the xml:space attribute or the one carried by the nearest
5097 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
5100 xmlNodeGetSpacePreserve(const xmlNode *cur) {
5103 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
5105 while (cur != NULL) {
5106 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
5107 if (space != NULL) {
5108 if (xmlStrEqual(space, BAD_CAST "preserve")) {
5112 if (xmlStrEqual(space, BAD_CAST "default")) {
5123 #ifdef LIBXML_TREE_ENABLED
5126 * @cur: the node being changed
5127 * @name: the new tag name
5129 * Set (or reset) the name of a node.
5132 xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
5135 const xmlChar *freeme = NULL;
5137 if (cur == NULL) return;
5138 if (name == NULL) return;
5141 case XML_CDATA_SECTION_NODE:
5142 case XML_COMMENT_NODE:
5143 case XML_DOCUMENT_TYPE_NODE:
5144 case XML_DOCUMENT_FRAG_NODE:
5145 case XML_NOTATION_NODE:
5146 case XML_HTML_DOCUMENT_NODE:
5147 case XML_NAMESPACE_DECL:
5148 case XML_XINCLUDE_START:
5149 case XML_XINCLUDE_END:
5150 #ifdef LIBXML_DOCB_ENABLED
5151 case XML_DOCB_DOCUMENT_NODE:
5154 case XML_ELEMENT_NODE:
5155 case XML_ATTRIBUTE_NODE:
5157 case XML_ENTITY_REF_NODE:
5158 case XML_ENTITY_NODE:
5160 case XML_DOCUMENT_NODE:
5161 case XML_ELEMENT_DECL:
5162 case XML_ATTRIBUTE_DECL:
5163 case XML_ENTITY_DECL:
5172 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
5174 cur->name = xmlDictLookup(dict, name, -1);
5176 if (cur->name != NULL)
5178 cur->name = xmlStrdup(name);
5182 xmlFree((xmlChar *) freeme);
5186 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
5189 * @cur: the node being changed
5190 * @uri: the new base URI
5192 * Set (or reset) the base URI of a node, i.e. the value of the
5193 * xml:base attribute.
5196 xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
5200 if (cur == NULL) return;
5203 case XML_CDATA_SECTION_NODE:
5204 case XML_COMMENT_NODE:
5205 case XML_DOCUMENT_TYPE_NODE:
5206 case XML_DOCUMENT_FRAG_NODE:
5207 case XML_NOTATION_NODE:
5209 case XML_ELEMENT_DECL:
5210 case XML_ATTRIBUTE_DECL:
5211 case XML_ENTITY_DECL:
5213 case XML_ENTITY_REF_NODE:
5214 case XML_ENTITY_NODE:
5215 case XML_NAMESPACE_DECL:
5216 case XML_XINCLUDE_START:
5217 case XML_XINCLUDE_END:
5219 case XML_ELEMENT_NODE:
5220 case XML_ATTRIBUTE_NODE:
5222 case XML_DOCUMENT_NODE:
5223 #ifdef LIBXML_DOCB_ENABLED
5224 case XML_DOCB_DOCUMENT_NODE:
5226 case XML_HTML_DOCUMENT_NODE: {
5227 xmlDocPtr doc = (xmlDocPtr) cur;
5229 if (doc->URL != NULL)
5230 xmlFree((xmlChar *) doc->URL);
5234 doc->URL = xmlPathToURI(uri);
5239 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5242 fixed = xmlPathToURI(uri);
5243 if (fixed != NULL) {
5244 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
5247 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
5250 #endif /* LIBXML_TREE_ENABLED */
5254 * @doc: the document the node pertains to
5255 * @cur: the node being checked
5257 * Searches for the BASE URL. The code should work on both XML
5258 * and HTML document even if base mechanisms are completely different.
5259 * It returns the base as defined in RFC 2396 sections
5260 * 5.1.1. Base URI within Document Content
5262 * 5.1.2. Base URI from the Encapsulating Entity
5263 * However it does not return the document base (5.1.3), use
5264 * doc->URL in this case
5266 * Returns a pointer to the base URL, or NULL if not found
5267 * It's up to the caller to free the memory with xmlFree().
5270 xmlNodeGetBase(const xmlDoc *doc, const xmlNode *cur) {
5271 xmlChar *oldbase = NULL;
5272 xmlChar *base, *newbase;
5274 if ((cur == NULL) && (doc == NULL))
5276 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
5278 if (doc == NULL) doc = cur->doc;
5279 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
5280 cur = doc->children;
5281 while ((cur != NULL) && (cur->name != NULL)) {
5282 if (cur->type != XML_ELEMENT_NODE) {
5286 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5287 cur = cur->children;
5290 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5291 cur = cur->children;
5294 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5295 return(xmlGetProp(cur, BAD_CAST "href"));
5301 while (cur != NULL) {
5302 if (cur->type == XML_ENTITY_DECL) {
5303 xmlEntityPtr ent = (xmlEntityPtr) cur;
5304 return(xmlStrdup(ent->URI));
5306 if (cur->type == XML_ELEMENT_NODE) {
5307 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
5309 if (oldbase != NULL) {
5310 newbase = xmlBuildURI(oldbase, base);
5311 if (newbase != NULL) {
5323 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
5324 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
5325 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
5331 if ((doc != NULL) && (doc->URL != NULL)) {
5332 if (oldbase == NULL)
5333 return(xmlStrdup(doc->URL));
5334 newbase = xmlBuildURI(oldbase, doc->URL);
5342 * xmlNodeBufGetContent:
5344 * @cur: the node being read
5346 * Read the value of a node @cur, this can be either the text carried
5347 * directly by this node if it's a TEXT node or the aggregate string
5348 * of the values carried by this node child's (TEXT and ENTITY_REF).
5349 * Entity references are substituted.
5350 * Fills up the buffer @buffer with this value
5352 * Returns 0 in case of success and -1 in case of error.
5355 xmlNodeBufGetContent(xmlBufferPtr buffer, const xmlNode *cur)
5360 if ((cur == NULL) || (buffer == NULL)) return(-1);
5361 buf = xmlBufFromBuffer(buffer);
5362 ret = xmlBufGetNodeContent(buf, cur);
5363 buffer = xmlBufBackToBuffer(buf);
5364 if ((ret < 0) || (buffer == NULL))
5370 * xmlBufGetNodeContent:
5371 * @buf: a buffer xmlBufPtr
5372 * @cur: the node being read
5374 * Read the value of a node @cur, this can be either the text carried
5375 * directly by this node if it's a TEXT node or the aggregate string
5376 * of the values carried by this node child's (TEXT and ENTITY_REF).
5377 * Entity references are substituted.
5378 * Fills up the buffer @buf with this value
5380 * Returns 0 in case of success and -1 in case of error.
5383 xmlBufGetNodeContent(xmlBufPtr buf, const xmlNode *cur)
5385 if ((cur == NULL) || (buf == NULL)) return(-1);
5386 switch (cur->type) {
5387 case XML_CDATA_SECTION_NODE:
5389 xmlBufCat(buf, cur->content);
5391 case XML_DOCUMENT_FRAG_NODE:
5392 case XML_ELEMENT_NODE:{
5393 const xmlNode *tmp = cur;
5395 while (tmp != NULL) {
5396 switch (tmp->type) {
5397 case XML_CDATA_SECTION_NODE:
5399 if (tmp->content != NULL)
5400 xmlBufCat(buf, tmp->content);
5402 case XML_ENTITY_REF_NODE:
5403 xmlBufGetNodeContent(buf, tmp);
5411 if (tmp->children != NULL) {
5412 if (tmp->children->type != XML_ENTITY_DECL) {
5413 tmp = tmp->children;
5420 if (tmp->next != NULL) {
5433 if (tmp->next != NULL) {
5437 } while (tmp != NULL);
5441 case XML_ATTRIBUTE_NODE:{
5442 xmlAttrPtr attr = (xmlAttrPtr) cur;
5443 xmlNodePtr tmp = attr->children;
5445 while (tmp != NULL) {
5446 if (tmp->type == XML_TEXT_NODE)
5447 xmlBufCat(buf, tmp->content);
5449 xmlBufGetNodeContent(buf, tmp);
5454 case XML_COMMENT_NODE:
5456 xmlBufCat(buf, cur->content);
5458 case XML_ENTITY_REF_NODE:{
5462 /* lookup entity declaration */
5463 ent = xmlGetDocEntity(cur->doc, cur->name);
5467 /* an entity content can be any "well balanced chunk",
5468 * i.e. the result of the content [43] production:
5469 * http://www.w3.org/TR/REC-xml#NT-content
5470 * -> we iterate through child nodes and recursive call
5471 * xmlNodeGetContent() which handles all possible node types */
5472 tmp = ent->children;
5474 xmlBufGetNodeContent(buf, tmp);
5479 case XML_ENTITY_NODE:
5480 case XML_DOCUMENT_TYPE_NODE:
5481 case XML_NOTATION_NODE:
5483 case XML_XINCLUDE_START:
5484 case XML_XINCLUDE_END:
5486 case XML_DOCUMENT_NODE:
5487 #ifdef LIBXML_DOCB_ENABLED
5488 case XML_DOCB_DOCUMENT_NODE:
5490 case XML_HTML_DOCUMENT_NODE:
5491 cur = cur->children;
5492 while (cur!= NULL) {
5493 if ((cur->type == XML_ELEMENT_NODE) ||
5494 (cur->type == XML_TEXT_NODE) ||
5495 (cur->type == XML_CDATA_SECTION_NODE)) {
5496 xmlBufGetNodeContent(buf, cur);
5501 case XML_NAMESPACE_DECL:
5502 xmlBufCat(buf, ((xmlNsPtr) cur)->href);
5504 case XML_ELEMENT_DECL:
5505 case XML_ATTRIBUTE_DECL:
5506 case XML_ENTITY_DECL:
5513 * xmlNodeGetContent:
5514 * @cur: the node being read
5516 * Read the value of a node, this can be either the text carried
5517 * directly by this node if it's a TEXT node or the aggregate string
5518 * of the values carried by this node child's (TEXT and ENTITY_REF).
5519 * Entity references are substituted.
5520 * Returns a new #xmlChar * or NULL if no content is available.
5521 * It's up to the caller to free the memory with xmlFree().
5524 xmlNodeGetContent(const xmlNode *cur)
5528 switch (cur->type) {
5529 case XML_DOCUMENT_FRAG_NODE:
5530 case XML_ELEMENT_NODE:{
5534 buf = xmlBufCreateSize(64);
5537 xmlBufGetNodeContent(buf, cur);
5538 ret = xmlBufDetach(buf);
5542 case XML_ATTRIBUTE_NODE:
5543 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
5544 case XML_COMMENT_NODE:
5546 if (cur->content != NULL)
5547 return (xmlStrdup(cur->content));
5549 case XML_ENTITY_REF_NODE:{
5554 /* lookup entity declaration */
5555 ent = xmlGetDocEntity(cur->doc, cur->name);
5559 buf = xmlBufCreate();
5563 xmlBufGetNodeContent(buf, cur);
5565 ret = xmlBufDetach(buf);
5569 case XML_ENTITY_NODE:
5570 case XML_DOCUMENT_TYPE_NODE:
5571 case XML_NOTATION_NODE:
5573 case XML_XINCLUDE_START:
5574 case XML_XINCLUDE_END:
5576 case XML_DOCUMENT_NODE:
5577 #ifdef LIBXML_DOCB_ENABLED
5578 case XML_DOCB_DOCUMENT_NODE:
5580 case XML_HTML_DOCUMENT_NODE: {
5584 buf = xmlBufCreate();
5588 xmlBufGetNodeContent(buf, (xmlNodePtr) cur);
5590 ret = xmlBufDetach(buf);
5594 case XML_NAMESPACE_DECL: {
5597 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5600 case XML_ELEMENT_DECL:
5603 case XML_ATTRIBUTE_DECL:
5606 case XML_ENTITY_DECL:
5609 case XML_CDATA_SECTION_NODE:
5611 if (cur->content != NULL)
5612 return (xmlStrdup(cur->content));
5619 * xmlNodeSetContent:
5620 * @cur: the node being modified
5621 * @content: the new value of the content
5623 * Replace the content of a node.
5624 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5625 * references, but XML special chars need to be escaped first by using
5626 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5629 xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5632 xmlGenericError(xmlGenericErrorContext,
5633 "xmlNodeSetContent : node == NULL\n");
5637 switch (cur->type) {
5638 case XML_DOCUMENT_FRAG_NODE:
5639 case XML_ELEMENT_NODE:
5640 case XML_ATTRIBUTE_NODE:
5641 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5642 cur->children = xmlStringGetNodeList(cur->doc, content);
5643 UPDATE_LAST_CHILD_AND_PARENT(cur)
5646 case XML_CDATA_SECTION_NODE:
5647 case XML_ENTITY_REF_NODE:
5648 case XML_ENTITY_NODE:
5650 case XML_COMMENT_NODE:
5651 if ((cur->content != NULL) &&
5652 (cur->content != (xmlChar *) &(cur->properties))) {
5653 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5654 (xmlDictOwns(cur->doc->dict, cur->content))))
5655 xmlFree(cur->content);
5657 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5658 cur->last = cur->children = NULL;
5659 if (content != NULL) {
5660 cur->content = xmlStrdup(content);
5662 cur->content = NULL;
5663 cur->properties = NULL;
5666 case XML_DOCUMENT_NODE:
5667 case XML_HTML_DOCUMENT_NODE:
5668 case XML_DOCUMENT_TYPE_NODE:
5669 case XML_XINCLUDE_START:
5670 case XML_XINCLUDE_END:
5671 #ifdef LIBXML_DOCB_ENABLED
5672 case XML_DOCB_DOCUMENT_NODE:
5675 case XML_NOTATION_NODE:
5679 case XML_NAMESPACE_DECL:
5681 case XML_ELEMENT_DECL:
5684 case XML_ATTRIBUTE_DECL:
5687 case XML_ENTITY_DECL:
5693 #ifdef LIBXML_TREE_ENABLED
5695 * xmlNodeSetContentLen:
5696 * @cur: the node being modified
5697 * @content: the new value of the content
5698 * @len: the size of @content
5700 * Replace the content of a node.
5701 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5702 * references, but XML special chars need to be escaped first by using
5703 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5706 xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5709 xmlGenericError(xmlGenericErrorContext,
5710 "xmlNodeSetContentLen : node == NULL\n");
5714 switch (cur->type) {
5715 case XML_DOCUMENT_FRAG_NODE:
5716 case XML_ELEMENT_NODE:
5717 case XML_ATTRIBUTE_NODE:
5718 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5719 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5720 UPDATE_LAST_CHILD_AND_PARENT(cur)
5723 case XML_CDATA_SECTION_NODE:
5724 case XML_ENTITY_REF_NODE:
5725 case XML_ENTITY_NODE:
5727 case XML_COMMENT_NODE:
5728 case XML_NOTATION_NODE:
5729 if ((cur->content != NULL) &&
5730 (cur->content != (xmlChar *) &(cur->properties))) {
5731 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5732 (xmlDictOwns(cur->doc->dict, cur->content))))
5733 xmlFree(cur->content);
5735 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5736 cur->children = cur->last = NULL;
5737 if (content != NULL) {
5738 cur->content = xmlStrndup(content, len);
5740 cur->content = NULL;
5741 cur->properties = NULL;
5744 case XML_DOCUMENT_NODE:
5746 case XML_HTML_DOCUMENT_NODE:
5747 case XML_DOCUMENT_TYPE_NODE:
5748 case XML_NAMESPACE_DECL:
5749 case XML_XINCLUDE_START:
5750 case XML_XINCLUDE_END:
5751 #ifdef LIBXML_DOCB_ENABLED
5752 case XML_DOCB_DOCUMENT_NODE:
5755 case XML_ELEMENT_DECL:
5758 case XML_ATTRIBUTE_DECL:
5761 case XML_ENTITY_DECL:
5766 #endif /* LIBXML_TREE_ENABLED */
5769 * xmlNodeAddContentLen:
5770 * @cur: the node being modified
5771 * @content: extra content
5772 * @len: the size of @content
5774 * Append the extra substring to the node content.
5775 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5776 * raw text, so unescaped XML special chars are allowed, entity
5777 * references are not supported.
5780 xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5783 xmlGenericError(xmlGenericErrorContext,
5784 "xmlNodeAddContentLen : node == NULL\n");
5788 if (len <= 0) return;
5789 switch (cur->type) {
5790 case XML_DOCUMENT_FRAG_NODE:
5791 case XML_ELEMENT_NODE: {
5792 xmlNodePtr last, newNode, tmp;
5795 newNode = xmlNewTextLen(content, len);
5796 if (newNode != NULL) {
5797 tmp = xmlAddChild(cur, newNode);
5800 if ((last != NULL) && (last->next == newNode)) {
5801 xmlTextMerge(last, newNode);
5806 case XML_ATTRIBUTE_NODE:
5809 case XML_CDATA_SECTION_NODE:
5810 case XML_ENTITY_REF_NODE:
5811 case XML_ENTITY_NODE:
5813 case XML_COMMENT_NODE:
5814 case XML_NOTATION_NODE:
5815 if (content != NULL) {
5816 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5817 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5818 xmlDictOwns(cur->doc->dict, cur->content))) {
5819 cur->content = xmlStrncatNew(cur->content, content, len);
5820 cur->properties = NULL;
5824 cur->content = xmlStrncat(cur->content, content, len);
5826 case XML_DOCUMENT_NODE:
5828 case XML_HTML_DOCUMENT_NODE:
5829 case XML_DOCUMENT_TYPE_NODE:
5830 case XML_NAMESPACE_DECL:
5831 case XML_XINCLUDE_START:
5832 case XML_XINCLUDE_END:
5833 #ifdef LIBXML_DOCB_ENABLED
5834 case XML_DOCB_DOCUMENT_NODE:
5837 case XML_ELEMENT_DECL:
5838 case XML_ATTRIBUTE_DECL:
5839 case XML_ENTITY_DECL:
5845 * xmlNodeAddContent:
5846 * @cur: the node being modified
5847 * @content: extra content
5849 * Append the extra substring to the node content.
5850 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5851 * raw text, so unescaped XML special chars are allowed, entity
5852 * references are not supported.
5855 xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5860 xmlGenericError(xmlGenericErrorContext,
5861 "xmlNodeAddContent : node == NULL\n");
5865 if (content == NULL) return;
5866 len = xmlStrlen(content);
5867 xmlNodeAddContentLen(cur, content, len);
5872 * @first: the first text node
5873 * @second: the second text node being merged
5875 * Merge two text nodes into one
5876 * Returns the first text node augmented
5879 xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5880 if (first == NULL) return(second);
5881 if (second == NULL) return(first);
5882 if (first->type != XML_TEXT_NODE) return(first);
5883 if (second->type != XML_TEXT_NODE) return(first);
5884 if (second->name != first->name)
5886 xmlNodeAddContent(first, second->content);
5887 xmlUnlinkNode(second);
5888 xmlFreeNode(second);
5892 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
5895 * @doc: the document
5896 * @node: the current node
5898 * Search all the namespace applying to a given element.
5899 * Returns an NULL terminated array of all the #xmlNsPtr found
5900 * that need to be freed by the caller or NULL if no
5901 * namespace if defined
5904 xmlGetNsList(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node)
5907 xmlNsPtr *ret = NULL;
5912 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5915 while (node != NULL) {
5916 if (node->type == XML_ELEMENT_NODE) {
5918 while (cur != NULL) {
5921 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5924 xmlTreeErrMemory("getting namespace list");
5929 for (i = 0; i < nbns; i++) {
5930 if ((cur->prefix == ret[i]->prefix) ||
5931 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5935 if (nbns >= maxns) {
5937 ret = (xmlNsPtr *) xmlRealloc(ret,
5942 xmlTreeErrMemory("getting namespace list");
5953 node = node->parent;
5957 #endif /* LIBXML_TREE_ENABLED */
5960 * xmlTreeEnsureXMLDecl:
5963 * Ensures that there is an XML namespace declaration on the doc.
5965 * Returns the XML ns-struct or NULL on API and internal errors.
5968 xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5972 if (doc->oldNs != NULL)
5973 return (doc->oldNs);
5976 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5979 "allocating the XML namespace");
5982 memset(ns, 0, sizeof(xmlNs));
5983 ns->type = XML_LOCAL_NAMESPACE;
5984 ns->href = xmlStrdup(XML_XML_NAMESPACE);
5985 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5993 * @doc: the document
5994 * @node: the current node
5995 * @nameSpace: the namespace prefix
5997 * Search a Ns registered under a given name space for a document.
5998 * recurse on the parents until it finds the defined namespace
5999 * or return NULL otherwise.
6000 * @nameSpace can be NULL, this is a search for the default namespace.
6001 * We don't allow to cross entities boundaries. If you don't declare
6002 * the namespace within those you will be in troubles !!! A warning
6003 * is generated to cover this case.
6005 * Returns the namespace pointer or NULL.
6008 xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
6011 const xmlNode *orig = node;
6013 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL);
6014 if ((nameSpace != NULL) &&
6015 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
6016 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6018 * The XML-1.0 namespace is normally held on the root
6019 * element. In this case exceptionally create it on the
6022 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6024 xmlTreeErrMemory("searching namespace");
6027 memset(cur, 0, sizeof(xmlNs));
6028 cur->type = XML_LOCAL_NAMESPACE;
6029 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6030 cur->prefix = xmlStrdup((const xmlChar *)"xml");
6031 cur->next = node->nsDef;
6041 * Return the XML namespace declaration held by the doc.
6043 if (doc->oldNs == NULL)
6044 return(xmlTreeEnsureXMLDecl(doc));
6048 while (node != NULL) {
6049 if ((node->type == XML_ENTITY_REF_NODE) ||
6050 (node->type == XML_ENTITY_NODE) ||
6051 (node->type == XML_ENTITY_DECL))
6053 if (node->type == XML_ELEMENT_NODE) {
6055 while (cur != NULL) {
6056 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6057 (cur->href != NULL))
6059 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6060 (cur->href != NULL) &&
6061 (xmlStrEqual(cur->prefix, nameSpace)))
6068 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6069 (cur->href != NULL))
6071 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6072 (cur->href != NULL) &&
6073 (xmlStrEqual(cur->prefix, nameSpace)))
6078 node = node->parent;
6085 * @doc: the document
6086 * @node: the current node
6087 * @ancestor: the ancestor carrying the namespace
6088 * @prefix: the namespace prefix
6090 * Verify that the given namespace held on @ancestor is still in scope
6093 * Returns 1 if true, 0 if false and -1 in case of error.
6096 xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
6097 xmlNodePtr ancestor, const xmlChar * prefix)
6101 while ((node != NULL) && (node != ancestor)) {
6102 if ((node->type == XML_ENTITY_REF_NODE) ||
6103 (node->type == XML_ENTITY_NODE) ||
6104 (node->type == XML_ENTITY_DECL))
6106 if (node->type == XML_ELEMENT_NODE) {
6108 while (tst != NULL) {
6109 if ((tst->prefix == NULL)
6110 && (prefix == NULL))
6112 if ((tst->prefix != NULL)
6114 && (xmlStrEqual(tst->prefix, prefix)))
6119 node = node->parent;
6121 if (node != ancestor)
6127 * xmlSearchNsByHref:
6128 * @doc: the document
6129 * @node: the current node
6130 * @href: the namespace value
6132 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
6133 * the defined namespace or return NULL otherwise.
6134 * Returns the namespace pointer or NULL.
6137 xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
6140 xmlNodePtr orig = node;
6143 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL))
6145 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
6147 * Only the document can hold the XML spec namespace.
6149 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6151 * The XML-1.0 namespace is normally held on the root
6152 * element. In this case exceptionally create it on the
6155 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6157 xmlTreeErrMemory("searching namespace");
6160 memset(cur, 0, sizeof(xmlNs));
6161 cur->type = XML_LOCAL_NAMESPACE;
6162 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6163 cur->prefix = xmlStrdup((const xmlChar *) "xml");
6164 cur->next = node->nsDef;
6174 * Return the XML namespace declaration held by the doc.
6176 if (doc->oldNs == NULL)
6177 return(xmlTreeEnsureXMLDecl(doc));
6181 is_attr = (node->type == XML_ATTRIBUTE_NODE);
6182 while (node != NULL) {
6183 if ((node->type == XML_ENTITY_REF_NODE) ||
6184 (node->type == XML_ENTITY_NODE) ||
6185 (node->type == XML_ENTITY_DECL))
6187 if (node->type == XML_ELEMENT_NODE) {
6189 while (cur != NULL) {
6190 if ((cur->href != NULL) && (href != NULL) &&
6191 (xmlStrEqual(cur->href, href))) {
6192 if (((!is_attr) || (cur->prefix != NULL)) &&
6193 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6201 if ((cur->href != NULL) && (href != NULL) &&
6202 (xmlStrEqual(cur->href, href))) {
6203 if (((!is_attr) || (cur->prefix != NULL)) &&
6204 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6210 node = node->parent;
6216 * xmlNewReconciliedNs:
6217 * @doc: the document
6218 * @tree: a node expected to hold the new namespace
6219 * @ns: the original namespace
6221 * This function tries to locate a namespace definition in a tree
6222 * ancestors, or create a new namespace definition node similar to
6223 * @ns trying to reuse the same prefix. However if the given prefix is
6224 * null (default namespace) or reused within the subtree defined by
6225 * @tree or on one of its ancestors then a new prefix is generated.
6226 * Returns the (new) namespace definition or NULL in case of error
6229 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
6234 if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) {
6236 xmlGenericError(xmlGenericErrorContext,
6237 "xmlNewReconciliedNs : tree == NULL\n");
6241 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
6243 xmlGenericError(xmlGenericErrorContext,
6244 "xmlNewReconciliedNs : ns == NULL\n");
6249 * Search an existing namespace definition inherited.
6251 def = xmlSearchNsByHref(doc, tree, ns->href);
6256 * Find a close prefix which is not already in use.
6257 * Let's strip namespace prefixes longer than 20 chars !
6259 if (ns->prefix == NULL)
6260 snprintf((char *) prefix, sizeof(prefix), "default");
6262 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
6264 def = xmlSearchNs(doc, tree, prefix);
6265 while (def != NULL) {
6266 if (counter > 1000) return(NULL);
6267 if (ns->prefix == NULL)
6268 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
6270 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
6271 (char *)ns->prefix, counter++);
6272 def = xmlSearchNs(doc, tree, prefix);
6276 * OK, now we are ready to create a new one.
6278 def = xmlNewNs(tree, ns->href, prefix);
6282 #ifdef LIBXML_TREE_ENABLED
6284 * xmlReconciliateNs:
6285 * @doc: the document
6286 * @tree: a node defining the subtree to reconciliate
6288 * This function checks that all the namespaces declared within the given
6289 * tree are properly declared. This is needed for example after Copy or Cut
6290 * and then paste operations. The subtree may still hold pointers to
6291 * namespace declarations outside the subtree or invalid/masked. As much
6292 * as possible the function try to reuse the existing namespaces found in
6293 * the new environment. If not possible the new namespaces are redeclared
6294 * on @tree at the top of the given subtree.
6295 * Returns the number of namespace declarations created or -1 in case of error.
6298 xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
6299 xmlNsPtr *oldNs = NULL;
6300 xmlNsPtr *newNs = NULL;
6305 xmlNodePtr node = tree;
6309 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6310 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
6311 if (node->doc != doc) return(-1);
6312 while (node != NULL) {
6314 * Reconciliate the node namespace
6316 if (node->ns != NULL) {
6318 * initialize the cache if needed
6320 if (sizeCache == 0) {
6322 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6324 if (oldNs == NULL) {
6325 xmlTreeErrMemory("fixing namespaces");
6328 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6330 if (newNs == NULL) {
6331 xmlTreeErrMemory("fixing namespaces");
6336 for (i = 0;i < nbCache;i++) {
6337 if (oldNs[i] == node->ns) {
6338 node->ns = newNs[i];
6344 * OK we need to recreate a new namespace definition
6346 n = xmlNewReconciliedNs(doc, tree, node->ns);
6347 if (n != NULL) { /* :-( what if else ??? */
6349 * check if we need to grow the cache buffers.
6351 if (sizeCache <= nbCache) {
6353 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
6355 if (oldNs == NULL) {
6356 xmlTreeErrMemory("fixing namespaces");
6360 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
6362 if (newNs == NULL) {
6363 xmlTreeErrMemory("fixing namespaces");
6369 oldNs[nbCache++] = node->ns;
6375 * now check for namespace hold by attributes on the node.
6377 if (node->type == XML_ELEMENT_NODE) {
6378 attr = node->properties;
6379 while (attr != NULL) {
6380 if (attr->ns != NULL) {
6382 * initialize the cache if needed
6384 if (sizeCache == 0) {
6386 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6388 if (oldNs == NULL) {
6389 xmlTreeErrMemory("fixing namespaces");
6392 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6394 if (newNs == NULL) {
6395 xmlTreeErrMemory("fixing namespaces");
6400 for (i = 0;i < nbCache;i++) {
6401 if (oldNs[i] == attr->ns) {
6402 attr->ns = newNs[i];
6408 * OK we need to recreate a new namespace definition
6410 n = xmlNewReconciliedNs(doc, tree, attr->ns);
6411 if (n != NULL) { /* :-( what if else ??? */
6413 * check if we need to grow the cache buffers.
6415 if (sizeCache <= nbCache) {
6417 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
6418 sizeCache * sizeof(xmlNsPtr));
6419 if (oldNs == NULL) {
6420 xmlTreeErrMemory("fixing namespaces");
6424 newNs = (xmlNsPtr *) xmlRealloc(newNs,
6425 sizeCache * sizeof(xmlNsPtr));
6426 if (newNs == NULL) {
6427 xmlTreeErrMemory("fixing namespaces");
6433 oldNs[nbCache++] = attr->ns;
6443 * Browse the full subtree, deep first
6445 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
6447 node = node->children;
6448 } else if ((node != tree) && (node->next != NULL)) {
6451 } else if (node != tree) {
6452 /* go up to parents->next if needed */
6453 while (node != tree) {
6454 if (node->parent != NULL)
6455 node = node->parent;
6456 if ((node != tree) && (node->next != NULL)) {
6460 if (node->parent == NULL) {
6465 /* exit condition */
6477 #endif /* LIBXML_TREE_ENABLED */
6480 xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name,
6481 const xmlChar *nsName, int useDTD)
6485 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6488 if (node->properties != NULL) {
6489 prop = node->properties;
6490 if (nsName == NULL) {
6492 * We want the attr to be in no namespace.
6495 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6499 } while (prop != NULL);
6502 * We want the attr to be in the specified namespace.
6505 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6506 ((prop->ns->href == nsName) ||
6507 xmlStrEqual(prop->ns->href, nsName)))
6512 } while (prop != NULL);
6516 #ifdef LIBXML_TREE_ENABLED
6520 * Check if there is a default/fixed attribute declaration in
6521 * the internal or external subset.
6523 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6524 xmlDocPtr doc = node->doc;
6525 xmlAttributePtr attrDecl = NULL;
6526 xmlChar *elemQName, *tmpstr = NULL;
6529 * We need the QName of the element for the DTD-lookup.
6531 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6532 tmpstr = xmlStrdup(node->ns->prefix);
6533 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6534 tmpstr = xmlStrcat(tmpstr, node->name);
6539 elemQName = (xmlChar *) node->name;
6540 if (nsName == NULL) {
6542 * The common and nice case: Attr in no namespace.
6544 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6545 elemQName, name, NULL);
6546 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6547 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6548 elemQName, name, NULL);
6551 xmlNsPtr *nsList, *cur;
6554 * The ugly case: Search using the prefixes of in-scope
6555 * ns-decls corresponding to @nsName.
6557 nsList = xmlGetNsList(node->doc, node);
6558 if (nsList == NULL) {
6564 while (*cur != NULL) {
6565 if (xmlStrEqual((*cur)->href, nsName)) {
6566 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6567 name, (*cur)->prefix);
6570 if (doc->extSubset != NULL) {
6571 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6572 name, (*cur)->prefix);
6584 * Only default/fixed attrs are relevant.
6586 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6587 return((xmlAttrPtr) attrDecl);
6589 #endif /* LIBXML_TREE_ENABLED */
6594 xmlGetPropNodeValueInternal(const xmlAttr *prop)
6598 if (prop->type == XML_ATTRIBUTE_NODE) {
6600 * Note that we return at least the empty string.
6601 * TODO: Do we really always want that?
6603 if (prop->children != NULL) {
6604 if ((prop->children->next == NULL) &&
6605 ((prop->children->type == XML_TEXT_NODE) ||
6606 (prop->children->type == XML_CDATA_SECTION_NODE)))
6609 * Optimization for the common case: only 1 text node.
6611 return(xmlStrdup(prop->children->content));
6615 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6620 return(xmlStrdup((xmlChar *)""));
6621 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6622 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6630 * @name: the attribute name
6632 * Search an attribute associated to a node
6633 * This function also looks in DTD attribute declaration for #FIXED or
6634 * default declaration values unless DTD use has been turned off.
6636 * Returns the attribute or the attribute declaration or NULL if
6637 * neither was found.
6640 xmlHasProp(const xmlNode *node, const xmlChar *name) {
6644 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6647 * Check on the properties attached to the node
6649 prop = node->properties;
6650 while (prop != NULL) {
6651 if (xmlStrEqual(prop->name, name)) {
6656 if (!xmlCheckDTD) return(NULL);
6659 * Check if there is a default declaration in the internal
6660 * or external subsets
6664 xmlAttributePtr attrDecl;
6665 if (doc->intSubset != NULL) {
6666 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6667 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6668 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6669 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6670 /* return attribute declaration only if a default value is given
6671 (that includes #FIXED declarations) */
6672 return((xmlAttrPtr) attrDecl);
6681 * @name: the attribute name
6682 * @nameSpace: the URI of the namespace
6684 * Search for an attribute associated to a node
6685 * This attribute has to be anchored in the namespace specified.
6686 * This does the entity substitution.
6687 * This function looks in DTD attribute declaration for #FIXED or
6688 * default declaration values unless DTD use has been turned off.
6689 * Note that a namespace of NULL indicates to use the default namespace.
6691 * Returns the attribute or the attribute declaration or NULL
6692 * if neither was found.
6695 xmlHasNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
6697 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
6703 * @name: the attribute name
6705 * Search and get the value of an attribute associated to a node
6706 * This does the entity substitution.
6707 * This function looks in DTD attribute declaration for #FIXED or
6708 * default declaration values unless DTD use has been turned off.
6709 * NOTE: this function acts independently of namespaces associated
6710 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6711 * for namespace aware processing.
6713 * Returns the attribute value or NULL if not found.
6714 * It's up to the caller to free the memory with xmlFree().
6717 xmlGetProp(const xmlNode *node, const xmlChar *name) {
6720 prop = xmlHasProp(node, name);
6723 return(xmlGetPropNodeValueInternal(prop));
6729 * @name: the attribute name
6731 * Search and get the value of an attribute associated to a node
6732 * This does the entity substitution.
6733 * This function looks in DTD attribute declaration for #FIXED or
6734 * default declaration values unless DTD use has been turned off.
6735 * This function is similar to xmlGetProp except it will accept only
6736 * an attribute in no namespace.
6738 * Returns the attribute value or NULL if not found.
6739 * It's up to the caller to free the memory with xmlFree().
6742 xmlGetNoNsProp(const xmlNode *node, const xmlChar *name) {
6745 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6748 return(xmlGetPropNodeValueInternal(prop));
6754 * @name: the attribute name
6755 * @nameSpace: the URI of the namespace
6757 * Search and get the value of an attribute associated to a node
6758 * This attribute has to be anchored in the namespace specified.
6759 * This does the entity substitution.
6760 * This function looks in DTD attribute declaration for #FIXED or
6761 * default declaration values unless DTD use has been turned off.
6763 * Returns the attribute value or NULL if not found.
6764 * It's up to the caller to free the memory with xmlFree().
6767 xmlGetNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
6770 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6773 return(xmlGetPropNodeValueInternal(prop));
6776 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6780 * @name: the attribute name
6782 * Remove an attribute carried by a node.
6783 * This handles only attributes in no namespace.
6784 * Returns 0 if successful, -1 if not found
6787 xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
6790 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6793 xmlUnlinkNode((xmlNodePtr) prop);
6801 * @ns: the namespace definition
6802 * @name: the attribute name
6804 * Remove an attribute carried by a node.
6805 * Returns 0 if successful, -1 if not found
6808 xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
6811 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6814 xmlUnlinkNode((xmlNodePtr) prop);
6820 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
6824 * @name: the attribute name (a QName)
6825 * @value: the attribute value
6827 * Set (or reset) an attribute carried by a node.
6828 * If @name has a prefix, then the corresponding
6829 * namespace-binding will be used, if in scope; it is an
6830 * error it there's no such ns-binding for the prefix in
6832 * Returns the attribute pointer.
6836 xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
6838 const xmlChar *nqname;
6840 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
6846 nqname = xmlSplitQName3(name, &len);
6847 if (nqname != NULL) {
6849 xmlChar *prefix = xmlStrndup(name, len);
6850 ns = xmlSearchNs(node->doc, node, prefix);
6854 return(xmlSetNsProp(node, ns, nqname, value));
6856 return(xmlSetNsProp(node, NULL, name, value));
6862 * @ns: the namespace definition
6863 * @name: the attribute name
6864 * @value: the attribute value
6866 * Set (or reset) an attribute carried by a node.
6867 * The ns structure must be in scope, this is not checked
6869 * Returns the attribute pointer.
6872 xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6873 const xmlChar *value)
6877 if (ns && (ns->href == NULL))
6879 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6882 * Modify the attribute's value.
6884 if (prop->atype == XML_ATTRIBUTE_ID) {
6885 xmlRemoveID(node->doc, prop);
6886 prop->atype = XML_ATTRIBUTE_ID;
6888 if (prop->children != NULL)
6889 xmlFreeNodeList(prop->children);
6890 prop->children = NULL;
6893 if (value != NULL) {
6896 if(!xmlCheckUTF8(value)) {
6897 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc,
6899 if (node->doc != NULL)
6900 node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
6902 prop->children = xmlNewDocText(node->doc, value);
6904 tmp = prop->children;
6905 while (tmp != NULL) {
6906 tmp->parent = (xmlNodePtr) prop;
6907 if (tmp->next == NULL)
6912 if (prop->atype == XML_ATTRIBUTE_ID)
6913 xmlAddID(NULL, node->doc, value, prop);
6917 * No equal attr found; create a new one.
6919 return(xmlNewPropInternal(node, ns, name, value, 0));
6922 #endif /* LIBXML_TREE_ENABLED */
6928 * Is this node a Text node ?
6929 * Returns 1 yes, 0 no
6932 xmlNodeIsText(const xmlNode *node) {
6933 if (node == NULL) return(0);
6935 if (node->type == XML_TEXT_NODE) return(1);
6943 * Checks whether this node is an empty or whitespace only
6944 * (and possibly ignorable) text-node.
6946 * Returns 1 yes, 0 no
6949 xmlIsBlankNode(const xmlNode *node) {
6951 if (node == NULL) return(0);
6953 if ((node->type != XML_TEXT_NODE) &&
6954 (node->type != XML_CDATA_SECTION_NODE))
6956 if (node->content == NULL) return(1);
6957 cur = node->content;
6959 if (!IS_BLANK_CH(*cur)) return(0);
6969 * @content: the content
6970 * @len: @content length
6972 * Concat the given string at the end of the existing node content
6974 * Returns -1 in case of error, 0 otherwise
6978 xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
6979 if (node == NULL) return(-1);
6981 if ((node->type != XML_TEXT_NODE) &&
6982 (node->type != XML_CDATA_SECTION_NODE) &&
6983 (node->type != XML_COMMENT_NODE) &&
6984 (node->type != XML_PI_NODE)) {
6986 xmlGenericError(xmlGenericErrorContext,
6987 "xmlTextConcat: node is not text nor CDATA\n");
6991 /* need to check if content is currently in the dictionary */
6992 if ((node->content == (xmlChar *) &(node->properties)) ||
6993 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6994 xmlDictOwns(node->doc->dict, node->content))) {
6995 node->content = xmlStrncatNew(node->content, content, len);
6997 node->content = xmlStrncat(node->content, content, len);
6999 node->properties = NULL;
7000 if (node->content == NULL)
7005 /************************************************************************
7007 * Output : to a FILE or in memory *
7009 ************************************************************************/
7014 * routine to create an XML buffer.
7015 * returns the new structure.
7018 xmlBufferCreate(void) {
7021 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7023 xmlTreeErrMemory("creating buffer");
7027 ret->size = xmlDefaultBufferSize;
7028 ret->alloc = xmlBufferAllocScheme;
7029 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
7030 if (ret->content == NULL) {
7031 xmlTreeErrMemory("creating buffer");
7035 ret->content[0] = 0;
7036 ret->contentIO = NULL;
7041 * xmlBufferCreateSize:
7042 * @size: initial size of buffer
7044 * routine to create an XML buffer.
7045 * returns the new structure.
7048 xmlBufferCreateSize(size_t size) {
7051 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7053 xmlTreeErrMemory("creating buffer");
7057 ret->alloc = xmlBufferAllocScheme;
7058 ret->size = (size ? size+2 : 0); /* +1 for ending null */
7060 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
7061 if (ret->content == NULL) {
7062 xmlTreeErrMemory("creating buffer");
7066 ret->content[0] = 0;
7068 ret->content = NULL;
7069 ret->contentIO = NULL;
7077 * Remove the string contained in a buffer and gie it back to the
7078 * caller. The buffer is reset to an empty content.
7079 * This doesn't work with immutable buffers as they can't be reset.
7081 * Returns the previous string contained by the buffer.
7084 xmlBufferDetach(xmlBufferPtr buf) {
7089 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
7093 buf->content = NULL;
7102 * xmlBufferCreateStatic:
7103 * @mem: the memory area
7104 * @size: the size in byte
7106 * routine to create an XML buffer from an immutable memory area.
7107 * The area won't be modified nor copied, and is expected to be
7108 * present until the end of the buffer lifetime.
7110 * returns the new structure.
7113 xmlBufferCreateStatic(void *mem, size_t size) {
7116 if ((mem == NULL) || (size == 0))
7119 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7121 xmlTreeErrMemory("creating buffer");
7126 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
7127 ret->content = (xmlChar *) mem;
7132 * xmlBufferSetAllocationScheme:
7133 * @buf: the buffer to tune
7134 * @scheme: allocation scheme to use
7136 * Sets the allocation scheme for this buffer
7139 xmlBufferSetAllocationScheme(xmlBufferPtr buf,
7140 xmlBufferAllocationScheme scheme) {
7143 xmlGenericError(xmlGenericErrorContext,
7144 "xmlBufferSetAllocationScheme: buf == NULL\n");
7148 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7149 (buf->alloc == XML_BUFFER_ALLOC_IO)) return;
7150 if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
7151 (scheme == XML_BUFFER_ALLOC_EXACT) ||
7152 (scheme == XML_BUFFER_ALLOC_HYBRID) ||
7153 (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
7154 buf->alloc = scheme;
7159 * @buf: the buffer to free
7161 * Frees an XML buffer. It frees both the content and the structure which
7165 xmlBufferFree(xmlBufferPtr buf) {
7168 xmlGenericError(xmlGenericErrorContext,
7169 "xmlBufferFree: buf == NULL\n");
7174 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7175 (buf->contentIO != NULL)) {
7176 xmlFree(buf->contentIO);
7177 } else if ((buf->content != NULL) &&
7178 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
7179 xmlFree(buf->content);
7191 xmlBufferEmpty(xmlBufferPtr buf) {
7192 if (buf == NULL) return;
7193 if (buf->content == NULL) return;
7195 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
7196 buf->content = BAD_CAST "";
7197 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7198 (buf->contentIO != NULL)) {
7199 size_t start_buf = buf->content - buf->contentIO;
7201 buf->size += start_buf;
7202 buf->content = buf->contentIO;
7203 buf->content[0] = 0;
7205 buf->content[0] = 0;
7211 * @buf: the buffer to dump
7212 * @len: the number of xmlChar to remove
7214 * Remove the beginning of an XML buffer.
7216 * Returns the number of #xmlChar removed, or -1 in case of failure.
7219 xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
7220 if (buf == NULL) return(-1);
7221 if (len == 0) return(0);
7222 if (len > buf->use) return(-1);
7225 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7226 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
7228 * we just move the content pointer, but also make sure
7229 * the perceived buffer size has shrinked accordingly
7231 buf->content += len;
7235 * sometimes though it maybe be better to really shrink
7238 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7239 size_t start_buf = buf->content - buf->contentIO;
7240 if (start_buf >= buf->size) {
7241 memmove(buf->contentIO, &buf->content[0], buf->use);
7242 buf->content = buf->contentIO;
7243 buf->content[buf->use] = 0;
7244 buf->size += start_buf;
7248 memmove(buf->content, &buf->content[len], buf->use);
7249 buf->content[buf->use] = 0;
7257 * @len: the minimum free size to allocate
7259 * Grow the available space of an XML buffer.
7261 * Returns the new available space or -1 in case of error
7264 xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
7268 if (buf == NULL) return(-1);
7270 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7271 if (len + buf->use < buf->size) return(0);
7274 * Windows has a BIG problem on realloc timing, so we try to double
7275 * the buffer size (if that's enough) (bug 146697)
7276 * Apparently BSD too, and it's probably best for linux too
7277 * On an embedded system this may be something to change
7280 if (buf->size > len)
7281 size = buf->size * 2;
7283 size = buf->use + len + 100;
7285 size = buf->use + len + 100;
7288 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7289 size_t start_buf = buf->content - buf->contentIO;
7291 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
7292 if (newbuf == NULL) {
7293 xmlTreeErrMemory("growing buffer");
7296 buf->contentIO = newbuf;
7297 buf->content = newbuf + start_buf;
7299 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
7300 if (newbuf == NULL) {
7301 xmlTreeErrMemory("growing buffer");
7304 buf->content = newbuf;
7307 return(buf->size - buf->use);
7312 * @file: the file output
7313 * @buf: the buffer to dump
7315 * Dumps an XML buffer to a FILE *.
7316 * Returns the number of #xmlChar written
7319 xmlBufferDump(FILE *file, xmlBufferPtr buf) {
7324 xmlGenericError(xmlGenericErrorContext,
7325 "xmlBufferDump: buf == NULL\n");
7329 if (buf->content == NULL) {
7331 xmlGenericError(xmlGenericErrorContext,
7332 "xmlBufferDump: buf->content == NULL\n");
7338 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
7346 * Function to extract the content of a buffer
7348 * Returns the internal content
7352 xmlBufferContent(const xmlBuffer *buf)
7357 return buf->content;
7364 * Function to get the length of a buffer
7366 * Returns the length of data in the internal content
7370 xmlBufferLength(const xmlBuffer *buf)
7380 * @buf: the buffer to resize
7381 * @size: the desired size
7383 * Resize a buffer to accommodate minimum size of @size.
7385 * Returns 0 in case of problems, 1 otherwise
7388 xmlBufferResize(xmlBufferPtr buf, unsigned int size)
7390 unsigned int newSize;
7391 xmlChar* rebuf = NULL;
7397 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7399 /* Don't resize if we don't have to */
7400 if (size < buf->size)
7403 /* figure out new size */
7404 switch (buf->alloc){
7405 case XML_BUFFER_ALLOC_IO:
7406 case XML_BUFFER_ALLOC_DOUBLEIT:
7407 /*take care of empty case*/
7408 newSize = (buf->size ? buf->size*2 : size + 10);
7409 while (size > newSize) {
7410 if (newSize > UINT_MAX / 2) {
7411 xmlTreeErrMemory("growing buffer");
7417 case XML_BUFFER_ALLOC_EXACT:
7420 case XML_BUFFER_ALLOC_HYBRID:
7421 if (buf->use < BASE_BUFFER_SIZE)
7424 newSize = buf->size * 2;
7425 while (size > newSize) {
7426 if (newSize > UINT_MAX / 2) {
7427 xmlTreeErrMemory("growing buffer");
7440 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7441 start_buf = buf->content - buf->contentIO;
7443 if (start_buf > newSize) {
7444 /* move data back to start */
7445 memmove(buf->contentIO, buf->content, buf->use);
7446 buf->content = buf->contentIO;
7447 buf->content[buf->use] = 0;
7448 buf->size += start_buf;
7450 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
7451 if (rebuf == NULL) {
7452 xmlTreeErrMemory("growing buffer");
7455 buf->contentIO = rebuf;
7456 buf->content = rebuf + start_buf;
7459 if (buf->content == NULL) {
7460 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7461 } else if (buf->size - buf->use < 100) {
7462 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
7465 * if we are reallocating a buffer far from being full, it's
7466 * better to make a new allocation and copy only the used range
7467 * and free the old one.
7469 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7470 if (rebuf != NULL) {
7471 memcpy(rebuf, buf->content, buf->use);
7472 xmlFree(buf->content);
7473 rebuf[buf->use] = 0;
7476 if (rebuf == NULL) {
7477 xmlTreeErrMemory("growing buffer");
7480 buf->content = rebuf;
7482 buf->size = newSize;
7489 * @buf: the buffer to dump
7490 * @str: the #xmlChar string
7491 * @len: the number of #xmlChar to add
7493 * Add a string range to an XML buffer. if len == -1, the length of
7494 * str is recomputed.
7496 * Returns 0 successful, a positive error code number otherwise
7497 * and -1 in case of internal or API error.
7500 xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
7501 unsigned int needSize;
7503 if ((str == NULL) || (buf == NULL)) {
7506 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7509 xmlGenericError(xmlGenericErrorContext,
7510 "xmlBufferAdd: len < 0\n");
7514 if (len == 0) return 0;
7517 len = xmlStrlen(str);
7519 if (len < 0) return -1;
7520 if (len == 0) return 0;
7522 needSize = buf->use + len + 2;
7523 if (needSize > buf->size){
7524 if (!xmlBufferResize(buf, needSize)){
7525 xmlTreeErrMemory("growing buffer");
7526 return XML_ERR_NO_MEMORY;
7530 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
7532 buf->content[buf->use] = 0;
7539 * @str: the #xmlChar string
7540 * @len: the number of #xmlChar to add
7542 * Add a string range to the beginning of an XML buffer.
7543 * if len == -1, the length of @str is recomputed.
7545 * Returns 0 successful, a positive error code number otherwise
7546 * and -1 in case of internal or API error.
7549 xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7550 unsigned int needSize;
7554 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7557 xmlGenericError(xmlGenericErrorContext,
7558 "xmlBufferAddHead: str == NULL\n");
7564 xmlGenericError(xmlGenericErrorContext,
7565 "xmlBufferAddHead: len < 0\n");
7569 if (len == 0) return 0;
7572 len = xmlStrlen(str);
7574 if (len <= 0) return -1;
7576 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7577 size_t start_buf = buf->content - buf->contentIO;
7579 if (start_buf > (unsigned int) len) {
7581 * We can add it in the space previously shrinked
7583 buf->content -= len;
7584 memmove(&buf->content[0], str, len);
7590 needSize = buf->use + len + 2;
7591 if (needSize > buf->size){
7592 if (!xmlBufferResize(buf, needSize)){
7593 xmlTreeErrMemory("growing buffer");
7594 return XML_ERR_NO_MEMORY;
7598 memmove(&buf->content[len], &buf->content[0], buf->use);
7599 memmove(&buf->content[0], str, len);
7601 buf->content[buf->use] = 0;
7607 * @buf: the buffer to add to
7608 * @str: the #xmlChar string
7610 * Append a zero terminated string to an XML buffer.
7612 * Returns 0 successful, a positive error code number otherwise
7613 * and -1 in case of internal or API error.
7616 xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
7619 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7620 if (str == NULL) return -1;
7621 return xmlBufferAdd(buf, str, -1);
7626 * @buf: the buffer to dump
7627 * @str: the C char string
7629 * Append a zero terminated C string to an XML buffer.
7631 * Returns 0 successful, a positive error code number otherwise
7632 * and -1 in case of internal or API error.
7635 xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7640 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7643 xmlGenericError(xmlGenericErrorContext,
7644 "xmlBufferCCat: str == NULL\n");
7648 for (cur = str;*cur != 0;cur++) {
7649 if (buf->use + 10 >= buf->size) {
7650 if (!xmlBufferResize(buf, buf->use+10)){
7651 xmlTreeErrMemory("growing buffer");
7652 return XML_ERR_NO_MEMORY;
7655 buf->content[buf->use++] = *cur;
7657 buf->content[buf->use] = 0;
7662 * xmlBufferWriteCHAR:
7663 * @buf: the XML buffer
7664 * @string: the string to add
7666 * routine which manages and grows an output buffer. This one adds
7667 * xmlChars at the end of the buffer.
7670 xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
7673 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7674 xmlBufferCat(buf, string);
7678 * xmlBufferWriteChar:
7679 * @buf: the XML buffer output
7680 * @string: the string to add
7682 * routine which manage and grows an output buffer. This one add
7683 * C chars at the end of the array.
7686 xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
7689 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7690 xmlBufferCCat(buf, string);
7695 * xmlBufferWriteQuotedString:
7696 * @buf: the XML buffer output
7697 * @string: the string to add
7699 * routine which manage and grows an output buffer. This one writes
7700 * a quoted or double quoted #xmlChar string, checking first if it holds
7701 * quote or double-quotes internally
7704 xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
7705 const xmlChar *cur, *base;
7708 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7709 if (xmlStrchr(string, '\"')) {
7710 if (xmlStrchr(string, '\'')) {
7712 xmlGenericError(xmlGenericErrorContext,
7713 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7715 xmlBufferCCat(buf, "\"");
7716 base = cur = string;
7720 xmlBufferAdd(buf, base, cur - base);
7721 xmlBufferAdd(buf, BAD_CAST """, 6);
7730 xmlBufferAdd(buf, base, cur - base);
7731 xmlBufferCCat(buf, "\"");
7734 xmlBufferCCat(buf, "\'");
7735 xmlBufferCat(buf, string);
7736 xmlBufferCCat(buf, "\'");
7739 xmlBufferCCat(buf, "\"");
7740 xmlBufferCat(buf, string);
7741 xmlBufferCCat(buf, "\"");
7747 * xmlGetDocCompressMode:
7748 * @doc: the document
7750 * get the compression ratio for a document, ZLIB based
7751 * Returns 0 (uncompressed) to 9 (max compression)
7754 xmlGetDocCompressMode (const xmlDoc *doc) {
7755 if (doc == NULL) return(-1);
7756 return(doc->compression);
7760 * xmlSetDocCompressMode:
7761 * @doc: the document
7762 * @mode: the compression ratio
7764 * set the compression ratio for a document, ZLIB based
7765 * Correct values: 0 (uncompressed) to 9 (max compression)
7768 xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7769 if (doc == NULL) return;
7770 if (mode < 0) doc->compression = 0;
7771 else if (mode > 9) doc->compression = 9;
7772 else doc->compression = mode;
7776 * xmlGetCompressMode:
7778 * get the default compression mode used, ZLIB based.
7779 * Returns 0 (uncompressed) to 9 (max compression)
7782 xmlGetCompressMode(void)
7784 return (xmlCompressMode);
7788 * xmlSetCompressMode:
7789 * @mode: the compression ratio
7791 * set the default compression mode used, ZLIB based
7792 * Correct values: 0 (uncompressed) to 9 (max compression)
7795 xmlSetCompressMode(int mode) {
7796 if (mode < 0) xmlCompressMode = 0;
7797 else if (mode > 9) xmlCompressMode = 9;
7798 else xmlCompressMode = mode;
7801 #define XML_TREE_NSMAP_PARENT -1
7802 #define XML_TREE_NSMAP_XML -2
7803 #define XML_TREE_NSMAP_DOC -3
7804 #define XML_TREE_NSMAP_CUSTOM -4
7806 typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7807 struct xmlNsMapItem {
7808 xmlNsMapItemPtr next;
7809 xmlNsMapItemPtr prev;
7810 xmlNsPtr oldNs; /* old ns decl reference */
7811 xmlNsPtr newNs; /* new ns decl reference */
7812 int shadowDepth; /* Shadowed at this depth */
7815 * >= 0 == @node's ns-decls
7816 * -1 == @parent's ns-decls
7817 * -2 == the doc->oldNs XML ns-decl
7818 * -3 == the doc->oldNs storage ns-decls
7819 * -4 == ns-decls provided via custom ns-handling
7824 typedef struct xmlNsMap *xmlNsMapPtr;
7826 xmlNsMapItemPtr first;
7827 xmlNsMapItemPtr last;
7828 xmlNsMapItemPtr pool;
7831 #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7832 #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7833 #define XML_NSMAP_POP(m, i) \
7835 (m)->last = (i)->prev; \
7836 if ((m)->last == NULL) \
7837 (m)->first = NULL; \
7839 (m)->last->next = NULL; \
7840 (i)->next = (m)->pool; \
7844 * xmlDOMWrapNsMapFree:
7850 xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7852 xmlNsMapItemPtr cur, tmp;
7857 while (cur != NULL) {
7863 while (cur != NULL) {
7872 * xmlDOMWrapNsMapAddItem:
7874 * @oldNs: the old ns-struct
7875 * @newNs: the new ns-struct
7876 * @depth: depth and ns-kind information
7878 * Adds an ns-mapping item.
7880 static xmlNsMapItemPtr
7881 xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
7882 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
7884 xmlNsMapItemPtr ret;
7889 if ((position != -1) && (position != 0))
7895 * Create the ns-map.
7897 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7899 xmlTreeErrMemory("allocating namespace map");
7902 memset(map, 0, sizeof(struct xmlNsMap));
7906 if (map->pool != NULL) {
7908 * Reuse an item from the pool.
7911 map->pool = ret->next;
7912 memset(ret, 0, sizeof(struct xmlNsMapItem));
7915 * Create a new item.
7917 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7919 xmlTreeErrMemory("allocating namespace map item");
7922 memset(ret, 0, sizeof(struct xmlNsMapItem));
7925 if (map->first == NULL) {
7931 } else if (position == -1) {
7935 ret->prev = map->last;
7936 map->last->next = ret;
7938 } else if (position == 0) {
7940 * Set on first position.
7942 map->first->prev = ret;
7943 ret->next = map->first;
7949 ret->shadowDepth = -1;
7955 * xmlDOMWrapStoreNs:
7957 * @nsName: the namespace name
7958 * @prefix: the prefix
7960 * Creates or reuses an xmlNs struct on doc->oldNs with
7961 * the given prefix and namespace name.
7963 * Returns the aquired ns struct or NULL in case of an API
7964 * or internal error.
7967 xmlDOMWrapStoreNs(xmlDocPtr doc,
7968 const xmlChar *nsName,
7969 const xmlChar *prefix)
7975 ns = xmlTreeEnsureXMLDecl(doc);
7978 if (ns->next != NULL) {
7981 while (ns != NULL) {
7982 if (((ns->prefix == prefix) ||
7983 xmlStrEqual(ns->prefix, prefix)) &&
7984 xmlStrEqual(ns->href, nsName)) {
7987 if (ns->next == NULL)
7994 ns->next = xmlNewNs(NULL, nsName, prefix);
8001 * xmlDOMWrapNewCtxt:
8003 * Allocates and initializes a new DOM-wrapper context.
8005 * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal error.
8008 xmlDOMWrapNewCtxt(void)
8010 xmlDOMWrapCtxtPtr ret;
8012 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
8014 xmlTreeErrMemory("allocating DOM-wrapper context");
8017 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
8022 * xmlDOMWrapFreeCtxt:
8023 * @ctxt: the DOM-wrapper context
8025 * Frees the DOM-wrapper context.
8028 xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
8032 if (ctxt->namespaceMap != NULL)
8033 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
8035 * TODO: Store the namespace map in the context.
8041 * xmlTreeLookupNsListByPrefix:
8042 * @nsList: a list of ns-structs
8043 * @prefix: the searched prefix
8045 * Searches for a ns-decl with the given prefix in @nsList.
8047 * Returns the ns-decl if found, NULL if not found and on
8051 xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
8059 if ((prefix == ns->prefix) ||
8060 xmlStrEqual(prefix, ns->prefix)) {
8064 } while (ns != NULL);
8071 * xmlDOMWrapNSNormGatherInScopeNs:
8072 * @map: the namespace map
8073 * @node: the node to start with
8075 * Puts in-scope namespaces into the ns-map.
8077 * Returns 0 on success, -1 on API or internal errors.
8080 xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
8088 if ((map == NULL) || (*map != NULL))
8090 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8093 * Get in-scope ns-decls of @parent.
8096 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
8097 if (cur->type == XML_ELEMENT_NODE) {
8098 if (cur->nsDef != NULL) {
8102 if (XML_NSMAP_NOTEMPTY(*map)) {
8104 * Skip shadowed prefixes.
8106 XML_NSMAP_FOREACH(*map, mi) {
8107 if ((ns->prefix == mi->newNs->prefix) ||
8108 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
8117 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
8118 ns, XML_TREE_NSMAP_PARENT);
8122 mi->shadowDepth = 0;
8124 } while (ns != NULL);
8133 * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
8134 * otherwise copy it, when it was in the source-dict.
8136 #define XML_TREE_ADOPT_STR(str) \
8137 if (adoptStr && (str != NULL)) { \
8138 if (destDoc->dict) { \
8139 const xmlChar *old = str; \
8140 str = xmlDictLookup(destDoc->dict, str, -1); \
8141 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
8142 (!xmlDictOwns(sourceDoc->dict, old))) \
8143 xmlFree((char *)old); \
8144 } else if ((sourceDoc) && (sourceDoc->dict) && \
8145 xmlDictOwns(sourceDoc->dict, str)) { \
8146 str = BAD_CAST xmlStrdup(str); \
8151 * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
8152 * put it in dest-dict or copy it.
8154 #define XML_TREE_ADOPT_STR_2(str) \
8155 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
8156 (sourceDoc->dict != NULL) && \
8157 xmlDictOwns(sourceDoc->dict, cur->content)) { \
8158 if (destDoc->dict) \
8159 cur->content = (xmlChar *) \
8160 xmlDictLookup(destDoc->dict, cur->content, -1); \
8162 cur->content = xmlStrdup(BAD_CAST cur->content); \
8166 * xmlDOMWrapNSNormAddNsMapItem2:
8168 * For internal use. Adds a ns-decl mapping.
8170 * Returns 0 on success, -1 on internal errors.
8173 xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
8174 xmlNsPtr oldNs, xmlNsPtr newNs)
8176 if (*list == NULL) {
8177 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
8178 if (*list == NULL) {
8179 xmlTreeErrMemory("alloc ns map item");
8184 } else if ((*number) >= (*size)) {
8186 *list = (xmlNsPtr *) xmlRealloc(*list,
8187 (*size) * 2 * sizeof(xmlNsPtr));
8188 if (*list == NULL) {
8189 xmlTreeErrMemory("realloc ns map item");
8193 (*list)[2 * (*number)] = oldNs;
8194 (*list)[2 * (*number) +1] = newNs;
8200 * xmlDOMWrapRemoveNode:
8201 * @ctxt: a DOM wrapper context
8203 * @node: the node to be removed.
8204 * @options: set of options, unused at the moment
8206 * Unlinks the given node from its owner.
8207 * This will substitute ns-references to node->nsDef for
8208 * ns-references to doc->oldNs, thus ensuring the removed
8209 * branch to be autark wrt ns-references.
8211 * NOTE: This function was not intensively tested.
8213 * Returns 0 on success, 1 if the node is not supported,
8214 * -1 on API and internal errors.
8217 xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
8218 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
8220 xmlNsPtr *list = NULL;
8221 int sizeList, nbList, i, j;
8224 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
8227 /* TODO: 0 or -1 ? */
8228 if (node->parent == NULL)
8231 switch (node->type) {
8233 case XML_CDATA_SECTION_NODE:
8234 case XML_ENTITY_REF_NODE:
8236 case XML_COMMENT_NODE:
8237 xmlUnlinkNode(node);
8239 case XML_ELEMENT_NODE:
8240 case XML_ATTRIBUTE_NODE:
8245 xmlUnlinkNode(node);
8247 * Save out-of-scope ns-references in doc->oldNs.
8250 switch (node->type) {
8251 case XML_ELEMENT_NODE:
8252 if ((ctxt == NULL) && (node->nsDef != NULL)) {
8255 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8256 &nbList, ns, ns) == -1)
8257 goto internal_error;
8259 } while (ns != NULL);
8261 /* No break on purpose. */
8262 case XML_ATTRIBUTE_NODE:
8263 if (node->ns != NULL) {
8268 for (i = 0, j = 0; i < nbList; i++, j += 2) {
8269 if (node->ns == list[j]) {
8270 node->ns = list[++j];
8282 * Add to doc's oldNs.
8284 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
8287 goto internal_error;
8293 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8294 &nbList, node->ns, ns) == -1)
8295 goto internal_error;
8299 if ((node->type == XML_ELEMENT_NODE) &&
8300 (node->properties != NULL)) {
8301 node = (xmlNodePtr) node->properties;
8309 if ((node->type == XML_ELEMENT_NODE) &&
8310 (node->children != NULL)) {
8311 node = node->children;
8317 if (node->next != NULL)
8320 node = node->parent;
8323 } while (node != NULL);
8336 * xmlSearchNsByNamespaceStrict:
8337 * @doc: the document
8338 * @node: the start node
8339 * @nsName: the searched namespace name
8340 * @retNs: the resulting ns-decl
8341 * @prefixed: if the found ns-decl must have a prefix (for attributes)
8343 * Dynamically searches for a ns-declaration which matches
8344 * the given @nsName in the ancestor-or-self axis of @node.
8346 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8347 * and internal errors.
8350 xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
8351 const xmlChar* nsName,
8352 xmlNsPtr *retNs, int prefixed)
8354 xmlNodePtr cur, prev = NULL, out = NULL;
8355 xmlNsPtr ns, prevns;
8357 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
8359 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8363 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
8364 *retNs = xmlTreeEnsureXMLDecl(doc);
8371 if (cur->type == XML_ELEMENT_NODE) {
8372 if (cur->nsDef != NULL) {
8373 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8374 if (prefixed && (ns->prefix == NULL))
8378 * Check the last level of ns-decls for a
8381 prevns = prev->nsDef;
8383 if ((prevns->prefix == ns->prefix) ||
8384 ((prevns->prefix != NULL) &&
8385 (ns->prefix != NULL) &&
8386 xmlStrEqual(prevns->prefix, ns->prefix))) {
8392 prevns = prevns->next;
8393 } while (prevns != NULL);
8398 * Ns-name comparison.
8400 if ((nsName == ns->href) ||
8401 xmlStrEqual(nsName, ns->href)) {
8403 * At this point the prefix can only be shadowed,
8404 * if we are the the (at least) 3rd level of
8410 ret = xmlNsInScope(doc, node, prev, ns->prefix);
8414 * TODO: Should we try to find a matching ns-name
8415 * only once? This here keeps on searching.
8416 * I think we should try further since, there might
8417 * be an other matching ns-decl with an unshadowed
8430 } else if ((cur->type == XML_ENTITY_NODE) ||
8431 (cur->type == XML_ENTITY_DECL))
8434 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8439 * xmlSearchNsByPrefixStrict:
8440 * @doc: the document
8441 * @node: the start node
8442 * @prefix: the searched namespace prefix
8443 * @retNs: the resulting ns-decl
8445 * Dynamically searches for a ns-declaration which matches
8446 * the given @nsName in the ancestor-or-self axis of @node.
8448 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8449 * and internal errors.
8452 xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
8453 const xmlChar* prefix,
8459 if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL))
8464 if (IS_STR_XML(prefix)) {
8466 *retNs = xmlTreeEnsureXMLDecl(doc);
8474 if (cur->type == XML_ELEMENT_NODE) {
8475 if (cur->nsDef != NULL) {
8478 if ((prefix == ns->prefix) ||
8479 xmlStrEqual(prefix, ns->prefix))
8482 * Disabled namespaces, e.g. xmlns:abc="".
8484 if (ns->href == NULL)
8491 } while (ns != NULL);
8493 } else if ((cur->type == XML_ENTITY_NODE) ||
8494 (cur->type == XML_ENTITY_DECL))
8497 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8502 * xmlDOMWrapNSNormDeclareNsForced:
8504 * @elem: the element-node to declare on
8505 * @nsName: the namespace-name of the ns-decl
8506 * @prefix: the preferred prefix of the ns-decl
8507 * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
8509 * Declares a new namespace on @elem. It tries to use the
8510 * given @prefix; if a ns-decl with the given prefix is already existent
8511 * on @elem, it will generate an other prefix.
8513 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8514 * and internal errors.
8517 xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
8519 const xmlChar *nsName,
8520 const xmlChar *prefix,
8526 const xmlChar *pref;
8529 if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
8532 * Create a ns-decl on @anchor.
8537 * Lookup whether the prefix is unused in elem's ns-decls.
8539 if ((elem->nsDef != NULL) &&
8540 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
8541 goto ns_next_prefix;
8542 if (checkShadow && elem->parent &&
8543 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8545 * Does it shadow ancestor ns-decls?
8547 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
8548 goto ns_next_prefix;
8550 ret = xmlNewNs(NULL, nsName, pref);
8553 if (elem->nsDef == NULL)
8556 xmlNsPtr ns2 = elem->nsDef;
8557 while (ns2->next != NULL)
8566 if (prefix == NULL) {
8567 snprintf((char *) buf, sizeof(buf),
8570 snprintf((char *) buf, sizeof(buf),
8571 "%.30s_%d", (char *)prefix, counter);
8572 pref = BAD_CAST buf;
8577 * xmlDOMWrapNSNormAquireNormalizedNs:
8579 * @elem: the element-node to declare namespaces on
8580 * @ns: the ns-struct to use for the search
8581 * @retNs: the found/created ns-struct
8582 * @nsMap: the ns-map
8583 * @depth: the current tree depth
8584 * @ancestorsOnly: search in ancestor ns-decls only
8585 * @prefixed: if the searched ns-decl must have a prefix (for attributes)
8587 * Searches for a matching ns-name in the ns-decls of @nsMap, if not
8588 * found it will either declare it on @elem, or store it in doc->oldNs.
8589 * If a new ns-decl needs to be declared on @elem, it tries to use the
8590 * @ns->prefix for it, if this prefix is already in use on @elem, it will
8591 * change the prefix or the new ns-decl.
8593 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8596 xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8608 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
8614 * Handle XML namespace.
8616 if (IS_STR_XML(ns->prefix)) {
8618 * Insert XML namespace mapping.
8620 *retNs = xmlTreeEnsureXMLDecl(doc);
8626 * If the search should be done in ancestors only and no
8627 * @elem (the first ancestor) was specified, then skip the search.
8629 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8630 (! (ancestorsOnly && (elem == NULL))))
8633 * Try to find an equal ns-name in in-scope ns-decls.
8635 XML_NSMAP_FOREACH(*nsMap, mi) {
8636 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8638 * ancestorsOnly: This should be turned on to gain speed,
8639 * if one knows that the branch itself was already
8640 * ns-wellformed and no stale references existed.
8641 * I.e. it searches in the ancestor axis only.
8643 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8644 /* Skip shadowed prefixes. */
8645 (mi->shadowDepth == -1) &&
8646 /* Skip xmlns="" or xmlns:foo="". */
8647 ((mi->newNs->href != NULL) &&
8648 (mi->newNs->href[0] != 0)) &&
8649 /* Ensure a prefix if wanted. */
8650 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8652 ((mi->newNs->href == ns->href) ||
8653 xmlStrEqual(mi->newNs->href, ns->href))) {
8654 /* Set the mapping. */
8662 * No luck, the namespace is out of scope or shadowed.
8668 * Store ns-decls in "oldNs" of the document-node.
8670 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8676 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
8677 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8685 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8690 if (*nsMap != NULL) {
8692 * Does it shadow ancestor ns-decls?
8694 XML_NSMAP_FOREACH(*nsMap, mi) {
8695 if ((mi->depth < depth) &&
8696 (mi->shadowDepth == -1) &&
8697 ((ns->prefix == mi->newNs->prefix) ||
8698 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8702 mi->shadowDepth = depth;
8707 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
8717 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
8718 } xmlDOMReconcileNSOptions;
8721 * xmlDOMWrapReconcileNamespaces:
8722 * @ctxt: DOM wrapper context, unused at the moment
8723 * @elem: the element-node
8724 * @options: option flags
8726 * Ensures that ns-references point to ns-decls hold on element-nodes.
8727 * Ensures that the tree is namespace wellformed by creating additional
8728 * ns-decls where needed. Note that, since prefixes of already existent
8729 * ns-decls can be shadowed by this process, it could break QNames in
8730 * attribute values or element content.
8732 * NOTE: This function was not intensively tested.
8734 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8738 xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8742 int depth = -1, adoptns = 0, parnsdone = 0;
8743 xmlNsPtr ns, prevns;
8745 xmlNodePtr cur, curElem = NULL;
8746 xmlNsMapPtr nsMap = NULL;
8747 xmlNsMapItemPtr /* topmi = NULL, */ mi;
8748 /* @ancestorsOnly should be set by an option flag. */
8749 int ancestorsOnly = 0;
8750 int optRemoveRedundantNS =
8751 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8752 xmlNsPtr *listRedund = NULL;
8753 int sizeRedund = 0, nbRedund = 0, ret, i, j;
8755 if ((elem == NULL) || (elem->doc == NULL) ||
8756 (elem->type != XML_ELEMENT_NODE))
8762 switch (cur->type) {
8763 case XML_ELEMENT_NODE:
8768 * Namespace declarations.
8770 if (cur->nsDef != NULL) {
8773 while (ns != NULL) {
8775 if ((elem->parent) &&
8776 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8778 * Gather ancestor in-scope ns-decls.
8780 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8781 elem->parent) == -1)
8782 goto internal_error;
8788 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8790 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
8791 XML_NSMAP_FOREACH(nsMap, mi) {
8792 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8793 (mi->shadowDepth == -1) &&
8794 ((ns->prefix == mi->newNs->prefix) ||
8795 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8796 ((ns->href == mi->newNs->href) ||
8797 xmlStrEqual(ns->href, mi->newNs->href)))
8800 * A redundant ns-decl was found.
8801 * Add it to the list of redundant ns-decls.
8803 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8804 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8805 goto internal_error;
8807 * Remove the ns-decl from the element-node.
8810 prevns->next = ns->next;
8812 cur->nsDef = ns->next;
8819 * Skip ns-references handling if the referenced
8820 * ns-decl is declared on the same element.
8822 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
8825 * Does it shadow any ns-decl?
8827 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8828 XML_NSMAP_FOREACH(nsMap, mi) {
8829 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8830 (mi->shadowDepth == -1) &&
8831 ((ns->prefix == mi->newNs->prefix) ||
8832 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8834 mi->shadowDepth = depth;
8841 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
8843 goto internal_error;
8852 /* No break on purpose. */
8853 case XML_ATTRIBUTE_NODE:
8854 /* No ns, no fun. */
8855 if (cur->ns == NULL)
8859 if ((elem->parent) &&
8860 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8861 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8862 elem->parent) == -1)
8863 goto internal_error;
8868 * Adjust the reference if this was a redundant ns-decl.
8871 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8872 if (cur->ns == listRedund[j]) {
8873 cur->ns = listRedund[++j];
8879 * Adopt ns-references.
8881 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8883 * Search for a mapping.
8885 XML_NSMAP_FOREACH(nsMap, mi) {
8886 if ((mi->shadowDepth == -1) &&
8887 (cur->ns == mi->oldNs)) {
8889 cur->ns = mi->newNs;
8895 * Aquire a normalized ns-decl and add it to the map.
8897 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8901 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8902 goto internal_error;
8906 if ((cur->type == XML_ELEMENT_NODE) &&
8907 (cur->properties != NULL)) {
8909 * Process attributes.
8911 cur = (xmlNodePtr) cur->properties;
8919 if ((cur->type == XML_ELEMENT_NODE) &&
8920 (cur->children != NULL)) {
8922 * Process content of element-nodes only.
8924 cur = cur->children;
8930 if (cur->type == XML_ELEMENT_NODE) {
8931 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8935 while ((nsMap->last != NULL) &&
8936 (nsMap->last->depth >= depth))
8938 XML_NSMAP_POP(nsMap, mi)
8943 XML_NSMAP_FOREACH(nsMap, mi) {
8944 if (mi->shadowDepth >= depth)
8945 mi->shadowDepth = -1;
8950 if (cur->next != NULL)
8953 if (cur->type == XML_ATTRIBUTE_NODE) {
8960 } while (cur != NULL);
8968 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8969 xmlFreeNs(listRedund[j]);
8971 xmlFree(listRedund);
8974 xmlDOMWrapNsMapFree(nsMap);
8979 * xmlDOMWrapAdoptBranch:
8980 * @ctxt: the optional context for custom processing
8981 * @sourceDoc: the optional sourceDoc
8982 * @node: the element-node to start with
8983 * @destDoc: the destination doc for adoption
8984 * @destParent: the optional new parent of @node in @destDoc
8985 * @options: option flags
8987 * Ensures that ns-references point to @destDoc: either to
8988 * elements->nsDef entries if @destParent is given, or to
8989 * @destDoc->oldNs otherwise.
8990 * If @destParent is given, it ensures that the tree is namespace
8991 * wellformed by creating additional ns-decls where needed.
8992 * Note that, since prefixes of already existent ns-decls can be
8993 * shadowed by this process, it could break QNames in attribute
8994 * values or element content.
8996 * NOTE: This function was not intensively tested.
8998 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9001 xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
9002 xmlDocPtr sourceDoc,
9005 xmlNodePtr destParent,
9006 int options ATTRIBUTE_UNUSED)
9009 xmlNodePtr cur, curElem = NULL;
9010 xmlNsMapPtr nsMap = NULL;
9013 int depth = -1, adoptStr = 1;
9014 /* gather @parent's ns-decls. */
9016 /* @ancestorsOnly should be set per option. */
9017 int ancestorsOnly = 0;
9020 * Optimize string adoption for equal or none dicts.
9022 if ((sourceDoc != NULL) &&
9023 (sourceDoc->dict == destDoc->dict))
9029 * Get the ns-map from the context if available.
9032 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9034 * Disable search for ns-decls in the parent-axis of the
9035 * desination element, if:
9036 * 1) there's no destination parent
9037 * 2) custom ns-reference handling is used
9039 if ((destParent == NULL) ||
9040 (ctxt && ctxt->getNsForNodeFunc))
9047 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9048 goto internal_error;
9050 while (cur != NULL) {
9052 * Paranoid source-doc sanity check.
9054 if (cur->doc != sourceDoc) {
9056 * We'll assume XIncluded nodes if the doc differs.
9057 * TODO: Do we need to reconciliate XIncluded nodes?
9058 * This here skips XIncluded nodes and tries to handle
9061 if (cur->next == NULL)
9065 if ((cur->type == XML_XINCLUDE_END) ||
9066 (cur->doc == node->doc))
9068 } while (cur->next != NULL);
9070 if (cur->doc != node->doc)
9074 switch (cur->type) {
9075 case XML_XINCLUDE_START:
9076 case XML_XINCLUDE_END:
9081 case XML_ELEMENT_NODE:
9085 * Namespace declarations.
9086 * - ns->href and ns->prefix are never in the dict, so
9087 * we need not move the values over to the destination dict.
9088 * - Note that for custom handling of ns-references,
9089 * the ns-decls need not be stored in the ns-map,
9090 * since they won't be referenced by node->ns.
9093 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
9097 * Gather @parent's in-scope ns-decls.
9099 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9101 goto internal_error;
9104 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9106 * NOTE: ns->prefix and ns->href are never in the dict.
9107 * XML_TREE_ADOPT_STR(ns->prefix)
9108 * XML_TREE_ADOPT_STR(ns->href)
9111 * Does it shadow any ns-decl?
9113 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9114 XML_NSMAP_FOREACH(nsMap, mi) {
9115 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9116 (mi->shadowDepth == -1) &&
9117 ((ns->prefix == mi->newNs->prefix) ||
9118 xmlStrEqual(ns->prefix,
9119 mi->newNs->prefix))) {
9121 mi->shadowDepth = depth;
9128 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9129 ns, ns, depth) == NULL)
9130 goto internal_error;
9133 /* No break on purpose. */
9134 case XML_ATTRIBUTE_NODE:
9135 /* No namespace, no fun. */
9136 if (cur->ns == NULL)
9140 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9142 goto internal_error;
9146 * Adopt ns-references.
9148 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9150 * Search for a mapping.
9152 XML_NSMAP_FOREACH(nsMap, mi) {
9153 if ((mi->shadowDepth == -1) &&
9154 (cur->ns == mi->oldNs)) {
9156 cur->ns = mi->newNs;
9162 * No matching namespace in scope. We need a new one.
9164 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
9166 * User-defined behaviour.
9168 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9169 cur->ns->href, cur->ns->prefix);
9171 * Insert mapping if ns is available; it's the users fault
9174 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9175 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9176 goto internal_error;
9180 * Aquire a normalized ns-decl and add it to the map.
9182 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9183 /* ns-decls on curElem or on destDoc->oldNs */
9184 destParent ? curElem : NULL,
9188 /* ns-decls must be prefixed for attributes. */
9189 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9190 goto internal_error;
9195 * Further node properties.
9196 * TODO: Is this all?
9198 XML_TREE_ADOPT_STR(cur->name)
9199 if (cur->type == XML_ELEMENT_NODE) {
9206 if (cur->properties != NULL) {
9208 * Process first attribute node.
9210 cur = (xmlNodePtr) cur->properties;
9217 if ((sourceDoc != NULL) &&
9218 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
9220 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
9222 ((xmlAttrPtr) cur)->atype = 0;
9223 ((xmlAttrPtr) cur)->psvi = NULL;
9227 case XML_CDATA_SECTION_NODE:
9229 * This puts the content in the dest dict, only if
9230 * it was previously in the source dict.
9232 XML_TREE_ADOPT_STR_2(cur->content)
9234 case XML_ENTITY_REF_NODE:
9236 * Remove reference to the entitity-node.
9238 cur->content = NULL;
9239 cur->children = NULL;
9241 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9244 * Assign new entity-node if available.
9246 ent = xmlGetDocEntity(destDoc, cur->name);
9248 cur->content = ent->content;
9249 cur->children = (xmlNodePtr) ent;
9250 cur->last = (xmlNodePtr) ent;
9255 XML_TREE_ADOPT_STR(cur->name)
9256 XML_TREE_ADOPT_STR_2(cur->content)
9258 case XML_COMMENT_NODE:
9261 goto internal_error;
9266 if (cur->children != NULL) {
9267 cur = cur->children;
9274 if ((cur->type == XML_ELEMENT_NODE) ||
9275 (cur->type == XML_XINCLUDE_START) ||
9276 (cur->type == XML_XINCLUDE_END))
9279 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9281 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9285 while ((nsMap->last != NULL) &&
9286 (nsMap->last->depth >= depth))
9288 XML_NSMAP_POP(nsMap, mi)
9293 XML_NSMAP_FOREACH(nsMap, mi) {
9294 if (mi->shadowDepth >= depth)
9295 mi->shadowDepth = -1;
9300 if (cur->next != NULL)
9302 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
9303 (cur->parent->children != NULL))
9305 cur = cur->parent->children;
9321 if (nsMap != NULL) {
9322 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9324 * Just cleanup the map but don't free.
9328 nsMap->last->next = nsMap->pool;
9329 nsMap->pool = nsMap->first;
9330 nsMap->first = NULL;
9333 xmlDOMWrapNsMapFree(nsMap);
9339 * xmlDOMWrapCloneNode:
9340 * @ctxt: the optional context for custom processing
9341 * @sourceDoc: the optional sourceDoc
9342 * @node: the node to start with
9343 * @resNode: the clone of the given @node
9344 * @destDoc: the destination doc
9345 * @destParent: the optional new parent of @node in @destDoc
9346 * @deep: descend into child if set
9347 * @options: option flags
9349 * References of out-of scope ns-decls are remapped to point to @destDoc:
9350 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9351 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
9352 * This is the case when you don't know already where the cloned branch
9355 * If @destParent is given, it ensures that the tree is namespace
9356 * wellformed by creating additional ns-decls where needed.
9357 * Note that, since prefixes of already existent ns-decls can be
9358 * shadowed by this process, it could break QNames in attribute
9359 * values or element content.
9361 * 1) What to do with XInclude? Currently this returns an error for XInclude.
9363 * Returns 0 if the operation succeeded,
9364 * 1 if a node of unsupported (or not yet supported) type was given,
9365 * -1 on API/internal errors.
9369 xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
9370 xmlDocPtr sourceDoc,
9372 xmlNodePtr *resNode,
9374 xmlNodePtr destParent,
9376 int options ATTRIBUTE_UNUSED)
9379 xmlNodePtr cur, curElem = NULL;
9380 xmlNsMapPtr nsMap = NULL;
9384 /* int adoptStr = 1; */
9385 /* gather @parent's ns-decls. */
9389 * TODO: @ancestorsOnly should be set per option.
9392 int ancestorsOnly = 0;
9393 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
9394 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
9395 xmlDictPtr dict; /* The destination dict */
9397 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
9400 * TODO: Initially we support only element-nodes.
9402 if (node->type != XML_ELEMENT_NODE)
9405 * Check node->doc sanity.
9407 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9408 (node->doc != sourceDoc)) {
9410 * Might be an XIncluded node.
9414 if (sourceDoc == NULL)
9415 sourceDoc = node->doc;
9416 if (sourceDoc == NULL)
9419 dict = destDoc->dict;
9421 * Reuse the namespace map of the context.
9424 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9429 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9432 while (cur != NULL) {
9433 if (cur->doc != sourceDoc) {
9435 * We'll assume XIncluded nodes if the doc differs.
9436 * TODO: Do we need to reconciliate XIncluded nodes?
9437 * TODO: This here returns -1 in this case.
9439 goto internal_error;
9442 * Create a new node.
9444 switch (cur->type) {
9445 case XML_XINCLUDE_START:
9446 case XML_XINCLUDE_END:
9448 * TODO: What to do with XInclude?
9450 goto internal_error;
9452 case XML_ELEMENT_NODE:
9454 case XML_CDATA_SECTION_NODE:
9455 case XML_COMMENT_NODE:
9457 case XML_DOCUMENT_FRAG_NODE:
9458 case XML_ENTITY_REF_NODE:
9459 case XML_ENTITY_NODE:
9461 * Nodes of xmlNode structure.
9463 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
9464 if (clone == NULL) {
9465 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
9466 goto internal_error;
9468 memset(clone, 0, sizeof(xmlNode));
9470 * Set hierachical links.
9472 if (resultClone != NULL) {
9473 clone->parent = parentClone;
9475 prevClone->next = clone;
9476 clone->prev = prevClone;
9478 parentClone->children = clone;
9480 resultClone = clone;
9483 case XML_ATTRIBUTE_NODE:
9485 * Attributes (xmlAttr).
9487 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
9488 if (clone == NULL) {
9489 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
9490 goto internal_error;
9492 memset(clone, 0, sizeof(xmlAttr));
9494 * Set hierachical links.
9495 * TODO: Change this to add to the end of attributes.
9497 if (resultClone != NULL) {
9498 clone->parent = parentClone;
9500 prevClone->next = clone;
9501 clone->prev = prevClone;
9503 parentClone->properties = (xmlAttrPtr) clone;
9505 resultClone = clone;
9509 * TODO QUESTION: Any other nodes expected?
9511 goto internal_error;
9514 clone->type = cur->type;
9515 clone->doc = destDoc;
9518 * Clone the name of the node if any.
9520 if (cur->name == xmlStringText)
9521 clone->name = xmlStringText;
9522 else if (cur->name == xmlStringTextNoenc)
9524 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9525 * in tree.c, it might be set in Libxslt via
9526 * "xsl:disable-output-escaping".
9528 clone->name = xmlStringTextNoenc;
9529 else if (cur->name == xmlStringComment)
9530 clone->name = xmlStringComment;
9531 else if (cur->name != NULL) {
9532 DICT_CONST_COPY(cur->name, clone->name);
9535 switch (cur->type) {
9536 case XML_XINCLUDE_START:
9537 case XML_XINCLUDE_END:
9542 case XML_ELEMENT_NODE:
9546 * Namespace declarations.
9548 if (cur->nsDef != NULL) {
9550 if (destParent && (ctxt == NULL)) {
9552 * Gather @parent's in-scope ns-decls.
9554 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9556 goto internal_error;
9561 * Clone namespace declarations.
9563 cloneNsDefSlot = &(clone->nsDef);
9564 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9566 * Create a new xmlNs.
9568 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
9569 if (cloneNs == NULL) {
9570 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
9571 "allocating namespace");
9574 memset(cloneNs, 0, sizeof(xmlNs));
9575 cloneNs->type = XML_LOCAL_NAMESPACE;
9577 if (ns->href != NULL)
9578 cloneNs->href = xmlStrdup(ns->href);
9579 if (ns->prefix != NULL)
9580 cloneNs->prefix = xmlStrdup(ns->prefix);
9582 *cloneNsDefSlot = cloneNs;
9583 cloneNsDefSlot = &(cloneNs->next);
9586 * Note that for custom handling of ns-references,
9587 * the ns-decls need not be stored in the ns-map,
9588 * since they won't be referenced by node->ns.
9590 if ((ctxt == NULL) ||
9591 (ctxt->getNsForNodeFunc == NULL))
9594 * Does it shadow any ns-decl?
9596 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9597 XML_NSMAP_FOREACH(nsMap, mi) {
9598 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9599 (mi->shadowDepth == -1) &&
9600 ((ns->prefix == mi->newNs->prefix) ||
9601 xmlStrEqual(ns->prefix,
9602 mi->newNs->prefix))) {
9604 * Mark as shadowed at the current
9607 mi->shadowDepth = depth;
9614 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9615 ns, cloneNs, depth) == NULL)
9616 goto internal_error;
9620 /* cur->ns will be processed further down. */
9622 case XML_ATTRIBUTE_NODE:
9623 /* IDs will be processed further down. */
9624 /* cur->ns will be processed further down. */
9627 case XML_CDATA_SECTION_NODE:
9629 * Note that this will also cover the values of attributes.
9631 DICT_COPY(cur->content, clone->content);
9633 case XML_ENTITY_NODE:
9634 /* TODO: What to do here? */
9636 case XML_ENTITY_REF_NODE:
9637 if (sourceDoc != destDoc) {
9638 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9641 * Different doc: Assign new entity-node if available.
9643 ent = xmlGetDocEntity(destDoc, cur->name);
9645 clone->content = ent->content;
9646 clone->children = (xmlNodePtr) ent;
9647 clone->last = (xmlNodePtr) ent;
9652 * Same doc: Use the current node's entity declaration
9655 clone->content = cur->content;
9656 clone->children = cur->children;
9657 clone->last = cur->last;
9661 DICT_COPY(cur->content, clone->content);
9663 case XML_COMMENT_NODE:
9664 DICT_COPY(cur->content, clone->content);
9667 goto internal_error;
9670 if (cur->ns == NULL)
9671 goto end_ns_reference;
9673 /* handle_ns_reference: */
9675 ** The following will take care of references to ns-decls ********
9676 ** and is intended only for element- and attribute-nodes.
9680 if (destParent && (ctxt == NULL)) {
9681 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
9682 goto internal_error;
9687 * Adopt ns-references.
9689 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9691 * Search for a mapping.
9693 XML_NSMAP_FOREACH(nsMap, mi) {
9694 if ((mi->shadowDepth == -1) &&
9695 (cur->ns == mi->oldNs)) {
9697 * This is the nice case: a mapping was found.
9699 clone->ns = mi->newNs;
9700 goto end_ns_reference;
9705 * No matching namespace in scope. We need a new one.
9707 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
9709 * User-defined behaviour.
9711 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9712 cur->ns->href, cur->ns->prefix);
9714 * Add user's mapping.
9716 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9717 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9718 goto internal_error;
9722 * Aquire a normalized ns-decl and add it to the map.
9724 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9725 /* ns-decls on curElem or on destDoc->oldNs */
9726 destParent ? curElem : NULL,
9729 /* if we need to search only in the ancestor-axis */
9731 /* ns-decls must be prefixed for attributes. */
9732 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9733 goto internal_error;
9740 * Some post-processing.
9742 * Handle ID attributes.
9744 if ((clone->type == XML_ATTRIBUTE_NODE) &&
9745 (clone->parent != NULL))
9747 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
9751 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9752 if (idVal != NULL) {
9753 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9754 /* TODO: error message. */
9756 goto internal_error;
9764 ** The following will traverse the tree **************************
9767 * Walk the element's attributes before descending into child-nodes.
9769 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9771 parentClone = clone;
9772 cur = (xmlNodePtr) cur->properties;
9777 * Descend into child-nodes.
9779 if (cur->children != NULL) {
9780 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9782 parentClone = clone;
9783 cur = cur->children;
9790 * At this point we are done with the node, its content
9791 * and an element-nodes's attribute-nodes.
9795 if ((cur->type == XML_ELEMENT_NODE) ||
9796 (cur->type == XML_XINCLUDE_START) ||
9797 (cur->type == XML_XINCLUDE_END)) {
9799 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9801 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9805 while ((nsMap->last != NULL) &&
9806 (nsMap->last->depth >= depth))
9808 XML_NSMAP_POP(nsMap, mi)
9813 XML_NSMAP_FOREACH(nsMap, mi) {
9814 if (mi->shadowDepth >= depth)
9815 mi->shadowDepth = -1;
9820 if (cur->next != NULL) {
9823 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9827 if (clone->parent != NULL)
9828 clone->parent->last = clone;
9829 clone = clone->parent;
9831 parentClone = clone->parent;
9833 * Process parent --> next;
9838 /* This is for attributes only. */
9839 clone = clone->parent;
9840 parentClone = clone->parent;
9842 * Process parent-element --> children.
9857 if (nsMap != NULL) {
9858 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9860 * Just cleanup the map but don't free.
9864 nsMap->last->next = nsMap->pool;
9865 nsMap->pool = nsMap->first;
9866 nsMap->first = NULL;
9869 xmlDOMWrapNsMapFree(nsMap);
9872 * TODO: Should we try a cleanup of the cloned node in case of a
9875 *resNode = resultClone;
9880 * xmlDOMWrapAdoptAttr:
9881 * @ctxt: the optional context for custom processing
9882 * @sourceDoc: the optional source document of attr
9883 * @attr: the attribute-node to be adopted
9884 * @destDoc: the destination doc for adoption
9885 * @destParent: the optional new parent of @attr in @destDoc
9886 * @options: option flags
9888 * @attr is adopted by @destDoc.
9889 * Ensures that ns-references point to @destDoc: either to
9890 * elements->nsDef entries if @destParent is given, or to
9891 * @destDoc->oldNs otherwise.
9893 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9896 xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9897 xmlDocPtr sourceDoc,
9900 xmlNodePtr destParent,
9901 int options ATTRIBUTE_UNUSED)
9906 if ((attr == NULL) || (destDoc == NULL))
9909 attr->doc = destDoc;
9910 if (attr->ns != NULL) {
9914 /* TODO: User defined. */
9916 /* XML Namespace. */
9917 if (IS_STR_XML(attr->ns->prefix)) {
9918 ns = xmlTreeEnsureXMLDecl(destDoc);
9919 } else if (destParent == NULL) {
9921 * Store in @destDoc->oldNs.
9923 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9926 * Declare on @destParent.
9928 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
9930 goto internal_error;
9932 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9933 attr->ns->href, attr->ns->prefix, 1);
9937 goto internal_error;
9941 XML_TREE_ADOPT_STR(attr->name);
9947 if (attr->children == NULL)
9949 cur = attr->children;
9950 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9951 goto internal_error;
9952 while (cur != NULL) {
9954 switch (cur->type) {
9956 case XML_CDATA_SECTION_NODE:
9957 XML_TREE_ADOPT_STR_2(cur->content)
9959 case XML_ENTITY_REF_NODE:
9961 * Remove reference to the entitity-node.
9963 cur->content = NULL;
9964 cur->children = NULL;
9966 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9969 * Assign new entity-node if available.
9971 ent = xmlGetDocEntity(destDoc, cur->name);
9973 cur->content = ent->content;
9974 cur->children = (xmlNodePtr) ent;
9975 cur->last = (xmlNodePtr) ent;
9982 if (cur->children != NULL) {
9983 cur = cur->children;
9987 if (cur == (xmlNodePtr) attr)
9989 if (cur->next != NULL)
10002 * xmlDOMWrapAdoptNode:
10003 * @ctxt: the optional context for custom processing
10004 * @sourceDoc: the optional sourceDoc
10005 * @node: the node to start with
10006 * @destDoc: the destination doc
10007 * @destParent: the optional new parent of @node in @destDoc
10008 * @options: option flags
10010 * References of out-of scope ns-decls are remapped to point to @destDoc:
10011 * 1) If @destParent is given, then nsDef entries on element-nodes are used
10012 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
10013 * This is the case when you have an unlinked node and just want to move it
10014 * to the context of
10016 * If @destParent is given, it ensures that the tree is namespace
10017 * wellformed by creating additional ns-decls where needed.
10018 * Note that, since prefixes of already existent ns-decls can be
10019 * shadowed by this process, it could break QNames in attribute
10020 * values or element content.
10021 * NOTE: This function was not intensively tested.
10023 * Returns 0 if the operation succeeded,
10024 * 1 if a node of unsupported type was given,
10025 * 2 if a node of not yet supported type was given and
10026 * -1 on API/internal errors.
10029 xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
10030 xmlDocPtr sourceDoc,
10033 xmlNodePtr destParent,
10036 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
10037 (destDoc == NULL) ||
10038 ((destParent != NULL) && (destParent->doc != destDoc)))
10041 * Check node->doc sanity.
10043 if ((node->doc != NULL) && (sourceDoc != NULL) &&
10044 (node->doc != sourceDoc)) {
10046 * Might be an XIncluded node.
10050 if (sourceDoc == NULL)
10051 sourceDoc = node->doc;
10052 if (sourceDoc == destDoc)
10054 switch (node->type) {
10055 case XML_ELEMENT_NODE:
10056 case XML_ATTRIBUTE_NODE:
10057 case XML_TEXT_NODE:
10058 case XML_CDATA_SECTION_NODE:
10059 case XML_ENTITY_REF_NODE:
10061 case XML_COMMENT_NODE:
10063 case XML_DOCUMENT_FRAG_NODE:
10064 /* TODO: Support document-fragment-nodes. */
10070 * Unlink only if @node was not already added to @destParent.
10072 if ((node->parent != NULL) && (destParent != node->parent))
10073 xmlUnlinkNode(node);
10075 if (node->type == XML_ELEMENT_NODE) {
10076 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
10077 destDoc, destParent, options));
10078 } else if (node->type == XML_ATTRIBUTE_NODE) {
10079 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
10080 (xmlAttrPtr) node, destDoc, destParent, options));
10082 xmlNodePtr cur = node;
10085 cur->doc = destDoc;
10087 * Optimize string adoption.
10089 if ((sourceDoc != NULL) &&
10090 (sourceDoc->dict == destDoc->dict))
10092 switch (node->type) {
10093 case XML_TEXT_NODE:
10094 case XML_CDATA_SECTION_NODE:
10095 XML_TREE_ADOPT_STR_2(node->content)
10097 case XML_ENTITY_REF_NODE:
10099 * Remove reference to the entitity-node.
10101 node->content = NULL;
10102 node->children = NULL;
10104 if ((destDoc->intSubset) || (destDoc->extSubset)) {
10107 * Assign new entity-node if available.
10109 ent = xmlGetDocEntity(destDoc, node->name);
10111 node->content = ent->content;
10112 node->children = (xmlNodePtr) ent;
10113 node->last = (xmlNodePtr) ent;
10116 XML_TREE_ADOPT_STR(node->name)
10118 case XML_PI_NODE: {
10119 XML_TREE_ADOPT_STR(node->name)
10120 XML_TREE_ADOPT_STR_2(node->content)
10130 #define bottom_tree
10131 #include "elfgcchack.h"