2 * tree.c : implementation of access function for an XML tree.
5 * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
7 * See Copyright for the status of this software.
16 #include <string.h> /* for memset() only ! */
28 #include <libxml/xmlmemory.h>
29 #include <libxml/tree.h>
30 #include <libxml/parser.h>
31 #include <libxml/uri.h>
32 #include <libxml/entities.h>
33 #include <libxml/valid.h>
34 #include <libxml/xmlerror.h>
35 #include <libxml/parserInternals.h>
36 #include <libxml/globals.h>
37 #ifdef LIBXML_HTML_ENABLED
38 #include <libxml/HTMLtree.h>
40 #ifdef LIBXML_DEBUG_ENABLED
41 #include <libxml/debugXML.h>
44 int __xmlRegisterCallbacks = 0;
46 /************************************************************************
48 * Forward declarations *
50 ************************************************************************/
52 xmlNsPtr xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
54 static xmlChar* xmlGetPropNodeValueInternal(xmlAttrPtr prop);
56 /************************************************************************
58 * Tree memory error handler *
60 ************************************************************************/
63 * @extra: extra informations
65 * Handle an out of memory condition
68 xmlTreeErrMemory(const char *extra)
70 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
75 * @code: the error number
76 * @extra: extra informations
78 * Handle an out of memory condition
81 xmlTreeErr(int code, xmlNodePtr node, const char *extra)
83 const char *msg = NULL;
86 case XML_TREE_INVALID_HEX:
87 msg = "invalid hexadecimal character value\n";
89 case XML_TREE_INVALID_DEC:
90 msg = "invalid decimal character value\n";
92 case XML_TREE_UNTERMINATED_ENTITY:
93 msg = "unterminated entity reference %15s\n";
96 msg = "unexpected error number\n";
98 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
101 /************************************************************************
103 * A few static variables and macros *
105 ************************************************************************/
106 /* #undef xmlStringText */
107 const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
108 /* #undef xmlStringTextNoenc */
109 const xmlChar xmlStringTextNoenc[] =
110 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
111 /* #undef xmlStringComment */
112 const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
114 static int xmlCompressMode = 0;
115 static int xmlCheckDTD = 1;
117 #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
118 xmlNodePtr ulccur = (n)->children; \
119 if (ulccur == NULL) { \
122 while (ulccur->next != NULL) { \
123 ulccur->parent = (n); \
124 ulccur = ulccur->next; \
126 ulccur->parent = (n); \
127 (n)->last = ulccur; \
130 #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
131 (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
133 /* #define DEBUG_BUFFER */
134 /* #define DEBUG_TREE */
136 /************************************************************************
138 * Functions to move to entities.c once the *
139 * API freeze is smoothen and they can be made public. *
141 ************************************************************************/
142 #include <libxml/hash.h>
144 #ifdef LIBXML_TREE_ENABLED
146 * xmlGetEntityFromDtd:
147 * @dtd: A pointer to the DTD to search
148 * @name: The entity name
150 * Do an entity lookup in the DTD entity hash table and
151 * return the corresponding entity, if found.
153 * Returns A pointer to the entity structure or NULL if not found.
156 xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
157 xmlEntitiesTablePtr table;
159 if((dtd != NULL) && (dtd->entities != NULL)) {
160 table = (xmlEntitiesTablePtr) dtd->entities;
161 return((xmlEntityPtr) xmlHashLookup(table, name));
162 /* return(xmlGetEntityFromTable(table, name)); */
167 * xmlGetParameterEntityFromDtd:
168 * @dtd: A pointer to the DTD to search
169 * @name: The entity name
171 * Do an entity lookup in the DTD pararmeter entity hash table and
172 * return the corresponding entity, if found.
174 * Returns A pointer to the entity structure or NULL if not found.
177 xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
178 xmlEntitiesTablePtr table;
180 if ((dtd != NULL) && (dtd->pentities != NULL)) {
181 table = (xmlEntitiesTablePtr) dtd->pentities;
182 return((xmlEntityPtr) xmlHashLookup(table, name));
183 /* return(xmlGetEntityFromTable(table, name)); */
187 #endif /* LIBXML_TREE_ENABLED */
189 /************************************************************************
191 * QName handling helper *
193 ************************************************************************/
198 * @prefix: the prefix
199 * @memory: preallocated memory
200 * @len: preallocated memory length
202 * Builds the QName @prefix:@ncname in @memory if there is enough space
203 * and prefix is not NULL nor empty, otherwise allocate a new string.
204 * If prefix is NULL or empty it returns ncname.
206 * Returns the new string which must be freed by the caller if different from
207 * @memory and @ncname or NULL in case of error
210 xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
211 xmlChar *memory, int len) {
215 if (ncname == NULL) return(NULL);
216 if (prefix == NULL) return((xmlChar *) ncname);
218 lenn = strlen((char *) ncname);
219 lenp = strlen((char *) prefix);
221 if ((memory == NULL) || (len < lenn + lenp + 2)) {
222 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
224 xmlTreeErrMemory("building QName");
230 memcpy(&ret[0], prefix, lenp);
232 memcpy(&ret[lenp + 1], ncname, lenn);
233 ret[lenn + lenp + 1] = 0;
239 * @name: the full QName
240 * @prefix: a xmlChar **
242 * parse an XML qualified name string
244 * [NS 5] QName ::= (Prefix ':')? LocalPart
246 * [NS 6] Prefix ::= NCName
248 * [NS 7] LocalPart ::= NCName
250 * Returns NULL if not a QName, otherwise the local part, and prefix
251 * is updated to get the Prefix if any.
255 xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
259 if (prefix == NULL) return(NULL);
261 if (name == NULL) return(NULL);
263 #ifndef XML_XML_NAMESPACE
264 /* xml: prefix is not really a namespace */
265 if ((name[0] == 'x') && (name[1] == 'm') &&
266 (name[2] == 'l') && (name[3] == ':'))
270 /* nasty but valid */
275 * we are not trying to validate but just to cut, and yes it will
276 * work even if this is as set of UTF-8 encoded chars
278 while ((name[len] != 0) && (name[len] != ':'))
284 *prefix = xmlStrndup(name, len);
285 if (*prefix == NULL) {
286 xmlTreeErrMemory("QName split");
289 ret = xmlStrdup(&name[len + 1]);
291 xmlTreeErrMemory("QName split");
292 if (*prefix != NULL) {
304 * @name: the full QName
307 * parse an XML qualified name string,i
309 * returns NULL if it is not a Qualified Name, otherwise, update len
310 * with the lenght in byte of the prefix and return a pointer
311 * to the start of the name without the prefix
315 xmlSplitQName3(const xmlChar *name, int *len) {
318 if (name == NULL) return(NULL);
319 if (len == NULL) return(NULL);
321 /* nasty but valid */
326 * we are not trying to validate but just to cut, and yes it will
327 * work even if this is as set of UTF-8 encoded chars
329 while ((name[l] != 0) && (name[l] != ':'))
340 /************************************************************************
342 * Check Name, NCName and QName strings *
344 ************************************************************************/
346 #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
348 #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)
351 * @value: the value to check
352 * @space: allow spaces in front and end of the string
354 * Check that a value conforms to the lexical space of NCName
356 * Returns 0 if this validates, a positive error code number otherwise
357 * and -1 in case of internal or API error.
360 xmlValidateNCName(const xmlChar *value, int space) {
361 const xmlChar *cur = value;
368 * First quick algorithm for ASCII range
371 while (IS_BLANK_CH(*cur)) cur++;
372 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
377 while (((*cur >= 'a') && (*cur <= 'z')) ||
378 ((*cur >= 'A') && (*cur <= 'Z')) ||
379 ((*cur >= '0') && (*cur <= '9')) ||
380 (*cur == '_') || (*cur == '-') || (*cur == '.'))
383 while (IS_BLANK_CH(*cur)) cur++;
389 * Second check for chars outside the ASCII range
392 c = CUR_SCHAR(cur, l);
394 while (IS_BLANK(c)) {
396 c = CUR_SCHAR(cur, l);
399 if ((!IS_LETTER(c)) && (c != '_'))
402 c = CUR_SCHAR(cur, l);
403 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
404 (c == '-') || (c == '_') || IS_COMBINING(c) ||
407 c = CUR_SCHAR(cur, l);
410 while (IS_BLANK(c)) {
412 c = CUR_SCHAR(cur, l);
422 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
425 * @value: the value to check
426 * @space: allow spaces in front and end of the string
428 * Check that a value conforms to the lexical space of QName
430 * Returns 0 if this validates, a positive error code number otherwise
431 * and -1 in case of internal or API error.
434 xmlValidateQName(const xmlChar *value, int space) {
435 const xmlChar *cur = value;
441 * First quick algorithm for ASCII range
444 while (IS_BLANK_CH(*cur)) cur++;
445 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
450 while (((*cur >= 'a') && (*cur <= 'z')) ||
451 ((*cur >= 'A') && (*cur <= 'Z')) ||
452 ((*cur >= '0') && (*cur <= '9')) ||
453 (*cur == '_') || (*cur == '-') || (*cur == '.'))
457 if (((*cur >= 'a') && (*cur <= 'z')) ||
458 ((*cur >= 'A') && (*cur <= 'Z')) ||
463 while (((*cur >= 'a') && (*cur <= 'z')) ||
464 ((*cur >= 'A') && (*cur <= 'Z')) ||
465 ((*cur >= '0') && (*cur <= '9')) ||
466 (*cur == '_') || (*cur == '-') || (*cur == '.'))
470 while (IS_BLANK_CH(*cur)) cur++;
476 * Second check for chars outside the ASCII range
479 c = CUR_SCHAR(cur, l);
481 while (IS_BLANK(c)) {
483 c = CUR_SCHAR(cur, l);
486 if ((!IS_LETTER(c)) && (c != '_'))
489 c = CUR_SCHAR(cur, l);
490 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
491 (c == '-') || (c == '_') || IS_COMBINING(c) ||
494 c = CUR_SCHAR(cur, l);
498 c = CUR_SCHAR(cur, l);
499 if ((!IS_LETTER(c)) && (c != '_'))
502 c = CUR_SCHAR(cur, l);
503 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
504 (c == '-') || (c == '_') || IS_COMBINING(c) ||
507 c = CUR_SCHAR(cur, l);
511 while (IS_BLANK(c)) {
513 c = CUR_SCHAR(cur, l);
523 * @value: the value to check
524 * @space: allow spaces in front and end of the string
526 * Check that a value conforms to the lexical space of Name
528 * Returns 0 if this validates, a positive error code number otherwise
529 * and -1 in case of internal or API error.
532 xmlValidateName(const xmlChar *value, int space) {
533 const xmlChar *cur = value;
539 * First quick algorithm for ASCII range
542 while (IS_BLANK_CH(*cur)) cur++;
543 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
544 (*cur == '_') || (*cur == ':'))
548 while (((*cur >= 'a') && (*cur <= 'z')) ||
549 ((*cur >= 'A') && (*cur <= 'Z')) ||
550 ((*cur >= '0') && (*cur <= '9')) ||
551 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
554 while (IS_BLANK_CH(*cur)) cur++;
560 * Second check for chars outside the ASCII range
563 c = CUR_SCHAR(cur, l);
565 while (IS_BLANK(c)) {
567 c = CUR_SCHAR(cur, l);
570 if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
573 c = CUR_SCHAR(cur, l);
574 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
575 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
577 c = CUR_SCHAR(cur, l);
580 while (IS_BLANK(c)) {
582 c = CUR_SCHAR(cur, l);
591 * xmlValidateNMToken:
592 * @value: the value to check
593 * @space: allow spaces in front and end of the string
595 * Check that a value conforms to the lexical space of NMToken
597 * Returns 0 if this validates, a positive error code number otherwise
598 * and -1 in case of internal or API error.
601 xmlValidateNMToken(const xmlChar *value, int space) {
602 const xmlChar *cur = value;
608 * First quick algorithm for ASCII range
611 while (IS_BLANK_CH(*cur)) cur++;
612 if (((*cur >= 'a') && (*cur <= 'z')) ||
613 ((*cur >= 'A') && (*cur <= 'Z')) ||
614 ((*cur >= '0') && (*cur <= '9')) ||
615 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
619 while (((*cur >= 'a') && (*cur <= 'z')) ||
620 ((*cur >= 'A') && (*cur <= 'Z')) ||
621 ((*cur >= '0') && (*cur <= '9')) ||
622 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
625 while (IS_BLANK_CH(*cur)) cur++;
631 * Second check for chars outside the ASCII range
634 c = CUR_SCHAR(cur, l);
636 while (IS_BLANK(c)) {
638 c = CUR_SCHAR(cur, l);
641 if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
642 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
645 c = CUR_SCHAR(cur, l);
646 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
647 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
649 c = CUR_SCHAR(cur, l);
652 while (IS_BLANK(c)) {
654 c = CUR_SCHAR(cur, l);
661 #endif /* LIBXML_TREE_ENABLED */
663 /************************************************************************
665 * Allocation and deallocation of basic structures *
667 ************************************************************************/
670 * xmlSetBufferAllocationScheme:
671 * @scheme: allocation method to use
673 * Set the buffer allocation method. Types are
674 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
675 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
676 * improves performance
679 xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
680 xmlBufferAllocScheme = scheme;
684 * xmlGetBufferAllocationScheme:
687 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
688 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
689 * improves performance
691 * Returns the current allocation scheme
693 xmlBufferAllocationScheme
694 xmlGetBufferAllocationScheme(void) {
695 return(xmlBufferAllocScheme);
700 * @node: the element carrying the namespace
701 * @href: the URI associated
702 * @prefix: the prefix for the namespace
704 * Creation of a new Namespace. This function will refuse to create
705 * a namespace with a similar prefix than an existing one present on this
707 * We use href==NULL in the case of an element creation where the namespace
709 * Returns a new namespace pointer or NULL
712 xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
715 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
718 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml")))
722 * Allocate a new Namespace and fill the fields.
724 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
726 xmlTreeErrMemory("building namespace");
729 memset(cur, 0, sizeof(xmlNs));
730 cur->type = XML_LOCAL_NAMESPACE;
733 cur->href = xmlStrdup(href);
735 cur->prefix = xmlStrdup(prefix);
738 * Add it at the end to preserve parsing order ...
739 * and checks for existing use of the prefix
742 if (node->nsDef == NULL) {
745 xmlNsPtr prev = node->nsDef;
747 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
748 (xmlStrEqual(prev->prefix, cur->prefix))) {
752 while (prev->next != NULL) {
754 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
755 (xmlStrEqual(prev->prefix, cur->prefix))) {
768 * @node: a node in the document
769 * @ns: a namespace pointer
771 * Associate a namespace to a node, a posteriori.
774 xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
777 xmlGenericError(xmlGenericErrorContext,
778 "xmlSetNs: node == NULL\n");
787 * @cur: the namespace pointer
789 * Free up the structures associated to a namespace
792 xmlFreeNs(xmlNsPtr cur) {
795 xmlGenericError(xmlGenericErrorContext,
796 "xmlFreeNs : ns == NULL\n");
800 if (cur->href != NULL) xmlFree((char *) cur->href);
801 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
807 * @cur: the first namespace pointer
809 * Free up all the structures associated to the chained namespaces.
812 xmlFreeNsList(xmlNsPtr cur) {
816 xmlGenericError(xmlGenericErrorContext,
817 "xmlFreeNsList : ns == NULL\n");
821 while (cur != NULL) {
830 * @doc: the document pointer
831 * @name: the DTD name
832 * @ExternalID: the external ID
833 * @SystemID: the system ID
835 * Creation of a new DTD for the external subset. To create an
836 * internal subset, use xmlCreateIntSubset().
838 * Returns a pointer to the new DTD structure
841 xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
842 const xmlChar *ExternalID, const xmlChar *SystemID) {
845 if ((doc != NULL) && (doc->extSubset != NULL)) {
847 xmlGenericError(xmlGenericErrorContext,
848 "xmlNewDtd(%s): document %s already have a DTD %s\n",
849 /* !!! */ (char *) name, doc->name,
850 /* !!! */ (char *)doc->extSubset->name);
856 * Allocate a new DTD and fill the fields.
858 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
860 xmlTreeErrMemory("building DTD");
863 memset(cur, 0 , sizeof(xmlDtd));
864 cur->type = XML_DTD_NODE;
867 cur->name = xmlStrdup(name);
868 if (ExternalID != NULL)
869 cur->ExternalID = xmlStrdup(ExternalID);
870 if (SystemID != NULL)
871 cur->SystemID = xmlStrdup(SystemID);
873 doc->extSubset = cur;
876 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
877 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
883 * @doc: the document pointer
885 * Get the internal subset of a document
886 * Returns a pointer to the DTD structure or NULL if not found
890 xmlGetIntSubset(xmlDocPtr doc) {
896 while (cur != NULL) {
897 if (cur->type == XML_DTD_NODE)
898 return((xmlDtdPtr) cur);
901 return((xmlDtdPtr) doc->intSubset);
905 * xmlCreateIntSubset:
906 * @doc: the document pointer
907 * @name: the DTD name
908 * @ExternalID: the external (PUBLIC) ID
909 * @SystemID: the system ID
911 * Create the internal subset of a document
912 * Returns a pointer to the new DTD structure
915 xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
916 const xmlChar *ExternalID, const xmlChar *SystemID) {
919 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
921 xmlGenericError(xmlGenericErrorContext,
923 "xmlCreateIntSubset(): document %s already have an internal subset\n",
930 * Allocate a new DTD and fill the fields.
932 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
934 xmlTreeErrMemory("building internal subset");
937 memset(cur, 0, sizeof(xmlDtd));
938 cur->type = XML_DTD_NODE;
941 cur->name = xmlStrdup(name);
942 if (cur->name == NULL) {
943 xmlTreeErrMemory("building internal subset");
948 if (ExternalID != NULL) {
949 cur->ExternalID = xmlStrdup(ExternalID);
950 if (cur->ExternalID == NULL) {
951 xmlTreeErrMemory("building internal subset");
952 if (cur->name != NULL)
953 xmlFree((char *)cur->name);
958 if (SystemID != NULL) {
959 cur->SystemID = xmlStrdup(SystemID);
960 if (cur->SystemID == NULL) {
961 xmlTreeErrMemory("building internal subset");
962 if (cur->name != NULL)
963 xmlFree((char *)cur->name);
964 if (cur->ExternalID != NULL)
965 xmlFree((char *)cur->ExternalID);
971 doc->intSubset = cur;
974 if (doc->children == NULL) {
975 doc->children = (xmlNodePtr) cur;
976 doc->last = (xmlNodePtr) cur;
978 if (doc->type == XML_HTML_DOCUMENT_NODE) {
981 prev = doc->children;
982 prev->prev = (xmlNodePtr) cur;
984 doc->children = (xmlNodePtr) cur;
988 next = doc->children;
989 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
992 cur->prev = doc->last;
993 cur->prev->next = (xmlNodePtr) cur;
995 doc->last = (xmlNodePtr) cur;
998 cur->prev = next->prev;
999 if (cur->prev == NULL)
1000 doc->children = (xmlNodePtr) cur;
1002 cur->prev->next = (xmlNodePtr) cur;
1003 next->prev = (xmlNodePtr) cur;
1009 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1010 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1018 * Free a string if it is not owned by the "dict" dictionnary in the
1021 #define DICT_FREE(str) \
1022 if ((str) && ((!dict) || \
1023 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1024 xmlFree((char *)(str));
1031 * Copy a string using a "dict" dictionnary in the current scope,
1034 #define DICT_COPY(str, cpy) \
1037 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1038 cpy = (xmlChar *) (str); \
1040 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1042 cpy = xmlStrdup((const xmlChar *)(str)); }
1048 * Copy a string using a "dict" dictionnary in the current scope,
1051 #define DICT_CONST_COPY(str, cpy) \
1054 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1055 cpy = (const xmlChar *) (str); \
1057 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1059 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1064 * @cur: the DTD structure to free up
1066 * Free a DTD structure.
1069 xmlFreeDtd(xmlDtdPtr cur) {
1070 xmlDictPtr dict = NULL;
1075 if (cur->doc != NULL) dict = cur->doc->dict;
1077 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1078 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1080 if (cur->children != NULL) {
1081 xmlNodePtr next, c = cur->children;
1084 * Cleanup all nodes which are not part of the specific lists
1085 * of notations, elements, attributes and entities.
1089 if ((c->type != XML_NOTATION_NODE) &&
1090 (c->type != XML_ELEMENT_DECL) &&
1091 (c->type != XML_ATTRIBUTE_DECL) &&
1092 (c->type != XML_ENTITY_DECL)) {
1099 DICT_FREE(cur->name)
1100 DICT_FREE(cur->SystemID)
1101 DICT_FREE(cur->ExternalID)
1103 if (cur->notations != NULL)
1104 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
1106 if (cur->elements != NULL)
1107 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1108 if (cur->attributes != NULL)
1109 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1110 if (cur->entities != NULL)
1111 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1112 if (cur->pentities != NULL)
1113 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1120 * @version: xmlChar string giving the version of XML "1.0"
1122 * Creates a new XML document
1124 * Returns a new document
1127 xmlNewDoc(const xmlChar *version) {
1130 if (version == NULL)
1131 version = (const xmlChar *) "1.0";
1134 * Allocate a new document and fill the fields.
1136 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1138 xmlTreeErrMemory("building doc");
1141 memset(cur, 0, sizeof(xmlDoc));
1142 cur->type = XML_DOCUMENT_NODE;
1144 cur->version = xmlStrdup(version);
1145 if (cur->version == NULL) {
1146 xmlTreeErrMemory("building doc");
1150 cur->standalone = -1;
1151 cur->compression = -1; /* not initialized */
1154 * The in memory encoding is always UTF8
1155 * This field will never change and would
1156 * be obsolete if not for binary compatibility.
1158 cur->charset = XML_CHAR_ENCODING_UTF8;
1160 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1161 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1167 * @cur: pointer to the document
1169 * Free up all the structures used by a document, tree included.
1172 xmlFreeDoc(xmlDocPtr cur) {
1173 xmlDtdPtr extSubset, intSubset;
1174 xmlDictPtr dict = NULL;
1178 xmlGenericError(xmlGenericErrorContext,
1179 "xmlFreeDoc : document == NULL\n");
1183 #ifdef LIBXML_DEBUG_RUNTIME
1184 #ifdef LIBXML_DEBUG_ENABLED
1185 xmlDebugCheckDocument(stderr, cur);
1189 if (cur != NULL) dict = cur->dict;
1191 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1192 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1195 * Do this before freeing the children list to avoid ID lookups
1197 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1199 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1201 extSubset = cur->extSubset;
1202 intSubset = cur->intSubset;
1203 if (intSubset == extSubset)
1205 if (extSubset != NULL) {
1206 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
1207 cur->extSubset = NULL;
1208 xmlFreeDtd(extSubset);
1210 if (intSubset != NULL) {
1211 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
1212 cur->intSubset = NULL;
1213 xmlFreeDtd(intSubset);
1216 if (cur->children != NULL) xmlFreeNodeList(cur->children);
1217 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
1219 DICT_FREE(cur->version)
1220 DICT_FREE(cur->name)
1221 DICT_FREE(cur->encoding)
1224 if (dict) xmlDictFree(dict);
1228 * xmlStringLenGetNodeList:
1229 * @doc: the document
1230 * @value: the value of the text
1231 * @len: the length of the string value
1233 * Parse the value string and build the node list associated. Should
1234 * produce a flat tree with only TEXTs and ENTITY_REFs.
1235 * Returns a pointer to the first child
1238 xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
1239 xmlNodePtr ret = NULL, last = NULL;
1242 const xmlChar *cur = value, *end = cur + len;
1246 if (value == NULL) return(NULL);
1249 while ((cur < end) && (*cur != 0)) {
1250 if (cur[0] == '&') {
1255 * Save the current text.
1258 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1259 xmlNodeAddContentLen(last, q, cur - q);
1261 node = xmlNewDocTextLen(doc, q, cur - q);
1262 if (node == NULL) return(ret);
1273 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1279 while (tmp != ';') { /* Non input consuming loop */
1280 if ((tmp >= '0') && (tmp <= '9'))
1281 charval = charval * 16 + (tmp - '0');
1282 else if ((tmp >= 'a') && (tmp <= 'f'))
1283 charval = charval * 16 + (tmp - 'a') + 10;
1284 else if ((tmp >= 'A') && (tmp <= 'F'))
1285 charval = charval * 16 + (tmp - 'A') + 10;
1287 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1301 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1307 while (tmp != ';') { /* Non input consuming loops */
1308 if ((tmp >= '0') && (tmp <= '9'))
1309 charval = charval * 10 + (tmp - '0');
1311 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1327 * Read the entity string
1331 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1332 if ((cur >= end) || (*cur == 0)) {
1333 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1339 * Predefined entities don't generate nodes
1341 val = xmlStrndup(q, cur - q);
1342 ent = xmlGetDocEntity(doc, val);
1343 if ((ent != NULL) &&
1344 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1346 node = xmlNewDocText(doc, ent->content);
1348 } else if (last->type != XML_TEXT_NODE) {
1349 node = xmlNewDocText(doc, ent->content);
1350 last = xmlAddNextSibling(last, node);
1352 xmlNodeAddContent(last, ent->content);
1356 * Create a new REFERENCE_REF node
1358 node = xmlNewReference(doc, val);
1360 if (val != NULL) xmlFree(val);
1363 else if ((ent != NULL) && (ent->children == NULL)) {
1366 ent->children = xmlStringGetNodeList(doc,
1367 (const xmlChar*)node->content);
1369 temp = ent->children;
1371 temp->parent = (xmlNodePtr)ent;
1379 last = xmlAddNextSibling(last, node);
1391 l = xmlCopyCharMultiByte(buf, charval);
1393 node = xmlNewDocText(doc, buf);
1398 last = xmlAddNextSibling(last, node);
1406 if ((cur != q) || (ret == NULL)) {
1408 * Handle the last piece of text.
1410 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1411 xmlNodeAddContentLen(last, q, cur - q);
1413 node = xmlNewDocTextLen(doc, q, cur - q);
1414 if (node == NULL) return(ret);
1418 last = xmlAddNextSibling(last, node);
1426 * xmlStringGetNodeList:
1427 * @doc: the document
1428 * @value: the value of the attribute
1430 * Parse the value string and build the node list associated. Should
1431 * produce a flat tree with only TEXTs and ENTITY_REFs.
1432 * Returns a pointer to the first child
1435 xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
1436 xmlNodePtr ret = NULL, last = NULL;
1439 const xmlChar *cur = value;
1443 if (value == NULL) return(NULL);
1447 if (cur[0] == '&') {
1452 * Save the current text.
1455 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1456 xmlNodeAddContentLen(last, q, cur - q);
1458 node = xmlNewDocTextLen(doc, q, cur - q);
1459 if (node == NULL) return(ret);
1470 if ((cur[1] == '#') && (cur[2] == 'x')) {
1473 while (tmp != ';') { /* Non input consuming loop */
1474 if ((tmp >= '0') && (tmp <= '9'))
1475 charval = charval * 16 + (tmp - '0');
1476 else if ((tmp >= 'a') && (tmp <= 'f'))
1477 charval = charval * 16 + (tmp - 'a') + 10;
1478 else if ((tmp >= 'A') && (tmp <= 'F'))
1479 charval = charval * 16 + (tmp - 'A') + 10;
1481 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1492 } else if (cur[1] == '#') {
1495 while (tmp != ';') { /* Non input consuming loops */
1496 if ((tmp >= '0') && (tmp <= '9'))
1497 charval = charval * 10 + (tmp - '0');
1499 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1512 * Read the entity string
1516 while ((*cur != 0) && (*cur != ';')) cur++;
1518 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1519 (xmlNodePtr) doc, (const char *) q);
1524 * Predefined entities don't generate nodes
1526 val = xmlStrndup(q, cur - q);
1527 ent = xmlGetDocEntity(doc, val);
1528 if ((ent != NULL) &&
1529 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1531 node = xmlNewDocText(doc, ent->content);
1533 } else if (last->type != XML_TEXT_NODE) {
1534 node = xmlNewDocText(doc, ent->content);
1535 last = xmlAddNextSibling(last, node);
1537 xmlNodeAddContent(last, ent->content);
1541 * Create a new REFERENCE_REF node
1543 node = xmlNewReference(doc, val);
1545 if (val != NULL) xmlFree(val);
1548 else if ((ent != NULL) && (ent->children == NULL)) {
1551 ent->children = xmlStringGetNodeList(doc,
1552 (const xmlChar*)node->content);
1554 temp = ent->children;
1556 temp->parent = (xmlNodePtr)ent;
1563 last = xmlAddNextSibling(last, node);
1575 len = xmlCopyCharMultiByte(buf, charval);
1577 node = xmlNewDocText(doc, buf);
1582 last = xmlAddNextSibling(last, node);
1591 if ((cur != q) || (ret == NULL)) {
1593 * Handle the last piece of text.
1595 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1596 xmlNodeAddContentLen(last, q, cur - q);
1598 node = xmlNewDocTextLen(doc, q, cur - q);
1599 if (node == NULL) return(ret);
1603 last = xmlAddNextSibling(last, node);
1611 * xmlNodeListGetString:
1612 * @doc: the document
1613 * @list: a Node list
1614 * @inLine: should we replace entity contents or show their external form
1616 * Build the string equivalent to the text contained in the Node list
1617 * made of TEXTs and ENTITY_REFs
1619 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1622 xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1624 xmlNodePtr node = list;
1625 xmlChar *ret = NULL;
1631 while (node != NULL) {
1632 if ((node->type == XML_TEXT_NODE) ||
1633 (node->type == XML_CDATA_SECTION_NODE)) {
1635 ret = xmlStrcat(ret, node->content);
1639 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1640 if (buffer != NULL) {
1641 ret = xmlStrcat(ret, buffer);
1645 } else if (node->type == XML_ENTITY_REF_NODE) {
1647 ent = xmlGetDocEntity(doc, node->name);
1651 /* an entity content can be any "well balanced chunk",
1652 * i.e. the result of the content [43] production:
1653 * http://www.w3.org/TR/REC-xml#NT-content.
1654 * So it can contain text, CDATA section or nested
1655 * entity reference nodes (among others).
1656 * -> we recursive call xmlNodeListGetString()
1657 * which handles these types */
1658 buffer = xmlNodeListGetString(doc, ent->children, 1);
1659 if (buffer != NULL) {
1660 ret = xmlStrcat(ret, buffer);
1664 ret = xmlStrcat(ret, node->content);
1671 ret = xmlStrncat(ret, buf, 1);
1672 ret = xmlStrcat(ret, node->name);
1675 ret = xmlStrncat(ret, buf, 1);
1680 xmlGenericError(xmlGenericErrorContext,
1681 "xmlGetNodeListString : invalid node type %d\n",
1690 #ifdef LIBXML_TREE_ENABLED
1692 * xmlNodeListGetRawString:
1693 * @doc: the document
1694 * @list: a Node list
1695 * @inLine: should we replace entity contents or show their external form
1697 * Builds the string equivalent to the text contained in the Node list
1698 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1699 * this function doesn't do any character encoding handling.
1701 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1704 xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1706 xmlNodePtr node = list;
1707 xmlChar *ret = NULL;
1713 while (node != NULL) {
1714 if ((node->type == XML_TEXT_NODE) ||
1715 (node->type == XML_CDATA_SECTION_NODE)) {
1717 ret = xmlStrcat(ret, node->content);
1721 buffer = xmlEncodeSpecialChars(doc, node->content);
1722 if (buffer != NULL) {
1723 ret = xmlStrcat(ret, buffer);
1727 } else if (node->type == XML_ENTITY_REF_NODE) {
1729 ent = xmlGetDocEntity(doc, node->name);
1733 /* an entity content can be any "well balanced chunk",
1734 * i.e. the result of the content [43] production:
1735 * http://www.w3.org/TR/REC-xml#NT-content.
1736 * So it can contain text, CDATA section or nested
1737 * entity reference nodes (among others).
1738 * -> we recursive call xmlNodeListGetRawString()
1739 * which handles these types */
1741 xmlNodeListGetRawString(doc, ent->children, 1);
1742 if (buffer != NULL) {
1743 ret = xmlStrcat(ret, buffer);
1747 ret = xmlStrcat(ret, node->content);
1754 ret = xmlStrncat(ret, buf, 1);
1755 ret = xmlStrcat(ret, node->name);
1758 ret = xmlStrncat(ret, buf, 1);
1763 xmlGenericError(xmlGenericErrorContext,
1764 "xmlGetNodeListString : invalid node type %d\n",
1772 #endif /* LIBXML_TREE_ENABLED */
1775 xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1776 const xmlChar * name, const xmlChar * value,
1780 xmlDocPtr doc = NULL;
1782 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
1784 xmlFree((xmlChar *) name);
1789 * Allocate a new property and fill the fields.
1791 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1794 xmlFree((xmlChar *) name);
1795 xmlTreeErrMemory("building attribute");
1798 memset(cur, 0, sizeof(xmlAttr));
1799 cur->type = XML_ATTRIBUTE_NODE;
1809 if ((doc != NULL) && (doc->dict != NULL))
1810 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1812 cur->name = xmlStrdup(name);
1816 if (value != NULL) {
1820 buffer = xmlEncodeEntitiesReentrant(doc, value);
1821 cur->children = xmlStringGetNodeList(doc, buffer);
1823 tmp = cur->children;
1824 while (tmp != NULL) {
1825 tmp->parent = (xmlNodePtr) cur;
1826 if (tmp->next == NULL)
1834 * Add it at the end to preserve parsing order ...
1837 if (node->properties == NULL) {
1838 node->properties = cur;
1840 xmlAttrPtr prev = node->properties;
1842 while (prev->next != NULL)
1849 if (xmlIsID((node == NULL) ? NULL : node->doc, node, cur) == 1)
1850 xmlAddID(NULL, node->doc, value, cur);
1852 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1853 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1857 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1858 defined(LIBXML_SCHEMAS_ENABLED)
1861 * @node: the holding node
1862 * @name: the name of the attribute
1863 * @value: the value of the attribute
1865 * Create a new property carried by a node.
1866 * Returns a pointer to the attribute
1869 xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1873 xmlGenericError(xmlGenericErrorContext,
1874 "xmlNewProp : name == NULL\n");
1879 return xmlNewPropInternal(node, NULL, name, value, 0);
1881 #endif /* LIBXML_TREE_ENABLED */
1885 * @node: the holding node
1886 * @ns: the namespace
1887 * @name: the name of the attribute
1888 * @value: the value of the attribute
1890 * Create a new property tagged with a namespace and carried by a node.
1891 * Returns a pointer to the attribute
1894 xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1895 const xmlChar *value) {
1899 xmlGenericError(xmlGenericErrorContext,
1900 "xmlNewNsProp : name == NULL\n");
1905 return xmlNewPropInternal(node, ns, name, value, 0);
1909 * xmlNewNsPropEatName:
1910 * @node: the holding node
1911 * @ns: the namespace
1912 * @name: the name of the attribute
1913 * @value: the value of the attribute
1915 * Create a new property tagged with a namespace and carried by a node.
1916 * Returns a pointer to the attribute
1919 xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1920 const xmlChar *value) {
1924 xmlGenericError(xmlGenericErrorContext,
1925 "xmlNewNsPropEatName : name == NULL\n");
1930 return xmlNewPropInternal(node, ns, name, value, 1);
1935 * @doc: the document
1936 * @name: the name of the attribute
1937 * @value: the value of the attribute
1939 * Create a new property carried by a document.
1940 * Returns a pointer to the attribute
1943 xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1948 xmlGenericError(xmlGenericErrorContext,
1949 "xmlNewDocProp : name == NULL\n");
1955 * Allocate a new property and fill the fields.
1957 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1959 xmlTreeErrMemory("building attribute");
1962 memset(cur, 0, sizeof(xmlAttr));
1963 cur->type = XML_ATTRIBUTE_NODE;
1965 if ((doc != NULL) && (doc->dict != NULL))
1966 cur->name = xmlDictLookup(doc->dict, name, -1);
1968 cur->name = xmlStrdup(name);
1970 if (value != NULL) {
1973 cur->children = xmlStringGetNodeList(doc, value);
1976 tmp = cur->children;
1977 while (tmp != NULL) {
1978 tmp->parent = (xmlNodePtr) cur;
1979 if (tmp->next == NULL)
1985 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1986 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1992 * @cur: the first property in the list
1994 * Free a property and all its siblings, all the children are freed too.
1997 xmlFreePropList(xmlAttrPtr cur) {
1999 if (cur == NULL) return;
2000 while (cur != NULL) {
2009 * @cur: an attribute
2011 * Free one attribute, all the content is freed too
2014 xmlFreeProp(xmlAttrPtr cur) {
2015 xmlDictPtr dict = NULL;
2016 if (cur == NULL) return;
2018 if (cur->doc != NULL) dict = cur->doc->dict;
2020 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
2021 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2023 /* Check for ID removal -> leading to invalid references ! */
2024 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2025 xmlRemoveID(cur->doc, cur);
2027 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2028 DICT_FREE(cur->name)
2034 * @cur: an attribute
2036 * Unlink and free one attribute, all the content is freed too
2037 * Note this doesn't work for namespace definition attributes
2039 * Returns 0 if success and -1 in case of error.
2042 xmlRemoveProp(xmlAttrPtr cur) {
2046 xmlGenericError(xmlGenericErrorContext,
2047 "xmlRemoveProp : cur == NULL\n");
2051 if (cur->parent == NULL) {
2053 xmlGenericError(xmlGenericErrorContext,
2054 "xmlRemoveProp : cur->parent == NULL\n");
2058 tmp = cur->parent->properties;
2060 cur->parent->properties = cur->next;
2061 if (cur->next != NULL)
2062 cur->next->prev = NULL;
2066 while (tmp != NULL) {
2067 if (tmp->next == cur) {
2068 tmp->next = cur->next;
2069 if (tmp->next != NULL)
2070 tmp->next->prev = tmp;
2077 xmlGenericError(xmlGenericErrorContext,
2078 "xmlRemoveProp : attribute not owned by its node\n");
2085 * @doc: the target document
2086 * @name: the processing instruction name
2087 * @content: the PI content
2089 * Creation of a processing instruction element.
2090 * Returns a pointer to the new node object.
2093 xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
2098 xmlGenericError(xmlGenericErrorContext,
2099 "xmlNewPI : name == NULL\n");
2105 * Allocate a new node and fill the fields.
2107 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2109 xmlTreeErrMemory("building PI");
2112 memset(cur, 0, sizeof(xmlNode));
2113 cur->type = XML_PI_NODE;
2115 if ((doc != NULL) && (doc->dict != NULL))
2116 cur->name = xmlDictLookup(doc->dict, name, -1);
2118 cur->name = xmlStrdup(name);
2119 if (content != NULL) {
2120 cur->content = xmlStrdup(content);
2124 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2125 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2131 * @name: the processing instruction name
2132 * @content: the PI content
2134 * Creation of a processing instruction element.
2135 * Use xmlDocNewPI preferably to get string interning
2137 * Returns a pointer to the new node object.
2140 xmlNewPI(const xmlChar *name, const xmlChar *content) {
2141 return(xmlNewDocPI(NULL, name, content));
2146 * @ns: namespace if any
2147 * @name: the node name
2149 * Creation of a new node element. @ns is optional (NULL).
2151 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2155 xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2160 xmlGenericError(xmlGenericErrorContext,
2161 "xmlNewNode : name == NULL\n");
2167 * Allocate a new node and fill the fields.
2169 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2171 xmlTreeErrMemory("building node");
2174 memset(cur, 0, sizeof(xmlNode));
2175 cur->type = XML_ELEMENT_NODE;
2177 cur->name = xmlStrdup(name);
2180 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2181 xmlRegisterNodeDefaultValue(cur);
2186 * xmlNewNodeEatName:
2187 * @ns: namespace if any
2188 * @name: the node name
2190 * Creation of a new node element. @ns is optional (NULL).
2192 * Returns a pointer to the new node object, with pointer @name as
2193 * new node's name. Use xmlNewNode() if a copy of @name string is
2194 * is needed as new node's name.
2197 xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2202 xmlGenericError(xmlGenericErrorContext,
2203 "xmlNewNode : name == NULL\n");
2209 * Allocate a new node and fill the fields.
2211 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2214 xmlTreeErrMemory("building node");
2217 memset(cur, 0, sizeof(xmlNode));
2218 cur->type = XML_ELEMENT_NODE;
2223 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2224 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2230 * @doc: the document
2231 * @ns: namespace if any
2232 * @name: the node name
2233 * @content: the XML text content if any
2235 * Creation of a new node element within a document. @ns and @content
2236 * are optional (NULL).
2237 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2238 * references, but XML special chars need to be escaped first by using
2239 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2240 * need entities support.
2242 * Returns a pointer to the new node object.
2245 xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2246 const xmlChar *name, const xmlChar *content) {
2249 if ((doc != NULL) && (doc->dict != NULL))
2250 cur = xmlNewNodeEatName(ns, (xmlChar *)
2251 xmlDictLookup(doc->dict, name, -1));
2253 cur = xmlNewNode(ns, name);
2256 if (content != NULL) {
2257 cur->children = xmlStringGetNodeList(doc, content);
2258 UPDATE_LAST_CHILD_AND_PARENT(cur)
2266 * xmlNewDocNodeEatName:
2267 * @doc: the document
2268 * @ns: namespace if any
2269 * @name: the node name
2270 * @content: the XML text content if any
2272 * Creation of a new node element within a document. @ns and @content
2273 * are optional (NULL).
2274 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2275 * references, but XML special chars need to be escaped first by using
2276 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2277 * need entities support.
2279 * Returns a pointer to the new node object.
2282 xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2283 xmlChar *name, const xmlChar *content) {
2286 cur = xmlNewNodeEatName(ns, name);
2289 if (content != NULL) {
2290 cur->children = xmlStringGetNodeList(doc, content);
2291 UPDATE_LAST_CHILD_AND_PARENT(cur)
2297 #ifdef LIBXML_TREE_ENABLED
2300 * @doc: the document
2301 * @ns: namespace if any
2302 * @name: the node name
2303 * @content: the text content if any
2305 * Creation of a new node element within a document. @ns and @content
2306 * are optional (NULL).
2308 * Returns a pointer to the new node object.
2311 xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2312 const xmlChar *name, const xmlChar *content) {
2315 cur = xmlNewDocNode(doc, ns, name, NULL);
2318 if (content != NULL) {
2319 cur->children = xmlNewDocText(doc, content);
2320 UPDATE_LAST_CHILD_AND_PARENT(cur)
2327 * xmlNewDocFragment:
2328 * @doc: the document owning the fragment
2330 * Creation of a new Fragment node.
2331 * Returns a pointer to the new node object.
2334 xmlNewDocFragment(xmlDocPtr doc) {
2338 * Allocate a new DocumentFragment node and fill the fields.
2340 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2342 xmlTreeErrMemory("building fragment");
2345 memset(cur, 0, sizeof(xmlNode));
2346 cur->type = XML_DOCUMENT_FRAG_NODE;
2350 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2351 xmlRegisterNodeDefaultValue(cur);
2354 #endif /* LIBXML_TREE_ENABLED */
2358 * @content: the text content
2360 * Creation of a new text node.
2361 * Returns a pointer to the new node object.
2364 xmlNewText(const xmlChar *content) {
2368 * Allocate a new node and fill the fields.
2370 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2372 xmlTreeErrMemory("building text");
2375 memset(cur, 0, sizeof(xmlNode));
2376 cur->type = XML_TEXT_NODE;
2378 cur->name = xmlStringText;
2379 if (content != NULL) {
2380 cur->content = xmlStrdup(content);
2383 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2384 xmlRegisterNodeDefaultValue(cur);
2388 #ifdef LIBXML_TREE_ENABLED
2391 * @parent: the parent node
2392 * @ns: a namespace if any
2393 * @name: the name of the child
2394 * @content: the text content of the child if any.
2396 * Creation of a new child element, added at the end of @parent children list.
2397 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2398 * created element inherits the namespace of @parent. If @content is non NULL,
2399 * a child TEXT node will be created containing the string @content.
2400 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2401 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
2402 * reserved XML chars that might appear in @content, such as the ampersand,
2403 * greater-than or less-than signs, are automatically replaced by their XML
2404 * escaped entity representations.
2406 * Returns a pointer to the new node object.
2409 xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2410 const xmlChar *name, const xmlChar *content) {
2411 xmlNodePtr cur, prev;
2413 if (parent == NULL) {
2415 xmlGenericError(xmlGenericErrorContext,
2416 "xmlNewTextChild : parent == NULL\n");
2423 xmlGenericError(xmlGenericErrorContext,
2424 "xmlNewTextChild : name == NULL\n");
2430 * Allocate a new node
2432 if (parent->type == XML_ELEMENT_NODE) {
2434 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2436 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2437 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2438 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2440 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2442 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2443 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2444 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2448 if (cur == NULL) return(NULL);
2451 * add the new element at the end of the children list.
2453 cur->type = XML_ELEMENT_NODE;
2454 cur->parent = parent;
2455 cur->doc = parent->doc;
2456 if (parent->children == NULL) {
2457 parent->children = cur;
2460 prev = parent->last;
2468 #endif /* LIBXML_TREE_ENABLED */
2472 * @doc: the document
2473 * @name: the char ref string, starting with # or "&# ... ;"
2475 * Creation of a new character reference node.
2476 * Returns a pointer to the new node object.
2479 xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2486 * Allocate a new node and fill the fields.
2488 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2490 xmlTreeErrMemory("building character reference");
2493 memset(cur, 0, sizeof(xmlNode));
2494 cur->type = XML_ENTITY_REF_NODE;
2497 if (name[0] == '&') {
2500 len = xmlStrlen(name);
2501 if (name[len - 1] == ';')
2502 cur->name = xmlStrndup(name, len - 1);
2504 cur->name = xmlStrndup(name, len);
2506 cur->name = xmlStrdup(name);
2508 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2509 xmlRegisterNodeDefaultValue(cur);
2515 * @doc: the document
2516 * @name: the reference name, or the reference string with & and ;
2518 * Creation of a new reference node.
2519 * Returns a pointer to the new node object.
2522 xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
2530 * Allocate a new node and fill the fields.
2532 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2534 xmlTreeErrMemory("building reference");
2537 memset(cur, 0, sizeof(xmlNode));
2538 cur->type = XML_ENTITY_REF_NODE;
2541 if (name[0] == '&') {
2544 len = xmlStrlen(name);
2545 if (name[len - 1] == ';')
2546 cur->name = xmlStrndup(name, len - 1);
2548 cur->name = xmlStrndup(name, len);
2550 cur->name = xmlStrdup(name);
2552 ent = xmlGetDocEntity(doc, cur->name);
2554 cur->content = ent->content;
2556 * The parent pointer in entity is a DTD pointer and thus is NOT
2557 * updated. Not sure if this is 100% correct.
2560 cur->children = (xmlNodePtr) ent;
2561 cur->last = (xmlNodePtr) ent;
2564 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2565 xmlRegisterNodeDefaultValue(cur);
2571 * @doc: the document
2572 * @content: the text content
2574 * Creation of a new text node within a document.
2575 * Returns a pointer to the new node object.
2578 xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
2581 cur = xmlNewText(content);
2582 if (cur != NULL) cur->doc = doc;
2588 * @content: the text content
2589 * @len: the text len.
2591 * Creation of a new text node with an extra parameter for the content's length
2592 * Returns a pointer to the new node object.
2595 xmlNewTextLen(const xmlChar *content, int len) {
2599 * Allocate a new node and fill the fields.
2601 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2603 xmlTreeErrMemory("building text");
2606 memset(cur, 0, sizeof(xmlNode));
2607 cur->type = XML_TEXT_NODE;
2609 cur->name = xmlStringText;
2610 if (content != NULL) {
2611 cur->content = xmlStrndup(content, len);
2614 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2615 xmlRegisterNodeDefaultValue(cur);
2621 * @doc: the document
2622 * @content: the text content
2623 * @len: the text len.
2625 * Creation of a new text node with an extra content length parameter. The
2626 * text node pertain to a given document.
2627 * Returns a pointer to the new node object.
2630 xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2633 cur = xmlNewTextLen(content, len);
2634 if (cur != NULL) cur->doc = doc;
2640 * @content: the comment content
2642 * Creation of a new node containing a comment.
2643 * Returns a pointer to the new node object.
2646 xmlNewComment(const xmlChar *content) {
2650 * Allocate a new node and fill the fields.
2652 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2654 xmlTreeErrMemory("building comment");
2657 memset(cur, 0, sizeof(xmlNode));
2658 cur->type = XML_COMMENT_NODE;
2660 cur->name = xmlStringComment;
2661 if (content != NULL) {
2662 cur->content = xmlStrdup(content);
2665 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2666 xmlRegisterNodeDefaultValue(cur);
2672 * @doc: the document
2673 * @content: the CDATA block content content
2674 * @len: the length of the block
2676 * Creation of a new node containing a CDATA block.
2677 * Returns a pointer to the new node object.
2680 xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2684 * Allocate a new node and fill the fields.
2686 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2688 xmlTreeErrMemory("building CDATA");
2691 memset(cur, 0, sizeof(xmlNode));
2692 cur->type = XML_CDATA_SECTION_NODE;
2695 if (content != NULL) {
2696 cur->content = xmlStrndup(content, len);
2699 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2700 xmlRegisterNodeDefaultValue(cur);
2706 * @doc: the document
2707 * @content: the comment content
2709 * Creation of a new node containing a comment within a document.
2710 * Returns a pointer to the new node object.
2713 xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2716 cur = xmlNewComment(content);
2717 if (cur != NULL) cur->doc = doc;
2723 * @tree: the top element
2724 * @doc: the document
2726 * update all nodes under the tree to point to the right document
2729 xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
2734 if (tree->doc != doc) {
2735 if(tree->type == XML_ELEMENT_NODE) {
2736 prop = tree->properties;
2737 while (prop != NULL) {
2739 xmlSetListDoc(prop->children, doc);
2743 if (tree->children != NULL)
2744 xmlSetListDoc(tree->children, doc);
2751 * @list: the first element
2752 * @doc: the document
2754 * update all nodes in the list to point to the right document
2757 xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2763 while (cur != NULL) {
2764 if (cur->doc != doc)
2765 xmlSetTreeDoc(cur, doc);
2770 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
2773 * @parent: the parent node
2774 * @ns: a namespace if any
2775 * @name: the name of the child
2776 * @content: the XML content of the child if any.
2778 * Creation of a new child element, added at the end of @parent children list.
2779 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2780 * created element inherits the namespace of @parent. If @content is non NULL,
2781 * a child list containing the TEXTs and ENTITY_REFs node will be created.
2782 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2783 * references. XML special chars must be escaped first by using
2784 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
2786 * Returns a pointer to the new node object.
2789 xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2790 const xmlChar *name, const xmlChar *content) {
2791 xmlNodePtr cur, prev;
2793 if (parent == NULL) {
2795 xmlGenericError(xmlGenericErrorContext,
2796 "xmlNewChild : parent == NULL\n");
2803 xmlGenericError(xmlGenericErrorContext,
2804 "xmlNewChild : name == NULL\n");
2810 * Allocate a new node
2812 if (parent->type == XML_ELEMENT_NODE) {
2814 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2816 cur = xmlNewDocNode(parent->doc, ns, name, content);
2817 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2818 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2820 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2822 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
2823 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2824 cur = xmlNewDocNode( parent->doc, ns, name, content);
2828 if (cur == NULL) return(NULL);
2831 * add the new element at the end of the children list.
2833 cur->type = XML_ELEMENT_NODE;
2834 cur->parent = parent;
2835 cur->doc = parent->doc;
2836 if (parent->children == NULL) {
2837 parent->children = cur;
2840 prev = parent->last;
2848 #endif /* LIBXML_TREE_ENABLED */
2851 * xmlAddPropSibling:
2852 * @prev: the attribute to which @prop is added after
2853 * @cur: the base attribute passed to calling function
2854 * @prop: the new attribute
2856 * Add a new attribute after @prev using @cur as base attribute.
2857 * When inserting before @cur, @prev is passed as @cur->prev.
2858 * When inserting after @cur, @prev is passed as @cur.
2859 * If an existing attribute is found it is detroyed prior to adding @prop.
2861 * Returns the attribute being inserted or NULL in case of error.
2864 xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2867 if (cur->type != XML_ATTRIBUTE_NODE)
2870 /* check if an attribute with the same name exists */
2871 if (prop->ns == NULL)
2872 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2874 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2876 if (prop->doc != cur->doc) {
2877 xmlSetTreeDoc(prop, cur->doc);
2879 prop->parent = cur->parent;
2882 prop->next = prev->next;
2885 prop->next->prev = prop;
2890 if (prop->prev == NULL && prop->parent != NULL)
2891 prop->parent->properties = (xmlAttrPtr) prop;
2892 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2893 /* different instance, destroy it (attributes must be unique) */
2894 xmlRemoveProp((xmlAttrPtr) attr);
2900 * xmlAddNextSibling:
2901 * @cur: the child node
2902 * @elem: the new node
2904 * Add a new node @elem as the next sibling of @cur
2905 * If the new node was already inserted in a document it is
2906 * first unlinked from its existing context.
2907 * As a result of text merging @elem may be freed.
2908 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2909 * If there is an attribute with equal name, it is first destroyed.
2911 * Returns the new node or NULL in case of error.
2914 xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2917 xmlGenericError(xmlGenericErrorContext,
2918 "xmlAddNextSibling : cur == NULL\n");
2924 xmlGenericError(xmlGenericErrorContext,
2925 "xmlAddNextSibling : elem == NULL\n");
2932 xmlGenericError(xmlGenericErrorContext,
2933 "xmlAddNextSibling : cur == elem\n");
2938 xmlUnlinkNode(elem);
2940 if (elem->type == XML_TEXT_NODE) {
2941 if (cur->type == XML_TEXT_NODE) {
2942 xmlNodeAddContent(cur, elem->content);
2946 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
2947 (cur->name == cur->next->name)) {
2950 tmp = xmlStrdup(elem->content);
2951 tmp = xmlStrcat(tmp, cur->next->content);
2952 xmlNodeSetContent(cur->next, tmp);
2957 } else if (elem->type == XML_ATTRIBUTE_NODE) {
2958 return xmlAddPropSibling(cur, cur, elem);
2961 if (elem->doc != cur->doc) {
2962 xmlSetTreeDoc(elem, cur->doc);
2964 elem->parent = cur->parent;
2966 elem->next = cur->next;
2968 if (elem->next != NULL)
2969 elem->next->prev = elem;
2970 if ((elem->parent != NULL) && (elem->parent->last == cur))
2971 elem->parent->last = elem;
2975 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
2976 defined(LIBXML_SCHEMAS_ENABLED)
2978 * xmlAddPrevSibling:
2979 * @cur: the child node
2980 * @elem: the new node
2982 * Add a new node @elem as the previous sibling of @cur
2983 * merging adjacent TEXT nodes (@elem may be freed)
2984 * If the new node was already inserted in a document it is
2985 * first unlinked from its existing context.
2986 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2987 * If there is an attribute with equal name, it is first destroyed.
2989 * Returns the new node or NULL in case of error.
2992 xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
2995 xmlGenericError(xmlGenericErrorContext,
2996 "xmlAddPrevSibling : cur == NULL\n");
3002 xmlGenericError(xmlGenericErrorContext,
3003 "xmlAddPrevSibling : elem == NULL\n");
3010 xmlGenericError(xmlGenericErrorContext,
3011 "xmlAddPrevSibling : cur == elem\n");
3016 xmlUnlinkNode(elem);
3018 if (elem->type == XML_TEXT_NODE) {
3019 if (cur->type == XML_TEXT_NODE) {
3022 tmp = xmlStrdup(elem->content);
3023 tmp = xmlStrcat(tmp, cur->content);
3024 xmlNodeSetContent(cur, tmp);
3029 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3030 (cur->name == cur->prev->name)) {
3031 xmlNodeAddContent(cur->prev, elem->content);
3035 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3036 return xmlAddPropSibling(cur->prev, cur, elem);
3039 if (elem->doc != cur->doc) {
3040 xmlSetTreeDoc(elem, cur->doc);
3042 elem->parent = cur->parent;
3044 elem->prev = cur->prev;
3046 if (elem->prev != NULL)
3047 elem->prev->next = elem;
3048 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
3049 elem->parent->children = elem;
3053 #endif /* LIBXML_TREE_ENABLED */
3057 * @cur: the child node
3058 * @elem: the new node
3060 * Add a new element @elem to the list of siblings of @cur
3061 * merging adjacent TEXT nodes (@elem may be freed)
3062 * If the new element was already inserted in a document it is
3063 * first unlinked from its existing context.
3065 * Returns the new element or NULL in case of error.
3068 xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3073 xmlGenericError(xmlGenericErrorContext,
3074 "xmlAddSibling : cur == NULL\n");
3081 xmlGenericError(xmlGenericErrorContext,
3082 "xmlAddSibling : elem == NULL\n");
3088 * Constant time is we can rely on the ->parent->last to find
3091 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
3092 (cur->parent->children != NULL) &&
3093 (cur->parent->last != NULL) &&
3094 (cur->parent->last->next == NULL)) {
3095 cur = cur->parent->last;
3097 while (cur->next != NULL) cur = cur->next;
3100 xmlUnlinkNode(elem);
3102 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3103 (cur->name == elem->name)) {
3104 xmlNodeAddContent(cur, elem->content);
3107 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3108 return xmlAddPropSibling(cur, cur, elem);
3111 if (elem->doc != cur->doc) {
3112 xmlSetTreeDoc(elem, cur->doc);
3114 parent = cur->parent;
3117 elem->parent = parent;
3120 parent->last = elem;
3127 * @parent: the parent node
3128 * @cur: the first node in the list
3130 * Add a list of node at the end of the child list of the parent
3131 * merging adjacent TEXT nodes (@cur may be freed)
3133 * Returns the last child or NULL in case of error.
3136 xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3139 if (parent == NULL) {
3141 xmlGenericError(xmlGenericErrorContext,
3142 "xmlAddChildList : parent == NULL\n");
3149 xmlGenericError(xmlGenericErrorContext,
3150 "xmlAddChildList : child == NULL\n");
3155 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3156 (cur->doc != parent->doc)) {
3158 xmlGenericError(xmlGenericErrorContext,
3159 "Elements moved to a different document\n");
3164 * add the first element at the end of the children list.
3167 if (parent->children == NULL) {
3168 parent->children = cur;
3171 * If cur and parent->last both are TEXT nodes, then merge them.
3173 if ((cur->type == XML_TEXT_NODE) &&
3174 (parent->last->type == XML_TEXT_NODE) &&
3175 (cur->name == parent->last->name)) {
3176 xmlNodeAddContent(parent->last, cur->content);
3178 * if it's the only child, nothing more to be done.
3180 if (cur->next == NULL) {
3182 return(parent->last);
3188 prev = parent->last;
3192 while (cur->next != NULL) {
3193 cur->parent = parent;
3194 if (cur->doc != parent->doc) {
3195 xmlSetTreeDoc(cur, parent->doc);
3199 cur->parent = parent;
3200 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
3208 * @parent: the parent node
3209 * @cur: the child node
3211 * Add a new node to @parent, at the end of the child (or property) list
3212 * merging adjacent TEXT nodes (in which case @cur is freed)
3213 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3214 * If there is an attribute with equal name, it is first destroyed.
3216 * Returns the child or NULL in case of error.
3219 xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3222 if (parent == NULL) {
3224 xmlGenericError(xmlGenericErrorContext,
3225 "xmlAddChild : parent == NULL\n");
3232 xmlGenericError(xmlGenericErrorContext,
3233 "xmlAddChild : child == NULL\n");
3238 if (parent == cur) {
3240 xmlGenericError(xmlGenericErrorContext,
3241 "xmlAddChild : parent == cur\n");
3246 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
3247 * cur is then freed.
3249 if (cur->type == XML_TEXT_NODE) {
3250 if ((parent->type == XML_TEXT_NODE) &&
3251 (parent->content != NULL) &&
3252 (parent->name == cur->name)) {
3253 xmlNodeAddContent(parent, cur->content);
3257 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
3258 (parent->last->name == cur->name) &&
3259 (parent->last != cur)) {
3260 xmlNodeAddContent(parent->last, cur->content);
3262 return(parent->last);
3267 * add the new element at the end of the children list.
3270 cur->parent = parent;
3271 if (cur->doc != parent->doc) {
3272 xmlSetTreeDoc(cur, parent->doc);
3274 /* this check prevents a loop on tree-traversions if a developer
3275 * tries to add a node to its parent multiple times
3283 if ((parent->type == XML_TEXT_NODE) &&
3284 (parent->content != NULL) &&
3286 xmlNodeAddContent(parent, cur->content);
3290 if (cur->type == XML_ATTRIBUTE_NODE) {
3291 if (parent->type != XML_ELEMENT_NODE)
3293 if (parent->properties == NULL) {
3294 parent->properties = (xmlAttrPtr) cur;
3296 /* check if an attribute with the same name exists */
3297 xmlAttrPtr lastattr;
3299 if (cur->ns == NULL)
3300 lastattr = xmlHasNsProp(parent, cur->name, NULL);
3302 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
3303 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
3304 /* different instance, destroy it (attributes must be unique) */
3305 xmlUnlinkNode((xmlNodePtr) lastattr);
3306 xmlFreeProp(lastattr);
3308 if (lastattr == (xmlAttrPtr) cur)
3311 lastattr = parent->properties;
3312 while (lastattr->next != NULL) {
3313 lastattr = lastattr->next;
3315 lastattr->next = (xmlAttrPtr) cur;
3316 ((xmlAttrPtr) cur)->prev = lastattr;
3319 if (parent->children == NULL) {
3320 parent->children = cur;
3323 prev = parent->last;
3334 * @parent: the parent node
3336 * Search the last child of a node.
3337 * Returns the last child or NULL if none.
3340 xmlGetLastChild(xmlNodePtr parent) {
3341 if (parent == NULL) {
3343 xmlGenericError(xmlGenericErrorContext,
3344 "xmlGetLastChild : parent == NULL\n");
3348 return(parent->last);
3353 * @cur: the first node in the list
3355 * Free a node and all its siblings, this is a recursive behaviour, all
3356 * the children are freed too.
3359 xmlFreeNodeList(xmlNodePtr cur) {
3361 xmlDictPtr dict = NULL;
3363 if (cur == NULL) return;
3364 if (cur->type == XML_NAMESPACE_DECL) {
3365 xmlFreeNsList((xmlNsPtr) cur);
3368 if ((cur->type == XML_DOCUMENT_NODE) ||
3369 #ifdef LIBXML_DOCB_ENABLED
3370 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
3372 (cur->type == XML_HTML_DOCUMENT_NODE)) {
3373 xmlFreeDoc((xmlDocPtr) cur);
3376 if (cur->doc != NULL) dict = cur->doc->dict;
3377 while (cur != NULL) {
3379 if (cur->type != XML_DTD_NODE) {
3381 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3382 xmlDeregisterNodeDefaultValue(cur);
3384 if ((cur->children != NULL) &&
3385 (cur->type != XML_ENTITY_REF_NODE))
3386 xmlFreeNodeList(cur->children);
3387 if (((cur->type == XML_ELEMENT_NODE) ||
3388 (cur->type == XML_XINCLUDE_START) ||
3389 (cur->type == XML_XINCLUDE_END)) &&
3390 (cur->properties != NULL))
3391 xmlFreePropList(cur->properties);
3392 if ((cur->type != XML_ELEMENT_NODE) &&
3393 (cur->type != XML_XINCLUDE_START) &&
3394 (cur->type != XML_XINCLUDE_END) &&
3395 (cur->type != XML_ENTITY_REF_NODE) &&
3396 (cur->content != (xmlChar *) &(cur->properties))) {
3397 DICT_FREE(cur->content)
3399 if (((cur->type == XML_ELEMENT_NODE) ||
3400 (cur->type == XML_XINCLUDE_START) ||
3401 (cur->type == XML_XINCLUDE_END)) &&
3402 (cur->nsDef != NULL))
3403 xmlFreeNsList(cur->nsDef);
3406 * When a node is a text node or a comment, it uses a global static
3407 * variable for the name of the node.
3408 * Otherwise the node name might come from the document's
3411 if ((cur->name != NULL) &&
3412 (cur->type != XML_TEXT_NODE) &&
3413 (cur->type != XML_COMMENT_NODE))
3414 DICT_FREE(cur->name)
3425 * Free a node, this is a recursive behaviour, all the children are freed too.
3426 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3429 xmlFreeNode(xmlNodePtr cur) {
3430 xmlDictPtr dict = NULL;
3432 if (cur == NULL) return;
3434 /* use xmlFreeDtd for DTD nodes */
3435 if (cur->type == XML_DTD_NODE) {
3436 xmlFreeDtd((xmlDtdPtr) cur);
3439 if (cur->type == XML_NAMESPACE_DECL) {
3440 xmlFreeNs((xmlNsPtr) cur);
3443 if (cur->type == XML_ATTRIBUTE_NODE) {
3444 xmlFreeProp((xmlAttrPtr) cur);
3448 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3449 xmlDeregisterNodeDefaultValue(cur);
3451 if (cur->doc != NULL) dict = cur->doc->dict;
3453 if ((cur->children != NULL) &&
3454 (cur->type != XML_ENTITY_REF_NODE))
3455 xmlFreeNodeList(cur->children);
3456 if (((cur->type == XML_ELEMENT_NODE) ||
3457 (cur->type == XML_XINCLUDE_START) ||
3458 (cur->type == XML_XINCLUDE_END)) &&
3459 (cur->properties != NULL))
3460 xmlFreePropList(cur->properties);
3461 if ((cur->type != XML_ELEMENT_NODE) &&
3462 (cur->content != NULL) &&
3463 (cur->type != XML_ENTITY_REF_NODE) &&
3464 (cur->type != XML_XINCLUDE_END) &&
3465 (cur->type != XML_XINCLUDE_START) &&
3466 (cur->content != (xmlChar *) &(cur->properties))) {
3467 DICT_FREE(cur->content)
3471 * When a node is a text node or a comment, it uses a global static
3472 * variable for the name of the node.
3473 * Otherwise the node name might come from the document's dictionnary
3475 if ((cur->name != NULL) &&
3476 (cur->type != XML_TEXT_NODE) &&
3477 (cur->type != XML_COMMENT_NODE))
3478 DICT_FREE(cur->name)
3480 if (((cur->type == XML_ELEMENT_NODE) ||
3481 (cur->type == XML_XINCLUDE_START) ||
3482 (cur->type == XML_XINCLUDE_END)) &&
3483 (cur->nsDef != NULL))
3484 xmlFreeNsList(cur->nsDef);
3492 * Unlink a node from it's current context, the node is not freed
3495 xmlUnlinkNode(xmlNodePtr cur) {
3498 xmlGenericError(xmlGenericErrorContext,
3499 "xmlUnlinkNode : node == NULL\n");
3503 if (cur->type == XML_DTD_NODE) {
3507 if (doc->intSubset == (xmlDtdPtr) cur)
3508 doc->intSubset = NULL;
3509 if (doc->extSubset == (xmlDtdPtr) cur)
3510 doc->extSubset = NULL;
3513 if (cur->parent != NULL) {
3515 parent = cur->parent;
3516 if (cur->type == XML_ATTRIBUTE_NODE) {
3517 if (parent->properties == (xmlAttrPtr) cur)
3518 parent->properties = ((xmlAttrPtr) cur)->next;
3520 if (parent->children == cur)
3521 parent->children = cur->next;
3522 if (parent->last == cur)
3523 parent->last = cur->prev;
3527 if (cur->next != NULL)
3528 cur->next->prev = cur->prev;
3529 if (cur->prev != NULL)
3530 cur->prev->next = cur->next;
3531 cur->next = cur->prev = NULL;
3534 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
3537 * @old: the old node
3540 * Unlink the old node from its current context, prune the new one
3541 * at the same place. If @cur was already inserted in a document it is
3542 * first unlinked from its existing context.
3544 * Returns the @old node
3547 xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
3548 if (old == cur) return(NULL);
3549 if ((old == NULL) || (old->parent == NULL)) {
3551 xmlGenericError(xmlGenericErrorContext,
3552 "xmlReplaceNode : old == NULL or without parent\n");
3563 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3565 xmlGenericError(xmlGenericErrorContext,
3566 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3570 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3572 xmlGenericError(xmlGenericErrorContext,
3573 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3578 xmlSetTreeDoc(cur, old->doc);
3579 cur->parent = old->parent;
3580 cur->next = old->next;
3581 if (cur->next != NULL)
3582 cur->next->prev = cur;
3583 cur->prev = old->prev;
3584 if (cur->prev != NULL)
3585 cur->prev->next = cur;
3586 if (cur->parent != NULL) {
3587 if (cur->type == XML_ATTRIBUTE_NODE) {
3588 if (cur->parent->properties == (xmlAttrPtr)old)
3589 cur->parent->properties = ((xmlAttrPtr) cur);
3591 if (cur->parent->children == old)
3592 cur->parent->children = cur;
3593 if (cur->parent->last == old)
3594 cur->parent->last = cur;
3597 old->next = old->prev = NULL;
3601 #endif /* LIBXML_TREE_ENABLED */
3603 /************************************************************************
3607 ************************************************************************/
3611 * @cur: the namespace
3613 * Do a copy of the namespace.
3615 * Returns: a new #xmlNsPtr, or NULL in case of error.
3618 xmlCopyNamespace(xmlNsPtr cur) {
3621 if (cur == NULL) return(NULL);
3622 switch (cur->type) {
3623 case XML_LOCAL_NAMESPACE:
3624 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3628 xmlGenericError(xmlGenericErrorContext,
3629 "xmlCopyNamespace: invalid type %d\n", cur->type);
3637 * xmlCopyNamespaceList:
3638 * @cur: the first namespace
3640 * Do a copy of an namespace list.
3642 * Returns: a new #xmlNsPtr, or NULL in case of error.
3645 xmlCopyNamespaceList(xmlNsPtr cur) {
3646 xmlNsPtr ret = NULL;
3647 xmlNsPtr p = NULL,q;
3649 while (cur != NULL) {
3650 q = xmlCopyNamespace(cur);
3663 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
3666 xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
3669 if (cur == NULL) return(NULL);
3671 ret = xmlNewDocProp(target->doc, cur->name, NULL);
3672 else if (doc != NULL)
3673 ret = xmlNewDocProp(doc, cur->name, NULL);
3674 else if (cur->parent != NULL)
3675 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3676 else if (cur->children != NULL)
3677 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3679 ret = xmlNewDocProp(NULL, cur->name, NULL);
3680 if (ret == NULL) return(NULL);
3681 ret->parent = target;
3683 if ((cur->ns != NULL) && (target != NULL)) {
3686 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3689 * Humm, we are copying an element whose namespace is defined
3690 * out of the new tree scope. Search it in the original tree
3691 * and add it at the top of the new tree
3693 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
3695 xmlNodePtr root = target;
3696 xmlNodePtr pred = NULL;
3698 while (root->parent != NULL) {
3700 root = root->parent;
3702 if (root == (xmlNodePtr) target->doc) {
3703 /* correct possibly cycling above the document elt */
3706 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3710 * we have to find something appropriate here since
3711 * we cant be sure, that the namespce we found is identified
3714 if (xmlStrEqual(ns->href, cur->ns->href)) {
3715 /* this is the nice case */
3719 * we are in trouble: we need a new reconcilied namespace.
3722 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
3729 if (cur->children != NULL) {
3732 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
3734 tmp = ret->children;
3735 while (tmp != NULL) {
3736 /* tmp->parent = (xmlNodePtr)ret; */
3737 if (tmp->next == NULL)
3745 if ((target!= NULL) && (cur!= NULL) &&
3746 (target->doc != NULL) && (cur->doc != NULL) &&
3747 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
3748 if (xmlIsID(cur->doc, cur->parent, cur)) {
3751 id = xmlNodeListGetString(cur->doc, cur->children, 1);
3753 xmlAddID(NULL, target->doc, id, ret);
3763 * @target: the element where the attribute will be grafted
3764 * @cur: the attribute
3766 * Do a copy of the attribute.
3768 * Returns: a new #xmlAttrPtr, or NULL in case of error.
3771 xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
3772 return xmlCopyPropInternal(NULL, target, cur);
3777 * @target: the element where the attributes will be grafted
3778 * @cur: the first attribute
3780 * Do a copy of an attribute list.
3782 * Returns: a new #xmlAttrPtr, or NULL in case of error.
3785 xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
3786 xmlAttrPtr ret = NULL;
3787 xmlAttrPtr p = NULL,q;
3789 while (cur != NULL) {
3790 q = xmlCopyProp(target, cur);
3806 * NOTE about the CopyNode operations !
3808 * They are split into external and internal parts for one
3809 * tricky reason: namespaces. Doing a direct copy of a node
3810 * say RPM:Copyright without changing the namespace pointer to
3811 * something else can produce stale links. One way to do it is
3812 * to keep a reference counter but this doesn't work as soon
3813 * as one move the element or the subtree out of the scope of
3814 * the existing namespace. The actual solution seems to add
3815 * a copy of the namespace at the top of the copied tree if
3816 * not available in the subtree.
3817 * Hence two functions, the public front-end call the inner ones
3818 * The argument "recursive" normally indicates a recursive copy
3819 * of the node with values 0 (no) and 1 (yes). For XInclude,
3820 * however, we allow a value of 2 to indicate copy properties and
3821 * namespace info, but don't recurse on children.
3825 xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
3829 if (node == NULL) return(NULL);
3830 switch (node->type) {
3832 case XML_CDATA_SECTION_NODE:
3833 case XML_ELEMENT_NODE:
3834 case XML_DOCUMENT_FRAG_NODE:
3835 case XML_ENTITY_REF_NODE:
3836 case XML_ENTITY_NODE:
3838 case XML_COMMENT_NODE:
3839 case XML_XINCLUDE_START:
3840 case XML_XINCLUDE_END:
3842 case XML_ATTRIBUTE_NODE:
3843 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
3844 case XML_NAMESPACE_DECL:
3845 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
3847 case XML_DOCUMENT_NODE:
3848 case XML_HTML_DOCUMENT_NODE:
3849 #ifdef LIBXML_DOCB_ENABLED
3850 case XML_DOCB_DOCUMENT_NODE:
3852 #ifdef LIBXML_TREE_ENABLED
3853 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
3854 #endif /* LIBXML_TREE_ENABLED */
3855 case XML_DOCUMENT_TYPE_NODE:
3856 case XML_NOTATION_NODE:
3858 case XML_ELEMENT_DECL:
3859 case XML_ATTRIBUTE_DECL:
3860 case XML_ENTITY_DECL:
3865 * Allocate a new node and fill the fields.
3867 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3869 xmlTreeErrMemory("copying node");
3872 memset(ret, 0, sizeof(xmlNode));
3873 ret->type = node->type;
3876 ret->parent = parent;
3877 if (node->name == xmlStringText)
3878 ret->name = xmlStringText;
3879 else if (node->name == xmlStringTextNoenc)
3880 ret->name = xmlStringTextNoenc;
3881 else if (node->name == xmlStringComment)
3882 ret->name = xmlStringComment;
3883 else if (node->name != NULL) {
3884 if ((doc != NULL) && (doc->dict != NULL))
3885 ret->name = xmlDictLookup(doc->dict, node->name, -1);
3887 ret->name = xmlStrdup(node->name);
3889 if ((node->type != XML_ELEMENT_NODE) &&
3890 (node->content != NULL) &&
3891 (node->type != XML_ENTITY_REF_NODE) &&
3892 (node->type != XML_XINCLUDE_END) &&
3893 (node->type != XML_XINCLUDE_START)) {
3894 ret->content = xmlStrdup(node->content);
3896 if (node->type == XML_ELEMENT_NODE)
3897 ret->line = node->line;
3899 if (parent != NULL) {
3903 * this is a tricky part for the node register thing:
3904 * in case ret does get coalesced in xmlAddChild
3905 * the deregister-node callback is called; so we register ret now already
3907 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
3908 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
3910 tmp = xmlAddChild(parent, ret);
3911 /* node could have coalesced */
3918 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
3919 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
3921 if (node->ns != NULL) {
3924 ns = xmlSearchNs(doc, ret, node->ns->prefix);
3927 * Humm, we are copying an element whose namespace is defined
3928 * out of the new tree scope. Search it in the original tree
3929 * and add it at the top of the new tree
3931 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
3933 xmlNodePtr root = ret;
3935 while (root->parent != NULL) root = root->parent;
3936 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3940 * reference the existing namespace definition in our own tree.
3945 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
3946 ret->properties = xmlCopyPropList(ret, node->properties);
3947 if (node->type == XML_ENTITY_REF_NODE) {
3948 if ((doc == NULL) || (node->doc != doc)) {
3950 * The copied node will go into a separate document, so
3951 * to avoid dangling references to the ENTITY_DECL node
3952 * we cannot keep the reference. Try to find it in the
3955 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
3957 ret->children = node->children;
3959 ret->last = ret->children;
3960 } else if ((node->children != NULL) && (extended != 2)) {
3961 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
3962 UPDATE_LAST_CHILD_AND_PARENT(ret)
3966 /* if parent != NULL we already registered the node above */
3967 if ((parent == NULL) &&
3968 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
3969 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
3974 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
3975 xmlNodePtr ret = NULL;
3976 xmlNodePtr p = NULL,q;
3978 while (node != NULL) {
3979 #ifdef LIBXML_TREE_ENABLED
3980 if (node->type == XML_DTD_NODE ) {
3985 if (doc->intSubset == NULL) {
3986 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
3989 doc->intSubset = (xmlDtdPtr) q;
3990 xmlAddChild(parent, q);
3992 q = (xmlNodePtr) doc->intSubset;
3993 xmlAddChild(parent, q);
3996 #endif /* LIBXML_TREE_ENABLED */
3997 q = xmlStaticCopyNode(node, doc, parent, 1);
4001 } else if (p != q) {
4002 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
4015 * @extended: if 1 do a recursive copy (properties, namespaces and children
4017 * if 2 copy properties and namespaces (when applicable)
4019 * Do a copy of the node.
4021 * Returns: a new #xmlNodePtr, or NULL in case of error.
4024 xmlCopyNode(const xmlNodePtr node, int extended) {
4027 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
4034 * @doc: the document
4035 * @extended: if 1 do a recursive copy (properties, namespaces and children
4037 * if 2 copy properties and namespaces (when applicable)
4039 * Do a copy of the node to a given document.
4041 * Returns: a new #xmlNodePtr, or NULL in case of error.
4044 xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) {
4047 ret = xmlStaticCopyNode(node, doc, NULL, extended);
4052 * xmlDocCopyNodeList:
4053 * @doc: the target document
4054 * @node: the first node in the list.
4056 * Do a recursive copy of the node list.
4058 * Returns: a new #xmlNodePtr, or NULL in case of error.
4060 xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) {
4061 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4067 * @node: the first node in the list.
4069 * Do a recursive copy of the node list.
4070 * Use xmlDocCopyNodeList() if possible to ensure string interning.
4072 * Returns: a new #xmlNodePtr, or NULL in case of error.
4074 xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
4075 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4079 #if defined(LIBXML_TREE_ENABLED)
4084 * Do a copy of the dtd.
4086 * Returns: a new #xmlDtdPtr, or NULL in case of error.
4089 xmlCopyDtd(xmlDtdPtr dtd) {
4091 xmlNodePtr cur, p = NULL, q;
4093 if (dtd == NULL) return(NULL);
4094 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4095 if (ret == NULL) return(NULL);
4096 if (dtd->entities != NULL)
4097 ret->entities = (void *) xmlCopyEntitiesTable(
4098 (xmlEntitiesTablePtr) dtd->entities);
4099 if (dtd->notations != NULL)
4100 ret->notations = (void *) xmlCopyNotationTable(
4101 (xmlNotationTablePtr) dtd->notations);
4102 if (dtd->elements != NULL)
4103 ret->elements = (void *) xmlCopyElementTable(
4104 (xmlElementTablePtr) dtd->elements);
4105 if (dtd->attributes != NULL)
4106 ret->attributes = (void *) xmlCopyAttributeTable(
4107 (xmlAttributeTablePtr) dtd->attributes);
4108 if (dtd->pentities != NULL)
4109 ret->pentities = (void *) xmlCopyEntitiesTable(
4110 (xmlEntitiesTablePtr) dtd->pentities);
4112 cur = dtd->children;
4113 while (cur != NULL) {
4116 if (cur->type == XML_ENTITY_DECL) {
4117 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4118 switch (tmp->etype) {
4119 case XML_INTERNAL_GENERAL_ENTITY:
4120 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4121 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4122 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4124 case XML_INTERNAL_PARAMETER_ENTITY:
4125 case XML_EXTERNAL_PARAMETER_ENTITY:
4127 xmlGetParameterEntityFromDtd(ret, tmp->name);
4129 case XML_INTERNAL_PREDEFINED_ENTITY:
4132 } else if (cur->type == XML_ELEMENT_DECL) {
4133 xmlElementPtr tmp = (xmlElementPtr) cur;
4135 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4136 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4137 xmlAttributePtr tmp = (xmlAttributePtr) cur;
4139 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4140 } else if (cur->type == XML_COMMENT_NODE) {
4141 q = xmlCopyNode(cur, 0);
4155 q->parent = (xmlNodePtr) ret;
4166 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
4169 * @doc: the document
4170 * @recursive: if not zero do a recursive copy.
4172 * Do a copy of the document info. If recursive, the content tree will
4173 * be copied too as well as DTD, namespaces and entities.
4175 * Returns: a new #xmlDocPtr, or NULL in case of error.
4178 xmlCopyDoc(xmlDocPtr doc, int recursive) {
4181 if (doc == NULL) return(NULL);
4182 ret = xmlNewDoc(doc->version);
4183 if (ret == NULL) return(NULL);
4184 if (doc->name != NULL)
4185 ret->name = xmlMemStrdup(doc->name);
4186 if (doc->encoding != NULL)
4187 ret->encoding = xmlStrdup(doc->encoding);
4188 if (doc->URL != NULL)
4189 ret->URL = xmlStrdup(doc->URL);
4190 ret->charset = doc->charset;
4191 ret->compression = doc->compression;
4192 ret->standalone = doc->standalone;
4193 if (!recursive) return(ret);
4196 ret->children = NULL;
4197 #ifdef LIBXML_TREE_ENABLED
4198 if (doc->intSubset != NULL) {
4199 ret->intSubset = xmlCopyDtd(doc->intSubset);
4200 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
4201 ret->intSubset->parent = ret;
4204 if (doc->oldNs != NULL)
4205 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4206 if (doc->children != NULL) {
4209 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4212 tmp = ret->children;
4213 while (tmp != NULL) {
4214 if (tmp->next == NULL)
4221 #endif /* LIBXML_TREE_ENABLED */
4223 /************************************************************************
4225 * Content access functions *
4227 ************************************************************************/
4233 * Get line number of @node. This requires activation of this option
4234 * before invoking the parser by calling xmlLineNumbersDefault(1)
4236 * Returns the line number if successful, -1 otherwise
4239 xmlGetLineNo(xmlNodePtr node)
4245 if ((node->type == XML_ELEMENT_NODE) ||
4246 (node->type == XML_TEXT_NODE) ||
4247 (node->type == XML_COMMENT_NODE) ||
4248 (node->type == XML_PI_NODE))
4249 result = (long) node->line;
4250 else if ((node->prev != NULL) &&
4251 ((node->prev->type == XML_ELEMENT_NODE) ||
4252 (node->prev->type == XML_TEXT_NODE) ||
4253 (node->prev->type == XML_COMMENT_NODE) ||
4254 (node->prev->type == XML_PI_NODE)))
4255 result = xmlGetLineNo(node->prev);
4256 else if ((node->parent != NULL) &&
4257 (node->parent->type == XML_ELEMENT_NODE))
4258 result = xmlGetLineNo(node->parent);
4263 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
4268 * Build a structure based Path for the given node
4270 * Returns the new path or NULL in case of error. The caller must free
4271 * the returned string
4274 xmlGetNodePath(xmlNodePtr node)
4276 xmlNodePtr cur, tmp, next;
4277 xmlChar *buffer = NULL, *temp;
4283 int occur = 0, generic;
4289 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4290 if (buffer == NULL) {
4291 xmlTreeErrMemory("getting node path");
4294 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4296 xmlTreeErrMemory("getting node path");
4307 if ((cur->type == XML_DOCUMENT_NODE) ||
4308 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4309 if (buffer[0] == '/')
4313 } else if (cur->type == XML_ELEMENT_NODE) {
4316 name = (const char *) cur->name;
4318 if (cur->ns->prefix != NULL) {
4319 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4320 (char *)cur->ns->prefix, (char *)cur->name);
4321 nametemp[sizeof(nametemp) - 1] = 0;
4325 * We cannot express named elements in the default
4326 * namespace, so use "*".
4335 * Thumbler index computation
4336 * TODO: the ocurence test seems bogus for namespaced names
4339 while (tmp != NULL) {
4340 if ((tmp->type == XML_ELEMENT_NODE) &&
4342 (xmlStrEqual(cur->name, tmp->name) &&
4343 ((tmp->ns == cur->ns) ||
4344 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4345 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4351 while (tmp != NULL && occur == 0) {
4352 if ((tmp->type == XML_ELEMENT_NODE) &&
4354 (xmlStrEqual(cur->name, tmp->name) &&
4355 ((tmp->ns == cur->ns) ||
4356 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4357 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4365 } else if (cur->type == XML_COMMENT_NODE) {
4371 * Thumbler index computation
4374 while (tmp != NULL) {
4375 if (tmp->type == XML_COMMENT_NODE)
4381 while (tmp != NULL && occur == 0) {
4382 if (tmp->type == XML_COMMENT_NODE)
4390 } else if ((cur->type == XML_TEXT_NODE) ||
4391 (cur->type == XML_CDATA_SECTION_NODE)) {
4397 * Thumbler index computation
4400 while (tmp != NULL) {
4401 if ((tmp->type == XML_TEXT_NODE) ||
4402 (tmp->type == XML_CDATA_SECTION_NODE))
4407 * Evaluate if this is the only text- or CDATA-section-node;
4408 * if yes, then we'll get "text()", otherwise "text()[1]".
4412 while (tmp != NULL) {
4413 if ((tmp->type == XML_TEXT_NODE) ||
4414 (tmp->type == XML_CDATA_SECTION_NODE))
4423 } else if (cur->type == XML_PI_NODE) {
4425 snprintf(nametemp, sizeof(nametemp) - 1,
4426 "processing-instruction('%s')", (char *)cur->name);
4427 nametemp[sizeof(nametemp) - 1] = 0;
4433 * Thumbler index computation
4436 while (tmp != NULL) {
4437 if ((tmp->type == XML_PI_NODE) &&
4438 (xmlStrEqual(cur->name, tmp->name)))
4444 while (tmp != NULL && occur == 0) {
4445 if ((tmp->type == XML_PI_NODE) &&
4446 (xmlStrEqual(cur->name, tmp->name)))
4455 } else if (cur->type == XML_ATTRIBUTE_NODE) {
4457 name = (const char *) (((xmlAttrPtr) cur)->name);
4459 if (cur->ns->prefix != NULL)
4460 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4461 (char *)cur->ns->prefix, (char *)cur->name);
4463 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4465 nametemp[sizeof(nametemp) - 1] = 0;
4468 next = ((xmlAttrPtr) cur)->parent;
4474 * Make sure there is enough room
4476 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4478 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4479 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4481 xmlTreeErrMemory("getting node path");
4487 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4489 xmlTreeErrMemory("getting node path");
4497 snprintf((char *) buf, buf_len, "%s%s%s",
4498 sep, name, (char *) buffer);
4500 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
4501 sep, name, occur, (char *) buffer);
4502 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
4504 } while (cur != NULL);
4508 #endif /* LIBXML_TREE_ENABLED */
4511 * xmlDocGetRootElement:
4512 * @doc: the document
4514 * Get the root element of the document (doc->children is a list
4515 * containing possibly comments, PIs, etc ...).
4517 * Returns the #xmlNodePtr for the root or NULL
4520 xmlDocGetRootElement(xmlDocPtr doc) {
4523 if (doc == NULL) return(NULL);
4524 ret = doc->children;
4525 while (ret != NULL) {
4526 if (ret->type == XML_ELEMENT_NODE)
4533 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
4535 * xmlDocSetRootElement:
4536 * @doc: the document
4537 * @root: the new document root element, if root is NULL no action is taken,
4538 * to remove a node from a document use xmlUnlinkNode(root) instead.
4540 * Set the root element of the document (doc->children is a list
4541 * containing possibly comments, PIs, etc ...).
4543 * Returns the old root element if any was found, NULL if root was NULL
4546 xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4547 xmlNodePtr old = NULL;
4549 if (doc == NULL) return(NULL);
4552 xmlUnlinkNode(root);
4553 xmlSetTreeDoc(root, doc);
4554 root->parent = (xmlNodePtr) doc;
4555 old = doc->children;
4556 while (old != NULL) {
4557 if (old->type == XML_ELEMENT_NODE)
4562 if (doc->children == NULL) {
4563 doc->children = root;
4566 xmlAddSibling(doc->children, root);
4569 xmlReplaceNode(old, root);
4575 #if defined(LIBXML_TREE_ENABLED)
4578 * @cur: the node being changed
4579 * @lang: the language description
4581 * Set the language of a node, i.e. the values of the xml:lang
4585 xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
4588 if (cur == NULL) return;
4591 case XML_CDATA_SECTION_NODE:
4592 case XML_COMMENT_NODE:
4593 case XML_DOCUMENT_NODE:
4594 case XML_DOCUMENT_TYPE_NODE:
4595 case XML_DOCUMENT_FRAG_NODE:
4596 case XML_NOTATION_NODE:
4597 case XML_HTML_DOCUMENT_NODE:
4599 case XML_ELEMENT_DECL:
4600 case XML_ATTRIBUTE_DECL:
4601 case XML_ENTITY_DECL:
4603 case XML_ENTITY_REF_NODE:
4604 case XML_ENTITY_NODE:
4605 case XML_NAMESPACE_DECL:
4606 #ifdef LIBXML_DOCB_ENABLED
4607 case XML_DOCB_DOCUMENT_NODE:
4609 case XML_XINCLUDE_START:
4610 case XML_XINCLUDE_END:
4612 case XML_ELEMENT_NODE:
4613 case XML_ATTRIBUTE_NODE:
4616 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4619 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
4621 #endif /* LIBXML_TREE_ENABLED */
4625 * @cur: the node being checked
4627 * Searches the language of a node, i.e. the values of the xml:lang
4628 * attribute or the one carried by the nearest ancestor.
4630 * Returns a pointer to the lang value, or NULL if not found
4631 * It's up to the caller to free the memory with xmlFree().
4634 xmlNodeGetLang(xmlNodePtr cur) {
4637 while (cur != NULL) {
4638 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
4647 #ifdef LIBXML_TREE_ENABLED
4649 * xmlNodeSetSpacePreserve:
4650 * @cur: the node being changed
4651 * @val: the xml:space value ("0": default, 1: "preserve")
4653 * Set (or reset) the space preserving behaviour of a node, i.e. the
4654 * value of the xml:space attribute.
4657 xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
4660 if (cur == NULL) return;
4663 case XML_CDATA_SECTION_NODE:
4664 case XML_COMMENT_NODE:
4665 case XML_DOCUMENT_NODE:
4666 case XML_DOCUMENT_TYPE_NODE:
4667 case XML_DOCUMENT_FRAG_NODE:
4668 case XML_NOTATION_NODE:
4669 case XML_HTML_DOCUMENT_NODE:
4671 case XML_ELEMENT_DECL:
4672 case XML_ATTRIBUTE_DECL:
4673 case XML_ENTITY_DECL:
4675 case XML_ENTITY_REF_NODE:
4676 case XML_ENTITY_NODE:
4677 case XML_NAMESPACE_DECL:
4678 case XML_XINCLUDE_START:
4679 case XML_XINCLUDE_END:
4680 #ifdef LIBXML_DOCB_ENABLED
4681 case XML_DOCB_DOCUMENT_NODE:
4684 case XML_ELEMENT_NODE:
4685 case XML_ATTRIBUTE_NODE:
4688 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4693 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
4696 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
4700 #endif /* LIBXML_TREE_ENABLED */
4703 * xmlNodeGetSpacePreserve:
4704 * @cur: the node being checked
4706 * Searches the space preserving behaviour of a node, i.e. the values
4707 * of the xml:space attribute or the one carried by the nearest
4710 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
4713 xmlNodeGetSpacePreserve(xmlNodePtr cur) {
4716 while (cur != NULL) {
4717 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
4718 if (space != NULL) {
4719 if (xmlStrEqual(space, BAD_CAST "preserve")) {
4723 if (xmlStrEqual(space, BAD_CAST "default")) {
4734 #ifdef LIBXML_TREE_ENABLED
4737 * @cur: the node being changed
4738 * @name: the new tag name
4740 * Set (or reset) the name of a node.
4743 xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
4747 if (cur == NULL) return;
4748 if (name == NULL) return;
4751 case XML_CDATA_SECTION_NODE:
4752 case XML_COMMENT_NODE:
4753 case XML_DOCUMENT_TYPE_NODE:
4754 case XML_DOCUMENT_FRAG_NODE:
4755 case XML_NOTATION_NODE:
4756 case XML_HTML_DOCUMENT_NODE:
4757 case XML_NAMESPACE_DECL:
4758 case XML_XINCLUDE_START:
4759 case XML_XINCLUDE_END:
4760 #ifdef LIBXML_DOCB_ENABLED
4761 case XML_DOCB_DOCUMENT_NODE:
4764 case XML_ELEMENT_NODE:
4765 case XML_ATTRIBUTE_NODE:
4767 case XML_ENTITY_REF_NODE:
4768 case XML_ENTITY_NODE:
4770 case XML_DOCUMENT_NODE:
4771 case XML_ELEMENT_DECL:
4772 case XML_ATTRIBUTE_DECL:
4773 case XML_ENTITY_DECL:
4782 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
4783 xmlFree((xmlChar *) cur->name);
4784 cur->name = xmlDictLookup(dict, name, -1);
4786 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
4787 cur->name = xmlStrdup(name);
4792 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
4795 * @cur: the node being changed
4796 * @uri: the new base URI
4798 * Set (or reset) the base URI of a node, i.e. the value of the
4799 * xml:base attribute.
4802 xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
4804 const xmlChar* fixed;
4806 if (cur == NULL) return;
4809 case XML_CDATA_SECTION_NODE:
4810 case XML_COMMENT_NODE:
4811 case XML_DOCUMENT_TYPE_NODE:
4812 case XML_DOCUMENT_FRAG_NODE:
4813 case XML_NOTATION_NODE:
4815 case XML_ELEMENT_DECL:
4816 case XML_ATTRIBUTE_DECL:
4817 case XML_ENTITY_DECL:
4819 case XML_ENTITY_REF_NODE:
4820 case XML_ENTITY_NODE:
4821 case XML_NAMESPACE_DECL:
4822 case XML_XINCLUDE_START:
4823 case XML_XINCLUDE_END:
4825 case XML_ELEMENT_NODE:
4826 case XML_ATTRIBUTE_NODE:
4828 case XML_DOCUMENT_NODE:
4829 #ifdef LIBXML_DOCB_ENABLED
4830 case XML_DOCB_DOCUMENT_NODE:
4832 case XML_HTML_DOCUMENT_NODE: {
4833 xmlDocPtr doc = (xmlDocPtr) cur;
4835 if (doc->URL != NULL)
4836 xmlFree((xmlChar *) doc->URL);
4840 doc->URL = xmlPathToURI(uri);
4845 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4848 fixed = xmlPathToURI(uri);
4849 if (fixed != NULL) {
4850 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
4853 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
4856 #endif /* LIBXML_TREE_ENABLED */
4860 * @doc: the document the node pertains to
4861 * @cur: the node being checked
4863 * Searches for the BASE URL. The code should work on both XML
4864 * and HTML document even if base mechanisms are completely different.
4865 * It returns the base as defined in RFC 2396 sections
4866 * 5.1.1. Base URI within Document Content
4868 * 5.1.2. Base URI from the Encapsulating Entity
4869 * However it does not return the document base (5.1.3), use
4870 * xmlDocumentGetBase() for this
4872 * Returns a pointer to the base URL, or NULL if not found
4873 * It's up to the caller to free the memory with xmlFree().
4876 xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
4877 xmlChar *oldbase = NULL;
4878 xmlChar *base, *newbase;
4880 if ((cur == NULL) && (doc == NULL))
4882 if (doc == NULL) doc = cur->doc;
4883 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
4884 cur = doc->children;
4885 while ((cur != NULL) && (cur->name != NULL)) {
4886 if (cur->type != XML_ELEMENT_NODE) {
4890 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
4891 cur = cur->children;
4894 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
4895 cur = cur->children;
4898 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
4899 return(xmlGetProp(cur, BAD_CAST "href"));
4905 while (cur != NULL) {
4906 if (cur->type == XML_ENTITY_DECL) {
4907 xmlEntityPtr ent = (xmlEntityPtr) cur;
4908 return(xmlStrdup(ent->URI));
4910 if (cur->type == XML_ELEMENT_NODE) {
4911 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
4913 if (oldbase != NULL) {
4914 newbase = xmlBuildURI(oldbase, base);
4915 if (newbase != NULL) {
4927 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
4928 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
4929 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
4935 if ((doc != NULL) && (doc->URL != NULL)) {
4936 if (oldbase == NULL)
4937 return(xmlStrdup(doc->URL));
4938 newbase = xmlBuildURI(oldbase, doc->URL);
4946 * xmlNodeBufGetContent:
4948 * @cur: the node being read
4950 * Read the value of a node @cur, this can be either the text carried
4951 * directly by this node if it's a TEXT node or the aggregate string
4952 * of the values carried by this node child's (TEXT and ENTITY_REF).
4953 * Entity references are substituted.
4954 * Fills up the buffer @buffer with this value
4956 * Returns 0 in case of success and -1 in case of error.
4959 xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
4961 if ((cur == NULL) || (buffer == NULL)) return(-1);
4962 switch (cur->type) {
4963 case XML_CDATA_SECTION_NODE:
4965 xmlBufferCat(buffer, cur->content);
4967 case XML_DOCUMENT_FRAG_NODE:
4968 case XML_ELEMENT_NODE:{
4969 xmlNodePtr tmp = cur;
4971 while (tmp != NULL) {
4972 switch (tmp->type) {
4973 case XML_CDATA_SECTION_NODE:
4975 if (tmp->content != NULL)
4976 xmlBufferCat(buffer, tmp->content);
4978 case XML_ENTITY_REF_NODE:
4979 xmlNodeBufGetContent(buffer, tmp);
4987 if (tmp->children != NULL) {
4988 if (tmp->children->type != XML_ENTITY_DECL) {
4989 tmp = tmp->children;
4996 if (tmp->next != NULL) {
5009 if (tmp->next != NULL) {
5013 } while (tmp != NULL);
5017 case XML_ATTRIBUTE_NODE:{
5018 xmlAttrPtr attr = (xmlAttrPtr) cur;
5019 xmlNodePtr tmp = attr->children;
5021 while (tmp != NULL) {
5022 if (tmp->type == XML_TEXT_NODE)
5023 xmlBufferCat(buffer, tmp->content);
5025 xmlNodeBufGetContent(buffer, tmp);
5030 case XML_COMMENT_NODE:
5032 xmlBufferCat(buffer, cur->content);
5034 case XML_ENTITY_REF_NODE:{
5038 /* lookup entity declaration */
5039 ent = xmlGetDocEntity(cur->doc, cur->name);
5043 /* an entity content can be any "well balanced chunk",
5044 * i.e. the result of the content [43] production:
5045 * http://www.w3.org/TR/REC-xml#NT-content
5046 * -> we iterate through child nodes and recursive call
5047 * xmlNodeGetContent() which handles all possible node types */
5048 tmp = ent->children;
5050 xmlNodeBufGetContent(buffer, tmp);
5055 case XML_ENTITY_NODE:
5056 case XML_DOCUMENT_TYPE_NODE:
5057 case XML_NOTATION_NODE:
5059 case XML_XINCLUDE_START:
5060 case XML_XINCLUDE_END:
5062 case XML_DOCUMENT_NODE:
5063 #ifdef LIBXML_DOCB_ENABLED
5064 case XML_DOCB_DOCUMENT_NODE:
5066 case XML_HTML_DOCUMENT_NODE:
5067 cur = cur->children;
5068 while (cur!= NULL) {
5069 if ((cur->type == XML_ELEMENT_NODE) ||
5070 (cur->type == XML_TEXT_NODE) ||
5071 (cur->type == XML_CDATA_SECTION_NODE)) {
5072 xmlNodeBufGetContent(buffer, cur);
5077 case XML_NAMESPACE_DECL:
5078 xmlBufferCat(buffer, ((xmlNsPtr) cur)->href);
5080 case XML_ELEMENT_DECL:
5081 case XML_ATTRIBUTE_DECL:
5082 case XML_ENTITY_DECL:
5088 * xmlNodeGetContent:
5089 * @cur: the node being read
5091 * Read the value of a node, this can be either the text carried
5092 * directly by this node if it's a TEXT node or the aggregate string
5093 * of the values carried by this node child's (TEXT and ENTITY_REF).
5094 * Entity references are substituted.
5095 * Returns a new #xmlChar * or NULL if no content is available.
5096 * It's up to the caller to free the memory with xmlFree().
5099 xmlNodeGetContent(xmlNodePtr cur)
5103 switch (cur->type) {
5104 case XML_DOCUMENT_FRAG_NODE:
5105 case XML_ELEMENT_NODE:{
5106 xmlBufferPtr buffer;
5109 buffer = xmlBufferCreateSize(64);
5112 xmlNodeBufGetContent(buffer, cur);
5113 ret = buffer->content;
5114 buffer->content = NULL;
5115 xmlBufferFree(buffer);
5118 case XML_ATTRIBUTE_NODE:
5119 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
5120 case XML_COMMENT_NODE:
5122 if (cur->content != NULL)
5123 return (xmlStrdup(cur->content));
5125 case XML_ENTITY_REF_NODE:{
5127 xmlBufferPtr buffer;
5130 /* lookup entity declaration */
5131 ent = xmlGetDocEntity(cur->doc, cur->name);
5135 buffer = xmlBufferCreate();
5139 xmlNodeBufGetContent(buffer, cur);
5141 ret = buffer->content;
5142 buffer->content = NULL;
5143 xmlBufferFree(buffer);
5146 case XML_ENTITY_NODE:
5147 case XML_DOCUMENT_TYPE_NODE:
5148 case XML_NOTATION_NODE:
5150 case XML_XINCLUDE_START:
5151 case XML_XINCLUDE_END:
5153 case XML_DOCUMENT_NODE:
5154 #ifdef LIBXML_DOCB_ENABLED
5155 case XML_DOCB_DOCUMENT_NODE:
5157 case XML_HTML_DOCUMENT_NODE: {
5158 xmlBufferPtr buffer;
5161 buffer = xmlBufferCreate();
5165 xmlNodeBufGetContent(buffer, (xmlNodePtr) cur);
5167 ret = buffer->content;
5168 buffer->content = NULL;
5169 xmlBufferFree(buffer);
5172 case XML_NAMESPACE_DECL: {
5175 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5178 case XML_ELEMENT_DECL:
5181 case XML_ATTRIBUTE_DECL:
5184 case XML_ENTITY_DECL:
5187 case XML_CDATA_SECTION_NODE:
5189 if (cur->content != NULL)
5190 return (xmlStrdup(cur->content));
5197 * xmlNodeSetContent:
5198 * @cur: the node being modified
5199 * @content: the new value of the content
5201 * Replace the content of a node.
5204 xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5207 xmlGenericError(xmlGenericErrorContext,
5208 "xmlNodeSetContent : node == NULL\n");
5212 switch (cur->type) {
5213 case XML_DOCUMENT_FRAG_NODE:
5214 case XML_ELEMENT_NODE:
5215 case XML_ATTRIBUTE_NODE:
5216 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5217 cur->children = xmlStringGetNodeList(cur->doc, content);
5218 UPDATE_LAST_CHILD_AND_PARENT(cur)
5221 case XML_CDATA_SECTION_NODE:
5222 case XML_ENTITY_REF_NODE:
5223 case XML_ENTITY_NODE:
5225 case XML_COMMENT_NODE:
5226 if ((cur->content != NULL) &&
5227 (cur->content != (xmlChar *) &(cur->properties))) {
5228 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5229 (xmlDictOwns(cur->doc->dict, cur->content))))
5230 xmlFree(cur->content);
5232 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5233 cur->last = cur->children = NULL;
5234 if (content != NULL) {
5235 cur->content = xmlStrdup(content);
5237 cur->content = NULL;
5238 cur->properties = NULL;
5241 case XML_DOCUMENT_NODE:
5242 case XML_HTML_DOCUMENT_NODE:
5243 case XML_DOCUMENT_TYPE_NODE:
5244 case XML_XINCLUDE_START:
5245 case XML_XINCLUDE_END:
5246 #ifdef LIBXML_DOCB_ENABLED
5247 case XML_DOCB_DOCUMENT_NODE:
5250 case XML_NOTATION_NODE:
5254 case XML_NAMESPACE_DECL:
5256 case XML_ELEMENT_DECL:
5259 case XML_ATTRIBUTE_DECL:
5262 case XML_ENTITY_DECL:
5268 #ifdef LIBXML_TREE_ENABLED
5270 * xmlNodeSetContentLen:
5271 * @cur: the node being modified
5272 * @content: the new value of the content
5273 * @len: the size of @content
5275 * Replace the content of a node.
5278 xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5281 xmlGenericError(xmlGenericErrorContext,
5282 "xmlNodeSetContentLen : node == NULL\n");
5286 switch (cur->type) {
5287 case XML_DOCUMENT_FRAG_NODE:
5288 case XML_ELEMENT_NODE:
5289 case XML_ATTRIBUTE_NODE:
5290 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5291 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5292 UPDATE_LAST_CHILD_AND_PARENT(cur)
5295 case XML_CDATA_SECTION_NODE:
5296 case XML_ENTITY_REF_NODE:
5297 case XML_ENTITY_NODE:
5299 case XML_COMMENT_NODE:
5300 case XML_NOTATION_NODE:
5301 if ((cur->content != NULL) &&
5302 (cur->content != (xmlChar *) &(cur->properties))) {
5303 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5304 (xmlDictOwns(cur->doc->dict, cur->content))))
5305 xmlFree(cur->content);
5307 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5308 cur->children = cur->last = NULL;
5309 if (content != NULL) {
5310 cur->content = xmlStrndup(content, len);
5312 cur->content = NULL;
5313 cur->properties = NULL;
5316 case XML_DOCUMENT_NODE:
5318 case XML_HTML_DOCUMENT_NODE:
5319 case XML_DOCUMENT_TYPE_NODE:
5320 case XML_NAMESPACE_DECL:
5321 case XML_XINCLUDE_START:
5322 case XML_XINCLUDE_END:
5323 #ifdef LIBXML_DOCB_ENABLED
5324 case XML_DOCB_DOCUMENT_NODE:
5327 case XML_ELEMENT_DECL:
5330 case XML_ATTRIBUTE_DECL:
5333 case XML_ENTITY_DECL:
5338 #endif /* LIBXML_TREE_ENABLED */
5341 * xmlNodeAddContentLen:
5342 * @cur: the node being modified
5343 * @content: extra content
5344 * @len: the size of @content
5346 * Append the extra substring to the node content.
5349 xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5352 xmlGenericError(xmlGenericErrorContext,
5353 "xmlNodeAddContentLen : node == NULL\n");
5357 if (len <= 0) return;
5358 switch (cur->type) {
5359 case XML_DOCUMENT_FRAG_NODE:
5360 case XML_ELEMENT_NODE: {
5361 xmlNodePtr last, newNode, tmp;
5364 newNode = xmlNewTextLen(content, len);
5365 if (newNode != NULL) {
5366 tmp = xmlAddChild(cur, newNode);
5369 if ((last != NULL) && (last->next == newNode)) {
5370 xmlTextMerge(last, newNode);
5375 case XML_ATTRIBUTE_NODE:
5378 case XML_CDATA_SECTION_NODE:
5379 case XML_ENTITY_REF_NODE:
5380 case XML_ENTITY_NODE:
5382 case XML_COMMENT_NODE:
5383 case XML_NOTATION_NODE:
5384 if (content != NULL) {
5385 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5386 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5387 xmlDictOwns(cur->doc->dict, cur->content))) {
5388 cur->content = xmlStrncatNew(cur->content, content, len);
5389 cur->properties = NULL;
5393 cur->content = xmlStrncat(cur->content, content, len);
5395 case XML_DOCUMENT_NODE:
5397 case XML_HTML_DOCUMENT_NODE:
5398 case XML_DOCUMENT_TYPE_NODE:
5399 case XML_NAMESPACE_DECL:
5400 case XML_XINCLUDE_START:
5401 case XML_XINCLUDE_END:
5402 #ifdef LIBXML_DOCB_ENABLED
5403 case XML_DOCB_DOCUMENT_NODE:
5406 case XML_ELEMENT_DECL:
5407 case XML_ATTRIBUTE_DECL:
5408 case XML_ENTITY_DECL:
5414 * xmlNodeAddContent:
5415 * @cur: the node being modified
5416 * @content: extra content
5418 * Append the extra substring to the node content.
5421 xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5426 xmlGenericError(xmlGenericErrorContext,
5427 "xmlNodeAddContent : node == NULL\n");
5431 if (content == NULL) return;
5432 len = xmlStrlen(content);
5433 xmlNodeAddContentLen(cur, content, len);
5438 * @first: the first text node
5439 * @second: the second text node being merged
5441 * Merge two text nodes into one
5442 * Returns the first text node augmented
5445 xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5446 if (first == NULL) return(second);
5447 if (second == NULL) return(first);
5448 if (first->type != XML_TEXT_NODE) return(first);
5449 if (second->type != XML_TEXT_NODE) return(first);
5450 if (second->name != first->name)
5452 xmlNodeAddContent(first, second->content);
5453 xmlUnlinkNode(second);
5454 xmlFreeNode(second);
5458 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
5461 * @doc: the document
5462 * @node: the current node
5464 * Search all the namespace applying to a given element.
5465 * Returns an NULL terminated array of all the #xmlNsPtr found
5466 * that need to be freed by the caller or NULL if no
5467 * namespace if defined
5470 xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5473 xmlNsPtr *ret = NULL;
5478 while (node != NULL) {
5479 if (node->type == XML_ELEMENT_NODE) {
5481 while (cur != NULL) {
5484 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5487 xmlTreeErrMemory("getting namespace list");
5492 for (i = 0; i < nbns; i++) {
5493 if ((cur->prefix == ret[i]->prefix) ||
5494 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5498 if (nbns >= maxns) {
5500 ret = (xmlNsPtr *) xmlRealloc(ret,
5505 xmlTreeErrMemory("getting namespace list");
5516 node = node->parent;
5520 #endif /* LIBXML_TREE_ENABLED */
5523 * xmlTreeEnsureXMLDecl:
5526 * Ensures that there is an XML namespace declaration on the doc.
5528 * Returns the XML ns-struct or NULL on API and internal errors.
5531 xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5535 if (doc->oldNs != NULL)
5536 return (doc->oldNs);
5539 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5542 "allocating the XML namespace");
5545 memset(ns, 0, sizeof(xmlNs));
5546 ns->type = XML_LOCAL_NAMESPACE;
5547 ns->href = xmlStrdup(XML_XML_NAMESPACE);
5548 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5556 * @doc: the document
5557 * @node: the current node
5558 * @nameSpace: the namespace prefix
5560 * Search a Ns registered under a given name space for a document.
5561 * recurse on the parents until it finds the defined namespace
5562 * or return NULL otherwise.
5563 * @nameSpace can be NULL, this is a search for the default namespace.
5564 * We don't allow to cross entities boundaries. If you don't declare
5565 * the namespace within those you will be in troubles !!! A warning
5566 * is generated to cover this case.
5568 * Returns the namespace pointer or NULL.
5571 xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
5574 xmlNodePtr orig = node;
5576 if (node == NULL) return(NULL);
5577 if ((nameSpace != NULL) &&
5578 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
5579 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5581 * The XML-1.0 namespace is normally held on the root
5582 * element. In this case exceptionally create it on the
5585 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5587 xmlTreeErrMemory("searching namespace");
5590 memset(cur, 0, sizeof(xmlNs));
5591 cur->type = XML_LOCAL_NAMESPACE;
5592 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5593 cur->prefix = xmlStrdup((const xmlChar *)"xml");
5594 cur->next = node->nsDef;
5604 * Return the XML namespace declaration held by the doc.
5606 if (doc->oldNs == NULL)
5607 return(xmlTreeEnsureXMLDecl(doc));
5611 while (node != NULL) {
5612 if ((node->type == XML_ENTITY_REF_NODE) ||
5613 (node->type == XML_ENTITY_NODE) ||
5614 (node->type == XML_ENTITY_DECL))
5616 if (node->type == XML_ELEMENT_NODE) {
5618 while (cur != NULL) {
5619 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5620 (cur->href != NULL))
5622 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5623 (cur->href != NULL) &&
5624 (xmlStrEqual(cur->prefix, nameSpace)))
5631 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5632 (cur->href != NULL))
5634 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5635 (cur->href != NULL) &&
5636 (xmlStrEqual(cur->prefix, nameSpace)))
5641 node = node->parent;
5648 * @doc: the document
5649 * @node: the current node
5650 * @ancestor: the ancestor carrying the namespace
5651 * @prefix: the namespace prefix
5653 * Verify that the given namespace held on @ancestor is still in scope
5656 * Returns 1 if true, 0 if false and -1 in case of error.
5659 xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5660 xmlNodePtr ancestor, const xmlChar * prefix)
5664 while ((node != NULL) && (node != ancestor)) {
5665 if ((node->type == XML_ENTITY_REF_NODE) ||
5666 (node->type == XML_ENTITY_NODE) ||
5667 (node->type == XML_ENTITY_DECL))
5669 if (node->type == XML_ELEMENT_NODE) {
5671 while (tst != NULL) {
5672 if ((tst->prefix == NULL)
5673 && (prefix == NULL))
5675 if ((tst->prefix != NULL)
5677 && (xmlStrEqual(tst->prefix, prefix)))
5682 node = node->parent;
5684 if (node != ancestor)
5690 * xmlSearchNsByHref:
5691 * @doc: the document
5692 * @node: the current node
5693 * @href: the namespace value
5695 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5696 * the defined namespace or return NULL otherwise.
5697 * Returns the namespace pointer or NULL.
5700 xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
5703 xmlNodePtr orig = node;
5706 if ((node == NULL) || (href == NULL))
5708 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
5710 * Only the document can hold the XML spec namespace.
5712 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5714 * The XML-1.0 namespace is normally held on the root
5715 * element. In this case exceptionally create it on the
5718 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5720 xmlTreeErrMemory("searching namespace");
5723 memset(cur, 0, sizeof(xmlNs));
5724 cur->type = XML_LOCAL_NAMESPACE;
5725 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5726 cur->prefix = xmlStrdup((const xmlChar *) "xml");
5727 cur->next = node->nsDef;
5737 * Return the XML namespace declaration held by the doc.
5739 if (doc->oldNs == NULL)
5740 return(xmlTreeEnsureXMLDecl(doc));
5744 is_attr = (node->type == XML_ATTRIBUTE_NODE);
5745 while (node != NULL) {
5746 if ((node->type == XML_ENTITY_REF_NODE) ||
5747 (node->type == XML_ENTITY_NODE) ||
5748 (node->type == XML_ENTITY_DECL))
5750 if (node->type == XML_ELEMENT_NODE) {
5752 while (cur != NULL) {
5753 if ((cur->href != NULL) && (href != NULL) &&
5754 (xmlStrEqual(cur->href, href))) {
5755 if (((!is_attr) || (cur->prefix != NULL)) &&
5756 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
5764 if ((cur->href != NULL) && (href != NULL) &&
5765 (xmlStrEqual(cur->href, href))) {
5766 if (((!is_attr) || (cur->prefix != NULL)) &&
5767 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
5773 node = node->parent;
5779 * xmlNewReconciliedNs:
5780 * @doc: the document
5781 * @tree: a node expected to hold the new namespace
5782 * @ns: the original namespace
5784 * This function tries to locate a namespace definition in a tree
5785 * ancestors, or create a new namespace definition node similar to
5786 * @ns trying to reuse the same prefix. However if the given prefix is
5787 * null (default namespace) or reused within the subtree defined by
5788 * @tree or on one of its ancestors then a new prefix is generated.
5789 * Returns the (new) namespace definition or NULL in case of error
5792 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
5799 xmlGenericError(xmlGenericErrorContext,
5800 "xmlNewReconciliedNs : tree == NULL\n");
5804 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
5806 xmlGenericError(xmlGenericErrorContext,
5807 "xmlNewReconciliedNs : ns == NULL\n");
5812 * Search an existing namespace definition inherited.
5814 def = xmlSearchNsByHref(doc, tree, ns->href);
5819 * Find a close prefix which is not already in use.
5820 * Let's strip namespace prefixes longer than 20 chars !
5822 if (ns->prefix == NULL)
5823 snprintf((char *) prefix, sizeof(prefix), "default");
5825 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
5827 def = xmlSearchNs(doc, tree, prefix);
5828 while (def != NULL) {
5829 if (counter > 1000) return(NULL);
5830 if (ns->prefix == NULL)
5831 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
5833 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
5834 (char *)ns->prefix, counter++);
5835 def = xmlSearchNs(doc, tree, prefix);
5839 * OK, now we are ready to create a new one.
5841 def = xmlNewNs(tree, ns->href, prefix);
5845 #ifdef LIBXML_TREE_ENABLED
5847 * xmlReconciliateNs:
5848 * @doc: the document
5849 * @tree: a node defining the subtree to reconciliate
5851 * This function checks that all the namespaces declared within the given
5852 * tree are properly declared. This is needed for example after Copy or Cut
5853 * and then paste operations. The subtree may still hold pointers to
5854 * namespace declarations outside the subtree or invalid/masked. As much
5855 * as possible the function try to reuse the existing namespaces found in
5856 * the new environment. If not possible the new namespaces are redeclared
5857 * on @tree at the top of the given subtree.
5858 * Returns the number of namespace declarations created or -1 in case of error.
5861 xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
5862 xmlNsPtr *oldNs = NULL;
5863 xmlNsPtr *newNs = NULL;
5868 xmlNodePtr node = tree;
5872 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
5873 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
5874 if (node->doc != doc) return(-1);
5875 while (node != NULL) {
5877 * Reconciliate the node namespace
5879 if (node->ns != NULL) {
5881 * initialize the cache if needed
5883 if (sizeCache == 0) {
5885 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5887 if (oldNs == NULL) {
5888 xmlTreeErrMemory("fixing namespaces");
5891 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5893 if (newNs == NULL) {
5894 xmlTreeErrMemory("fixing namespaces");
5899 for (i = 0;i < nbCache;i++) {
5900 if (oldNs[i] == node->ns) {
5901 node->ns = newNs[i];
5907 * OK we need to recreate a new namespace definition
5909 n = xmlNewReconciliedNs(doc, tree, node->ns);
5910 if (n != NULL) { /* :-( what if else ??? */
5912 * check if we need to grow the cache buffers.
5914 if (sizeCache <= nbCache) {
5916 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5918 if (oldNs == NULL) {
5919 xmlTreeErrMemory("fixing namespaces");
5923 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5925 if (newNs == NULL) {
5926 xmlTreeErrMemory("fixing namespaces");
5932 oldNs[nbCache++] = node->ns;
5938 * now check for namespace hold by attributes on the node.
5940 if (node->type == XML_ELEMENT_NODE) {
5941 attr = node->properties;
5942 while (attr != NULL) {
5943 if (attr->ns != NULL) {
5945 * initialize the cache if needed
5947 if (sizeCache == 0) {
5949 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5951 if (oldNs == NULL) {
5952 xmlTreeErrMemory("fixing namespaces");
5955 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5957 if (newNs == NULL) {
5958 xmlTreeErrMemory("fixing namespaces");
5963 for (i = 0;i < nbCache;i++) {
5964 if (oldNs[i] == attr->ns) {
5965 attr->ns = newNs[i];
5971 * OK we need to recreate a new namespace definition
5973 n = xmlNewReconciliedNs(doc, tree, attr->ns);
5974 if (n != NULL) { /* :-( what if else ??? */
5976 * check if we need to grow the cache buffers.
5978 if (sizeCache <= nbCache) {
5980 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
5981 sizeCache * sizeof(xmlNsPtr));
5982 if (oldNs == NULL) {
5983 xmlTreeErrMemory("fixing namespaces");
5987 newNs = (xmlNsPtr *) xmlRealloc(newNs,
5988 sizeCache * sizeof(xmlNsPtr));
5989 if (newNs == NULL) {
5990 xmlTreeErrMemory("fixing namespaces");
5996 oldNs[nbCache++] = attr->ns;
6006 * Browse the full subtree, deep first
6008 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
6010 node = node->children;
6011 } else if ((node != tree) && (node->next != NULL)) {
6014 } else if (node != tree) {
6015 /* go up to parents->next if needed */
6016 while (node != tree) {
6017 if (node->parent != NULL)
6018 node = node->parent;
6019 if ((node != tree) && (node->next != NULL)) {
6023 if (node->parent == NULL) {
6028 /* exit condition */
6040 #endif /* LIBXML_TREE_ENABLED */
6043 xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name,
6044 const xmlChar *nsName, int useDTD)
6048 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6051 if (node->properties != NULL) {
6052 prop = node->properties;
6053 if (nsName == NULL) {
6055 * We want the attr to be in no namespace.
6058 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6062 } while (prop != NULL);
6065 * We want the attr to be in the specified namespace.
6068 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6069 ((prop->ns->href == nsName) ||
6070 xmlStrEqual(prop->ns->href, nsName)))
6075 } while (prop != NULL);
6079 #ifdef LIBXML_TREE_ENABLED
6083 * Check if there is a default/fixed attribute declaration in
6084 * the internal or external subset.
6086 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6087 xmlDocPtr doc = node->doc;
6088 xmlAttributePtr attrDecl = NULL;
6089 xmlChar *elemQName, *tmpstr = NULL;
6092 * We need the QName of the element for the DTD-lookup.
6094 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6095 tmpstr = xmlStrdup(node->ns->prefix);
6096 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6097 tmpstr = xmlStrcat(tmpstr, node->name);
6102 elemQName = (xmlChar *) node->name;
6103 if (nsName == NULL) {
6105 * The common and nice case: Attr in no namespace.
6107 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6108 elemQName, name, NULL);
6109 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6110 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6111 elemQName, name, NULL);
6114 xmlNsPtr *nsList, *cur;
6117 * The ugly case: Search using the prefixes of in-scope
6118 * ns-decls corresponding to @nsName.
6120 nsList = xmlGetNsList(node->doc, node);
6121 if (nsList == NULL) {
6127 while (*cur != NULL) {
6128 if (xmlStrEqual((*cur)->href, nsName)) {
6129 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6130 name, (*cur)->prefix);
6133 if (doc->extSubset != NULL) {
6134 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6135 name, (*cur)->prefix);
6147 * Only default/fixed attrs are relevant.
6149 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6150 return((xmlAttrPtr) attrDecl);
6152 #endif /* LIBXML_TREE_ENABLED */
6157 xmlGetPropNodeValueInternal(xmlAttrPtr prop)
6161 if (prop->type == XML_ATTRIBUTE_NODE) {
6163 * Note that we return at least the empty string.
6164 * TODO: Do we really always want that?
6166 if (prop->children != NULL) {
6167 if ((prop->children->next == NULL) &&
6168 ((prop->children->type == XML_TEXT_NODE) ||
6169 (prop->children->type == XML_CDATA_SECTION_NODE)))
6172 * Optimization for the common case: only 1 text node.
6174 return(xmlStrdup(prop->children->content));
6178 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6183 return(xmlStrdup((xmlChar *)""));
6184 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6185 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6193 * @name: the attribute name
6195 * Search an attribute associated to a node
6196 * This function also looks in DTD attribute declaration for #FIXED or
6197 * default declaration values unless DTD use has been turned off.
6199 * Returns the attribute or the attribute declaration or NULL if
6200 * neither was found.
6203 xmlHasProp(xmlNodePtr node, const xmlChar *name) {
6207 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6210 * Check on the properties attached to the node
6212 prop = node->properties;
6213 while (prop != NULL) {
6214 if (xmlStrEqual(prop->name, name)) {
6219 if (!xmlCheckDTD) return(NULL);
6222 * Check if there is a default declaration in the internal
6223 * or external subsets
6227 xmlAttributePtr attrDecl;
6228 if (doc->intSubset != NULL) {
6229 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6230 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6231 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6232 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6233 /* return attribute declaration only if a default value is given
6234 (that includes #FIXED declarations) */
6235 return((xmlAttrPtr) attrDecl);
6244 * @name: the attribute name
6245 * @nameSpace: the URI of the namespace
6247 * Search for an attribute associated to a node
6248 * This attribute has to be anchored in the namespace specified.
6249 * This does the entity substitution.
6250 * This function looks in DTD attribute declaration for #FIXED or
6251 * default declaration values unless DTD use has been turned off.
6252 * Note that a namespace of NULL indicates to use the default namespace.
6254 * Returns the attribute or the attribute declaration or NULL
6255 * if neither was found.
6258 xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
6260 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
6266 * @name: the attribute name
6268 * Search and get the value of an attribute associated to a node
6269 * This does the entity substitution.
6270 * This function looks in DTD attribute declaration for #FIXED or
6271 * default declaration values unless DTD use has been turned off.
6272 * NOTE: this function acts independently of namespaces associated
6273 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6274 * for namespace aware processing.
6276 * Returns the attribute value or NULL if not found.
6277 * It's up to the caller to free the memory with xmlFree().
6280 xmlGetProp(xmlNodePtr node, const xmlChar *name) {
6283 prop = xmlHasProp(node, name);
6286 return(xmlGetPropNodeValueInternal(prop));
6292 * @name: the attribute name
6294 * Search and get the value of an attribute associated to a node
6295 * This does the entity substitution.
6296 * This function looks in DTD attribute declaration for #FIXED or
6297 * default declaration values unless DTD use has been turned off.
6298 * This function is similar to xmlGetProp except it will accept only
6299 * an attribute in no namespace.
6301 * Returns the attribute value or NULL if not found.
6302 * It's up to the caller to free the memory with xmlFree().
6305 xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
6308 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6311 return(xmlGetPropNodeValueInternal(prop));
6317 * @name: the attribute name
6318 * @nameSpace: the URI of the namespace
6320 * Search and get the value of an attribute associated to a node
6321 * This attribute has to be anchored in the namespace specified.
6322 * This does the entity substitution.
6323 * This function looks in DTD attribute declaration for #FIXED or
6324 * default declaration values unless DTD use has been turned off.
6326 * Returns the attribute value or NULL if not found.
6327 * It's up to the caller to free the memory with xmlFree().
6330 xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
6333 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6336 return(xmlGetPropNodeValueInternal(prop));
6339 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6343 * @name: the attribute name
6345 * Remove an attribute carried by a node.
6346 * This handles only attributes in no namespace.
6347 * Returns 0 if successful, -1 if not found
6350 xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
6353 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6356 xmlUnlinkNode((xmlNodePtr) prop);
6364 * @ns: the namespace definition
6365 * @name: the attribute name
6367 * Remove an attribute carried by a node.
6368 * Returns 0 if successful, -1 if not found
6371 xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
6374 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6377 xmlUnlinkNode((xmlNodePtr) prop);
6383 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
6387 * @name: the attribute name (a QName)
6388 * @value: the attribute value
6390 * Set (or reset) an attribute carried by a node.
6391 * If @name has a prefix, then the corresponding
6392 * namespace-binding will be used, if in scope; it is an
6393 * error it there's no such ns-binding for the prefix in
6395 * Returns the attribute pointer.
6399 xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
6401 const xmlChar *nqname;
6403 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
6409 nqname = xmlSplitQName3(name, &len);
6410 if (nqname != NULL) {
6412 xmlChar *prefix = xmlStrndup(name, len);
6413 ns = xmlSearchNs(node->doc, node, prefix);
6417 return(xmlSetNsProp(node, ns, nqname, value));
6419 return(xmlSetNsProp(node, NULL, name, value));
6425 * @ns: the namespace definition
6426 * @name: the attribute name
6427 * @value: the attribute value
6429 * Set (or reset) an attribute carried by a node.
6430 * The ns structure must be in scope, this is not checked
6432 * Returns the attribute pointer.
6435 xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6436 const xmlChar *value)
6440 if (ns && (ns->href == NULL))
6442 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6445 * Modify the attribute's value.
6447 if (prop->atype == XML_ATTRIBUTE_ID) {
6448 xmlRemoveID(node->doc, prop);
6449 prop->atype = XML_ATTRIBUTE_ID;
6451 if (prop->children != NULL)
6452 xmlFreeNodeList(prop->children);
6453 prop->children = NULL;
6456 if (value != NULL) {
6460 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6461 prop->children = xmlStringGetNodeList(node->doc, buffer);
6463 tmp = prop->children;
6464 while (tmp != NULL) {
6465 tmp->parent = (xmlNodePtr) prop;
6466 if (tmp->next == NULL)
6472 if (prop->atype == XML_ATTRIBUTE_ID)
6473 xmlAddID(NULL, node->doc, value, prop);
6477 * No equal attr found; create a new one.
6479 return(xmlNewPropInternal(node, ns, name, value, 0));
6482 #endif /* LIBXML_TREE_ENABLED */
6488 * Is this node a Text node ?
6489 * Returns 1 yes, 0 no
6492 xmlNodeIsText(xmlNodePtr node) {
6493 if (node == NULL) return(0);
6495 if (node->type == XML_TEXT_NODE) return(1);
6503 * Checks whether this node is an empty or whitespace only
6504 * (and possibly ignorable) text-node.
6506 * Returns 1 yes, 0 no
6509 xmlIsBlankNode(xmlNodePtr node) {
6511 if (node == NULL) return(0);
6513 if ((node->type != XML_TEXT_NODE) &&
6514 (node->type != XML_CDATA_SECTION_NODE))
6516 if (node->content == NULL) return(1);
6517 cur = node->content;
6519 if (!IS_BLANK_CH(*cur)) return(0);
6529 * @content: the content
6530 * @len: @content length
6532 * Concat the given string at the end of the existing node content
6534 * Returns -1 in case of error, 0 otherwise
6538 xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
6539 if (node == NULL) return(-1);
6541 if ((node->type != XML_TEXT_NODE) &&
6542 (node->type != XML_CDATA_SECTION_NODE) &&
6543 (node->type != XML_COMMENT_NODE) &&
6544 (node->type != XML_PI_NODE)) {
6546 xmlGenericError(xmlGenericErrorContext,
6547 "xmlTextConcat: node is not text nor CDATA\n");
6551 /* need to check if content is currently in the dictionary */
6552 if ((node->content == (xmlChar *) &(node->properties)) ||
6553 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6554 xmlDictOwns(node->doc->dict, node->content))) {
6555 node->content = xmlStrncatNew(node->content, content, len);
6557 node->content = xmlStrncat(node->content, content, len);
6559 node->properties = NULL;
6560 if (node->content == NULL)
6565 /************************************************************************
6567 * Output : to a FILE or in memory *
6569 ************************************************************************/
6574 * routine to create an XML buffer.
6575 * returns the new structure.
6578 xmlBufferCreate(void) {
6581 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6583 xmlTreeErrMemory("creating buffer");
6587 ret->size = xmlDefaultBufferSize;
6588 ret->alloc = xmlBufferAllocScheme;
6589 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
6590 if (ret->content == NULL) {
6591 xmlTreeErrMemory("creating buffer");
6595 ret->content[0] = 0;
6600 * xmlBufferCreateSize:
6601 * @size: initial size of buffer
6603 * routine to create an XML buffer.
6604 * returns the new structure.
6607 xmlBufferCreateSize(size_t size) {
6610 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6612 xmlTreeErrMemory("creating buffer");
6616 ret->alloc = xmlBufferAllocScheme;
6617 ret->size = (size ? size+2 : 0); /* +1 for ending null */
6619 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
6620 if (ret->content == NULL) {
6621 xmlTreeErrMemory("creating buffer");
6625 ret->content[0] = 0;
6627 ret->content = NULL;
6632 * xmlBufferCreateStatic:
6633 * @mem: the memory area
6634 * @size: the size in byte
6636 * routine to create an XML buffer from an immutable memory area.
6637 * The area won't be modified nor copied, and is expected to be
6638 * present until the end of the buffer lifetime.
6640 * returns the new structure.
6643 xmlBufferCreateStatic(void *mem, size_t size) {
6646 if ((mem == NULL) || (size == 0))
6649 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6651 xmlTreeErrMemory("creating buffer");
6656 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
6657 ret->content = (xmlChar *) mem;
6662 * xmlBufferSetAllocationScheme:
6663 * @buf: the buffer to tune
6664 * @scheme: allocation scheme to use
6666 * Sets the allocation scheme for this buffer
6669 xmlBufferSetAllocationScheme(xmlBufferPtr buf,
6670 xmlBufferAllocationScheme scheme) {
6673 xmlGenericError(xmlGenericErrorContext,
6674 "xmlBufferSetAllocationScheme: buf == NULL\n");
6678 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
6680 buf->alloc = scheme;
6685 * @buf: the buffer to free
6687 * Frees an XML buffer. It frees both the content and the structure which
6691 xmlBufferFree(xmlBufferPtr buf) {
6694 xmlGenericError(xmlGenericErrorContext,
6695 "xmlBufferFree: buf == NULL\n");
6700 if ((buf->content != NULL) &&
6701 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
6702 xmlFree(buf->content);
6714 xmlBufferEmpty(xmlBufferPtr buf) {
6715 if (buf == NULL) return;
6716 if (buf->content == NULL) return;
6718 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
6719 buf->content = BAD_CAST "";
6721 memset(buf->content, 0, buf->size);
6727 * @buf: the buffer to dump
6728 * @len: the number of xmlChar to remove
6730 * Remove the beginning of an XML buffer.
6732 * Returns the number of #xmlChar removed, or -1 in case of failure.
6735 xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
6736 if (buf == NULL) return(-1);
6737 if (len == 0) return(0);
6738 if (len > buf->use) return(-1);
6741 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
6742 buf->content += len;
6744 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
6745 buf->content[buf->use] = 0;
6753 * @len: the minimum free size to allocate
6755 * Grow the available space of an XML buffer.
6757 * Returns the new available space or -1 in case of error
6760 xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
6764 if (buf == NULL) return(-1);
6766 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
6767 if (len + buf->use < buf->size) return(0);
6770 * Windows has a BIG problem on realloc timing, so we try to double
6771 * the buffer size (if that's enough) (bug 146697)
6774 if (buf->size > len)
6775 size = buf->size * 2;
6777 size = buf->use + len + 100;
6779 size = buf->use + len + 100;
6782 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
6783 if (newbuf == NULL) {
6784 xmlTreeErrMemory("growing buffer");
6787 buf->content = newbuf;
6789 return(buf->size - buf->use);
6794 * @file: the file output
6795 * @buf: the buffer to dump
6797 * Dumps an XML buffer to a FILE *.
6798 * Returns the number of #xmlChar written
6801 xmlBufferDump(FILE *file, xmlBufferPtr buf) {
6806 xmlGenericError(xmlGenericErrorContext,
6807 "xmlBufferDump: buf == NULL\n");
6811 if (buf->content == NULL) {
6813 xmlGenericError(xmlGenericErrorContext,
6814 "xmlBufferDump: buf->content == NULL\n");
6820 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
6828 * Function to extract the content of a buffer
6830 * Returns the internal content
6834 xmlBufferContent(const xmlBufferPtr buf)
6839 return buf->content;
6846 * Function to get the length of a buffer
6848 * Returns the length of data in the internal content
6852 xmlBufferLength(const xmlBufferPtr buf)
6862 * @buf: the buffer to resize
6863 * @size: the desired size
6865 * Resize a buffer to accommodate minimum size of @size.
6867 * Returns 0 in case of problems, 1 otherwise
6870 xmlBufferResize(xmlBufferPtr buf, unsigned int size)
6872 unsigned int newSize;
6873 xmlChar* rebuf = NULL;
6878 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
6880 /* Don't resize if we don't have to */
6881 if (size < buf->size)
6884 /* figure out new size */
6885 switch (buf->alloc){
6886 case XML_BUFFER_ALLOC_DOUBLEIT:
6887 /*take care of empty case*/
6888 newSize = (buf->size ? buf->size*2 : size + 10);
6889 while (size > newSize) newSize *= 2;
6891 case XML_BUFFER_ALLOC_EXACT:
6899 if (buf->content == NULL)
6900 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
6901 else if (buf->size - buf->use < 100) {
6902 rebuf = (xmlChar *) xmlRealloc(buf->content,
6903 newSize * sizeof(xmlChar));
6906 * if we are reallocating a buffer far from being full, it's
6907 * better to make a new allocation and copy only the used range
6908 * and free the old one.
6910 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
6911 if (rebuf != NULL) {
6912 memcpy(rebuf, buf->content, buf->use);
6913 xmlFree(buf->content);
6914 rebuf[buf->use] = 0;
6917 if (rebuf == NULL) {
6918 xmlTreeErrMemory("growing buffer");
6921 buf->content = rebuf;
6922 buf->size = newSize;
6929 * @buf: the buffer to dump
6930 * @str: the #xmlChar string
6931 * @len: the number of #xmlChar to add
6933 * Add a string range to an XML buffer. if len == -1, the length of
6934 * str is recomputed.
6936 * Returns 0 successful, a positive error code number otherwise
6937 * and -1 in case of internal or API error.
6940 xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
6941 unsigned int needSize;
6943 if ((str == NULL) || (buf == NULL)) {
6946 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
6949 xmlGenericError(xmlGenericErrorContext,
6950 "xmlBufferAdd: len < 0\n");
6954 if (len == 0) return 0;
6957 len = xmlStrlen(str);
6959 if (len <= 0) return -1;
6961 needSize = buf->use + len + 2;
6962 if (needSize > buf->size){
6963 if (!xmlBufferResize(buf, needSize)){
6964 xmlTreeErrMemory("growing buffer");
6965 return XML_ERR_NO_MEMORY;
6969 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
6971 buf->content[buf->use] = 0;
6978 * @str: the #xmlChar string
6979 * @len: the number of #xmlChar to add
6981 * Add a string range to the beginning of an XML buffer.
6982 * if len == -1, the length of @str is recomputed.
6984 * Returns 0 successful, a positive error code number otherwise
6985 * and -1 in case of internal or API error.
6988 xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
6989 unsigned int needSize;
6993 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
6996 xmlGenericError(xmlGenericErrorContext,
6997 "xmlBufferAddHead: str == NULL\n");
7003 xmlGenericError(xmlGenericErrorContext,
7004 "xmlBufferAddHead: len < 0\n");
7008 if (len == 0) return 0;
7011 len = xmlStrlen(str);
7013 if (len <= 0) return -1;
7015 needSize = buf->use + len + 2;
7016 if (needSize > buf->size){
7017 if (!xmlBufferResize(buf, needSize)){
7018 xmlTreeErrMemory("growing buffer");
7019 return XML_ERR_NO_MEMORY;
7023 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
7024 memmove(&buf->content[0], str, len * sizeof(xmlChar));
7026 buf->content[buf->use] = 0;
7032 * @buf: the buffer to add to
7033 * @str: the #xmlChar string
7035 * Append a zero terminated string to an XML buffer.
7037 * Returns 0 successful, a positive error code number otherwise
7038 * and -1 in case of internal or API error.
7041 xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
7044 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7045 if (str == NULL) return -1;
7046 return xmlBufferAdd(buf, str, -1);
7051 * @buf: the buffer to dump
7052 * @str: the C char string
7054 * Append a zero terminated C string to an XML buffer.
7056 * Returns 0 successful, a positive error code number otherwise
7057 * and -1 in case of internal or API error.
7060 xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7065 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7068 xmlGenericError(xmlGenericErrorContext,
7069 "xmlBufferCCat: str == NULL\n");
7073 for (cur = str;*cur != 0;cur++) {
7074 if (buf->use + 10 >= buf->size) {
7075 if (!xmlBufferResize(buf, buf->use+10)){
7076 xmlTreeErrMemory("growing buffer");
7077 return XML_ERR_NO_MEMORY;
7080 buf->content[buf->use++] = *cur;
7082 buf->content[buf->use] = 0;
7087 * xmlBufferWriteCHAR:
7088 * @buf: the XML buffer
7089 * @string: the string to add
7091 * routine which manages and grows an output buffer. This one adds
7092 * xmlChars at the end of the buffer.
7095 xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
7098 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7099 xmlBufferCat(buf, string);
7103 * xmlBufferWriteChar:
7104 * @buf: the XML buffer output
7105 * @string: the string to add
7107 * routine which manage and grows an output buffer. This one add
7108 * C chars at the end of the array.
7111 xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
7114 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7115 xmlBufferCCat(buf, string);
7120 * xmlBufferWriteQuotedString:
7121 * @buf: the XML buffer output
7122 * @string: the string to add
7124 * routine which manage and grows an output buffer. This one writes
7125 * a quoted or double quoted #xmlChar string, checking first if it holds
7126 * quote or double-quotes internally
7129 xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
7130 const xmlChar *cur, *base;
7133 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7134 if (xmlStrchr(string, '\"')) {
7135 if (xmlStrchr(string, '\'')) {
7137 xmlGenericError(xmlGenericErrorContext,
7138 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7140 xmlBufferCCat(buf, "\"");
7141 base = cur = string;
7145 xmlBufferAdd(buf, base, cur - base);
7146 xmlBufferAdd(buf, BAD_CAST """, 6);
7155 xmlBufferAdd(buf, base, cur - base);
7156 xmlBufferCCat(buf, "\"");
7159 xmlBufferCCat(buf, "\'");
7160 xmlBufferCat(buf, string);
7161 xmlBufferCCat(buf, "\'");
7164 xmlBufferCCat(buf, "\"");
7165 xmlBufferCat(buf, string);
7166 xmlBufferCCat(buf, "\"");
7172 * xmlGetDocCompressMode:
7173 * @doc: the document
7175 * get the compression ratio for a document, ZLIB based
7176 * Returns 0 (uncompressed) to 9 (max compression)
7179 xmlGetDocCompressMode (xmlDocPtr doc) {
7180 if (doc == NULL) return(-1);
7181 return(doc->compression);
7185 * xmlSetDocCompressMode:
7186 * @doc: the document
7187 * @mode: the compression ratio
7189 * set the compression ratio for a document, ZLIB based
7190 * Correct values: 0 (uncompressed) to 9 (max compression)
7193 xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7194 if (doc == NULL) return;
7195 if (mode < 0) doc->compression = 0;
7196 else if (mode > 9) doc->compression = 9;
7197 else doc->compression = mode;
7201 * xmlGetCompressMode:
7203 * get the default compression mode used, ZLIB based.
7204 * Returns 0 (uncompressed) to 9 (max compression)
7207 xmlGetCompressMode(void)
7209 return (xmlCompressMode);
7213 * xmlSetCompressMode:
7214 * @mode: the compression ratio
7216 * set the default compression mode used, ZLIB based
7217 * Correct values: 0 (uncompressed) to 9 (max compression)
7220 xmlSetCompressMode(int mode) {
7221 if (mode < 0) xmlCompressMode = 0;
7222 else if (mode > 9) xmlCompressMode = 9;
7223 else xmlCompressMode = mode;
7226 #define XML_TREE_NSMAP_PARENT -1
7227 #define XML_TREE_NSMAP_XML -2
7228 #define XML_TREE_NSMAP_DOC -3
7229 #define XML_TREE_NSMAP_CUSTOM -4
7231 typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7232 struct xmlNsMapItem {
7233 xmlNsMapItemPtr next;
7234 xmlNsMapItemPtr prev;
7235 xmlNsPtr oldNs; /* old ns decl reference */
7236 xmlNsPtr newNs; /* new ns decl reference */
7237 int shadowDepth; /* Shadowed at this depth */
7240 * >= 0 == @node's ns-decls
7241 * -1 == @parent's ns-decls
7242 * -2 == the doc->oldNs XML ns-decl
7243 * -3 == the doc->oldNs storage ns-decls
7244 * -4 == ns-decls provided via custom ns-handling
7249 typedef struct xmlNsMap *xmlNsMapPtr;
7251 xmlNsMapItemPtr first;
7252 xmlNsMapItemPtr last;
7253 xmlNsMapItemPtr pool;
7256 #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7257 #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7258 #define XML_NSMAP_POP(m, i) \
7260 (m)->last = (i)->prev; \
7261 if ((m)->last == NULL) \
7262 (m)->first = NULL; \
7264 (m)->last->next = NULL; \
7265 (i)->next = (m)->pool; \
7269 * xmlDOMWrapNsMapFree:
7275 xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7277 xmlNsMapItemPtr cur, tmp;
7282 while (cur != NULL) {
7288 while (cur != NULL) {
7297 * xmlDOMWrapNsMapAddItem:
7299 * @oldNs: the old ns-struct
7300 * @newNs: the new ns-struct
7301 * @depth: depth and ns-kind information
7303 * Adds an ns-mapping item.
7305 static xmlNsMapItemPtr
7306 xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
7307 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
7309 xmlNsMapItemPtr ret;
7314 if ((position != -1) && (position != 0))
7320 * Create the ns-map.
7322 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7324 xmlTreeErrMemory("allocating namespace map");
7327 memset(map, 0, sizeof(struct xmlNsMap));
7331 if (map->pool != NULL) {
7333 * Reuse an item from the pool.
7336 map->pool = ret->next;
7337 memset(ret, 0, sizeof(struct xmlNsMapItem));
7340 * Create a new item.
7342 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7344 xmlTreeErrMemory("allocating namespace map item");
7347 memset(ret, 0, sizeof(struct xmlNsMapItem));
7350 if (map->first == NULL) {
7356 } else if (position == -1) {
7360 ret->prev = map->last;
7361 map->last->next = ret;
7363 } else if (position == 0) {
7365 * Set on first position.
7367 map->first->prev = ret;
7368 ret->next = map->first;
7375 ret->shadowDepth = -1;
7381 * xmlDOMWrapStoreNs:
7383 * @nsName: the namespace name
7384 * @prefix: the prefix
7386 * Creates or reuses an xmlNs struct on doc->oldNs with
7387 * the given prefix and namespace name.
7389 * Returns the aquired ns struct or NULL in case of an API
7390 * or internal error.
7393 xmlDOMWrapStoreNs(xmlDocPtr doc,
7394 const xmlChar *nsName,
7395 const xmlChar *prefix)
7401 ns = xmlTreeEnsureXMLDecl(doc);
7404 if (ns->next != NULL) {
7407 while (ns != NULL) {
7408 if (((ns->prefix == prefix) ||
7409 xmlStrEqual(ns->prefix, prefix)) &&
7410 xmlStrEqual(ns->href, nsName)) {
7413 if (ns->next == NULL)
7419 ns->next = xmlNewNs(NULL, nsName, prefix);
7424 * xmlDOMWrapNewCtxt:
7426 * Allocates and initializes a new DOM-wrapper context.
7428 * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror.
7431 xmlDOMWrapNewCtxt(void)
7433 xmlDOMWrapCtxtPtr ret;
7435 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
7437 xmlTreeErrMemory("allocating DOM-wrapper context");
7440 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
7445 * xmlDOMWrapFreeCtxt:
7446 * @ctxt: the DOM-wrapper context
7448 * Frees the DOM-wrapper context.
7451 xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
7455 if (ctxt->namespaceMap != NULL)
7456 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
7458 * TODO: Store the namespace map in the context.
7464 * xmlTreeLookupNsListByPrefix:
7465 * @nsList: a list of ns-structs
7466 * @prefix: the searched prefix
7468 * Searches for a ns-decl with the given prefix in @nsList.
7470 * Returns the ns-decl if found, NULL if not found and on
7474 xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
7482 if ((prefix == ns->prefix) ||
7483 xmlStrEqual(prefix, ns->prefix)) {
7487 } while (ns != NULL);
7494 * xmlDOMWrapNSNormGatherInScopeNs:
7495 * @map: the namespace map
7496 * @node: the node to start with
7498 * Puts in-scope namespaces into the ns-map.
7500 * Returns 0 on success, -1 on API or internal errors.
7503 xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
7511 if ((map == NULL) || (*map != NULL))
7514 * Get in-scope ns-decls of @parent.
7517 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
7518 if (cur->type == XML_ELEMENT_NODE) {
7519 if (cur->nsDef != NULL) {
7523 if (XML_NSMAP_NOTEMPTY(*map)) {
7525 * Skip shadowed prefixes.
7527 XML_NSMAP_FOREACH(*map, mi) {
7528 if ((ns->prefix == mi->newNs->prefix) ||
7529 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
7538 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
7539 ns, XML_TREE_NSMAP_PARENT);
7543 mi->shadowDepth = 0;
7545 } while (ns != NULL);
7554 * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
7555 * otherwise copy it, when it was in the source-dict.
7557 #define XML_TREE_ADOPT_STR(str) \
7558 if (adoptStr && (str != NULL)) { \
7559 if (destDoc->dict) { \
7560 const xmlChar *old = str; \
7561 str = xmlDictLookup(destDoc->dict, str, -1); \
7562 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
7563 (!xmlDictOwns(sourceDoc->dict, old))) \
7564 xmlFree((char *)old); \
7565 } else if ((sourceDoc) && (sourceDoc->dict) && \
7566 xmlDictOwns(sourceDoc->dict, str)) { \
7567 str = BAD_CAST xmlStrdup(str); \
7572 * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
7573 * put it in dest-dict or copy it.
7575 #define XML_TREE_ADOPT_STR_2(str) \
7576 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
7577 (sourceDoc->dict != NULL) && \
7578 xmlDictOwns(sourceDoc->dict, cur->content)) { \
7579 if (destDoc->dict) \
7580 cur->content = (xmlChar *) \
7581 xmlDictLookup(destDoc->dict, cur->content, -1); \
7583 cur->content = xmlStrdup(BAD_CAST cur->content); \
7587 * xmlDOMWrapNSNormAddNsMapItem2:
7589 * For internal use. Adds a ns-decl mapping.
7591 * Returns 0 on success, -1 on internal errors.
7594 xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
7595 xmlNsPtr oldNs, xmlNsPtr newNs)
7597 if (*list == NULL) {
7598 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
7599 if (*list == NULL) {
7600 xmlTreeErrMemory("alloc ns map item");
7605 } else if ((*number) >= (*size)) {
7607 *list = (xmlNsPtr *) xmlRealloc(*list,
7608 (*size) * 2 * sizeof(xmlNsPtr));
7609 if (*list == NULL) {
7610 xmlTreeErrMemory("realloc ns map item");
7614 (*list)[2 * (*number)] = oldNs;
7615 (*list)[2 * (*number) +1] = newNs;
7621 * xmlDOMWrapRemoveNode:
7622 * @ctxt: a DOM wrapper context
7624 * @node: the node to be removed.
7625 * @options: set of options, unused at the moment
7627 * Unlinks the given node from its owner.
7628 * This will substitute ns-references to node->nsDef for
7629 * ns-references to doc->oldNs, thus ensuring the removed
7630 * branch to be autark wrt ns-references.
7632 * NOTE: This function was not intensively tested.
7634 * Returns 0 on success, 1 if the node is not supported,
7635 * -1 on API and internal errors.
7638 xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
7639 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
7641 xmlNsPtr *list = NULL;
7642 int sizeList, nbList, i, j;
7645 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
7648 /* TODO: 0 or -1 ? */
7649 if (node->parent == NULL)
7652 switch (node->type) {
7654 case XML_CDATA_SECTION_NODE:
7655 case XML_ENTITY_REF_NODE:
7657 case XML_COMMENT_NODE:
7658 xmlUnlinkNode(node);
7660 case XML_ELEMENT_NODE:
7661 case XML_ATTRIBUTE_NODE:
7666 xmlUnlinkNode(node);
7668 * Save out-of-scope ns-references in doc->oldNs.
7671 switch (node->type) {
7672 case XML_ELEMENT_NODE:
7673 if ((ctxt == NULL) && (node->nsDef != NULL)) {
7676 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7677 &nbList, ns, ns) == -1)
7678 goto internal_error;
7680 } while (ns != NULL);
7682 /* No break on purpose. */
7683 case XML_ATTRIBUTE_NODE:
7684 if (node->ns != NULL) {
7689 for (i = 0, j = 0; i < nbList; i++, j += 2) {
7690 if (node->ns == list[j]) {
7691 node->ns = list[++j];
7703 * Add to doc's oldNs.
7705 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
7708 goto internal_error;
7714 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7715 &nbList, node->ns, ns) == -1)
7716 goto internal_error;
7720 if ((node->type == XML_ELEMENT_NODE) &&
7721 (node->properties != NULL)) {
7722 node = (xmlNodePtr) node->properties;
7730 if ((node->type == XML_ELEMENT_NODE) &&
7731 (node->children != NULL)) {
7732 node = node->children;
7738 if (node->next != NULL)
7741 node = node->parent;
7744 } while (node != NULL);
7757 * xmlSearchNsByNamespaceStrict:
7758 * @doc: the document
7759 * @node: the start node
7760 * @nsName: the searched namespace name
7761 * @retNs: the resulting ns-decl
7762 * @prefixed: if the found ns-decl must have a prefix (for attributes)
7764 * Dynamically searches for a ns-declaration which matches
7765 * the given @nsName in the ancestor-or-self axis of @node.
7767 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
7768 * and internal errors.
7771 xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
7772 const xmlChar* nsName,
7773 xmlNsPtr *retNs, int prefixed)
7775 xmlNodePtr cur, prev = NULL, out = NULL;
7776 xmlNsPtr ns, prevns;
7778 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
7782 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
7783 *retNs = xmlTreeEnsureXMLDecl(doc);
7790 if (cur->type == XML_ELEMENT_NODE) {
7791 if (cur->nsDef != NULL) {
7792 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
7793 if (prefixed && (ns->prefix == NULL))
7797 * Check the last level of ns-decls for a
7800 prevns = prev->nsDef;
7802 if ((prevns->prefix == ns->prefix) ||
7803 ((prevns->prefix != NULL) &&
7804 (ns->prefix != NULL) &&
7805 xmlStrEqual(prevns->prefix, ns->prefix))) {
7811 prevns = prevns->next;
7812 } while (prevns != NULL);
7817 * Ns-name comparison.
7819 if ((nsName == ns->href) ||
7820 xmlStrEqual(nsName, ns->href)) {
7822 * At this point the prefix can only be shadowed,
7823 * if we are the the (at least) 3rd level of
7829 ret = xmlNsInScope(doc, node, prev, ns->prefix);
7833 * TODO: Should we try to find a matching ns-name
7834 * only once? This here keeps on searching.
7835 * I think we should try further since, there might
7836 * be an other matching ns-decl with an unshadowed
7849 } else if ((cur->type == XML_ENTITY_NODE) ||
7850 (cur->type == XML_ENTITY_DECL))
7853 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
7858 * xmlSearchNsByPrefixStrict:
7859 * @doc: the document
7860 * @node: the start node
7861 * @prefix: the searched namespace prefix
7862 * @retNs: the resulting ns-decl
7864 * Dynamically searches for a ns-declaration which matches
7865 * the given @nsName in the ancestor-or-self axis of @node.
7867 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
7868 * and internal errors.
7871 xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
7872 const xmlChar* prefix,
7878 if ((doc == NULL) || (node == NULL))
7883 if (IS_STR_XML(prefix)) {
7885 *retNs = xmlTreeEnsureXMLDecl(doc);
7893 if (cur->type == XML_ELEMENT_NODE) {
7894 if (cur->nsDef != NULL) {
7897 if ((prefix == ns->prefix) ||
7898 xmlStrEqual(prefix, ns->prefix))
7901 * Disabled namespaces, e.g. xmlns:abc="".
7903 if (ns->href == NULL)
7910 } while (ns != NULL);
7912 } else if ((cur->type == XML_ENTITY_NODE) ||
7913 (cur->type == XML_ENTITY_DECL))
7916 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
7921 * xmlDOMWrapNSNormDeclareNsForced:
7923 * @elem: the element-node to declare on
7924 * @nsName: the namespace-name of the ns-decl
7925 * @prefix: the preferred prefix of the ns-decl
7926 * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
7928 * Declares a new namespace on @elem. It tries to use the
7929 * given @prefix; if a ns-decl with the given prefix is already existent
7930 * on @elem, it will generate an other prefix.
7932 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
7933 * and internal errors.
7936 xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
7938 const xmlChar *nsName,
7939 const xmlChar *prefix,
7945 const xmlChar *pref;
7948 * Create a ns-decl on @anchor.
7953 * Lookup whether the prefix is unused in elem's ns-decls.
7955 if ((elem->nsDef != NULL) &&
7956 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
7957 goto ns_next_prefix;
7958 if (checkShadow && elem->parent &&
7959 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
7961 * Does it shadow ancestor ns-decls?
7963 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
7964 goto ns_next_prefix;
7966 ret = xmlNewNs(NULL, nsName, pref);
7969 if (elem->nsDef == NULL)
7972 xmlNsPtr ns2 = elem->nsDef;
7973 while (ns2->next != NULL)
7982 if (prefix == NULL) {
7983 snprintf((char *) buf, sizeof(buf),
7986 snprintf((char *) buf, sizeof(buf),
7987 "%.30s_%d", (char *)prefix, counter);
7988 pref = BAD_CAST buf;
7993 * xmlDOMWrapNSNormAquireNormalizedNs:
7995 * @elem: the element-node to declare namespaces on
7996 * @ns: the ns-struct to use for the search
7997 * @retNs: the found/created ns-struct
7998 * @nsMap: the ns-map
7999 * @depth: the current tree depth
8000 * @ancestorsOnly: search in ancestor ns-decls only
8001 * @prefixed: if the searched ns-decl must have a prefix (for attributes)
8003 * Searches for a matching ns-name in the ns-decls of @nsMap, if not
8004 * found it will either declare it on @elem, or store it in doc->oldNs.
8005 * If a new ns-decl needs to be declared on @elem, it tries to use the
8006 * @ns->prefix for it, if this prefix is already in use on @elem, it will
8007 * change the prefix or the new ns-decl.
8009 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8012 xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8024 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
8030 * Handle XML namespace.
8032 if (IS_STR_XML(ns->prefix)) {
8034 * Insert XML namespace mapping.
8036 *retNs = xmlTreeEnsureXMLDecl(doc);
8042 * If the search should be done in ancestors only and no
8043 * @elem (the first ancestor) was specified, then skip the search.
8045 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8046 (! (ancestorsOnly && (elem == NULL))))
8049 * Try to find an equal ns-name in in-scope ns-decls.
8051 XML_NSMAP_FOREACH(*nsMap, mi) {
8052 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8054 * ancestorsOnly: This should be turned on to gain speed,
8055 * if one knows that the branch itself was already
8056 * ns-wellformed and no stale references existed.
8057 * I.e. it searches in the ancestor axis only.
8059 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8060 /* Skip shadowed prefixes. */
8061 (mi->shadowDepth == -1) &&
8062 /* Skip xmlns="" or xmlns:foo="". */
8063 ((mi->newNs->href != NULL) &&
8064 (mi->newNs->href[0] != 0)) &&
8065 /* Ensure a prefix if wanted. */
8066 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8068 ((mi->newNs->href == ns->href) ||
8069 xmlStrEqual(mi->newNs->href, ns->href))) {
8070 /* Set the mapping. */
8078 * No luck, the namespace is out of scope or shadowed.
8084 * Store ns-decls in "oldNs" of the document-node.
8086 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8092 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
8093 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8101 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8106 if (*nsMap != NULL) {
8108 * Does it shadow ancestor ns-decls?
8110 XML_NSMAP_FOREACH(*nsMap, mi) {
8111 if ((mi->depth < depth) &&
8112 (mi->shadowDepth == -1) &&
8113 ((ns->prefix == mi->newNs->prefix) ||
8114 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8118 mi->shadowDepth = depth;
8123 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
8133 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
8134 } xmlDOMReconcileNSOptions;
8137 * xmlDOMWrapReconcileNamespaces:
8138 * @ctxt: DOM wrapper context, unused at the moment
8139 * @elem: the element-node
8140 * @options: option flags
8142 * Ensures that ns-references point to ns-decls hold on element-nodes.
8143 * Ensures that the tree is namespace wellformed by creating additional
8144 * ns-decls where needed. Note that, since prefixes of already existent
8145 * ns-decls can be shadowed by this process, it could break QNames in
8146 * attribute values or element content.
8148 * NOTE: This function was not intensively tested.
8150 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8154 xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8158 int depth = -1, adoptns = 0, parnsdone = 0;
8159 xmlNsPtr ns, prevns;
8161 xmlNodePtr cur, curElem = NULL;
8162 xmlNsMapPtr nsMap = NULL;
8163 xmlNsMapItemPtr /* topmi = NULL, */ mi;
8164 /* @ancestorsOnly should be set by an option flag. */
8165 int ancestorsOnly = 0;
8166 int optRemoveRedundantNS =
8167 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8168 xmlNsPtr *listRedund = NULL;
8169 int sizeRedund = 0, nbRedund = 0, ret, i, j;
8171 if ((elem == NULL) || (elem->doc == NULL) ||
8172 (elem->type != XML_ELEMENT_NODE))
8178 switch (cur->type) {
8179 case XML_ELEMENT_NODE:
8184 * Namespace declarations.
8186 if (cur->nsDef != NULL) {
8189 while (ns != NULL) {
8191 if ((elem->parent) &&
8192 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8194 * Gather ancestor in-scope ns-decls.
8196 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8197 elem->parent) == -1)
8198 goto internal_error;
8204 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8206 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
8207 XML_NSMAP_FOREACH(nsMap, mi) {
8208 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8209 (mi->shadowDepth == -1) &&
8210 ((ns->prefix == mi->newNs->prefix) ||
8211 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8212 ((ns->href == mi->newNs->href) ||
8213 xmlStrEqual(ns->href, mi->newNs->href)))
8216 * A redundant ns-decl was found.
8217 * Add it to the list of redundant ns-decls.
8219 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8220 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8221 goto internal_error;
8223 * Remove the ns-decl from the element-node.
8226 prevns->next = ns->next;
8228 cur->nsDef = ns->next;
8235 * Skip ns-references handling if the referenced
8236 * ns-decl is declared on the same element.
8238 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
8241 * Does it shadow any ns-decl?
8243 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8244 XML_NSMAP_FOREACH(nsMap, mi) {
8245 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8246 (mi->shadowDepth == -1) &&
8247 ((ns->prefix == mi->newNs->prefix) ||
8248 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8250 mi->shadowDepth = depth;
8257 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
8259 goto internal_error;
8268 /* No break on purpose. */
8269 case XML_ATTRIBUTE_NODE:
8270 /* No ns, no fun. */
8271 if (cur->ns == NULL)
8275 if ((elem->parent) &&
8276 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8277 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8278 elem->parent) == -1)
8279 goto internal_error;
8284 * Adjust the reference if this was a redundant ns-decl.
8287 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8288 if (cur->ns == listRedund[j]) {
8289 cur->ns = listRedund[++j];
8295 * Adopt ns-references.
8297 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8299 * Search for a mapping.
8301 XML_NSMAP_FOREACH(nsMap, mi) {
8302 if ((mi->shadowDepth == -1) &&
8303 (cur->ns == mi->oldNs)) {
8305 cur->ns = mi->newNs;
8311 * Aquire a normalized ns-decl and add it to the map.
8313 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8317 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8318 goto internal_error;
8322 if ((cur->type == XML_ELEMENT_NODE) &&
8323 (cur->properties != NULL)) {
8325 * Process attributes.
8327 cur = (xmlNodePtr) cur->properties;
8335 if ((cur->type == XML_ELEMENT_NODE) &&
8336 (cur->children != NULL)) {
8338 * Process content of element-nodes only.
8340 cur = cur->children;
8346 if (cur->type == XML_ELEMENT_NODE) {
8347 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8351 while ((nsMap->last != NULL) &&
8352 (nsMap->last->depth >= depth))
8354 XML_NSMAP_POP(nsMap, mi)
8359 XML_NSMAP_FOREACH(nsMap, mi) {
8360 if (mi->shadowDepth >= depth)
8361 mi->shadowDepth = -1;
8366 if (cur->next != NULL)
8369 if (cur->type == XML_ATTRIBUTE_NODE) {
8376 } while (cur != NULL);
8384 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8385 xmlFreeNs(listRedund[j]);
8387 xmlFree(listRedund);
8390 xmlDOMWrapNsMapFree(nsMap);
8395 * xmlDOMWrapAdoptBranch:
8396 * @ctxt: the optional context for custom processing
8397 * @sourceDoc: the optional sourceDoc
8398 * @node: the element-node to start with
8399 * @destDoc: the destination doc for adoption
8400 * @destParent: the optional new parent of @node in @destDoc
8401 * @options: option flags
8403 * Ensures that ns-references point to @destDoc: either to
8404 * elements->nsDef entries if @destParent is given, or to
8405 * @destDoc->oldNs otherwise.
8406 * If @destParent is given, it ensures that the tree is namespace
8407 * wellformed by creating additional ns-decls where needed.
8408 * Note that, since prefixes of already existent ns-decls can be
8409 * shadowed by this process, it could break QNames in attribute
8410 * values or element content.
8412 * NOTE: This function was not intensively tested.
8414 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8417 xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8418 xmlDocPtr sourceDoc,
8421 xmlNodePtr destParent,
8422 int options ATTRIBUTE_UNUSED)
8425 xmlNodePtr cur, curElem = NULL;
8426 xmlNsMapPtr nsMap = NULL;
8429 int depth = -1, adoptStr = 1;
8430 /* gather @parent's ns-decls. */
8432 /* @ancestorsOnly should be set per option. */
8433 int ancestorsOnly = 0;
8436 * Optimize string adoption for equal or none dicts.
8438 if ((sourceDoc != NULL) &&
8439 (sourceDoc->dict == destDoc->dict))
8445 * Get the ns-map from the context if available.
8448 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8450 * Disable search for ns-decls in the parent-axis of the
8451 * desination element, if:
8452 * 1) there's no destination parent
8453 * 2) custom ns-reference handling is used
8455 if ((destParent == NULL) ||
8456 (ctxt && ctxt->getNsForNodeFunc))
8463 while (cur != NULL) {
8465 * Paranoid source-doc sanity check.
8467 if (cur->doc != sourceDoc) {
8469 * We'll assume XIncluded nodes if the doc differs.
8470 * TODO: Do we need to reconciliate XIncluded nodes?
8471 * This here skips XIncluded nodes and tries to handle
8474 if (cur->next == NULL)
8478 if ((cur->type == XML_XINCLUDE_END) ||
8479 (cur->doc == node->doc))
8481 } while (cur->next != NULL);
8483 if (cur->doc != node->doc)
8487 switch (cur->type) {
8488 case XML_XINCLUDE_START:
8489 case XML_XINCLUDE_END:
8494 case XML_ELEMENT_NODE:
8498 * Namespace declarations.
8499 * - ns->href and ns->prefix are never in the dict, so
8500 * we need not move the values over to the destination dict.
8501 * - Note that for custom handling of ns-references,
8502 * the ns-decls need not be stored in the ns-map,
8503 * since they won't be referenced by node->ns.
8506 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
8510 * Gather @parent's in-scope ns-decls.
8512 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8514 goto internal_error;
8517 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8519 * NOTE: ns->prefix and ns->href are never in the dict.
8520 * XML_TREE_ADOPT_STR(ns->prefix)
8521 * XML_TREE_ADOPT_STR(ns->href)
8524 * Does it shadow any ns-decl?
8526 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8527 XML_NSMAP_FOREACH(nsMap, mi) {
8528 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8529 (mi->shadowDepth == -1) &&
8530 ((ns->prefix == mi->newNs->prefix) ||
8531 xmlStrEqual(ns->prefix,
8532 mi->newNs->prefix))) {
8534 mi->shadowDepth = depth;
8541 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
8542 ns, ns, depth) == NULL)
8543 goto internal_error;
8546 /* No break on purpose. */
8547 case XML_ATTRIBUTE_NODE:
8548 /* No namespace, no fun. */
8549 if (cur->ns == NULL)
8553 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8555 goto internal_error;
8559 * Adopt ns-references.
8561 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8563 * Search for a mapping.
8565 XML_NSMAP_FOREACH(nsMap, mi) {
8566 if ((mi->shadowDepth == -1) &&
8567 (cur->ns == mi->oldNs)) {
8569 cur->ns = mi->newNs;
8575 * No matching namespace in scope. We need a new one.
8577 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
8579 * User-defined behaviour.
8581 ns = ctxt->getNsForNodeFunc(ctxt, cur,
8582 cur->ns->href, cur->ns->prefix);
8584 * Insert mapping if ns is available; it's the users fault
8587 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
8588 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
8589 goto internal_error;
8593 * Aquire a normalized ns-decl and add it to the map.
8595 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
8596 /* ns-decls on curElem or on destDoc->oldNs */
8597 destParent ? curElem : NULL,
8601 /* ns-decls must be prefixed for attributes. */
8602 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8603 goto internal_error;
8608 * Further node properties.
8609 * TODO: Is this all?
8611 XML_TREE_ADOPT_STR(cur->name)
8612 if (cur->type == XML_ELEMENT_NODE) {
8619 if (cur->properties != NULL) {
8621 * Process first attribute node.
8623 cur = (xmlNodePtr) cur->properties;
8630 if ((sourceDoc != NULL) &&
8631 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
8633 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
8635 ((xmlAttrPtr) cur)->atype = 0;
8636 ((xmlAttrPtr) cur)->psvi = NULL;
8640 case XML_CDATA_SECTION_NODE:
8642 * This puts the content in the dest dict, only if
8643 * it was previously in the source dict.
8645 XML_TREE_ADOPT_STR_2(cur->content)
8647 case XML_ENTITY_REF_NODE:
8649 * Remove reference to the entitity-node.
8651 cur->content = NULL;
8652 cur->children = NULL;
8654 if ((destDoc->intSubset) || (destDoc->extSubset)) {
8657 * Assign new entity-node if available.
8659 ent = xmlGetDocEntity(destDoc, cur->name);
8661 cur->content = ent->content;
8662 cur->children = (xmlNodePtr) ent;
8663 cur->last = (xmlNodePtr) ent;
8668 XML_TREE_ADOPT_STR(cur->name)
8669 XML_TREE_ADOPT_STR_2(cur->content)
8671 case XML_COMMENT_NODE:
8674 goto internal_error;
8679 if (cur->children != NULL) {
8680 cur = cur->children;
8687 if ((cur->type == XML_ELEMENT_NODE) ||
8688 (cur->type == XML_XINCLUDE_START) ||
8689 (cur->type == XML_XINCLUDE_END))
8692 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
8694 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8698 while ((nsMap->last != NULL) &&
8699 (nsMap->last->depth >= depth))
8701 XML_NSMAP_POP(nsMap, mi)
8706 XML_NSMAP_FOREACH(nsMap, mi) {
8707 if (mi->shadowDepth >= depth)
8708 mi->shadowDepth = -1;
8713 if (cur->next != NULL)
8715 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
8716 (cur->parent->children != NULL))
8718 cur = cur->parent->children;
8734 if (nsMap != NULL) {
8735 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
8737 * Just cleanup the map but don't free.
8741 nsMap->last->next = nsMap->pool;
8742 nsMap->pool = nsMap->first;
8743 nsMap->first = NULL;
8746 xmlDOMWrapNsMapFree(nsMap);
8752 * xmlDOMWrapCloneNode:
8753 * @ctxt: the optional context for custom processing
8754 * @sourceDoc: the optional sourceDoc
8755 * @node: the node to start with
8756 * @resNode: the clone of the given @node
8757 * @destDoc: the destination doc
8758 * @destParent: the optional new parent of @node in @destDoc
8759 * @deep: descend into child if set
8760 * @options: option flags
8762 * References of out-of scope ns-decls are remapped to point to @destDoc:
8763 * 1) If @destParent is given, then nsDef entries on element-nodes are used
8764 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
8765 * This is the case when you don't know already where the cloned branch
8768 * If @destParent is given, it ensures that the tree is namespace
8769 * wellformed by creating additional ns-decls where needed.
8770 * Note that, since prefixes of already existent ns-decls can be
8771 * shadowed by this process, it could break QNames in attribute
8772 * values or element content.
8774 * 1) What to do with XInclude? Currently this returns an error for XInclude.
8776 * Returns 0 if the operation succeeded,
8777 * 1 if a node of unsupported (or not yet supported) type was given,
8778 * -1 on API/internal errors.
8782 xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
8783 xmlDocPtr sourceDoc,
8785 xmlNodePtr *resNode,
8787 xmlNodePtr destParent,
8789 int options ATTRIBUTE_UNUSED)
8792 xmlNodePtr cur, curElem = NULL;
8793 xmlNsMapPtr nsMap = NULL;
8797 /* int adoptStr = 1; */
8798 /* gather @parent's ns-decls. */
8802 * TODO: @ancestorsOnly should be set per option.
8805 int ancestorsOnly = 0;
8806 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
8807 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
8808 xmlDictPtr dict; /* The destination dict */
8810 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
8813 * TODO: Initially we support only element-nodes.
8815 if (node->type != XML_ELEMENT_NODE)
8818 * Check node->doc sanity.
8820 if ((node->doc != NULL) && (sourceDoc != NULL) &&
8821 (node->doc != sourceDoc)) {
8823 * Might be an XIncluded node.
8827 if (sourceDoc == NULL)
8828 sourceDoc = node->doc;
8829 if (sourceDoc == NULL)
8832 dict = destDoc->dict;
8834 * Reuse the namespace map of the context.
8837 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8842 while (cur != NULL) {
8843 if (cur->doc != sourceDoc) {
8845 * We'll assume XIncluded nodes if the doc differs.
8846 * TODO: Do we need to reconciliate XIncluded nodes?
8847 * TODO: This here returns -1 in this case.
8849 goto internal_error;
8852 * Create a new node.
8854 switch (cur->type) {
8855 case XML_XINCLUDE_START:
8856 case XML_XINCLUDE_END:
8858 * TODO: What to do with XInclude?
8860 goto internal_error;
8862 case XML_ELEMENT_NODE:
8864 case XML_CDATA_SECTION_NODE:
8865 case XML_COMMENT_NODE:
8867 case XML_DOCUMENT_FRAG_NODE:
8868 case XML_ENTITY_REF_NODE:
8869 case XML_ENTITY_NODE:
8871 * Nodes of xmlNode structure.
8873 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
8874 if (clone == NULL) {
8875 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
8876 goto internal_error;
8878 memset(clone, 0, sizeof(xmlNode));
8880 * Set hierachical links.
8882 if (resultClone != NULL) {
8883 clone->parent = parentClone;
8885 prevClone->next = clone;
8886 clone->prev = prevClone;
8888 parentClone->children = clone;
8890 resultClone = clone;
8893 case XML_ATTRIBUTE_NODE:
8895 * Attributes (xmlAttr).
8897 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
8898 if (clone == NULL) {
8899 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
8900 goto internal_error;
8902 memset(clone, 0, sizeof(xmlAttr));
8904 * Set hierachical links.
8905 * TODO: Change this to add to the end of attributes.
8907 if (resultClone != NULL) {
8908 clone->parent = parentClone;
8910 prevClone->next = clone;
8911 clone->prev = prevClone;
8913 parentClone->properties = (xmlAttrPtr) clone;
8915 resultClone = clone;
8919 * TODO QUESTION: Any other nodes expected?
8921 goto internal_error;
8924 clone->type = cur->type;
8925 clone->doc = destDoc;
8928 * Clone the name of the node if any.
8930 if (cur->name == xmlStringText)
8931 clone->name = xmlStringText;
8932 else if (cur->name == xmlStringTextNoenc)
8934 * NOTE: Although xmlStringTextNoenc is never assigned to a node
8935 * in tree.c, it might be set in Libxslt via
8936 * "xsl:disable-output-escaping".
8938 clone->name = xmlStringTextNoenc;
8939 else if (cur->name == xmlStringComment)
8940 clone->name = xmlStringComment;
8941 else if (cur->name != NULL) {
8942 DICT_CONST_COPY(cur->name, clone->name);
8945 switch (cur->type) {
8946 case XML_XINCLUDE_START:
8947 case XML_XINCLUDE_END:
8952 case XML_ELEMENT_NODE:
8956 * Namespace declarations.
8958 if (cur->nsDef != NULL) {
8960 if (destParent && (ctxt == NULL)) {
8962 * Gather @parent's in-scope ns-decls.
8964 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8966 goto internal_error;
8971 * Clone namespace declarations.
8973 cloneNsDefSlot = &(clone->nsDef);
8974 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8976 * Create a new xmlNs.
8978 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
8979 if (cloneNs == NULL) {
8980 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
8981 "allocating namespace");
8984 memset(cloneNs, 0, sizeof(xmlNs));
8985 cloneNs->type = XML_LOCAL_NAMESPACE;
8987 if (ns->href != NULL)
8988 cloneNs->href = xmlStrdup(ns->href);
8989 if (ns->prefix != NULL)
8990 cloneNs->prefix = xmlStrdup(ns->prefix);
8992 *cloneNsDefSlot = cloneNs;
8993 cloneNsDefSlot = &(cloneNs->next);
8996 * Note that for custom handling of ns-references,
8997 * the ns-decls need not be stored in the ns-map,
8998 * since they won't be referenced by node->ns.
9000 if ((ctxt == NULL) ||
9001 (ctxt->getNsForNodeFunc == NULL))
9004 * Does it shadow any ns-decl?
9006 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9007 XML_NSMAP_FOREACH(nsMap, mi) {
9008 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9009 (mi->shadowDepth == -1) &&
9010 ((ns->prefix == mi->newNs->prefix) ||
9011 xmlStrEqual(ns->prefix,
9012 mi->newNs->prefix))) {
9014 * Mark as shadowed at the current
9017 mi->shadowDepth = depth;
9024 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9025 ns, cloneNs, depth) == NULL)
9026 goto internal_error;
9030 /* cur->ns will be processed further down. */
9032 case XML_ATTRIBUTE_NODE:
9033 /* IDs will be processed further down. */
9034 /* cur->ns will be processed further down. */
9037 case XML_CDATA_SECTION_NODE:
9039 * Note that this will also cover the values of attributes.
9041 DICT_COPY(cur->content, clone->content);
9043 case XML_ENTITY_NODE:
9044 /* TODO: What to do here? */
9046 case XML_ENTITY_REF_NODE:
9047 if (sourceDoc != destDoc) {
9048 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9051 * Different doc: Assign new entity-node if available.
9053 ent = xmlGetDocEntity(destDoc, cur->name);
9055 clone->content = ent->content;
9056 clone->children = (xmlNodePtr) ent;
9057 clone->last = (xmlNodePtr) ent;
9062 * Same doc: Use the current node's entity declaration
9065 clone->content = cur->content;
9066 clone->children = cur->children;
9067 clone->last = cur->last;
9071 DICT_COPY(cur->content, clone->content);
9073 case XML_COMMENT_NODE:
9074 DICT_COPY(cur->content, clone->content);
9077 goto internal_error;
9080 if (cur->ns == NULL)
9081 goto end_ns_reference;
9083 /* handle_ns_reference: */
9085 ** The following will take care of references to ns-decls ********
9086 ** and is intended only for element- and attribute-nodes.
9090 if (destParent && (ctxt == NULL)) {
9091 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
9092 goto internal_error;
9097 * Adopt ns-references.
9099 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9101 * Search for a mapping.
9103 XML_NSMAP_FOREACH(nsMap, mi) {
9104 if ((mi->shadowDepth == -1) &&
9105 (cur->ns == mi->oldNs)) {
9107 * This is the nice case: a mapping was found.
9109 clone->ns = mi->newNs;
9110 goto end_ns_reference;
9115 * No matching namespace in scope. We need a new one.
9117 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
9119 * User-defined behaviour.
9121 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9122 cur->ns->href, cur->ns->prefix);
9124 * Add user's mapping.
9126 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9127 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9128 goto internal_error;
9132 * Aquire a normalized ns-decl and add it to the map.
9134 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9135 /* ns-decls on curElem or on destDoc->oldNs */
9136 destParent ? curElem : NULL,
9139 /* if we need to search only in the ancestor-axis */
9141 /* ns-decls must be prefixed for attributes. */
9142 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9143 goto internal_error;
9150 * Some post-processing.
9152 * Handle ID attributes.
9154 if ((clone->type == XML_ATTRIBUTE_NODE) &&
9155 (clone->parent != NULL))
9157 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
9161 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9162 if (idVal != NULL) {
9163 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9164 /* TODO: error message. */
9166 goto internal_error;
9174 ** The following will traverse the tree **************************
9177 * Walk the element's attributes before descending into child-nodes.
9179 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9181 parentClone = clone;
9182 cur = (xmlNodePtr) cur->properties;
9187 * Descend into child-nodes.
9189 if (cur->children != NULL) {
9190 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9192 parentClone = clone;
9193 cur = cur->children;
9200 * At this point we are done with the node, its content
9201 * and an element-nodes's attribute-nodes.
9205 if ((cur->type == XML_ELEMENT_NODE) ||
9206 (cur->type == XML_XINCLUDE_START) ||
9207 (cur->type == XML_XINCLUDE_END)) {
9209 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9211 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9215 while ((nsMap->last != NULL) &&
9216 (nsMap->last->depth >= depth))
9218 XML_NSMAP_POP(nsMap, mi)
9223 XML_NSMAP_FOREACH(nsMap, mi) {
9224 if (mi->shadowDepth >= depth)
9225 mi->shadowDepth = -1;
9230 if (cur->next != NULL) {
9233 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9237 if (clone->parent != NULL)
9238 clone->parent->last = clone;
9239 clone = clone->parent;
9240 parentClone = clone->parent;
9242 * Process parent --> next;
9247 /* This is for attributes only. */
9248 clone = clone->parent;
9249 parentClone = clone->parent;
9251 * Process parent-element --> children.
9266 if (nsMap != NULL) {
9267 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9269 * Just cleanup the map but don't free.
9273 nsMap->last->next = nsMap->pool;
9274 nsMap->pool = nsMap->first;
9275 nsMap->first = NULL;
9278 xmlDOMWrapNsMapFree(nsMap);
9281 * TODO: Should we try a cleanup of the cloned node in case of a
9284 *resNode = resultClone;
9289 * xmlDOMWrapAdoptAttr:
9290 * @ctxt: the optional context for custom processing
9291 * @sourceDoc: the optional source document of attr
9292 * @attr: the attribute-node to be adopted
9293 * @destDoc: the destination doc for adoption
9294 * @destParent: the optional new parent of @attr in @destDoc
9295 * @options: option flags
9297 * @attr is adopted by @destDoc.
9298 * Ensures that ns-references point to @destDoc: either to
9299 * elements->nsDef entries if @destParent is given, or to
9300 * @destDoc->oldNs otherwise.
9302 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9305 xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9306 xmlDocPtr sourceDoc,
9309 xmlNodePtr destParent,
9310 int options ATTRIBUTE_UNUSED)
9315 if ((attr == NULL) || (destDoc == NULL))
9318 attr->doc = destDoc;
9319 if (attr->ns != NULL) {
9323 /* TODO: User defined. */
9325 /* XML Namespace. */
9326 if (IS_STR_XML(attr->ns->prefix)) {
9327 ns = xmlTreeEnsureXMLDecl(destDoc);
9328 } else if (destParent == NULL) {
9330 * Store in @destDoc->oldNs.
9332 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9335 * Declare on @destParent.
9337 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
9339 goto internal_error;
9341 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9342 attr->ns->href, attr->ns->prefix, 1);
9346 goto internal_error;
9350 XML_TREE_ADOPT_STR(attr->name);
9356 if (attr->children == NULL)
9358 cur = attr->children;
9359 while (cur != NULL) {
9361 switch (cur->type) {
9363 case XML_CDATA_SECTION_NODE:
9364 XML_TREE_ADOPT_STR_2(cur->content)
9366 case XML_ENTITY_REF_NODE:
9368 * Remove reference to the entitity-node.
9370 cur->content = NULL;
9371 cur->children = NULL;
9373 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9376 * Assign new entity-node if available.
9378 ent = xmlGetDocEntity(destDoc, cur->name);
9380 cur->content = ent->content;
9381 cur->children = (xmlNodePtr) ent;
9382 cur->last = (xmlNodePtr) ent;
9389 if (cur->children != NULL) {
9390 cur = cur->children;
9394 if (cur == (xmlNodePtr) attr)
9396 if (cur->next != NULL)
9409 * xmlDOMWrapAdoptNode:
9410 * @ctxt: the optional context for custom processing
9411 * @sourceDoc: the optional sourceDoc
9412 * @node: the node to start with
9413 * @destDoc: the destination doc
9414 * @destParent: the optional new parent of @node in @destDoc
9415 * @options: option flags
9417 * References of out-of scope ns-decls are remapped to point to @destDoc:
9418 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9419 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
9420 * This is the case when you have an unliked node and just want to move it
9423 * If @destParent is given, it ensures that the tree is namespace
9424 * wellformed by creating additional ns-decls where needed.
9425 * Note that, since prefixes of already existent ns-decls can be
9426 * shadowed by this process, it could break QNames in attribute
9427 * values or element content.
9428 * NOTE: This function was not intensively tested.
9430 * Returns 0 if the operation succeeded,
9431 * 1 if a node of unsupported type was given,
9432 * 2 if a node of not yet supported type was given and
9433 * -1 on API/internal errors.
9436 xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
9437 xmlDocPtr sourceDoc,
9440 xmlNodePtr destParent,
9443 if ((node == NULL) || (destDoc == NULL) ||
9444 ((destParent != NULL) && (destParent->doc != destDoc)))
9447 * Check node->doc sanity.
9449 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9450 (node->doc != sourceDoc)) {
9452 * Might be an XIncluded node.
9456 if (sourceDoc == NULL)
9457 sourceDoc = node->doc;
9458 if (sourceDoc == destDoc)
9460 switch (node->type) {
9461 case XML_ELEMENT_NODE:
9462 case XML_ATTRIBUTE_NODE:
9464 case XML_CDATA_SECTION_NODE:
9465 case XML_ENTITY_REF_NODE:
9467 case XML_COMMENT_NODE:
9469 case XML_DOCUMENT_FRAG_NODE:
9470 /* TODO: Support document-fragment-nodes. */
9476 * Unlink only if @node was not already added to @destParent.
9478 if ((node->parent != NULL) && (destParent != node->parent))
9479 xmlUnlinkNode(node);
9481 if (node->type == XML_ELEMENT_NODE) {
9482 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
9483 destDoc, destParent, options));
9484 } else if (node->type == XML_ATTRIBUTE_NODE) {
9485 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
9486 (xmlAttrPtr) node, destDoc, destParent, options));
9488 xmlNodePtr cur = node;
9493 * Optimize string adoption.
9495 if ((sourceDoc != NULL) &&
9496 (sourceDoc->dict == destDoc->dict))
9498 switch (node->type) {
9500 case XML_CDATA_SECTION_NODE:
9501 XML_TREE_ADOPT_STR_2(node->content)
9503 case XML_ENTITY_REF_NODE:
9505 * Remove reference to the entitity-node.
9507 node->content = NULL;
9508 node->children = NULL;
9510 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9513 * Assign new entity-node if available.
9515 ent = xmlGetDocEntity(destDoc, node->name);
9517 node->content = ent->content;
9518 node->children = (xmlNodePtr) ent;
9519 node->last = (xmlNodePtr) ent;
9522 XML_TREE_ADOPT_STR(node->name)
9525 XML_TREE_ADOPT_STR(node->name)
9526 XML_TREE_ADOPT_STR_2(node->content)
9537 #include "elfgcchack.h"