2 * tree.c : implementation of access function for an XML tree.
5 * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
7 * See Copyright for the status of this software.
16 #include <string.h> /* for memset() only ! */
28 #include <libxml/xmlmemory.h>
29 #include <libxml/tree.h>
30 #include <libxml/parser.h>
31 #include <libxml/uri.h>
32 #include <libxml/entities.h>
33 #include <libxml/valid.h>
34 #include <libxml/xmlerror.h>
35 #include <libxml/parserInternals.h>
36 #include <libxml/globals.h>
37 #ifdef LIBXML_HTML_ENABLED
38 #include <libxml/HTMLtree.h>
40 #ifdef LIBXML_DEBUG_ENABLED
41 #include <libxml/debugXML.h>
47 int __xmlRegisterCallbacks = 0;
49 /************************************************************************
51 * Forward declarations *
53 ************************************************************************/
56 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
58 static xmlChar* xmlGetPropNodeValueInternal(xmlAttrPtr prop);
60 /************************************************************************
62 * Tree memory error handler *
64 ************************************************************************/
67 * @extra: extra informations
69 * Handle an out of memory condition
72 xmlTreeErrMemory(const char *extra)
74 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
79 * @code: the error number
80 * @extra: extra informations
82 * Handle an out of memory condition
85 xmlTreeErr(int code, xmlNodePtr node, const char *extra)
87 const char *msg = NULL;
90 case XML_TREE_INVALID_HEX:
91 msg = "invalid hexadecimal character value\n";
93 case XML_TREE_INVALID_DEC:
94 msg = "invalid decimal character value\n";
96 case XML_TREE_UNTERMINATED_ENTITY:
97 msg = "unterminated entity reference %15s\n";
99 case XML_TREE_NOT_UTF8:
100 msg = "string is not in UTF-8\n";
103 msg = "unexpected error number\n";
105 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
108 /************************************************************************
110 * A few static variables and macros *
112 ************************************************************************/
113 /* #undef xmlStringText */
114 const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
115 /* #undef xmlStringTextNoenc */
116 const xmlChar xmlStringTextNoenc[] =
117 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
118 /* #undef xmlStringComment */
119 const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
121 static int xmlCompressMode = 0;
122 static int xmlCheckDTD = 1;
124 #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
125 xmlNodePtr ulccur = (n)->children; \
126 if (ulccur == NULL) { \
129 while (ulccur->next != NULL) { \
130 ulccur->parent = (n); \
131 ulccur = ulccur->next; \
133 ulccur->parent = (n); \
134 (n)->last = ulccur; \
137 #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
138 (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
140 /* #define DEBUG_BUFFER */
141 /* #define DEBUG_TREE */
143 /************************************************************************
145 * Functions to move to entities.c once the *
146 * API freeze is smoothen and they can be made public. *
148 ************************************************************************/
149 #include <libxml/hash.h>
151 #ifdef LIBXML_TREE_ENABLED
153 * xmlGetEntityFromDtd:
154 * @dtd: A pointer to the DTD to search
155 * @name: The entity name
157 * Do an entity lookup in the DTD entity hash table and
158 * return the corresponding entity, if found.
160 * Returns A pointer to the entity structure or NULL if not found.
163 xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
164 xmlEntitiesTablePtr table;
166 if((dtd != NULL) && (dtd->entities != NULL)) {
167 table = (xmlEntitiesTablePtr) dtd->entities;
168 return((xmlEntityPtr) xmlHashLookup(table, name));
169 /* return(xmlGetEntityFromTable(table, name)); */
174 * xmlGetParameterEntityFromDtd:
175 * @dtd: A pointer to the DTD to search
176 * @name: The entity name
178 * Do an entity lookup in the DTD pararmeter entity hash table and
179 * return the corresponding entity, if found.
181 * Returns A pointer to the entity structure or NULL if not found.
184 xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
185 xmlEntitiesTablePtr table;
187 if ((dtd != NULL) && (dtd->pentities != NULL)) {
188 table = (xmlEntitiesTablePtr) dtd->pentities;
189 return((xmlEntityPtr) xmlHashLookup(table, name));
190 /* return(xmlGetEntityFromTable(table, name)); */
194 #endif /* LIBXML_TREE_ENABLED */
196 /************************************************************************
198 * QName handling helper *
200 ************************************************************************/
205 * @prefix: the prefix
206 * @memory: preallocated memory
207 * @len: preallocated memory length
209 * Builds the QName @prefix:@ncname in @memory if there is enough space
210 * and prefix is not NULL nor empty, otherwise allocate a new string.
211 * If prefix is NULL or empty it returns ncname.
213 * Returns the new string which must be freed by the caller if different from
214 * @memory and @ncname or NULL in case of error
217 xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
218 xmlChar *memory, int len) {
222 if (ncname == NULL) return(NULL);
223 if (prefix == NULL) return((xmlChar *) ncname);
225 lenn = strlen((char *) ncname);
226 lenp = strlen((char *) prefix);
228 if ((memory == NULL) || (len < lenn + lenp + 2)) {
229 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
231 xmlTreeErrMemory("building QName");
237 memcpy(&ret[0], prefix, lenp);
239 memcpy(&ret[lenp + 1], ncname, lenn);
240 ret[lenn + lenp + 1] = 0;
246 * @name: the full QName
247 * @prefix: a xmlChar **
249 * parse an XML qualified name string
251 * [NS 5] QName ::= (Prefix ':')? LocalPart
253 * [NS 6] Prefix ::= NCName
255 * [NS 7] LocalPart ::= NCName
257 * Returns NULL if not a QName, otherwise the local part, and prefix
258 * is updated to get the Prefix if any.
262 xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
266 if (prefix == NULL) return(NULL);
268 if (name == NULL) return(NULL);
270 #ifndef XML_XML_NAMESPACE
271 /* xml: prefix is not really a namespace */
272 if ((name[0] == 'x') && (name[1] == 'm') &&
273 (name[2] == 'l') && (name[3] == ':'))
277 /* nasty but valid */
282 * we are not trying to validate but just to cut, and yes it will
283 * work even if this is as set of UTF-8 encoded chars
285 while ((name[len] != 0) && (name[len] != ':'))
291 *prefix = xmlStrndup(name, len);
292 if (*prefix == NULL) {
293 xmlTreeErrMemory("QName split");
296 ret = xmlStrdup(&name[len + 1]);
298 xmlTreeErrMemory("QName split");
299 if (*prefix != NULL) {
311 * @name: the full QName
314 * parse an XML qualified name string,i
316 * returns NULL if it is not a Qualified Name, otherwise, update len
317 * with the length in byte of the prefix and return a pointer
318 * to the start of the name without the prefix
322 xmlSplitQName3(const xmlChar *name, int *len) {
325 if (name == NULL) return(NULL);
326 if (len == NULL) return(NULL);
328 /* nasty but valid */
333 * we are not trying to validate but just to cut, and yes it will
334 * work even if this is as set of UTF-8 encoded chars
336 while ((name[l] != 0) && (name[l] != ':'))
347 /************************************************************************
349 * Check Name, NCName and QName strings *
351 ************************************************************************/
353 #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
355 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_DEBUG_ENABLED) || defined (LIBXML_HTML_ENABLED) || defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED)
358 * @value: the value to check
359 * @space: allow spaces in front and end of the string
361 * Check that a value conforms to the lexical space of NCName
363 * Returns 0 if this validates, a positive error code number otherwise
364 * and -1 in case of internal or API error.
367 xmlValidateNCName(const xmlChar *value, int space) {
368 const xmlChar *cur = value;
375 * First quick algorithm for ASCII range
378 while (IS_BLANK_CH(*cur)) cur++;
379 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
384 while (((*cur >= 'a') && (*cur <= 'z')) ||
385 ((*cur >= 'A') && (*cur <= 'Z')) ||
386 ((*cur >= '0') && (*cur <= '9')) ||
387 (*cur == '_') || (*cur == '-') || (*cur == '.'))
390 while (IS_BLANK_CH(*cur)) cur++;
396 * Second check for chars outside the ASCII range
399 c = CUR_SCHAR(cur, l);
401 while (IS_BLANK(c)) {
403 c = CUR_SCHAR(cur, l);
406 if ((!IS_LETTER(c)) && (c != '_'))
409 c = CUR_SCHAR(cur, l);
410 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
411 (c == '-') || (c == '_') || IS_COMBINING(c) ||
414 c = CUR_SCHAR(cur, l);
417 while (IS_BLANK(c)) {
419 c = CUR_SCHAR(cur, l);
429 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
432 * @value: the value to check
433 * @space: allow spaces in front and end of the string
435 * Check that a value conforms to the lexical space of QName
437 * Returns 0 if this validates, a positive error code number otherwise
438 * and -1 in case of internal or API error.
441 xmlValidateQName(const xmlChar *value, int space) {
442 const xmlChar *cur = value;
448 * First quick algorithm for ASCII range
451 while (IS_BLANK_CH(*cur)) cur++;
452 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
457 while (((*cur >= 'a') && (*cur <= 'z')) ||
458 ((*cur >= 'A') && (*cur <= 'Z')) ||
459 ((*cur >= '0') && (*cur <= '9')) ||
460 (*cur == '_') || (*cur == '-') || (*cur == '.'))
464 if (((*cur >= 'a') && (*cur <= 'z')) ||
465 ((*cur >= 'A') && (*cur <= 'Z')) ||
470 while (((*cur >= 'a') && (*cur <= 'z')) ||
471 ((*cur >= 'A') && (*cur <= 'Z')) ||
472 ((*cur >= '0') && (*cur <= '9')) ||
473 (*cur == '_') || (*cur == '-') || (*cur == '.'))
477 while (IS_BLANK_CH(*cur)) cur++;
483 * Second check for chars outside the ASCII range
486 c = CUR_SCHAR(cur, l);
488 while (IS_BLANK(c)) {
490 c = CUR_SCHAR(cur, l);
493 if ((!IS_LETTER(c)) && (c != '_'))
496 c = CUR_SCHAR(cur, l);
497 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
498 (c == '-') || (c == '_') || IS_COMBINING(c) ||
501 c = CUR_SCHAR(cur, l);
505 c = CUR_SCHAR(cur, l);
506 if ((!IS_LETTER(c)) && (c != '_'))
509 c = CUR_SCHAR(cur, l);
510 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
511 (c == '-') || (c == '_') || IS_COMBINING(c) ||
514 c = CUR_SCHAR(cur, l);
518 while (IS_BLANK(c)) {
520 c = CUR_SCHAR(cur, l);
530 * @value: the value to check
531 * @space: allow spaces in front and end of the string
533 * Check that a value conforms to the lexical space of Name
535 * Returns 0 if this validates, a positive error code number otherwise
536 * and -1 in case of internal or API error.
539 xmlValidateName(const xmlChar *value, int space) {
540 const xmlChar *cur = value;
546 * First quick algorithm for ASCII range
549 while (IS_BLANK_CH(*cur)) cur++;
550 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
551 (*cur == '_') || (*cur == ':'))
555 while (((*cur >= 'a') && (*cur <= 'z')) ||
556 ((*cur >= 'A') && (*cur <= 'Z')) ||
557 ((*cur >= '0') && (*cur <= '9')) ||
558 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
561 while (IS_BLANK_CH(*cur)) cur++;
567 * Second check for chars outside the ASCII range
570 c = CUR_SCHAR(cur, l);
572 while (IS_BLANK(c)) {
574 c = CUR_SCHAR(cur, l);
577 if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
580 c = CUR_SCHAR(cur, l);
581 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
582 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
584 c = CUR_SCHAR(cur, l);
587 while (IS_BLANK(c)) {
589 c = CUR_SCHAR(cur, l);
598 * xmlValidateNMToken:
599 * @value: the value to check
600 * @space: allow spaces in front and end of the string
602 * Check that a value conforms to the lexical space of NMToken
604 * Returns 0 if this validates, a positive error code number otherwise
605 * and -1 in case of internal or API error.
608 xmlValidateNMToken(const xmlChar *value, int space) {
609 const xmlChar *cur = value;
615 * First quick algorithm for ASCII range
618 while (IS_BLANK_CH(*cur)) cur++;
619 if (((*cur >= 'a') && (*cur <= 'z')) ||
620 ((*cur >= 'A') && (*cur <= 'Z')) ||
621 ((*cur >= '0') && (*cur <= '9')) ||
622 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
626 while (((*cur >= 'a') && (*cur <= 'z')) ||
627 ((*cur >= 'A') && (*cur <= 'Z')) ||
628 ((*cur >= '0') && (*cur <= '9')) ||
629 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
632 while (IS_BLANK_CH(*cur)) cur++;
638 * Second check for chars outside the ASCII range
641 c = CUR_SCHAR(cur, l);
643 while (IS_BLANK(c)) {
645 c = CUR_SCHAR(cur, l);
648 if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
649 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
652 c = CUR_SCHAR(cur, l);
653 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
654 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
656 c = CUR_SCHAR(cur, l);
659 while (IS_BLANK(c)) {
661 c = CUR_SCHAR(cur, l);
668 #endif /* LIBXML_TREE_ENABLED */
670 /************************************************************************
672 * Allocation and deallocation of basic structures *
674 ************************************************************************/
677 * xmlSetBufferAllocationScheme:
678 * @scheme: allocation method to use
680 * Set the buffer allocation method. Types are
681 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
682 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
683 * improves performance
686 xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
687 if ((scheme == XML_BUFFER_ALLOC_EXACT) ||
688 (scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
689 (scheme == XML_BUFFER_ALLOC_HYBRID))
690 xmlBufferAllocScheme = scheme;
694 * xmlGetBufferAllocationScheme:
697 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
698 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
699 * improves performance
700 * XML_BUFFER_ALLOC_HYBRID - use exact sizes on small strings to keep memory usage tight
701 * in normal usage, and doubleit on large strings to avoid
702 * pathological performance.
704 * Returns the current allocation scheme
706 xmlBufferAllocationScheme
707 xmlGetBufferAllocationScheme(void) {
708 return(xmlBufferAllocScheme);
713 * @node: the element carrying the namespace
714 * @href: the URI associated
715 * @prefix: the prefix for the namespace
717 * Creation of a new Namespace. This function will refuse to create
718 * a namespace with a similar prefix than an existing one present on this
720 * We use href==NULL in the case of an element creation where the namespace
722 * Returns a new namespace pointer or NULL
725 xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
728 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
731 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
732 /* xml namespace is predefined, no need to add it */
733 if (xmlStrEqual(href, XML_XML_NAMESPACE))
737 * Problem, this is an attempt to bind xml prefix to a wrong
738 * namespace, which breaks
739 * Namespace constraint: Reserved Prefixes and Namespace Names
740 * from XML namespace. But documents authors may not care in
741 * their context so let's proceed.
746 * Allocate a new Namespace and fill the fields.
748 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
750 xmlTreeErrMemory("building namespace");
753 memset(cur, 0, sizeof(xmlNs));
754 cur->type = XML_LOCAL_NAMESPACE;
757 cur->href = xmlStrdup(href);
759 cur->prefix = xmlStrdup(prefix);
762 * Add it at the end to preserve parsing order ...
763 * and checks for existing use of the prefix
766 if (node->nsDef == NULL) {
769 xmlNsPtr prev = node->nsDef;
771 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
772 (xmlStrEqual(prev->prefix, cur->prefix))) {
776 while (prev->next != NULL) {
778 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
779 (xmlStrEqual(prev->prefix, cur->prefix))) {
792 * @node: a node in the document
793 * @ns: a namespace pointer
795 * Associate a namespace to a node, a posteriori.
798 xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
801 xmlGenericError(xmlGenericErrorContext,
802 "xmlSetNs: node == NULL\n");
811 * @cur: the namespace pointer
813 * Free up the structures associated to a namespace
816 xmlFreeNs(xmlNsPtr cur) {
819 xmlGenericError(xmlGenericErrorContext,
820 "xmlFreeNs : ns == NULL\n");
824 if (cur->href != NULL) xmlFree((char *) cur->href);
825 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
831 * @cur: the first namespace pointer
833 * Free up all the structures associated to the chained namespaces.
836 xmlFreeNsList(xmlNsPtr cur) {
840 xmlGenericError(xmlGenericErrorContext,
841 "xmlFreeNsList : ns == NULL\n");
845 while (cur != NULL) {
854 * @doc: the document pointer
855 * @name: the DTD name
856 * @ExternalID: the external ID
857 * @SystemID: the system ID
859 * Creation of a new DTD for the external subset. To create an
860 * internal subset, use xmlCreateIntSubset().
862 * Returns a pointer to the new DTD structure
865 xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
866 const xmlChar *ExternalID, const xmlChar *SystemID) {
869 if ((doc != NULL) && (doc->extSubset != NULL)) {
871 xmlGenericError(xmlGenericErrorContext,
872 "xmlNewDtd(%s): document %s already have a DTD %s\n",
873 /* !!! */ (char *) name, doc->name,
874 /* !!! */ (char *)doc->extSubset->name);
880 * Allocate a new DTD and fill the fields.
882 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
884 xmlTreeErrMemory("building DTD");
887 memset(cur, 0 , sizeof(xmlDtd));
888 cur->type = XML_DTD_NODE;
891 cur->name = xmlStrdup(name);
892 if (ExternalID != NULL)
893 cur->ExternalID = xmlStrdup(ExternalID);
894 if (SystemID != NULL)
895 cur->SystemID = xmlStrdup(SystemID);
897 doc->extSubset = cur;
900 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
901 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
907 * @doc: the document pointer
909 * Get the internal subset of a document
910 * Returns a pointer to the DTD structure or NULL if not found
914 xmlGetIntSubset(xmlDocPtr doc) {
920 while (cur != NULL) {
921 if (cur->type == XML_DTD_NODE)
922 return((xmlDtdPtr) cur);
925 return((xmlDtdPtr) doc->intSubset);
929 * xmlCreateIntSubset:
930 * @doc: the document pointer
931 * @name: the DTD name
932 * @ExternalID: the external (PUBLIC) ID
933 * @SystemID: the system ID
935 * Create the internal subset of a document
936 * Returns a pointer to the new DTD structure
939 xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
940 const xmlChar *ExternalID, const xmlChar *SystemID) {
943 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
945 xmlGenericError(xmlGenericErrorContext,
947 "xmlCreateIntSubset(): document %s already have an internal subset\n",
954 * Allocate a new DTD and fill the fields.
956 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
958 xmlTreeErrMemory("building internal subset");
961 memset(cur, 0, sizeof(xmlDtd));
962 cur->type = XML_DTD_NODE;
965 cur->name = xmlStrdup(name);
966 if (cur->name == NULL) {
967 xmlTreeErrMemory("building internal subset");
972 if (ExternalID != NULL) {
973 cur->ExternalID = xmlStrdup(ExternalID);
974 if (cur->ExternalID == NULL) {
975 xmlTreeErrMemory("building internal subset");
976 if (cur->name != NULL)
977 xmlFree((char *)cur->name);
982 if (SystemID != NULL) {
983 cur->SystemID = xmlStrdup(SystemID);
984 if (cur->SystemID == NULL) {
985 xmlTreeErrMemory("building internal subset");
986 if (cur->name != NULL)
987 xmlFree((char *)cur->name);
988 if (cur->ExternalID != NULL)
989 xmlFree((char *)cur->ExternalID);
995 doc->intSubset = cur;
998 if (doc->children == NULL) {
999 doc->children = (xmlNodePtr) cur;
1000 doc->last = (xmlNodePtr) cur;
1002 if (doc->type == XML_HTML_DOCUMENT_NODE) {
1005 prev = doc->children;
1006 prev->prev = (xmlNodePtr) cur;
1008 doc->children = (xmlNodePtr) cur;
1012 next = doc->children;
1013 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
1016 cur->prev = doc->last;
1017 cur->prev->next = (xmlNodePtr) cur;
1019 doc->last = (xmlNodePtr) cur;
1022 cur->prev = next->prev;
1023 if (cur->prev == NULL)
1024 doc->children = (xmlNodePtr) cur;
1026 cur->prev->next = (xmlNodePtr) cur;
1027 next->prev = (xmlNodePtr) cur;
1033 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1034 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1042 * Free a string if it is not owned by the "dict" dictionnary in the
1045 #define DICT_FREE(str) \
1046 if ((str) && ((!dict) || \
1047 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1048 xmlFree((char *)(str));
1055 * Copy a string using a "dict" dictionnary in the current scope,
1058 #define DICT_COPY(str, cpy) \
1061 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1062 cpy = (xmlChar *) (str); \
1064 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1066 cpy = xmlStrdup((const xmlChar *)(str)); }
1072 * Copy a string using a "dict" dictionnary in the current scope,
1075 #define DICT_CONST_COPY(str, cpy) \
1078 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1079 cpy = (const xmlChar *) (str); \
1081 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1083 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1088 * @cur: the DTD structure to free up
1090 * Free a DTD structure.
1093 xmlFreeDtd(xmlDtdPtr cur) {
1094 xmlDictPtr dict = NULL;
1099 if (cur->doc != NULL) dict = cur->doc->dict;
1101 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1102 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1104 if (cur->children != NULL) {
1105 xmlNodePtr next, c = cur->children;
1108 * Cleanup all nodes which are not part of the specific lists
1109 * of notations, elements, attributes and entities.
1113 if ((c->type != XML_NOTATION_NODE) &&
1114 (c->type != XML_ELEMENT_DECL) &&
1115 (c->type != XML_ATTRIBUTE_DECL) &&
1116 (c->type != XML_ENTITY_DECL)) {
1123 DICT_FREE(cur->name)
1124 DICT_FREE(cur->SystemID)
1125 DICT_FREE(cur->ExternalID)
1127 if (cur->notations != NULL)
1128 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
1130 if (cur->elements != NULL)
1131 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1132 if (cur->attributes != NULL)
1133 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1134 if (cur->entities != NULL)
1135 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1136 if (cur->pentities != NULL)
1137 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1144 * @version: xmlChar string giving the version of XML "1.0"
1146 * Creates a new XML document
1148 * Returns a new document
1151 xmlNewDoc(const xmlChar *version) {
1154 if (version == NULL)
1155 version = (const xmlChar *) "1.0";
1158 * Allocate a new document and fill the fields.
1160 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1162 xmlTreeErrMemory("building doc");
1165 memset(cur, 0, sizeof(xmlDoc));
1166 cur->type = XML_DOCUMENT_NODE;
1168 cur->version = xmlStrdup(version);
1169 if (cur->version == NULL) {
1170 xmlTreeErrMemory("building doc");
1174 cur->standalone = -1;
1175 cur->compression = -1; /* not initialized */
1177 cur->parseFlags = 0;
1178 cur->properties = XML_DOC_USERBUILT;
1180 * The in memory encoding is always UTF8
1181 * This field will never change and would
1182 * be obsolete if not for binary compatibility.
1184 cur->charset = XML_CHAR_ENCODING_UTF8;
1186 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1187 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1193 * @cur: pointer to the document
1195 * Free up all the structures used by a document, tree included.
1198 xmlFreeDoc(xmlDocPtr cur) {
1199 xmlDtdPtr extSubset, intSubset;
1200 xmlDictPtr dict = NULL;
1204 xmlGenericError(xmlGenericErrorContext,
1205 "xmlFreeDoc : document == NULL\n");
1209 #ifdef LIBXML_DEBUG_RUNTIME
1210 #ifdef LIBXML_DEBUG_ENABLED
1211 xmlDebugCheckDocument(stderr, cur);
1215 if (cur != NULL) dict = cur->dict;
1217 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1218 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1221 * Do this before freeing the children list to avoid ID lookups
1223 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1225 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1227 extSubset = cur->extSubset;
1228 intSubset = cur->intSubset;
1229 if (intSubset == extSubset)
1231 if (extSubset != NULL) {
1232 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
1233 cur->extSubset = NULL;
1234 xmlFreeDtd(extSubset);
1236 if (intSubset != NULL) {
1237 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
1238 cur->intSubset = NULL;
1239 xmlFreeDtd(intSubset);
1242 if (cur->children != NULL) xmlFreeNodeList(cur->children);
1243 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
1245 DICT_FREE(cur->version)
1246 DICT_FREE(cur->name)
1247 DICT_FREE(cur->encoding)
1250 if (dict) xmlDictFree(dict);
1254 * xmlStringLenGetNodeList:
1255 * @doc: the document
1256 * @value: the value of the text
1257 * @len: the length of the string value
1259 * Parse the value string and build the node list associated. Should
1260 * produce a flat tree with only TEXTs and ENTITY_REFs.
1261 * Returns a pointer to the first child
1264 xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
1265 xmlNodePtr ret = NULL, last = NULL;
1268 const xmlChar *cur = value, *end = cur + len;
1273 if (value == NULL) return(NULL);
1275 buf = xmlBufCreateSize(0);
1276 if (buf == NULL) return(NULL);
1277 xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID);
1280 while ((cur < end) && (*cur != 0)) {
1281 if (cur[0] == '&') {
1286 * Save the current text.
1289 if (xmlBufAdd(buf, q, cur - q))
1293 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1299 while (tmp != ';') { /* Non input consuming loop */
1300 if ((tmp >= '0') && (tmp <= '9'))
1301 charval = charval * 16 + (tmp - '0');
1302 else if ((tmp >= 'a') && (tmp <= 'f'))
1303 charval = charval * 16 + (tmp - 'a') + 10;
1304 else if ((tmp >= 'A') && (tmp <= 'F'))
1305 charval = charval * 16 + (tmp - 'A') + 10;
1307 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1321 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1327 while (tmp != ';') { /* Non input consuming loops */
1328 if ((tmp >= '0') && (tmp <= '9'))
1329 charval = charval * 10 + (tmp - '0');
1331 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1347 * Read the entity string
1351 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1352 if ((cur >= end) || (*cur == 0)) {
1353 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1359 * Predefined entities don't generate nodes
1361 val = xmlStrndup(q, cur - q);
1362 ent = xmlGetDocEntity(doc, val);
1363 if ((ent != NULL) &&
1364 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1366 if (xmlBufCat(buf, ent->content))
1371 * Flush buffer so far
1373 if (!xmlBufIsEmpty(buf)) {
1374 node = xmlNewDocText(doc, NULL);
1376 if (val != NULL) xmlFree(val);
1379 node->content = xmlBufDetach(buf);
1384 last = xmlAddNextSibling(last, node);
1389 * Create a new REFERENCE_REF node
1391 node = xmlNewReference(doc, val);
1393 if (val != NULL) xmlFree(val);
1396 else if ((ent != NULL) && (ent->children == NULL)) {
1399 ent->children = xmlStringGetNodeList(doc,
1400 (const xmlChar*)node->content);
1402 temp = ent->children;
1404 temp->parent = (xmlNodePtr)ent;
1412 last = xmlAddNextSibling(last, node);
1424 l = xmlCopyCharMultiByte(buffer, charval);
1427 if (xmlBufCat(buf, buffer))
1437 * Handle the last piece of text.
1439 if (xmlBufAdd(buf, q, cur - q))
1443 if (!xmlBufIsEmpty(buf)) {
1444 node = xmlNewDocText(doc, NULL);
1445 if (node == NULL) goto out;
1446 node->content = xmlBufDetach(buf);
1451 last = xmlAddNextSibling(last, node);
1453 } else if (ret == NULL) {
1454 ret = xmlNewDocText(doc, BAD_CAST "");
1463 * xmlStringGetNodeList:
1464 * @doc: the document
1465 * @value: the value of the attribute
1467 * Parse the value string and build the node list associated. Should
1468 * produce a flat tree with only TEXTs and ENTITY_REFs.
1469 * Returns a pointer to the first child
1472 xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
1473 xmlNodePtr ret = NULL, last = NULL;
1476 const xmlChar *cur = value;
1481 if (value == NULL) return(NULL);
1483 buf = xmlBufCreateSize(0);
1484 if (buf == NULL) return(NULL);
1485 xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID);
1489 if (cur[0] == '&') {
1494 * Save the current text.
1497 if (xmlBufAdd(buf, q, cur - q))
1501 if ((cur[1] == '#') && (cur[2] == 'x')) {
1504 while (tmp != ';') { /* Non input consuming loop */
1505 if ((tmp >= '0') && (tmp <= '9'))
1506 charval = charval * 16 + (tmp - '0');
1507 else if ((tmp >= 'a') && (tmp <= 'f'))
1508 charval = charval * 16 + (tmp - 'a') + 10;
1509 else if ((tmp >= 'A') && (tmp <= 'F'))
1510 charval = charval * 16 + (tmp - 'A') + 10;
1512 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1523 } else if (cur[1] == '#') {
1526 while (tmp != ';') { /* Non input consuming loops */
1527 if ((tmp >= '0') && (tmp <= '9'))
1528 charval = charval * 10 + (tmp - '0');
1530 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1543 * Read the entity string
1547 while ((*cur != 0) && (*cur != ';')) cur++;
1549 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1550 (xmlNodePtr) doc, (const char *) q);
1555 * Predefined entities don't generate nodes
1557 val = xmlStrndup(q, cur - q);
1558 ent = xmlGetDocEntity(doc, val);
1559 if ((ent != NULL) &&
1560 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1562 if (xmlBufCat(buf, ent->content))
1567 * Flush buffer so far
1569 if (!xmlBufIsEmpty(buf)) {
1570 node = xmlNewDocText(doc, NULL);
1571 node->content = xmlBufDetach(buf);
1576 last = xmlAddNextSibling(last, node);
1581 * Create a new REFERENCE_REF node
1583 node = xmlNewReference(doc, val);
1585 if (val != NULL) xmlFree(val);
1588 else if ((ent != NULL) && (ent->children == NULL)) {
1591 ent->children = xmlStringGetNodeList(doc,
1592 (const xmlChar*)node->content);
1594 temp = ent->children;
1596 temp->parent = (xmlNodePtr)ent;
1603 last = xmlAddNextSibling(last, node);
1615 len = xmlCopyCharMultiByte(buffer, charval);
1618 if (xmlBufCat(buf, buffer))
1625 if ((cur != q) || (ret == NULL)) {
1627 * Handle the last piece of text.
1629 xmlBufAdd(buf, q, cur - q);
1632 if (!xmlBufIsEmpty(buf)) {
1633 node = xmlNewDocText(doc, NULL);
1634 node->content = xmlBufDetach(buf);
1639 last = xmlAddNextSibling(last, node);
1649 * xmlNodeListGetString:
1650 * @doc: the document
1651 * @list: a Node list
1652 * @inLine: should we replace entity contents or show their external form
1654 * Build the string equivalent to the text contained in the Node list
1655 * made of TEXTs and ENTITY_REFs
1657 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1660 xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1662 xmlNodePtr node = list;
1663 xmlChar *ret = NULL;
1669 if ((list->parent != NULL) && (list->parent->type == XML_ATTRIBUTE_NODE))
1674 while (node != NULL) {
1675 if ((node->type == XML_TEXT_NODE) ||
1676 (node->type == XML_CDATA_SECTION_NODE)) {
1678 ret = xmlStrcat(ret, node->content);
1683 buffer = xmlEncodeAttributeEntities(doc, node->content);
1685 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1686 if (buffer != NULL) {
1687 ret = xmlStrcat(ret, buffer);
1691 } else if (node->type == XML_ENTITY_REF_NODE) {
1693 ent = xmlGetDocEntity(doc, node->name);
1697 /* an entity content can be any "well balanced chunk",
1698 * i.e. the result of the content [43] production:
1699 * http://www.w3.org/TR/REC-xml#NT-content.
1700 * So it can contain text, CDATA section or nested
1701 * entity reference nodes (among others).
1702 * -> we recursive call xmlNodeListGetString()
1703 * which handles these types */
1704 buffer = xmlNodeListGetString(doc, ent->children, 1);
1705 if (buffer != NULL) {
1706 ret = xmlStrcat(ret, buffer);
1710 ret = xmlStrcat(ret, node->content);
1717 ret = xmlStrncat(ret, buf, 1);
1718 ret = xmlStrcat(ret, node->name);
1721 ret = xmlStrncat(ret, buf, 1);
1726 xmlGenericError(xmlGenericErrorContext,
1727 "xmlGetNodeListString : invalid node type %d\n",
1736 #ifdef LIBXML_TREE_ENABLED
1738 * xmlNodeListGetRawString:
1739 * @doc: the document
1740 * @list: a Node list
1741 * @inLine: should we replace entity contents or show their external form
1743 * Builds the string equivalent to the text contained in the Node list
1744 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1745 * this function doesn't do any character encoding handling.
1747 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1750 xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1752 xmlNodePtr node = list;
1753 xmlChar *ret = NULL;
1759 while (node != NULL) {
1760 if ((node->type == XML_TEXT_NODE) ||
1761 (node->type == XML_CDATA_SECTION_NODE)) {
1763 ret = xmlStrcat(ret, node->content);
1767 buffer = xmlEncodeSpecialChars(doc, node->content);
1768 if (buffer != NULL) {
1769 ret = xmlStrcat(ret, buffer);
1773 } else if (node->type == XML_ENTITY_REF_NODE) {
1775 ent = xmlGetDocEntity(doc, node->name);
1779 /* an entity content can be any "well balanced chunk",
1780 * i.e. the result of the content [43] production:
1781 * http://www.w3.org/TR/REC-xml#NT-content.
1782 * So it can contain text, CDATA section or nested
1783 * entity reference nodes (among others).
1784 * -> we recursive call xmlNodeListGetRawString()
1785 * which handles these types */
1787 xmlNodeListGetRawString(doc, ent->children, 1);
1788 if (buffer != NULL) {
1789 ret = xmlStrcat(ret, buffer);
1793 ret = xmlStrcat(ret, node->content);
1800 ret = xmlStrncat(ret, buf, 1);
1801 ret = xmlStrcat(ret, node->name);
1804 ret = xmlStrncat(ret, buf, 1);
1809 xmlGenericError(xmlGenericErrorContext,
1810 "xmlGetNodeListString : invalid node type %d\n",
1818 #endif /* LIBXML_TREE_ENABLED */
1821 xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1822 const xmlChar * name, const xmlChar * value,
1826 xmlDocPtr doc = NULL;
1828 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
1829 if ((eatname == 1) &&
1830 ((node->doc == NULL) ||
1831 (!(xmlDictOwns(node->doc->dict, name)))))
1832 xmlFree((xmlChar *) name);
1837 * Allocate a new property and fill the fields.
1839 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1841 if ((eatname == 1) &&
1842 ((node == NULL) || (node->doc == NULL) ||
1843 (!(xmlDictOwns(node->doc->dict, name)))))
1844 xmlFree((xmlChar *) name);
1845 xmlTreeErrMemory("building attribute");
1848 memset(cur, 0, sizeof(xmlAttr));
1849 cur->type = XML_ATTRIBUTE_NODE;
1859 if ((doc != NULL) && (doc->dict != NULL))
1860 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1862 cur->name = xmlStrdup(name);
1866 if (value != NULL) {
1869 if(!xmlCheckUTF8(value)) {
1870 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc,
1873 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1875 cur->children = xmlNewDocText(doc, value);
1877 tmp = cur->children;
1878 while (tmp != NULL) {
1879 tmp->parent = (xmlNodePtr) cur;
1880 if (tmp->next == NULL)
1887 * Add it at the end to preserve parsing order ...
1890 if (node->properties == NULL) {
1891 node->properties = cur;
1893 xmlAttrPtr prev = node->properties;
1895 while (prev->next != NULL)
1902 if ((value != NULL) && (node != NULL) &&
1903 (xmlIsID(node->doc, node, cur) == 1))
1904 xmlAddID(NULL, node->doc, value, cur);
1906 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1907 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1911 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1912 defined(LIBXML_SCHEMAS_ENABLED)
1915 * @node: the holding node
1916 * @name: the name of the attribute
1917 * @value: the value of the attribute
1919 * Create a new property carried by a node.
1920 * Returns a pointer to the attribute
1923 xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1927 xmlGenericError(xmlGenericErrorContext,
1928 "xmlNewProp : name == NULL\n");
1933 return xmlNewPropInternal(node, NULL, name, value, 0);
1935 #endif /* LIBXML_TREE_ENABLED */
1939 * @node: the holding node
1940 * @ns: the namespace
1941 * @name: the name of the attribute
1942 * @value: the value of the attribute
1944 * Create a new property tagged with a namespace and carried by a node.
1945 * Returns a pointer to the attribute
1948 xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1949 const xmlChar *value) {
1953 xmlGenericError(xmlGenericErrorContext,
1954 "xmlNewNsProp : name == NULL\n");
1959 return xmlNewPropInternal(node, ns, name, value, 0);
1963 * xmlNewNsPropEatName:
1964 * @node: the holding node
1965 * @ns: the namespace
1966 * @name: the name of the attribute
1967 * @value: the value of the attribute
1969 * Create a new property tagged with a namespace and carried by a node.
1970 * Returns a pointer to the attribute
1973 xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1974 const xmlChar *value) {
1978 xmlGenericError(xmlGenericErrorContext,
1979 "xmlNewNsPropEatName : name == NULL\n");
1984 return xmlNewPropInternal(node, ns, name, value, 1);
1989 * @doc: the document
1990 * @name: the name of the attribute
1991 * @value: the value of the attribute
1993 * Create a new property carried by a document.
1994 * Returns a pointer to the attribute
1997 xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
2002 xmlGenericError(xmlGenericErrorContext,
2003 "xmlNewDocProp : name == NULL\n");
2009 * Allocate a new property and fill the fields.
2011 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
2013 xmlTreeErrMemory("building attribute");
2016 memset(cur, 0, sizeof(xmlAttr));
2017 cur->type = XML_ATTRIBUTE_NODE;
2019 if ((doc != NULL) && (doc->dict != NULL))
2020 cur->name = xmlDictLookup(doc->dict, name, -1);
2022 cur->name = xmlStrdup(name);
2024 if (value != NULL) {
2027 cur->children = xmlStringGetNodeList(doc, value);
2030 tmp = cur->children;
2031 while (tmp != NULL) {
2032 tmp->parent = (xmlNodePtr) cur;
2033 if (tmp->next == NULL)
2039 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2040 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2046 * @cur: the first property in the list
2048 * Free a property and all its siblings, all the children are freed too.
2051 xmlFreePropList(xmlAttrPtr cur) {
2053 if (cur == NULL) return;
2054 while (cur != NULL) {
2063 * @cur: an attribute
2065 * Free one attribute, all the content is freed too
2068 xmlFreeProp(xmlAttrPtr cur) {
2069 xmlDictPtr dict = NULL;
2070 if (cur == NULL) return;
2072 if (cur->doc != NULL) dict = cur->doc->dict;
2074 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
2075 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2077 /* Check for ID removal -> leading to invalid references ! */
2078 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2079 xmlRemoveID(cur->doc, cur);
2081 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2082 DICT_FREE(cur->name)
2088 * @cur: an attribute
2090 * Unlink and free one attribute, all the content is freed too
2091 * Note this doesn't work for namespace definition attributes
2093 * Returns 0 if success and -1 in case of error.
2096 xmlRemoveProp(xmlAttrPtr cur) {
2100 xmlGenericError(xmlGenericErrorContext,
2101 "xmlRemoveProp : cur == NULL\n");
2105 if (cur->parent == NULL) {
2107 xmlGenericError(xmlGenericErrorContext,
2108 "xmlRemoveProp : cur->parent == NULL\n");
2112 tmp = cur->parent->properties;
2114 cur->parent->properties = cur->next;
2115 if (cur->next != NULL)
2116 cur->next->prev = NULL;
2120 while (tmp != NULL) {
2121 if (tmp->next == cur) {
2122 tmp->next = cur->next;
2123 if (tmp->next != NULL)
2124 tmp->next->prev = tmp;
2131 xmlGenericError(xmlGenericErrorContext,
2132 "xmlRemoveProp : attribute not owned by its node\n");
2139 * @doc: the target document
2140 * @name: the processing instruction name
2141 * @content: the PI content
2143 * Creation of a processing instruction element.
2144 * Returns a pointer to the new node object.
2147 xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
2152 xmlGenericError(xmlGenericErrorContext,
2153 "xmlNewPI : name == NULL\n");
2159 * Allocate a new node and fill the fields.
2161 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2163 xmlTreeErrMemory("building PI");
2166 memset(cur, 0, sizeof(xmlNode));
2167 cur->type = XML_PI_NODE;
2169 if ((doc != NULL) && (doc->dict != NULL))
2170 cur->name = xmlDictLookup(doc->dict, name, -1);
2172 cur->name = xmlStrdup(name);
2173 if (content != NULL) {
2174 cur->content = xmlStrdup(content);
2178 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2179 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2185 * @name: the processing instruction name
2186 * @content: the PI content
2188 * Creation of a processing instruction element.
2189 * Use xmlDocNewPI preferably to get string interning
2191 * Returns a pointer to the new node object.
2194 xmlNewPI(const xmlChar *name, const xmlChar *content) {
2195 return(xmlNewDocPI(NULL, name, content));
2200 * @ns: namespace if any
2201 * @name: the node name
2203 * Creation of a new node element. @ns is optional (NULL).
2205 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2209 xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2214 xmlGenericError(xmlGenericErrorContext,
2215 "xmlNewNode : name == NULL\n");
2221 * Allocate a new node and fill the fields.
2223 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2225 xmlTreeErrMemory("building node");
2228 memset(cur, 0, sizeof(xmlNode));
2229 cur->type = XML_ELEMENT_NODE;
2231 cur->name = xmlStrdup(name);
2234 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2235 xmlRegisterNodeDefaultValue(cur);
2240 * xmlNewNodeEatName:
2241 * @ns: namespace if any
2242 * @name: the node name
2244 * Creation of a new node element. @ns is optional (NULL).
2246 * Returns a pointer to the new node object, with pointer @name as
2247 * new node's name. Use xmlNewNode() if a copy of @name string is
2248 * is needed as new node's name.
2251 xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2256 xmlGenericError(xmlGenericErrorContext,
2257 "xmlNewNode : name == NULL\n");
2263 * Allocate a new node and fill the fields.
2265 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2267 xmlTreeErrMemory("building node");
2268 /* we can't check here that name comes from the doc dictionnary */
2271 memset(cur, 0, sizeof(xmlNode));
2272 cur->type = XML_ELEMENT_NODE;
2277 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2278 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2284 * @doc: the document
2285 * @ns: namespace if any
2286 * @name: the node name
2287 * @content: the XML text content if any
2289 * Creation of a new node element within a document. @ns and @content
2290 * are optional (NULL).
2291 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2292 * references, but XML special chars need to be escaped first by using
2293 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2294 * need entities support.
2296 * Returns a pointer to the new node object.
2299 xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2300 const xmlChar *name, const xmlChar *content) {
2303 if ((doc != NULL) && (doc->dict != NULL))
2304 cur = xmlNewNodeEatName(ns, (xmlChar *)
2305 xmlDictLookup(doc->dict, name, -1));
2307 cur = xmlNewNode(ns, name);
2310 if (content != NULL) {
2311 cur->children = xmlStringGetNodeList(doc, content);
2312 UPDATE_LAST_CHILD_AND_PARENT(cur)
2320 * xmlNewDocNodeEatName:
2321 * @doc: the document
2322 * @ns: namespace if any
2323 * @name: the node name
2324 * @content: the XML text content if any
2326 * Creation of a new node element within a document. @ns and @content
2327 * are optional (NULL).
2328 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2329 * references, but XML special chars need to be escaped first by using
2330 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2331 * need entities support.
2333 * Returns a pointer to the new node object.
2336 xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2337 xmlChar *name, const xmlChar *content) {
2340 cur = xmlNewNodeEatName(ns, name);
2343 if (content != NULL) {
2344 cur->children = xmlStringGetNodeList(doc, content);
2345 UPDATE_LAST_CHILD_AND_PARENT(cur)
2348 /* if name don't come from the doc dictionnary free it here */
2349 if ((name != NULL) && (doc != NULL) &&
2350 (!(xmlDictOwns(doc->dict, name))))
2356 #ifdef LIBXML_TREE_ENABLED
2359 * @doc: the document
2360 * @ns: namespace if any
2361 * @name: the node name
2362 * @content: the text content if any
2364 * Creation of a new node element within a document. @ns and @content
2365 * are optional (NULL).
2367 * Returns a pointer to the new node object.
2370 xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2371 const xmlChar *name, const xmlChar *content) {
2374 cur = xmlNewDocNode(doc, ns, name, NULL);
2377 if (content != NULL) {
2378 cur->children = xmlNewDocText(doc, content);
2379 UPDATE_LAST_CHILD_AND_PARENT(cur)
2386 * xmlNewDocFragment:
2387 * @doc: the document owning the fragment
2389 * Creation of a new Fragment node.
2390 * Returns a pointer to the new node object.
2393 xmlNewDocFragment(xmlDocPtr doc) {
2397 * Allocate a new DocumentFragment node and fill the fields.
2399 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2401 xmlTreeErrMemory("building fragment");
2404 memset(cur, 0, sizeof(xmlNode));
2405 cur->type = XML_DOCUMENT_FRAG_NODE;
2409 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2410 xmlRegisterNodeDefaultValue(cur);
2413 #endif /* LIBXML_TREE_ENABLED */
2417 * @content: the text content
2419 * Creation of a new text node.
2420 * Returns a pointer to the new node object.
2423 xmlNewText(const xmlChar *content) {
2427 * Allocate a new node and fill the fields.
2429 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2431 xmlTreeErrMemory("building text");
2434 memset(cur, 0, sizeof(xmlNode));
2435 cur->type = XML_TEXT_NODE;
2437 cur->name = xmlStringText;
2438 if (content != NULL) {
2439 cur->content = xmlStrdup(content);
2442 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2443 xmlRegisterNodeDefaultValue(cur);
2447 #ifdef LIBXML_TREE_ENABLED
2450 * @parent: the parent node
2451 * @ns: a namespace if any
2452 * @name: the name of the child
2453 * @content: the text content of the child if any.
2455 * Creation of a new child element, added at the end of @parent children list.
2456 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2457 * created element inherits the namespace of @parent. If @content is non NULL,
2458 * a child TEXT node will be created containing the string @content.
2459 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2460 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
2461 * reserved XML chars that might appear in @content, such as the ampersand,
2462 * greater-than or less-than signs, are automatically replaced by their XML
2463 * escaped entity representations.
2465 * Returns a pointer to the new node object.
2468 xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2469 const xmlChar *name, const xmlChar *content) {
2470 xmlNodePtr cur, prev;
2472 if (parent == NULL) {
2474 xmlGenericError(xmlGenericErrorContext,
2475 "xmlNewTextChild : parent == NULL\n");
2482 xmlGenericError(xmlGenericErrorContext,
2483 "xmlNewTextChild : name == NULL\n");
2489 * Allocate a new node
2491 if (parent->type == XML_ELEMENT_NODE) {
2493 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2495 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2496 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2497 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2499 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2501 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2502 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2503 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2507 if (cur == NULL) return(NULL);
2510 * add the new element at the end of the children list.
2512 cur->type = XML_ELEMENT_NODE;
2513 cur->parent = parent;
2514 cur->doc = parent->doc;
2515 if (parent->children == NULL) {
2516 parent->children = cur;
2519 prev = parent->last;
2527 #endif /* LIBXML_TREE_ENABLED */
2531 * @doc: the document
2532 * @name: the char ref string, starting with # or "&# ... ;"
2534 * Creation of a new character reference node.
2535 * Returns a pointer to the new node object.
2538 xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2545 * Allocate a new node and fill the fields.
2547 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2549 xmlTreeErrMemory("building character reference");
2552 memset(cur, 0, sizeof(xmlNode));
2553 cur->type = XML_ENTITY_REF_NODE;
2556 if (name[0] == '&') {
2559 len = xmlStrlen(name);
2560 if (name[len - 1] == ';')
2561 cur->name = xmlStrndup(name, len - 1);
2563 cur->name = xmlStrndup(name, len);
2565 cur->name = xmlStrdup(name);
2567 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2568 xmlRegisterNodeDefaultValue(cur);
2574 * @doc: the document
2575 * @name: the reference name, or the reference string with & and ;
2577 * Creation of a new reference node.
2578 * Returns a pointer to the new node object.
2581 xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
2589 * Allocate a new node and fill the fields.
2591 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2593 xmlTreeErrMemory("building reference");
2596 memset(cur, 0, sizeof(xmlNode));
2597 cur->type = XML_ENTITY_REF_NODE;
2600 if (name[0] == '&') {
2603 len = xmlStrlen(name);
2604 if (name[len - 1] == ';')
2605 cur->name = xmlStrndup(name, len - 1);
2607 cur->name = xmlStrndup(name, len);
2609 cur->name = xmlStrdup(name);
2611 ent = xmlGetDocEntity(doc, cur->name);
2613 cur->content = ent->content;
2615 * The parent pointer in entity is a DTD pointer and thus is NOT
2616 * updated. Not sure if this is 100% correct.
2619 cur->children = (xmlNodePtr) ent;
2620 cur->last = (xmlNodePtr) ent;
2623 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2624 xmlRegisterNodeDefaultValue(cur);
2630 * @doc: the document
2631 * @content: the text content
2633 * Creation of a new text node within a document.
2634 * Returns a pointer to the new node object.
2637 xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
2640 cur = xmlNewText(content);
2641 if (cur != NULL) cur->doc = doc;
2647 * @content: the text content
2648 * @len: the text len.
2650 * Creation of a new text node with an extra parameter for the content's length
2651 * Returns a pointer to the new node object.
2654 xmlNewTextLen(const xmlChar *content, int len) {
2658 * Allocate a new node and fill the fields.
2660 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2662 xmlTreeErrMemory("building text");
2665 memset(cur, 0, sizeof(xmlNode));
2666 cur->type = XML_TEXT_NODE;
2668 cur->name = xmlStringText;
2669 if (content != NULL) {
2670 cur->content = xmlStrndup(content, len);
2673 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2674 xmlRegisterNodeDefaultValue(cur);
2680 * @doc: the document
2681 * @content: the text content
2682 * @len: the text len.
2684 * Creation of a new text node with an extra content length parameter. The
2685 * text node pertain to a given document.
2686 * Returns a pointer to the new node object.
2689 xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2692 cur = xmlNewTextLen(content, len);
2693 if (cur != NULL) cur->doc = doc;
2699 * @content: the comment content
2701 * Creation of a new node containing a comment.
2702 * Returns a pointer to the new node object.
2705 xmlNewComment(const xmlChar *content) {
2709 * Allocate a new node and fill the fields.
2711 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2713 xmlTreeErrMemory("building comment");
2716 memset(cur, 0, sizeof(xmlNode));
2717 cur->type = XML_COMMENT_NODE;
2719 cur->name = xmlStringComment;
2720 if (content != NULL) {
2721 cur->content = xmlStrdup(content);
2724 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2725 xmlRegisterNodeDefaultValue(cur);
2731 * @doc: the document
2732 * @content: the CDATA block content content
2733 * @len: the length of the block
2735 * Creation of a new node containing a CDATA block.
2736 * Returns a pointer to the new node object.
2739 xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2743 * Allocate a new node and fill the fields.
2745 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2747 xmlTreeErrMemory("building CDATA");
2750 memset(cur, 0, sizeof(xmlNode));
2751 cur->type = XML_CDATA_SECTION_NODE;
2754 if (content != NULL) {
2755 cur->content = xmlStrndup(content, len);
2758 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2759 xmlRegisterNodeDefaultValue(cur);
2765 * @doc: the document
2766 * @content: the comment content
2768 * Creation of a new node containing a comment within a document.
2769 * Returns a pointer to the new node object.
2772 xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2775 cur = xmlNewComment(content);
2776 if (cur != NULL) cur->doc = doc;
2782 * @tree: the top element
2783 * @doc: the document
2785 * update all nodes under the tree to point to the right document
2788 xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
2791 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
2793 if (tree->doc != doc) {
2794 if(tree->type == XML_ELEMENT_NODE) {
2795 prop = tree->properties;
2796 while (prop != NULL) {
2798 xmlSetListDoc(prop->children, doc);
2802 if (tree->children != NULL)
2803 xmlSetListDoc(tree->children, doc);
2810 * @list: the first element
2811 * @doc: the document
2813 * update all nodes in the list to point to the right document
2816 xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2819 if ((list == NULL) || (list->type == XML_NAMESPACE_DECL))
2822 while (cur != NULL) {
2823 if (cur->doc != doc)
2824 xmlSetTreeDoc(cur, doc);
2829 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
2832 * @parent: the parent node
2833 * @ns: a namespace if any
2834 * @name: the name of the child
2835 * @content: the XML content of the child if any.
2837 * Creation of a new child element, added at the end of @parent children list.
2838 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2839 * created element inherits the namespace of @parent. If @content is non NULL,
2840 * a child list containing the TEXTs and ENTITY_REFs node will be created.
2841 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2842 * references. XML special chars must be escaped first by using
2843 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
2845 * Returns a pointer to the new node object.
2848 xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2849 const xmlChar *name, const xmlChar *content) {
2850 xmlNodePtr cur, prev;
2852 if (parent == NULL) {
2854 xmlGenericError(xmlGenericErrorContext,
2855 "xmlNewChild : parent == NULL\n");
2862 xmlGenericError(xmlGenericErrorContext,
2863 "xmlNewChild : name == NULL\n");
2869 * Allocate a new node
2871 if (parent->type == XML_ELEMENT_NODE) {
2873 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2875 cur = xmlNewDocNode(parent->doc, ns, name, content);
2876 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2877 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2879 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2881 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
2882 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2883 cur = xmlNewDocNode( parent->doc, ns, name, content);
2887 if (cur == NULL) return(NULL);
2890 * add the new element at the end of the children list.
2892 cur->type = XML_ELEMENT_NODE;
2893 cur->parent = parent;
2894 cur->doc = parent->doc;
2895 if (parent->children == NULL) {
2896 parent->children = cur;
2899 prev = parent->last;
2907 #endif /* LIBXML_TREE_ENABLED */
2910 * xmlAddPropSibling:
2911 * @prev: the attribute to which @prop is added after
2912 * @cur: the base attribute passed to calling function
2913 * @prop: the new attribute
2915 * Add a new attribute after @prev using @cur as base attribute.
2916 * When inserting before @cur, @prev is passed as @cur->prev.
2917 * When inserting after @cur, @prev is passed as @cur.
2918 * If an existing attribute is found it is detroyed prior to adding @prop.
2920 * Returns the attribute being inserted or NULL in case of error.
2923 xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2926 if ((cur == NULL) || (cur->type != XML_ATTRIBUTE_NODE) ||
2927 (prop == NULL) || (prop->type != XML_ATTRIBUTE_NODE) ||
2928 ((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)))
2931 /* check if an attribute with the same name exists */
2932 if (prop->ns == NULL)
2933 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2935 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2937 if (prop->doc != cur->doc) {
2938 xmlSetTreeDoc(prop, cur->doc);
2940 prop->parent = cur->parent;
2943 prop->next = prev->next;
2946 prop->next->prev = prop;
2951 if (prop->prev == NULL && prop->parent != NULL)
2952 prop->parent->properties = (xmlAttrPtr) prop;
2953 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2954 /* different instance, destroy it (attributes must be unique) */
2955 xmlRemoveProp((xmlAttrPtr) attr);
2961 * xmlAddNextSibling:
2962 * @cur: the child node
2963 * @elem: the new node
2965 * Add a new node @elem as the next sibling of @cur
2966 * If the new node was already inserted in a document it is
2967 * first unlinked from its existing context.
2968 * As a result of text merging @elem may be freed.
2969 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2970 * If there is an attribute with equal name, it is first destroyed.
2972 * Returns the new node or NULL in case of error.
2975 xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2976 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
2978 xmlGenericError(xmlGenericErrorContext,
2979 "xmlAddNextSibling : cur == NULL\n");
2983 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
2985 xmlGenericError(xmlGenericErrorContext,
2986 "xmlAddNextSibling : elem == NULL\n");
2993 xmlGenericError(xmlGenericErrorContext,
2994 "xmlAddNextSibling : cur == elem\n");
2999 xmlUnlinkNode(elem);
3001 if (elem->type == XML_TEXT_NODE) {
3002 if (cur->type == XML_TEXT_NODE) {
3003 xmlNodeAddContent(cur, elem->content);
3007 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
3008 (cur->name == cur->next->name)) {
3011 tmp = xmlStrdup(elem->content);
3012 tmp = xmlStrcat(tmp, cur->next->content);
3013 xmlNodeSetContent(cur->next, tmp);
3018 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3019 return xmlAddPropSibling(cur, cur, elem);
3022 if (elem->doc != cur->doc) {
3023 xmlSetTreeDoc(elem, cur->doc);
3025 elem->parent = cur->parent;
3027 elem->next = cur->next;
3029 if (elem->next != NULL)
3030 elem->next->prev = elem;
3031 if ((elem->parent != NULL) && (elem->parent->last == cur))
3032 elem->parent->last = elem;
3036 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
3037 defined(LIBXML_SCHEMAS_ENABLED)
3039 * xmlAddPrevSibling:
3040 * @cur: the child node
3041 * @elem: the new node
3043 * Add a new node @elem as the previous sibling of @cur
3044 * merging adjacent TEXT nodes (@elem may be freed)
3045 * If the new node was already inserted in a document it is
3046 * first unlinked from its existing context.
3047 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3048 * If there is an attribute with equal name, it is first destroyed.
3050 * Returns the new node or NULL in case of error.
3053 xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
3054 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3056 xmlGenericError(xmlGenericErrorContext,
3057 "xmlAddPrevSibling : cur == NULL\n");
3061 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
3063 xmlGenericError(xmlGenericErrorContext,
3064 "xmlAddPrevSibling : elem == NULL\n");
3071 xmlGenericError(xmlGenericErrorContext,
3072 "xmlAddPrevSibling : cur == elem\n");
3077 xmlUnlinkNode(elem);
3079 if (elem->type == XML_TEXT_NODE) {
3080 if (cur->type == XML_TEXT_NODE) {
3083 tmp = xmlStrdup(elem->content);
3084 tmp = xmlStrcat(tmp, cur->content);
3085 xmlNodeSetContent(cur, tmp);
3090 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3091 (cur->name == cur->prev->name)) {
3092 xmlNodeAddContent(cur->prev, elem->content);
3096 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3097 return xmlAddPropSibling(cur->prev, cur, elem);
3100 if (elem->doc != cur->doc) {
3101 xmlSetTreeDoc(elem, cur->doc);
3103 elem->parent = cur->parent;
3105 elem->prev = cur->prev;
3107 if (elem->prev != NULL)
3108 elem->prev->next = elem;
3109 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
3110 elem->parent->children = elem;
3114 #endif /* LIBXML_TREE_ENABLED */
3118 * @cur: the child node
3119 * @elem: the new node
3121 * Add a new element @elem to the list of siblings of @cur
3122 * merging adjacent TEXT nodes (@elem may be freed)
3123 * If the new element was already inserted in a document it is
3124 * first unlinked from its existing context.
3126 * Returns the new element or NULL in case of error.
3129 xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3132 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3134 xmlGenericError(xmlGenericErrorContext,
3135 "xmlAddSibling : cur == NULL\n");
3140 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
3142 xmlGenericError(xmlGenericErrorContext,
3143 "xmlAddSibling : elem == NULL\n");
3150 xmlGenericError(xmlGenericErrorContext,
3151 "xmlAddSibling : cur == elem\n");
3157 * Constant time is we can rely on the ->parent->last to find
3160 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
3161 (cur->parent->children != NULL) &&
3162 (cur->parent->last != NULL) &&
3163 (cur->parent->last->next == NULL)) {
3164 cur = cur->parent->last;
3166 while (cur->next != NULL) cur = cur->next;
3169 xmlUnlinkNode(elem);
3171 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3172 (cur->name == elem->name)) {
3173 xmlNodeAddContent(cur, elem->content);
3176 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3177 return xmlAddPropSibling(cur, cur, elem);
3180 if (elem->doc != cur->doc) {
3181 xmlSetTreeDoc(elem, cur->doc);
3183 parent = cur->parent;
3186 elem->parent = parent;
3189 parent->last = elem;
3196 * @parent: the parent node
3197 * @cur: the first node in the list
3199 * Add a list of node at the end of the child list of the parent
3200 * merging adjacent TEXT nodes (@cur may be freed)
3202 * Returns the last child or NULL in case of error.
3205 xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3208 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3210 xmlGenericError(xmlGenericErrorContext,
3211 "xmlAddChildList : parent == NULL\n");
3216 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3218 xmlGenericError(xmlGenericErrorContext,
3219 "xmlAddChildList : child == NULL\n");
3224 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3225 (cur->doc != parent->doc)) {
3227 xmlGenericError(xmlGenericErrorContext,
3228 "Elements moved to a different document\n");
3233 * add the first element at the end of the children list.
3236 if (parent->children == NULL) {
3237 parent->children = cur;
3240 * If cur and parent->last both are TEXT nodes, then merge them.
3242 if ((cur->type == XML_TEXT_NODE) &&
3243 (parent->last->type == XML_TEXT_NODE) &&
3244 (cur->name == parent->last->name)) {
3245 xmlNodeAddContent(parent->last, cur->content);
3247 * if it's the only child, nothing more to be done.
3249 if (cur->next == NULL) {
3251 return(parent->last);
3257 prev = parent->last;
3261 while (cur->next != NULL) {
3262 cur->parent = parent;
3263 if (cur->doc != parent->doc) {
3264 xmlSetTreeDoc(cur, parent->doc);
3268 cur->parent = parent;
3269 /* the parent may not be linked to a doc ! */
3270 if (cur->doc != parent->doc) {
3271 xmlSetTreeDoc(cur, parent->doc);
3280 * @parent: the parent node
3281 * @cur: the child node
3283 * Add a new node to @parent, at the end of the child (or property) list
3284 * merging adjacent TEXT nodes (in which case @cur is freed)
3285 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3286 * If there is an attribute with equal name, it is first destroyed.
3288 * Returns the child or NULL in case of error.
3291 xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3294 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3296 xmlGenericError(xmlGenericErrorContext,
3297 "xmlAddChild : parent == NULL\n");
3302 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3304 xmlGenericError(xmlGenericErrorContext,
3305 "xmlAddChild : child == NULL\n");
3310 if (parent == cur) {
3312 xmlGenericError(xmlGenericErrorContext,
3313 "xmlAddChild : parent == cur\n");
3318 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
3319 * cur is then freed.
3321 if (cur->type == XML_TEXT_NODE) {
3322 if ((parent->type == XML_TEXT_NODE) &&
3323 (parent->content != NULL) &&
3324 (parent->name == cur->name)) {
3325 xmlNodeAddContent(parent, cur->content);
3329 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
3330 (parent->last->name == cur->name) &&
3331 (parent->last != cur)) {
3332 xmlNodeAddContent(parent->last, cur->content);
3334 return(parent->last);
3339 * add the new element at the end of the children list.
3342 cur->parent = parent;
3343 if (cur->doc != parent->doc) {
3344 xmlSetTreeDoc(cur, parent->doc);
3346 /* this check prevents a loop on tree-traversions if a developer
3347 * tries to add a node to its parent multiple times
3355 if ((parent->type == XML_TEXT_NODE) &&
3356 (parent->content != NULL) &&
3358 xmlNodeAddContent(parent, cur->content);
3362 if (cur->type == XML_ATTRIBUTE_NODE) {
3363 if (parent->type != XML_ELEMENT_NODE)
3365 if (parent->properties != NULL) {
3366 /* check if an attribute with the same name exists */
3367 xmlAttrPtr lastattr;
3369 if (cur->ns == NULL)
3370 lastattr = xmlHasNsProp(parent, cur->name, NULL);
3372 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
3373 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
3374 /* different instance, destroy it (attributes must be unique) */
3375 xmlUnlinkNode((xmlNodePtr) lastattr);
3376 xmlFreeProp(lastattr);
3378 if (lastattr == (xmlAttrPtr) cur)
3382 if (parent->properties == NULL) {
3383 parent->properties = (xmlAttrPtr) cur;
3386 xmlAttrPtr lastattr = parent->properties;
3387 while (lastattr->next != NULL) {
3388 lastattr = lastattr->next;
3390 lastattr->next = (xmlAttrPtr) cur;
3391 ((xmlAttrPtr) cur)->prev = lastattr;
3394 if (parent->children == NULL) {
3395 parent->children = cur;
3398 prev = parent->last;
3409 * @parent: the parent node
3411 * Search the last child of a node.
3412 * Returns the last child or NULL if none.
3415 xmlGetLastChild(xmlNodePtr parent) {
3416 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
3418 xmlGenericError(xmlGenericErrorContext,
3419 "xmlGetLastChild : parent == NULL\n");
3423 return(parent->last);
3426 #ifdef LIBXML_TREE_ENABLED
3428 * 5 interfaces from DOM ElementTraversal
3432 * xmlChildElementCount:
3433 * @parent: the parent node
3435 * Finds the current number of child nodes of that element which are
3437 * Note the handling of entities references is different than in
3438 * the W3C DOM element traversal spec since we don't have back reference
3439 * from entities content to entities references.
3441 * Returns the count of element child or 0 if not available
3444 xmlChildElementCount(xmlNodePtr parent) {
3445 unsigned long ret = 0;
3446 xmlNodePtr cur = NULL;
3450 switch (parent->type) {
3451 case XML_ELEMENT_NODE:
3452 case XML_ENTITY_NODE:
3453 case XML_DOCUMENT_NODE:
3454 case XML_HTML_DOCUMENT_NODE:
3455 cur = parent->children;
3460 while (cur != NULL) {
3461 if (cur->type == XML_ELEMENT_NODE)
3469 * xmlFirstElementChild:
3470 * @parent: the parent node
3472 * Finds the first child node of that element which is a Element node
3473 * Note the handling of entities references is different than in
3474 * the W3C DOM element traversal spec since we don't have back reference
3475 * from entities content to entities references.
3477 * Returns the first element child or NULL if not available
3480 xmlFirstElementChild(xmlNodePtr parent) {
3481 xmlNodePtr cur = NULL;
3485 switch (parent->type) {
3486 case XML_ELEMENT_NODE:
3487 case XML_ENTITY_NODE:
3488 case XML_DOCUMENT_NODE:
3489 case XML_HTML_DOCUMENT_NODE:
3490 cur = parent->children;
3495 while (cur != NULL) {
3496 if (cur->type == XML_ELEMENT_NODE)
3504 * xmlLastElementChild:
3505 * @parent: the parent node
3507 * Finds the last child node of that element which is a Element node
3508 * Note the handling of entities references is different than in
3509 * the W3C DOM element traversal spec since we don't have back reference
3510 * from entities content to entities references.
3512 * Returns the last element child or NULL if not available
3515 xmlLastElementChild(xmlNodePtr parent) {
3516 xmlNodePtr cur = NULL;
3520 switch (parent->type) {
3521 case XML_ELEMENT_NODE:
3522 case XML_ENTITY_NODE:
3523 case XML_DOCUMENT_NODE:
3524 case XML_HTML_DOCUMENT_NODE:
3530 while (cur != NULL) {
3531 if (cur->type == XML_ELEMENT_NODE)
3539 * xmlPreviousElementSibling:
3540 * @node: the current node
3542 * Finds the first closest previous sibling of the node which is an
3544 * Note the handling of entities references is different than in
3545 * the W3C DOM element traversal spec since we don't have back reference
3546 * from entities content to entities references.
3548 * Returns the previous element sibling or NULL if not available
3551 xmlPreviousElementSibling(xmlNodePtr node) {
3554 switch (node->type) {
3555 case XML_ELEMENT_NODE:
3557 case XML_CDATA_SECTION_NODE:
3558 case XML_ENTITY_REF_NODE:
3559 case XML_ENTITY_NODE:
3561 case XML_COMMENT_NODE:
3562 case XML_XINCLUDE_START:
3563 case XML_XINCLUDE_END:
3569 while (node != NULL) {
3570 if (node->type == XML_ELEMENT_NODE)
3578 * xmlNextElementSibling:
3579 * @node: the current node
3581 * Finds the first closest next sibling of the node which is an
3583 * Note the handling of entities references is different than in
3584 * the W3C DOM element traversal spec since we don't have back reference
3585 * from entities content to entities references.
3587 * Returns the next element sibling or NULL if not available
3590 xmlNextElementSibling(xmlNodePtr node) {
3593 switch (node->type) {
3594 case XML_ELEMENT_NODE:
3596 case XML_CDATA_SECTION_NODE:
3597 case XML_ENTITY_REF_NODE:
3598 case XML_ENTITY_NODE:
3600 case XML_COMMENT_NODE:
3602 case XML_XINCLUDE_START:
3603 case XML_XINCLUDE_END:
3609 while (node != NULL) {
3610 if (node->type == XML_ELEMENT_NODE)
3617 #endif /* LIBXML_TREE_ENABLED */
3621 * @cur: the first node in the list
3623 * Free a node and all its siblings, this is a recursive behaviour, all
3624 * the children are freed too.
3627 xmlFreeNodeList(xmlNodePtr cur) {
3629 xmlDictPtr dict = NULL;
3631 if (cur == NULL) return;
3632 if (cur->type == XML_NAMESPACE_DECL) {
3633 xmlFreeNsList((xmlNsPtr) cur);
3636 if ((cur->type == XML_DOCUMENT_NODE) ||
3637 #ifdef LIBXML_DOCB_ENABLED
3638 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
3640 (cur->type == XML_HTML_DOCUMENT_NODE)) {
3641 xmlFreeDoc((xmlDocPtr) cur);
3644 if (cur->doc != NULL) dict = cur->doc->dict;
3645 while (cur != NULL) {
3647 if (cur->type != XML_DTD_NODE) {
3649 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3650 xmlDeregisterNodeDefaultValue(cur);
3652 if ((cur->children != NULL) &&
3653 (cur->type != XML_ENTITY_REF_NODE))
3654 xmlFreeNodeList(cur->children);
3655 if (((cur->type == XML_ELEMENT_NODE) ||
3656 (cur->type == XML_XINCLUDE_START) ||
3657 (cur->type == XML_XINCLUDE_END)) &&
3658 (cur->properties != NULL))
3659 xmlFreePropList(cur->properties);
3660 if ((cur->type != XML_ELEMENT_NODE) &&
3661 (cur->type != XML_XINCLUDE_START) &&
3662 (cur->type != XML_XINCLUDE_END) &&
3663 (cur->type != XML_ENTITY_REF_NODE) &&
3664 (cur->content != (xmlChar *) &(cur->properties))) {
3665 DICT_FREE(cur->content)
3667 if (((cur->type == XML_ELEMENT_NODE) ||
3668 (cur->type == XML_XINCLUDE_START) ||
3669 (cur->type == XML_XINCLUDE_END)) &&
3670 (cur->nsDef != NULL))
3671 xmlFreeNsList(cur->nsDef);
3674 * When a node is a text node or a comment, it uses a global static
3675 * variable for the name of the node.
3676 * Otherwise the node name might come from the document's
3679 if ((cur->name != NULL) &&
3680 (cur->type != XML_TEXT_NODE) &&
3681 (cur->type != XML_COMMENT_NODE))
3682 DICT_FREE(cur->name)
3693 * Free a node, this is a recursive behaviour, all the children are freed too.
3694 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3697 xmlFreeNode(xmlNodePtr cur) {
3698 xmlDictPtr dict = NULL;
3700 if (cur == NULL) return;
3702 /* use xmlFreeDtd for DTD nodes */
3703 if (cur->type == XML_DTD_NODE) {
3704 xmlFreeDtd((xmlDtdPtr) cur);
3707 if (cur->type == XML_NAMESPACE_DECL) {
3708 xmlFreeNs((xmlNsPtr) cur);
3711 if (cur->type == XML_ATTRIBUTE_NODE) {
3712 xmlFreeProp((xmlAttrPtr) cur);
3716 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3717 xmlDeregisterNodeDefaultValue(cur);
3719 if (cur->doc != NULL) dict = cur->doc->dict;
3721 if (cur->type == XML_ENTITY_DECL) {
3722 xmlEntityPtr ent = (xmlEntityPtr) cur;
3723 DICT_FREE(ent->SystemID);
3724 DICT_FREE(ent->ExternalID);
3726 if ((cur->children != NULL) &&
3727 (cur->type != XML_ENTITY_REF_NODE))
3728 xmlFreeNodeList(cur->children);
3729 if (((cur->type == XML_ELEMENT_NODE) ||
3730 (cur->type == XML_XINCLUDE_START) ||
3731 (cur->type == XML_XINCLUDE_END)) &&
3732 (cur->properties != NULL))
3733 xmlFreePropList(cur->properties);
3734 if ((cur->type != XML_ELEMENT_NODE) &&
3735 (cur->content != NULL) &&
3736 (cur->type != XML_ENTITY_REF_NODE) &&
3737 (cur->type != XML_XINCLUDE_END) &&
3738 (cur->type != XML_XINCLUDE_START) &&
3739 (cur->content != (xmlChar *) &(cur->properties))) {
3740 DICT_FREE(cur->content)
3744 * When a node is a text node or a comment, it uses a global static
3745 * variable for the name of the node.
3746 * Otherwise the node name might come from the document's dictionnary
3748 if ((cur->name != NULL) &&
3749 (cur->type != XML_TEXT_NODE) &&
3750 (cur->type != XML_COMMENT_NODE))
3751 DICT_FREE(cur->name)
3753 if (((cur->type == XML_ELEMENT_NODE) ||
3754 (cur->type == XML_XINCLUDE_START) ||
3755 (cur->type == XML_XINCLUDE_END)) &&
3756 (cur->nsDef != NULL))
3757 xmlFreeNsList(cur->nsDef);
3765 * Unlink a node from it's current context, the node is not freed
3766 * If one need to free the node, use xmlFreeNode() routine after the
3767 * unlink to discard it.
3768 * Note that namespace nodes can't be unlinked as they do not have
3769 * pointer to their parent.
3772 xmlUnlinkNode(xmlNodePtr cur) {
3775 xmlGenericError(xmlGenericErrorContext,
3776 "xmlUnlinkNode : node == NULL\n");
3780 if (cur->type == XML_NAMESPACE_DECL)
3782 if (cur->type == XML_DTD_NODE) {
3786 if (doc->intSubset == (xmlDtdPtr) cur)
3787 doc->intSubset = NULL;
3788 if (doc->extSubset == (xmlDtdPtr) cur)
3789 doc->extSubset = NULL;
3792 if (cur->type == XML_ENTITY_DECL) {
3796 if (doc->intSubset != NULL) {
3797 if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur)
3798 xmlHashRemoveEntry(doc->intSubset->entities, cur->name,
3800 if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur)
3801 xmlHashRemoveEntry(doc->intSubset->pentities, cur->name,
3804 if (doc->extSubset != NULL) {
3805 if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur)
3806 xmlHashRemoveEntry(doc->extSubset->entities, cur->name,
3808 if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur)
3809 xmlHashRemoveEntry(doc->extSubset->pentities, cur->name,
3814 if (cur->parent != NULL) {
3816 parent = cur->parent;
3817 if (cur->type == XML_ATTRIBUTE_NODE) {
3818 if (parent->properties == (xmlAttrPtr) cur)
3819 parent->properties = ((xmlAttrPtr) cur)->next;
3821 if (parent->children == cur)
3822 parent->children = cur->next;
3823 if (parent->last == cur)
3824 parent->last = cur->prev;
3828 if (cur->next != NULL)
3829 cur->next->prev = cur->prev;
3830 if (cur->prev != NULL)
3831 cur->prev->next = cur->next;
3832 cur->next = cur->prev = NULL;
3835 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
3838 * @old: the old node
3841 * Unlink the old node from its current context, prune the new one
3842 * at the same place. If @cur was already inserted in a document it is
3843 * first unlinked from its existing context.
3845 * Returns the @old node
3848 xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
3849 if (old == cur) return(NULL);
3850 if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) ||
3851 (old->parent == NULL)) {
3853 xmlGenericError(xmlGenericErrorContext,
3854 "xmlReplaceNode : old == NULL or without parent\n");
3858 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
3865 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3867 xmlGenericError(xmlGenericErrorContext,
3868 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3872 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3874 xmlGenericError(xmlGenericErrorContext,
3875 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3880 xmlSetTreeDoc(cur, old->doc);
3881 cur->parent = old->parent;
3882 cur->next = old->next;
3883 if (cur->next != NULL)
3884 cur->next->prev = cur;
3885 cur->prev = old->prev;
3886 if (cur->prev != NULL)
3887 cur->prev->next = cur;
3888 if (cur->parent != NULL) {
3889 if (cur->type == XML_ATTRIBUTE_NODE) {
3890 if (cur->parent->properties == (xmlAttrPtr)old)
3891 cur->parent->properties = ((xmlAttrPtr) cur);
3893 if (cur->parent->children == old)
3894 cur->parent->children = cur;
3895 if (cur->parent->last == old)
3896 cur->parent->last = cur;
3899 old->next = old->prev = NULL;
3903 #endif /* LIBXML_TREE_ENABLED */
3905 /************************************************************************
3909 ************************************************************************/
3913 * @cur: the namespace
3915 * Do a copy of the namespace.
3917 * Returns: a new #xmlNsPtr, or NULL in case of error.
3920 xmlCopyNamespace(xmlNsPtr cur) {
3923 if (cur == NULL) return(NULL);
3924 switch (cur->type) {
3925 case XML_LOCAL_NAMESPACE:
3926 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3930 xmlGenericError(xmlGenericErrorContext,
3931 "xmlCopyNamespace: invalid type %d\n", cur->type);
3939 * xmlCopyNamespaceList:
3940 * @cur: the first namespace
3942 * Do a copy of an namespace list.
3944 * Returns: a new #xmlNsPtr, or NULL in case of error.
3947 xmlCopyNamespaceList(xmlNsPtr cur) {
3948 xmlNsPtr ret = NULL;
3949 xmlNsPtr p = NULL,q;
3951 while (cur != NULL) {
3952 q = xmlCopyNamespace(cur);
3965 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
3968 xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
3971 if (cur == NULL) return(NULL);
3972 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
3975 ret = xmlNewDocProp(target->doc, cur->name, NULL);
3976 else if (doc != NULL)
3977 ret = xmlNewDocProp(doc, cur->name, NULL);
3978 else if (cur->parent != NULL)
3979 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3980 else if (cur->children != NULL)
3981 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3983 ret = xmlNewDocProp(NULL, cur->name, NULL);
3984 if (ret == NULL) return(NULL);
3985 ret->parent = target;
3987 if ((cur->ns != NULL) && (target != NULL)) {
3990 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3993 * Humm, we are copying an element whose namespace is defined
3994 * out of the new tree scope. Search it in the original tree
3995 * and add it at the top of the new tree
3997 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
3999 xmlNodePtr root = target;
4000 xmlNodePtr pred = NULL;
4002 while (root->parent != NULL) {
4004 root = root->parent;
4006 if (root == (xmlNodePtr) target->doc) {
4007 /* correct possibly cycling above the document elt */
4010 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4014 * we have to find something appropriate here since
4015 * we cant be sure, that the namespce we found is identified
4018 if (xmlStrEqual(ns->href, cur->ns->href)) {
4019 /* this is the nice case */
4023 * we are in trouble: we need a new reconcilied namespace.
4026 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
4033 if (cur->children != NULL) {
4036 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
4038 tmp = ret->children;
4039 while (tmp != NULL) {
4040 /* tmp->parent = (xmlNodePtr)ret; */
4041 if (tmp->next == NULL)
4049 if ((target!= NULL) && (cur!= NULL) &&
4050 (target->doc != NULL) && (cur->doc != NULL) &&
4051 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
4052 if (xmlIsID(cur->doc, cur->parent, cur)) {
4055 id = xmlNodeListGetString(cur->doc, cur->children, 1);
4057 xmlAddID(NULL, target->doc, id, ret);
4067 * @target: the element where the attribute will be grafted
4068 * @cur: the attribute
4070 * Do a copy of the attribute.
4072 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4075 xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
4076 return xmlCopyPropInternal(NULL, target, cur);
4081 * @target: the element where the attributes will be grafted
4082 * @cur: the first attribute
4084 * Do a copy of an attribute list.
4086 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4089 xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
4090 xmlAttrPtr ret = NULL;
4091 xmlAttrPtr p = NULL,q;
4093 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4095 while (cur != NULL) {
4096 q = xmlCopyProp(target, cur);
4112 * NOTE about the CopyNode operations !
4114 * They are split into external and internal parts for one
4115 * tricky reason: namespaces. Doing a direct copy of a node
4116 * say RPM:Copyright without changing the namespace pointer to
4117 * something else can produce stale links. One way to do it is
4118 * to keep a reference counter but this doesn't work as soon
4119 * as one move the element or the subtree out of the scope of
4120 * the existing namespace. The actual solution seems to add
4121 * a copy of the namespace at the top of the copied tree if
4122 * not available in the subtree.
4123 * Hence two functions, the public front-end call the inner ones
4124 * The argument "recursive" normally indicates a recursive copy
4125 * of the node with values 0 (no) and 1 (yes). For XInclude,
4126 * however, we allow a value of 2 to indicate copy properties and
4127 * namespace info, but don't recurse on children.
4131 xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
4135 if (node == NULL) return(NULL);
4136 switch (node->type) {
4138 case XML_CDATA_SECTION_NODE:
4139 case XML_ELEMENT_NODE:
4140 case XML_DOCUMENT_FRAG_NODE:
4141 case XML_ENTITY_REF_NODE:
4142 case XML_ENTITY_NODE:
4144 case XML_COMMENT_NODE:
4145 case XML_XINCLUDE_START:
4146 case XML_XINCLUDE_END:
4148 case XML_ATTRIBUTE_NODE:
4149 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
4150 case XML_NAMESPACE_DECL:
4151 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
4153 case XML_DOCUMENT_NODE:
4154 case XML_HTML_DOCUMENT_NODE:
4155 #ifdef LIBXML_DOCB_ENABLED
4156 case XML_DOCB_DOCUMENT_NODE:
4158 #ifdef LIBXML_TREE_ENABLED
4159 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
4160 #endif /* LIBXML_TREE_ENABLED */
4161 case XML_DOCUMENT_TYPE_NODE:
4162 case XML_NOTATION_NODE:
4164 case XML_ELEMENT_DECL:
4165 case XML_ATTRIBUTE_DECL:
4166 case XML_ENTITY_DECL:
4171 * Allocate a new node and fill the fields.
4173 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4175 xmlTreeErrMemory("copying node");
4178 memset(ret, 0, sizeof(xmlNode));
4179 ret->type = node->type;
4182 ret->parent = parent;
4183 if (node->name == xmlStringText)
4184 ret->name = xmlStringText;
4185 else if (node->name == xmlStringTextNoenc)
4186 ret->name = xmlStringTextNoenc;
4187 else if (node->name == xmlStringComment)
4188 ret->name = xmlStringComment;
4189 else if (node->name != NULL) {
4190 if ((doc != NULL) && (doc->dict != NULL))
4191 ret->name = xmlDictLookup(doc->dict, node->name, -1);
4193 ret->name = xmlStrdup(node->name);
4195 if ((node->type != XML_ELEMENT_NODE) &&
4196 (node->content != NULL) &&
4197 (node->type != XML_ENTITY_REF_NODE) &&
4198 (node->type != XML_XINCLUDE_END) &&
4199 (node->type != XML_XINCLUDE_START)) {
4200 ret->content = xmlStrdup(node->content);
4202 if (node->type == XML_ELEMENT_NODE)
4203 ret->line = node->line;
4205 if (parent != NULL) {
4209 * this is a tricky part for the node register thing:
4210 * in case ret does get coalesced in xmlAddChild
4211 * the deregister-node callback is called; so we register ret now already
4213 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
4214 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4216 tmp = xmlAddChild(parent, ret);
4217 /* node could have coalesced */
4224 if (((node->type == XML_ELEMENT_NODE) ||
4225 (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
4226 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
4228 if (node->ns != NULL) {
4231 ns = xmlSearchNs(doc, ret, node->ns->prefix);
4234 * Humm, we are copying an element whose namespace is defined
4235 * out of the new tree scope. Search it in the original tree
4236 * and add it at the top of the new tree
4238 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
4240 xmlNodePtr root = ret;
4242 while (root->parent != NULL) root = root->parent;
4243 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4245 ret->ns = xmlNewReconciliedNs(doc, ret, node->ns);
4249 * reference the existing namespace definition in our own tree.
4254 if (((node->type == XML_ELEMENT_NODE) ||
4255 (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
4256 ret->properties = xmlCopyPropList(ret, node->properties);
4257 if (node->type == XML_ENTITY_REF_NODE) {
4258 if ((doc == NULL) || (node->doc != doc)) {
4260 * The copied node will go into a separate document, so
4261 * to avoid dangling references to the ENTITY_DECL node
4262 * we cannot keep the reference. Try to find it in the
4265 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
4267 ret->children = node->children;
4269 ret->last = ret->children;
4270 } else if ((node->children != NULL) && (extended != 2)) {
4271 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
4272 UPDATE_LAST_CHILD_AND_PARENT(ret)
4276 /* if parent != NULL we already registered the node above */
4277 if ((parent == NULL) &&
4278 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
4279 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4284 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
4285 xmlNodePtr ret = NULL;
4286 xmlNodePtr p = NULL,q;
4288 while (node != NULL) {
4289 #ifdef LIBXML_TREE_ENABLED
4290 if (node->type == XML_DTD_NODE ) {
4295 if (doc->intSubset == NULL) {
4296 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
4299 doc->intSubset = (xmlDtdPtr) q;
4300 xmlAddChild(parent, q);
4302 q = (xmlNodePtr) doc->intSubset;
4303 xmlAddChild(parent, q);
4306 #endif /* LIBXML_TREE_ENABLED */
4307 q = xmlStaticCopyNode(node, doc, parent, 1);
4311 } else if (p != q) {
4312 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
4325 * @extended: if 1 do a recursive copy (properties, namespaces and children
4327 * if 2 copy properties and namespaces (when applicable)
4329 * Do a copy of the node.
4331 * Returns: a new #xmlNodePtr, or NULL in case of error.
4334 xmlCopyNode(const xmlNodePtr node, int extended) {
4337 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
4344 * @doc: the document
4345 * @extended: if 1 do a recursive copy (properties, namespaces and children
4347 * if 2 copy properties and namespaces (when applicable)
4349 * Do a copy of the node to a given document.
4351 * Returns: a new #xmlNodePtr, or NULL in case of error.
4354 xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) {
4357 ret = xmlStaticCopyNode(node, doc, NULL, extended);
4362 * xmlDocCopyNodeList:
4363 * @doc: the target document
4364 * @node: the first node in the list.
4366 * Do a recursive copy of the node list.
4368 * Returns: a new #xmlNodePtr, or NULL in case of error.
4370 xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) {
4371 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4377 * @node: the first node in the list.
4379 * Do a recursive copy of the node list.
4380 * Use xmlDocCopyNodeList() if possible to ensure string interning.
4382 * Returns: a new #xmlNodePtr, or NULL in case of error.
4384 xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
4385 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4389 #if defined(LIBXML_TREE_ENABLED)
4394 * Do a copy of the dtd.
4396 * Returns: a new #xmlDtdPtr, or NULL in case of error.
4399 xmlCopyDtd(xmlDtdPtr dtd) {
4401 xmlNodePtr cur, p = NULL, q;
4403 if (dtd == NULL) return(NULL);
4404 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4405 if (ret == NULL) return(NULL);
4406 if (dtd->entities != NULL)
4407 ret->entities = (void *) xmlCopyEntitiesTable(
4408 (xmlEntitiesTablePtr) dtd->entities);
4409 if (dtd->notations != NULL)
4410 ret->notations = (void *) xmlCopyNotationTable(
4411 (xmlNotationTablePtr) dtd->notations);
4412 if (dtd->elements != NULL)
4413 ret->elements = (void *) xmlCopyElementTable(
4414 (xmlElementTablePtr) dtd->elements);
4415 if (dtd->attributes != NULL)
4416 ret->attributes = (void *) xmlCopyAttributeTable(
4417 (xmlAttributeTablePtr) dtd->attributes);
4418 if (dtd->pentities != NULL)
4419 ret->pentities = (void *) xmlCopyEntitiesTable(
4420 (xmlEntitiesTablePtr) dtd->pentities);
4422 cur = dtd->children;
4423 while (cur != NULL) {
4426 if (cur->type == XML_ENTITY_DECL) {
4427 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4428 switch (tmp->etype) {
4429 case XML_INTERNAL_GENERAL_ENTITY:
4430 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4431 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4432 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4434 case XML_INTERNAL_PARAMETER_ENTITY:
4435 case XML_EXTERNAL_PARAMETER_ENTITY:
4437 xmlGetParameterEntityFromDtd(ret, tmp->name);
4439 case XML_INTERNAL_PREDEFINED_ENTITY:
4442 } else if (cur->type == XML_ELEMENT_DECL) {
4443 xmlElementPtr tmp = (xmlElementPtr) cur;
4445 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4446 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4447 xmlAttributePtr tmp = (xmlAttributePtr) cur;
4449 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4450 } else if (cur->type == XML_COMMENT_NODE) {
4451 q = xmlCopyNode(cur, 0);
4465 q->parent = (xmlNodePtr) ret;
4476 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
4479 * @doc: the document
4480 * @recursive: if not zero do a recursive copy.
4482 * Do a copy of the document info. If recursive, the content tree will
4483 * be copied too as well as DTD, namespaces and entities.
4485 * Returns: a new #xmlDocPtr, or NULL in case of error.
4488 xmlCopyDoc(xmlDocPtr doc, int recursive) {
4491 if (doc == NULL) return(NULL);
4492 ret = xmlNewDoc(doc->version);
4493 if (ret == NULL) return(NULL);
4494 if (doc->name != NULL)
4495 ret->name = xmlMemStrdup(doc->name);
4496 if (doc->encoding != NULL)
4497 ret->encoding = xmlStrdup(doc->encoding);
4498 if (doc->URL != NULL)
4499 ret->URL = xmlStrdup(doc->URL);
4500 ret->charset = doc->charset;
4501 ret->compression = doc->compression;
4502 ret->standalone = doc->standalone;
4503 if (!recursive) return(ret);
4506 ret->children = NULL;
4507 #ifdef LIBXML_TREE_ENABLED
4508 if (doc->intSubset != NULL) {
4509 ret->intSubset = xmlCopyDtd(doc->intSubset);
4510 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
4511 ret->intSubset->parent = ret;
4514 if (doc->oldNs != NULL)
4515 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4516 if (doc->children != NULL) {
4519 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4522 tmp = ret->children;
4523 while (tmp != NULL) {
4524 if (tmp->next == NULL)
4531 #endif /* LIBXML_TREE_ENABLED */
4533 /************************************************************************
4535 * Content access functions *
4537 ************************************************************************/
4540 * xmlGetLineNoInternal:
4542 * @depth: used to limit any risk of recursion
4544 * Get line number of @node.
4545 * Try to override the limitation of lines being store in 16 bits ints
4547 * Returns the line number if successful, -1 otherwise
4550 xmlGetLineNoInternal(xmlNodePtr node, int depth)
4559 if ((node->type == XML_ELEMENT_NODE) ||
4560 (node->type == XML_TEXT_NODE) ||
4561 (node->type == XML_COMMENT_NODE) ||
4562 (node->type == XML_PI_NODE)) {
4563 if (node->line == 65535) {
4564 if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL))
4565 result = (long) node->psvi;
4566 else if ((node->type == XML_ELEMENT_NODE) &&
4567 (node->children != NULL))
4568 result = xmlGetLineNoInternal(node->children, depth + 1);
4569 else if (node->next != NULL)
4570 result = xmlGetLineNoInternal(node->next, depth + 1);
4571 else if (node->prev != NULL)
4572 result = xmlGetLineNoInternal(node->prev, depth + 1);
4574 if ((result == -1) || (result == 65535))
4575 result = (long) node->line;
4576 } else if ((node->prev != NULL) &&
4577 ((node->prev->type == XML_ELEMENT_NODE) ||
4578 (node->prev->type == XML_TEXT_NODE) ||
4579 (node->prev->type == XML_COMMENT_NODE) ||
4580 (node->prev->type == XML_PI_NODE)))
4581 result = xmlGetLineNoInternal(node->prev, depth + 1);
4582 else if ((node->parent != NULL) &&
4583 (node->parent->type == XML_ELEMENT_NODE))
4584 result = xmlGetLineNoInternal(node->parent, depth + 1);
4593 * Get line number of @node.
4594 * Try to override the limitation of lines being store in 16 bits ints
4595 * if XML_PARSE_BIG_LINES parser option was used
4597 * Returns the line number if successful, -1 otherwise
4600 xmlGetLineNo(xmlNodePtr node)
4602 return(xmlGetLineNoInternal(node, 0));
4605 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
4610 * Build a structure based Path for the given node
4612 * Returns the new path or NULL in case of error. The caller must free
4613 * the returned string
4616 xmlGetNodePath(xmlNodePtr node)
4618 xmlNodePtr cur, tmp, next;
4619 xmlChar *buffer = NULL, *temp;
4625 int occur = 0, generic;
4627 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
4631 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4632 if (buffer == NULL) {
4633 xmlTreeErrMemory("getting node path");
4636 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4638 xmlTreeErrMemory("getting node path");
4649 if ((cur->type == XML_DOCUMENT_NODE) ||
4650 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4651 if (buffer[0] == '/')
4655 } else if (cur->type == XML_ELEMENT_NODE) {
4658 name = (const char *) cur->name;
4660 if (cur->ns->prefix != NULL) {
4661 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4662 (char *)cur->ns->prefix, (char *)cur->name);
4663 nametemp[sizeof(nametemp) - 1] = 0;
4667 * We cannot express named elements in the default
4668 * namespace, so use "*".
4677 * Thumbler index computation
4678 * TODO: the ocurence test seems bogus for namespaced names
4681 while (tmp != NULL) {
4682 if ((tmp->type == XML_ELEMENT_NODE) &&
4684 (xmlStrEqual(cur->name, tmp->name) &&
4685 ((tmp->ns == cur->ns) ||
4686 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4687 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4693 while (tmp != NULL && occur == 0) {
4694 if ((tmp->type == XML_ELEMENT_NODE) &&
4696 (xmlStrEqual(cur->name, tmp->name) &&
4697 ((tmp->ns == cur->ns) ||
4698 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4699 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4707 } else if (cur->type == XML_COMMENT_NODE) {
4713 * Thumbler index computation
4716 while (tmp != NULL) {
4717 if (tmp->type == XML_COMMENT_NODE)
4723 while (tmp != NULL && occur == 0) {
4724 if (tmp->type == XML_COMMENT_NODE)
4732 } else if ((cur->type == XML_TEXT_NODE) ||
4733 (cur->type == XML_CDATA_SECTION_NODE)) {
4739 * Thumbler index computation
4742 while (tmp != NULL) {
4743 if ((tmp->type == XML_TEXT_NODE) ||
4744 (tmp->type == XML_CDATA_SECTION_NODE))
4749 * Evaluate if this is the only text- or CDATA-section-node;
4750 * if yes, then we'll get "text()", otherwise "text()[1]".
4754 while (tmp != NULL) {
4755 if ((tmp->type == XML_TEXT_NODE) ||
4756 (tmp->type == XML_CDATA_SECTION_NODE))
4765 } else if (cur->type == XML_PI_NODE) {
4767 snprintf(nametemp, sizeof(nametemp) - 1,
4768 "processing-instruction('%s')", (char *)cur->name);
4769 nametemp[sizeof(nametemp) - 1] = 0;
4775 * Thumbler index computation
4778 while (tmp != NULL) {
4779 if ((tmp->type == XML_PI_NODE) &&
4780 (xmlStrEqual(cur->name, tmp->name)))
4786 while (tmp != NULL && occur == 0) {
4787 if ((tmp->type == XML_PI_NODE) &&
4788 (xmlStrEqual(cur->name, tmp->name)))
4797 } else if (cur->type == XML_ATTRIBUTE_NODE) {
4799 name = (const char *) (((xmlAttrPtr) cur)->name);
4801 if (cur->ns->prefix != NULL)
4802 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4803 (char *)cur->ns->prefix, (char *)cur->name);
4805 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4807 nametemp[sizeof(nametemp) - 1] = 0;
4810 next = ((xmlAttrPtr) cur)->parent;
4816 * Make sure there is enough room
4818 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4820 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4821 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4823 xmlTreeErrMemory("getting node path");
4829 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4831 xmlTreeErrMemory("getting node path");
4839 snprintf((char *) buf, buf_len, "%s%s%s",
4840 sep, name, (char *) buffer);
4842 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
4843 sep, name, occur, (char *) buffer);
4844 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
4846 } while (cur != NULL);
4850 #endif /* LIBXML_TREE_ENABLED */
4853 * xmlDocGetRootElement:
4854 * @doc: the document
4856 * Get the root element of the document (doc->children is a list
4857 * containing possibly comments, PIs, etc ...).
4859 * Returns the #xmlNodePtr for the root or NULL
4862 xmlDocGetRootElement(xmlDocPtr doc) {
4865 if (doc == NULL) return(NULL);
4866 ret = doc->children;
4867 while (ret != NULL) {
4868 if (ret->type == XML_ELEMENT_NODE)
4875 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
4877 * xmlDocSetRootElement:
4878 * @doc: the document
4879 * @root: the new document root element, if root is NULL no action is taken,
4880 * to remove a node from a document use xmlUnlinkNode(root) instead.
4882 * Set the root element of the document (doc->children is a list
4883 * containing possibly comments, PIs, etc ...).
4885 * Returns the old root element if any was found, NULL if root was NULL
4888 xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4889 xmlNodePtr old = NULL;
4891 if (doc == NULL) return(NULL);
4892 if ((root == NULL) || (root->type == XML_NAMESPACE_DECL))
4894 xmlUnlinkNode(root);
4895 xmlSetTreeDoc(root, doc);
4896 root->parent = (xmlNodePtr) doc;
4897 old = doc->children;
4898 while (old != NULL) {
4899 if (old->type == XML_ELEMENT_NODE)
4904 if (doc->children == NULL) {
4905 doc->children = root;
4908 xmlAddSibling(doc->children, root);
4911 xmlReplaceNode(old, root);
4917 #if defined(LIBXML_TREE_ENABLED)
4920 * @cur: the node being changed
4921 * @lang: the language description
4923 * Set the language of a node, i.e. the values of the xml:lang
4927 xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
4930 if (cur == NULL) return;
4933 case XML_CDATA_SECTION_NODE:
4934 case XML_COMMENT_NODE:
4935 case XML_DOCUMENT_NODE:
4936 case XML_DOCUMENT_TYPE_NODE:
4937 case XML_DOCUMENT_FRAG_NODE:
4938 case XML_NOTATION_NODE:
4939 case XML_HTML_DOCUMENT_NODE:
4941 case XML_ELEMENT_DECL:
4942 case XML_ATTRIBUTE_DECL:
4943 case XML_ENTITY_DECL:
4945 case XML_ENTITY_REF_NODE:
4946 case XML_ENTITY_NODE:
4947 case XML_NAMESPACE_DECL:
4948 #ifdef LIBXML_DOCB_ENABLED
4949 case XML_DOCB_DOCUMENT_NODE:
4951 case XML_XINCLUDE_START:
4952 case XML_XINCLUDE_END:
4954 case XML_ELEMENT_NODE:
4955 case XML_ATTRIBUTE_NODE:
4958 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4961 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
4963 #endif /* LIBXML_TREE_ENABLED */
4967 * @cur: the node being checked
4969 * Searches the language of a node, i.e. the values of the xml:lang
4970 * attribute or the one carried by the nearest ancestor.
4972 * Returns a pointer to the lang value, or NULL if not found
4973 * It's up to the caller to free the memory with xmlFree().
4976 xmlNodeGetLang(xmlNodePtr cur) {
4979 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
4981 while (cur != NULL) {
4982 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
4991 #ifdef LIBXML_TREE_ENABLED
4993 * xmlNodeSetSpacePreserve:
4994 * @cur: the node being changed
4995 * @val: the xml:space value ("0": default, 1: "preserve")
4997 * Set (or reset) the space preserving behaviour of a node, i.e. the
4998 * value of the xml:space attribute.
5001 xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
5004 if (cur == NULL) return;
5007 case XML_CDATA_SECTION_NODE:
5008 case XML_COMMENT_NODE:
5009 case XML_DOCUMENT_NODE:
5010 case XML_DOCUMENT_TYPE_NODE:
5011 case XML_DOCUMENT_FRAG_NODE:
5012 case XML_NOTATION_NODE:
5013 case XML_HTML_DOCUMENT_NODE:
5015 case XML_ELEMENT_DECL:
5016 case XML_ATTRIBUTE_DECL:
5017 case XML_ENTITY_DECL:
5019 case XML_ENTITY_REF_NODE:
5020 case XML_ENTITY_NODE:
5021 case XML_NAMESPACE_DECL:
5022 case XML_XINCLUDE_START:
5023 case XML_XINCLUDE_END:
5024 #ifdef LIBXML_DOCB_ENABLED
5025 case XML_DOCB_DOCUMENT_NODE:
5028 case XML_ELEMENT_NODE:
5029 case XML_ATTRIBUTE_NODE:
5032 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5037 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
5040 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
5044 #endif /* LIBXML_TREE_ENABLED */
5047 * xmlNodeGetSpacePreserve:
5048 * @cur: the node being checked
5050 * Searches the space preserving behaviour of a node, i.e. the values
5051 * of the xml:space attribute or the one carried by the nearest
5054 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
5057 xmlNodeGetSpacePreserve(xmlNodePtr cur) {
5060 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
5062 while (cur != NULL) {
5063 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
5064 if (space != NULL) {
5065 if (xmlStrEqual(space, BAD_CAST "preserve")) {
5069 if (xmlStrEqual(space, BAD_CAST "default")) {
5080 #ifdef LIBXML_TREE_ENABLED
5083 * @cur: the node being changed
5084 * @name: the new tag name
5086 * Set (or reset) the name of a node.
5089 xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
5093 if (cur == NULL) return;
5094 if (name == NULL) return;
5097 case XML_CDATA_SECTION_NODE:
5098 case XML_COMMENT_NODE:
5099 case XML_DOCUMENT_TYPE_NODE:
5100 case XML_DOCUMENT_FRAG_NODE:
5101 case XML_NOTATION_NODE:
5102 case XML_HTML_DOCUMENT_NODE:
5103 case XML_NAMESPACE_DECL:
5104 case XML_XINCLUDE_START:
5105 case XML_XINCLUDE_END:
5106 #ifdef LIBXML_DOCB_ENABLED
5107 case XML_DOCB_DOCUMENT_NODE:
5110 case XML_ELEMENT_NODE:
5111 case XML_ATTRIBUTE_NODE:
5113 case XML_ENTITY_REF_NODE:
5114 case XML_ENTITY_NODE:
5116 case XML_DOCUMENT_NODE:
5117 case XML_ELEMENT_DECL:
5118 case XML_ATTRIBUTE_DECL:
5119 case XML_ENTITY_DECL:
5128 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
5129 xmlFree((xmlChar *) cur->name);
5130 cur->name = xmlDictLookup(dict, name, -1);
5132 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
5133 cur->name = xmlStrdup(name);
5138 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
5141 * @cur: the node being changed
5142 * @uri: the new base URI
5144 * Set (or reset) the base URI of a node, i.e. the value of the
5145 * xml:base attribute.
5148 xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
5152 if (cur == NULL) return;
5155 case XML_CDATA_SECTION_NODE:
5156 case XML_COMMENT_NODE:
5157 case XML_DOCUMENT_TYPE_NODE:
5158 case XML_DOCUMENT_FRAG_NODE:
5159 case XML_NOTATION_NODE:
5161 case XML_ELEMENT_DECL:
5162 case XML_ATTRIBUTE_DECL:
5163 case XML_ENTITY_DECL:
5165 case XML_ENTITY_REF_NODE:
5166 case XML_ENTITY_NODE:
5167 case XML_NAMESPACE_DECL:
5168 case XML_XINCLUDE_START:
5169 case XML_XINCLUDE_END:
5171 case XML_ELEMENT_NODE:
5172 case XML_ATTRIBUTE_NODE:
5174 case XML_DOCUMENT_NODE:
5175 #ifdef LIBXML_DOCB_ENABLED
5176 case XML_DOCB_DOCUMENT_NODE:
5178 case XML_HTML_DOCUMENT_NODE: {
5179 xmlDocPtr doc = (xmlDocPtr) cur;
5181 if (doc->URL != NULL)
5182 xmlFree((xmlChar *) doc->URL);
5186 doc->URL = xmlPathToURI(uri);
5191 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5194 fixed = xmlPathToURI(uri);
5195 if (fixed != NULL) {
5196 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
5199 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
5202 #endif /* LIBXML_TREE_ENABLED */
5206 * @doc: the document the node pertains to
5207 * @cur: the node being checked
5209 * Searches for the BASE URL. The code should work on both XML
5210 * and HTML document even if base mechanisms are completely different.
5211 * It returns the base as defined in RFC 2396 sections
5212 * 5.1.1. Base URI within Document Content
5214 * 5.1.2. Base URI from the Encapsulating Entity
5215 * However it does not return the document base (5.1.3), use
5216 * doc->URL in this case
5218 * Returns a pointer to the base URL, or NULL if not found
5219 * It's up to the caller to free the memory with xmlFree().
5222 xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
5223 xmlChar *oldbase = NULL;
5224 xmlChar *base, *newbase;
5226 if ((cur == NULL) && (doc == NULL))
5228 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
5230 if (doc == NULL) doc = cur->doc;
5231 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
5232 cur = doc->children;
5233 while ((cur != NULL) && (cur->name != NULL)) {
5234 if (cur->type != XML_ELEMENT_NODE) {
5238 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5239 cur = cur->children;
5242 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5243 cur = cur->children;
5246 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5247 return(xmlGetProp(cur, BAD_CAST "href"));
5253 while (cur != NULL) {
5254 if (cur->type == XML_ENTITY_DECL) {
5255 xmlEntityPtr ent = (xmlEntityPtr) cur;
5256 return(xmlStrdup(ent->URI));
5258 if (cur->type == XML_ELEMENT_NODE) {
5259 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
5261 if (oldbase != NULL) {
5262 newbase = xmlBuildURI(oldbase, base);
5263 if (newbase != NULL) {
5275 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
5276 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
5277 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
5283 if ((doc != NULL) && (doc->URL != NULL)) {
5284 if (oldbase == NULL)
5285 return(xmlStrdup(doc->URL));
5286 newbase = xmlBuildURI(oldbase, doc->URL);
5294 * xmlNodeBufGetContent:
5296 * @cur: the node being read
5298 * Read the value of a node @cur, this can be either the text carried
5299 * directly by this node if it's a TEXT node or the aggregate string
5300 * of the values carried by this node child's (TEXT and ENTITY_REF).
5301 * Entity references are substituted.
5302 * Fills up the buffer @buffer with this value
5304 * Returns 0 in case of success and -1 in case of error.
5307 xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
5312 if ((cur == NULL) || (buffer == NULL)) return(-1);
5313 buf = xmlBufFromBuffer(buffer);
5314 ret = xmlBufGetNodeContent(buf, cur);
5315 buffer = xmlBufBackToBuffer(buf);
5316 if ((ret < 0) || (buffer == NULL))
5322 * xmlBufGetNodeContent:
5323 * @buf: a buffer xmlBufPtr
5324 * @cur: the node being read
5326 * Read the value of a node @cur, this can be either the text carried
5327 * directly by this node if it's a TEXT node or the aggregate string
5328 * of the values carried by this node child's (TEXT and ENTITY_REF).
5329 * Entity references are substituted.
5330 * Fills up the buffer @buffer with this value
5332 * Returns 0 in case of success and -1 in case of error.
5335 xmlBufGetNodeContent(xmlBufPtr buf, xmlNodePtr cur)
5337 if ((cur == NULL) || (buf == NULL)) return(-1);
5338 switch (cur->type) {
5339 case XML_CDATA_SECTION_NODE:
5341 xmlBufCat(buf, cur->content);
5343 case XML_DOCUMENT_FRAG_NODE:
5344 case XML_ELEMENT_NODE:{
5345 xmlNodePtr tmp = cur;
5347 while (tmp != NULL) {
5348 switch (tmp->type) {
5349 case XML_CDATA_SECTION_NODE:
5351 if (tmp->content != NULL)
5352 xmlBufCat(buf, tmp->content);
5354 case XML_ENTITY_REF_NODE:
5355 xmlBufGetNodeContent(buf, tmp);
5363 if (tmp->children != NULL) {
5364 if (tmp->children->type != XML_ENTITY_DECL) {
5365 tmp = tmp->children;
5372 if (tmp->next != NULL) {
5385 if (tmp->next != NULL) {
5389 } while (tmp != NULL);
5393 case XML_ATTRIBUTE_NODE:{
5394 xmlAttrPtr attr = (xmlAttrPtr) cur;
5395 xmlNodePtr tmp = attr->children;
5397 while (tmp != NULL) {
5398 if (tmp->type == XML_TEXT_NODE)
5399 xmlBufCat(buf, tmp->content);
5401 xmlBufGetNodeContent(buf, tmp);
5406 case XML_COMMENT_NODE:
5408 xmlBufCat(buf, cur->content);
5410 case XML_ENTITY_REF_NODE:{
5414 /* lookup entity declaration */
5415 ent = xmlGetDocEntity(cur->doc, cur->name);
5419 /* an entity content can be any "well balanced chunk",
5420 * i.e. the result of the content [43] production:
5421 * http://www.w3.org/TR/REC-xml#NT-content
5422 * -> we iterate through child nodes and recursive call
5423 * xmlNodeGetContent() which handles all possible node types */
5424 tmp = ent->children;
5426 xmlBufGetNodeContent(buf, tmp);
5431 case XML_ENTITY_NODE:
5432 case XML_DOCUMENT_TYPE_NODE:
5433 case XML_NOTATION_NODE:
5435 case XML_XINCLUDE_START:
5436 case XML_XINCLUDE_END:
5438 case XML_DOCUMENT_NODE:
5439 #ifdef LIBXML_DOCB_ENABLED
5440 case XML_DOCB_DOCUMENT_NODE:
5442 case XML_HTML_DOCUMENT_NODE:
5443 cur = cur->children;
5444 while (cur!= NULL) {
5445 if ((cur->type == XML_ELEMENT_NODE) ||
5446 (cur->type == XML_TEXT_NODE) ||
5447 (cur->type == XML_CDATA_SECTION_NODE)) {
5448 xmlBufGetNodeContent(buf, cur);
5453 case XML_NAMESPACE_DECL:
5454 xmlBufCat(buf, ((xmlNsPtr) cur)->href);
5456 case XML_ELEMENT_DECL:
5457 case XML_ATTRIBUTE_DECL:
5458 case XML_ENTITY_DECL:
5465 * xmlNodeGetContent:
5466 * @cur: the node being read
5468 * Read the value of a node, this can be either the text carried
5469 * directly by this node if it's a TEXT node or the aggregate string
5470 * of the values carried by this node child's (TEXT and ENTITY_REF).
5471 * Entity references are substituted.
5472 * Returns a new #xmlChar * or NULL if no content is available.
5473 * It's up to the caller to free the memory with xmlFree().
5476 xmlNodeGetContent(xmlNodePtr cur)
5480 switch (cur->type) {
5481 case XML_DOCUMENT_FRAG_NODE:
5482 case XML_ELEMENT_NODE:{
5486 buf = xmlBufCreateSize(64);
5489 xmlBufGetNodeContent(buf, cur);
5490 ret = xmlBufDetach(buf);
5494 case XML_ATTRIBUTE_NODE:
5495 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
5496 case XML_COMMENT_NODE:
5498 if (cur->content != NULL)
5499 return (xmlStrdup(cur->content));
5501 case XML_ENTITY_REF_NODE:{
5506 /* lookup entity declaration */
5507 ent = xmlGetDocEntity(cur->doc, cur->name);
5511 buf = xmlBufCreate();
5515 xmlBufGetNodeContent(buf, cur);
5517 ret = xmlBufDetach(buf);
5521 case XML_ENTITY_NODE:
5522 case XML_DOCUMENT_TYPE_NODE:
5523 case XML_NOTATION_NODE:
5525 case XML_XINCLUDE_START:
5526 case XML_XINCLUDE_END:
5528 case XML_DOCUMENT_NODE:
5529 #ifdef LIBXML_DOCB_ENABLED
5530 case XML_DOCB_DOCUMENT_NODE:
5532 case XML_HTML_DOCUMENT_NODE: {
5536 buf = xmlBufCreate();
5540 xmlBufGetNodeContent(buf, (xmlNodePtr) cur);
5542 ret = xmlBufDetach(buf);
5546 case XML_NAMESPACE_DECL: {
5549 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5552 case XML_ELEMENT_DECL:
5555 case XML_ATTRIBUTE_DECL:
5558 case XML_ENTITY_DECL:
5561 case XML_CDATA_SECTION_NODE:
5563 if (cur->content != NULL)
5564 return (xmlStrdup(cur->content));
5571 * xmlNodeSetContent:
5572 * @cur: the node being modified
5573 * @content: the new value of the content
5575 * Replace the content of a node.
5576 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5577 * references, but XML special chars need to be escaped first by using
5578 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5581 xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5584 xmlGenericError(xmlGenericErrorContext,
5585 "xmlNodeSetContent : node == NULL\n");
5589 switch (cur->type) {
5590 case XML_DOCUMENT_FRAG_NODE:
5591 case XML_ELEMENT_NODE:
5592 case XML_ATTRIBUTE_NODE:
5593 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5594 cur->children = xmlStringGetNodeList(cur->doc, content);
5595 UPDATE_LAST_CHILD_AND_PARENT(cur)
5598 case XML_CDATA_SECTION_NODE:
5599 case XML_ENTITY_REF_NODE:
5600 case XML_ENTITY_NODE:
5602 case XML_COMMENT_NODE:
5603 if ((cur->content != NULL) &&
5604 (cur->content != (xmlChar *) &(cur->properties))) {
5605 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5606 (xmlDictOwns(cur->doc->dict, cur->content))))
5607 xmlFree(cur->content);
5609 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5610 cur->last = cur->children = NULL;
5611 if (content != NULL) {
5612 cur->content = xmlStrdup(content);
5614 cur->content = NULL;
5615 cur->properties = NULL;
5618 case XML_DOCUMENT_NODE:
5619 case XML_HTML_DOCUMENT_NODE:
5620 case XML_DOCUMENT_TYPE_NODE:
5621 case XML_XINCLUDE_START:
5622 case XML_XINCLUDE_END:
5623 #ifdef LIBXML_DOCB_ENABLED
5624 case XML_DOCB_DOCUMENT_NODE:
5627 case XML_NOTATION_NODE:
5631 case XML_NAMESPACE_DECL:
5633 case XML_ELEMENT_DECL:
5636 case XML_ATTRIBUTE_DECL:
5639 case XML_ENTITY_DECL:
5645 #ifdef LIBXML_TREE_ENABLED
5647 * xmlNodeSetContentLen:
5648 * @cur: the node being modified
5649 * @content: the new value of the content
5650 * @len: the size of @content
5652 * Replace the content of a node.
5653 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5654 * references, but XML special chars need to be escaped first by using
5655 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5658 xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5661 xmlGenericError(xmlGenericErrorContext,
5662 "xmlNodeSetContentLen : node == NULL\n");
5666 switch (cur->type) {
5667 case XML_DOCUMENT_FRAG_NODE:
5668 case XML_ELEMENT_NODE:
5669 case XML_ATTRIBUTE_NODE:
5670 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5671 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5672 UPDATE_LAST_CHILD_AND_PARENT(cur)
5675 case XML_CDATA_SECTION_NODE:
5676 case XML_ENTITY_REF_NODE:
5677 case XML_ENTITY_NODE:
5679 case XML_COMMENT_NODE:
5680 case XML_NOTATION_NODE:
5681 if ((cur->content != NULL) &&
5682 (cur->content != (xmlChar *) &(cur->properties))) {
5683 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5684 (xmlDictOwns(cur->doc->dict, cur->content))))
5685 xmlFree(cur->content);
5687 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5688 cur->children = cur->last = NULL;
5689 if (content != NULL) {
5690 cur->content = xmlStrndup(content, len);
5692 cur->content = NULL;
5693 cur->properties = NULL;
5696 case XML_DOCUMENT_NODE:
5698 case XML_HTML_DOCUMENT_NODE:
5699 case XML_DOCUMENT_TYPE_NODE:
5700 case XML_NAMESPACE_DECL:
5701 case XML_XINCLUDE_START:
5702 case XML_XINCLUDE_END:
5703 #ifdef LIBXML_DOCB_ENABLED
5704 case XML_DOCB_DOCUMENT_NODE:
5707 case XML_ELEMENT_DECL:
5710 case XML_ATTRIBUTE_DECL:
5713 case XML_ENTITY_DECL:
5718 #endif /* LIBXML_TREE_ENABLED */
5721 * xmlNodeAddContentLen:
5722 * @cur: the node being modified
5723 * @content: extra content
5724 * @len: the size of @content
5726 * Append the extra substring to the node content.
5727 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5728 * raw text, so unescaped XML special chars are allowed, entity
5729 * references are not supported.
5732 xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5735 xmlGenericError(xmlGenericErrorContext,
5736 "xmlNodeAddContentLen : node == NULL\n");
5740 if (len <= 0) return;
5741 switch (cur->type) {
5742 case XML_DOCUMENT_FRAG_NODE:
5743 case XML_ELEMENT_NODE: {
5744 xmlNodePtr last, newNode, tmp;
5747 newNode = xmlNewTextLen(content, len);
5748 if (newNode != NULL) {
5749 tmp = xmlAddChild(cur, newNode);
5752 if ((last != NULL) && (last->next == newNode)) {
5753 xmlTextMerge(last, newNode);
5758 case XML_ATTRIBUTE_NODE:
5761 case XML_CDATA_SECTION_NODE:
5762 case XML_ENTITY_REF_NODE:
5763 case XML_ENTITY_NODE:
5765 case XML_COMMENT_NODE:
5766 case XML_NOTATION_NODE:
5767 if (content != NULL) {
5768 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5769 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5770 xmlDictOwns(cur->doc->dict, cur->content))) {
5771 cur->content = xmlStrncatNew(cur->content, content, len);
5772 cur->properties = NULL;
5776 cur->content = xmlStrncat(cur->content, content, len);
5778 case XML_DOCUMENT_NODE:
5780 case XML_HTML_DOCUMENT_NODE:
5781 case XML_DOCUMENT_TYPE_NODE:
5782 case XML_NAMESPACE_DECL:
5783 case XML_XINCLUDE_START:
5784 case XML_XINCLUDE_END:
5785 #ifdef LIBXML_DOCB_ENABLED
5786 case XML_DOCB_DOCUMENT_NODE:
5789 case XML_ELEMENT_DECL:
5790 case XML_ATTRIBUTE_DECL:
5791 case XML_ENTITY_DECL:
5797 * xmlNodeAddContent:
5798 * @cur: the node being modified
5799 * @content: extra content
5801 * Append the extra substring to the node content.
5802 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5803 * raw text, so unescaped XML special chars are allowed, entity
5804 * references are not supported.
5807 xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5812 xmlGenericError(xmlGenericErrorContext,
5813 "xmlNodeAddContent : node == NULL\n");
5817 if (content == NULL) return;
5818 len = xmlStrlen(content);
5819 xmlNodeAddContentLen(cur, content, len);
5824 * @first: the first text node
5825 * @second: the second text node being merged
5827 * Merge two text nodes into one
5828 * Returns the first text node augmented
5831 xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5832 if (first == NULL) return(second);
5833 if (second == NULL) return(first);
5834 if (first->type != XML_TEXT_NODE) return(first);
5835 if (second->type != XML_TEXT_NODE) return(first);
5836 if (second->name != first->name)
5838 xmlNodeAddContent(first, second->content);
5839 xmlUnlinkNode(second);
5840 xmlFreeNode(second);
5844 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
5847 * @doc: the document
5848 * @node: the current node
5850 * Search all the namespace applying to a given element.
5851 * Returns an NULL terminated array of all the #xmlNsPtr found
5852 * that need to be freed by the caller or NULL if no
5853 * namespace if defined
5856 xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5859 xmlNsPtr *ret = NULL;
5864 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5867 while (node != NULL) {
5868 if (node->type == XML_ELEMENT_NODE) {
5870 while (cur != NULL) {
5873 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5876 xmlTreeErrMemory("getting namespace list");
5881 for (i = 0; i < nbns; i++) {
5882 if ((cur->prefix == ret[i]->prefix) ||
5883 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5887 if (nbns >= maxns) {
5889 ret = (xmlNsPtr *) xmlRealloc(ret,
5894 xmlTreeErrMemory("getting namespace list");
5905 node = node->parent;
5909 #endif /* LIBXML_TREE_ENABLED */
5912 * xmlTreeEnsureXMLDecl:
5915 * Ensures that there is an XML namespace declaration on the doc.
5917 * Returns the XML ns-struct or NULL on API and internal errors.
5920 xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5924 if (doc->oldNs != NULL)
5925 return (doc->oldNs);
5928 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5931 "allocating the XML namespace");
5934 memset(ns, 0, sizeof(xmlNs));
5935 ns->type = XML_LOCAL_NAMESPACE;
5936 ns->href = xmlStrdup(XML_XML_NAMESPACE);
5937 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5945 * @doc: the document
5946 * @node: the current node
5947 * @nameSpace: the namespace prefix
5949 * Search a Ns registered under a given name space for a document.
5950 * recurse on the parents until it finds the defined namespace
5951 * or return NULL otherwise.
5952 * @nameSpace can be NULL, this is a search for the default namespace.
5953 * We don't allow to cross entities boundaries. If you don't declare
5954 * the namespace within those you will be in troubles !!! A warning
5955 * is generated to cover this case.
5957 * Returns the namespace pointer or NULL.
5960 xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
5963 xmlNodePtr orig = node;
5965 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL);
5966 if ((nameSpace != NULL) &&
5967 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
5968 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5970 * The XML-1.0 namespace is normally held on the root
5971 * element. In this case exceptionally create it on the
5974 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5976 xmlTreeErrMemory("searching namespace");
5979 memset(cur, 0, sizeof(xmlNs));
5980 cur->type = XML_LOCAL_NAMESPACE;
5981 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5982 cur->prefix = xmlStrdup((const xmlChar *)"xml");
5983 cur->next = node->nsDef;
5993 * Return the XML namespace declaration held by the doc.
5995 if (doc->oldNs == NULL)
5996 return(xmlTreeEnsureXMLDecl(doc));
6000 while (node != NULL) {
6001 if ((node->type == XML_ENTITY_REF_NODE) ||
6002 (node->type == XML_ENTITY_NODE) ||
6003 (node->type == XML_ENTITY_DECL))
6005 if (node->type == XML_ELEMENT_NODE) {
6007 while (cur != NULL) {
6008 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6009 (cur->href != NULL))
6011 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6012 (cur->href != NULL) &&
6013 (xmlStrEqual(cur->prefix, nameSpace)))
6020 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6021 (cur->href != NULL))
6023 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6024 (cur->href != NULL) &&
6025 (xmlStrEqual(cur->prefix, nameSpace)))
6030 node = node->parent;
6037 * @doc: the document
6038 * @node: the current node
6039 * @ancestor: the ancestor carrying the namespace
6040 * @prefix: the namespace prefix
6042 * Verify that the given namespace held on @ancestor is still in scope
6045 * Returns 1 if true, 0 if false and -1 in case of error.
6048 xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
6049 xmlNodePtr ancestor, const xmlChar * prefix)
6053 while ((node != NULL) && (node != ancestor)) {
6054 if ((node->type == XML_ENTITY_REF_NODE) ||
6055 (node->type == XML_ENTITY_NODE) ||
6056 (node->type == XML_ENTITY_DECL))
6058 if (node->type == XML_ELEMENT_NODE) {
6060 while (tst != NULL) {
6061 if ((tst->prefix == NULL)
6062 && (prefix == NULL))
6064 if ((tst->prefix != NULL)
6066 && (xmlStrEqual(tst->prefix, prefix)))
6071 node = node->parent;
6073 if (node != ancestor)
6079 * xmlSearchNsByHref:
6080 * @doc: the document
6081 * @node: the current node
6082 * @href: the namespace value
6084 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
6085 * the defined namespace or return NULL otherwise.
6086 * Returns the namespace pointer or NULL.
6089 xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
6092 xmlNodePtr orig = node;
6095 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL))
6097 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
6099 * Only the document can hold the XML spec namespace.
6101 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6103 * The XML-1.0 namespace is normally held on the root
6104 * element. In this case exceptionally create it on the
6107 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6109 xmlTreeErrMemory("searching namespace");
6112 memset(cur, 0, sizeof(xmlNs));
6113 cur->type = XML_LOCAL_NAMESPACE;
6114 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6115 cur->prefix = xmlStrdup((const xmlChar *) "xml");
6116 cur->next = node->nsDef;
6126 * Return the XML namespace declaration held by the doc.
6128 if (doc->oldNs == NULL)
6129 return(xmlTreeEnsureXMLDecl(doc));
6133 is_attr = (node->type == XML_ATTRIBUTE_NODE);
6134 while (node != NULL) {
6135 if ((node->type == XML_ENTITY_REF_NODE) ||
6136 (node->type == XML_ENTITY_NODE) ||
6137 (node->type == XML_ENTITY_DECL))
6139 if (node->type == XML_ELEMENT_NODE) {
6141 while (cur != NULL) {
6142 if ((cur->href != NULL) && (href != NULL) &&
6143 (xmlStrEqual(cur->href, href))) {
6144 if (((!is_attr) || (cur->prefix != NULL)) &&
6145 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6153 if ((cur->href != NULL) && (href != NULL) &&
6154 (xmlStrEqual(cur->href, href))) {
6155 if (((!is_attr) || (cur->prefix != NULL)) &&
6156 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6162 node = node->parent;
6168 * xmlNewReconciliedNs:
6169 * @doc: the document
6170 * @tree: a node expected to hold the new namespace
6171 * @ns: the original namespace
6173 * This function tries to locate a namespace definition in a tree
6174 * ancestors, or create a new namespace definition node similar to
6175 * @ns trying to reuse the same prefix. However if the given prefix is
6176 * null (default namespace) or reused within the subtree defined by
6177 * @tree or on one of its ancestors then a new prefix is generated.
6178 * Returns the (new) namespace definition or NULL in case of error
6181 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
6186 if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) {
6188 xmlGenericError(xmlGenericErrorContext,
6189 "xmlNewReconciliedNs : tree == NULL\n");
6193 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
6195 xmlGenericError(xmlGenericErrorContext,
6196 "xmlNewReconciliedNs : ns == NULL\n");
6201 * Search an existing namespace definition inherited.
6203 def = xmlSearchNsByHref(doc, tree, ns->href);
6208 * Find a close prefix which is not already in use.
6209 * Let's strip namespace prefixes longer than 20 chars !
6211 if (ns->prefix == NULL)
6212 snprintf((char *) prefix, sizeof(prefix), "default");
6214 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
6216 def = xmlSearchNs(doc, tree, prefix);
6217 while (def != NULL) {
6218 if (counter > 1000) return(NULL);
6219 if (ns->prefix == NULL)
6220 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
6222 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
6223 (char *)ns->prefix, counter++);
6224 def = xmlSearchNs(doc, tree, prefix);
6228 * OK, now we are ready to create a new one.
6230 def = xmlNewNs(tree, ns->href, prefix);
6234 #ifdef LIBXML_TREE_ENABLED
6236 * xmlReconciliateNs:
6237 * @doc: the document
6238 * @tree: a node defining the subtree to reconciliate
6240 * This function checks that all the namespaces declared within the given
6241 * tree are properly declared. This is needed for example after Copy or Cut
6242 * and then paste operations. The subtree may still hold pointers to
6243 * namespace declarations outside the subtree or invalid/masked. As much
6244 * as possible the function try to reuse the existing namespaces found in
6245 * the new environment. If not possible the new namespaces are redeclared
6246 * on @tree at the top of the given subtree.
6247 * Returns the number of namespace declarations created or -1 in case of error.
6250 xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
6251 xmlNsPtr *oldNs = NULL;
6252 xmlNsPtr *newNs = NULL;
6257 xmlNodePtr node = tree;
6261 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6262 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
6263 if (node->doc != doc) return(-1);
6264 while (node != NULL) {
6266 * Reconciliate the node namespace
6268 if (node->ns != NULL) {
6270 * initialize the cache if needed
6272 if (sizeCache == 0) {
6274 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6276 if (oldNs == NULL) {
6277 xmlTreeErrMemory("fixing namespaces");
6280 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6282 if (newNs == NULL) {
6283 xmlTreeErrMemory("fixing namespaces");
6288 for (i = 0;i < nbCache;i++) {
6289 if (oldNs[i] == node->ns) {
6290 node->ns = newNs[i];
6296 * OK we need to recreate a new namespace definition
6298 n = xmlNewReconciliedNs(doc, tree, node->ns);
6299 if (n != NULL) { /* :-( what if else ??? */
6301 * check if we need to grow the cache buffers.
6303 if (sizeCache <= nbCache) {
6305 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
6307 if (oldNs == NULL) {
6308 xmlTreeErrMemory("fixing namespaces");
6312 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
6314 if (newNs == NULL) {
6315 xmlTreeErrMemory("fixing namespaces");
6321 oldNs[nbCache++] = node->ns;
6327 * now check for namespace hold by attributes on the node.
6329 if (node->type == XML_ELEMENT_NODE) {
6330 attr = node->properties;
6331 while (attr != NULL) {
6332 if (attr->ns != NULL) {
6334 * initialize the cache if needed
6336 if (sizeCache == 0) {
6338 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6340 if (oldNs == NULL) {
6341 xmlTreeErrMemory("fixing namespaces");
6344 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6346 if (newNs == NULL) {
6347 xmlTreeErrMemory("fixing namespaces");
6352 for (i = 0;i < nbCache;i++) {
6353 if (oldNs[i] == attr->ns) {
6354 attr->ns = newNs[i];
6360 * OK we need to recreate a new namespace definition
6362 n = xmlNewReconciliedNs(doc, tree, attr->ns);
6363 if (n != NULL) { /* :-( what if else ??? */
6365 * check if we need to grow the cache buffers.
6367 if (sizeCache <= nbCache) {
6369 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
6370 sizeCache * sizeof(xmlNsPtr));
6371 if (oldNs == NULL) {
6372 xmlTreeErrMemory("fixing namespaces");
6376 newNs = (xmlNsPtr *) xmlRealloc(newNs,
6377 sizeCache * sizeof(xmlNsPtr));
6378 if (newNs == NULL) {
6379 xmlTreeErrMemory("fixing namespaces");
6385 oldNs[nbCache++] = attr->ns;
6395 * Browse the full subtree, deep first
6397 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
6399 node = node->children;
6400 } else if ((node != tree) && (node->next != NULL)) {
6403 } else if (node != tree) {
6404 /* go up to parents->next if needed */
6405 while (node != tree) {
6406 if (node->parent != NULL)
6407 node = node->parent;
6408 if ((node != tree) && (node->next != NULL)) {
6412 if (node->parent == NULL) {
6417 /* exit condition */
6429 #endif /* LIBXML_TREE_ENABLED */
6432 xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name,
6433 const xmlChar *nsName, int useDTD)
6437 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6440 if (node->properties != NULL) {
6441 prop = node->properties;
6442 if (nsName == NULL) {
6444 * We want the attr to be in no namespace.
6447 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6451 } while (prop != NULL);
6454 * We want the attr to be in the specified namespace.
6457 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6458 ((prop->ns->href == nsName) ||
6459 xmlStrEqual(prop->ns->href, nsName)))
6464 } while (prop != NULL);
6468 #ifdef LIBXML_TREE_ENABLED
6472 * Check if there is a default/fixed attribute declaration in
6473 * the internal or external subset.
6475 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6476 xmlDocPtr doc = node->doc;
6477 xmlAttributePtr attrDecl = NULL;
6478 xmlChar *elemQName, *tmpstr = NULL;
6481 * We need the QName of the element for the DTD-lookup.
6483 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6484 tmpstr = xmlStrdup(node->ns->prefix);
6485 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6486 tmpstr = xmlStrcat(tmpstr, node->name);
6491 elemQName = (xmlChar *) node->name;
6492 if (nsName == NULL) {
6494 * The common and nice case: Attr in no namespace.
6496 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6497 elemQName, name, NULL);
6498 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6499 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6500 elemQName, name, NULL);
6503 xmlNsPtr *nsList, *cur;
6506 * The ugly case: Search using the prefixes of in-scope
6507 * ns-decls corresponding to @nsName.
6509 nsList = xmlGetNsList(node->doc, node);
6510 if (nsList == NULL) {
6516 while (*cur != NULL) {
6517 if (xmlStrEqual((*cur)->href, nsName)) {
6518 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6519 name, (*cur)->prefix);
6522 if (doc->extSubset != NULL) {
6523 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6524 name, (*cur)->prefix);
6536 * Only default/fixed attrs are relevant.
6538 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6539 return((xmlAttrPtr) attrDecl);
6541 #endif /* LIBXML_TREE_ENABLED */
6546 xmlGetPropNodeValueInternal(xmlAttrPtr prop)
6550 if (prop->type == XML_ATTRIBUTE_NODE) {
6552 * Note that we return at least the empty string.
6553 * TODO: Do we really always want that?
6555 if (prop->children != NULL) {
6556 if ((prop->children->next == NULL) &&
6557 ((prop->children->type == XML_TEXT_NODE) ||
6558 (prop->children->type == XML_CDATA_SECTION_NODE)))
6561 * Optimization for the common case: only 1 text node.
6563 return(xmlStrdup(prop->children->content));
6567 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6572 return(xmlStrdup((xmlChar *)""));
6573 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6574 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6582 * @name: the attribute name
6584 * Search an attribute associated to a node
6585 * This function also looks in DTD attribute declaration for #FIXED or
6586 * default declaration values unless DTD use has been turned off.
6588 * Returns the attribute or the attribute declaration or NULL if
6589 * neither was found.
6592 xmlHasProp(xmlNodePtr node, const xmlChar *name) {
6596 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6599 * Check on the properties attached to the node
6601 prop = node->properties;
6602 while (prop != NULL) {
6603 if (xmlStrEqual(prop->name, name)) {
6608 if (!xmlCheckDTD) return(NULL);
6611 * Check if there is a default declaration in the internal
6612 * or external subsets
6616 xmlAttributePtr attrDecl;
6617 if (doc->intSubset != NULL) {
6618 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6619 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6620 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6621 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6622 /* return attribute declaration only if a default value is given
6623 (that includes #FIXED declarations) */
6624 return((xmlAttrPtr) attrDecl);
6633 * @name: the attribute name
6634 * @nameSpace: the URI of the namespace
6636 * Search for an attribute associated to a node
6637 * This attribute has to be anchored in the namespace specified.
6638 * This does the entity substitution.
6639 * This function looks in DTD attribute declaration for #FIXED or
6640 * default declaration values unless DTD use has been turned off.
6641 * Note that a namespace of NULL indicates to use the default namespace.
6643 * Returns the attribute or the attribute declaration or NULL
6644 * if neither was found.
6647 xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
6649 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
6655 * @name: the attribute name
6657 * Search and get the value of an attribute associated to a node
6658 * This does the entity substitution.
6659 * This function looks in DTD attribute declaration for #FIXED or
6660 * default declaration values unless DTD use has been turned off.
6661 * NOTE: this function acts independently of namespaces associated
6662 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6663 * for namespace aware processing.
6665 * Returns the attribute value or NULL if not found.
6666 * It's up to the caller to free the memory with xmlFree().
6669 xmlGetProp(xmlNodePtr node, const xmlChar *name) {
6672 prop = xmlHasProp(node, name);
6675 return(xmlGetPropNodeValueInternal(prop));
6681 * @name: the attribute name
6683 * Search and get the value of an attribute associated to a node
6684 * This does the entity substitution.
6685 * This function looks in DTD attribute declaration for #FIXED or
6686 * default declaration values unless DTD use has been turned off.
6687 * This function is similar to xmlGetProp except it will accept only
6688 * an attribute in no namespace.
6690 * Returns the attribute value or NULL if not found.
6691 * It's up to the caller to free the memory with xmlFree().
6694 xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
6697 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6700 return(xmlGetPropNodeValueInternal(prop));
6706 * @name: the attribute name
6707 * @nameSpace: the URI of the namespace
6709 * Search and get the value of an attribute associated to a node
6710 * This attribute has to be anchored in the namespace specified.
6711 * This does the entity substitution.
6712 * This function looks in DTD attribute declaration for #FIXED or
6713 * default declaration values unless DTD use has been turned off.
6715 * Returns the attribute value or NULL if not found.
6716 * It's up to the caller to free the memory with xmlFree().
6719 xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
6722 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6725 return(xmlGetPropNodeValueInternal(prop));
6728 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6732 * @name: the attribute name
6734 * Remove an attribute carried by a node.
6735 * This handles only attributes in no namespace.
6736 * Returns 0 if successful, -1 if not found
6739 xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
6742 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6745 xmlUnlinkNode((xmlNodePtr) prop);
6753 * @ns: the namespace definition
6754 * @name: the attribute name
6756 * Remove an attribute carried by a node.
6757 * Returns 0 if successful, -1 if not found
6760 xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
6763 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6766 xmlUnlinkNode((xmlNodePtr) prop);
6772 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
6776 * @name: the attribute name (a QName)
6777 * @value: the attribute value
6779 * Set (or reset) an attribute carried by a node.
6780 * If @name has a prefix, then the corresponding
6781 * namespace-binding will be used, if in scope; it is an
6782 * error it there's no such ns-binding for the prefix in
6784 * Returns the attribute pointer.
6788 xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
6790 const xmlChar *nqname;
6792 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
6798 nqname = xmlSplitQName3(name, &len);
6799 if (nqname != NULL) {
6801 xmlChar *prefix = xmlStrndup(name, len);
6802 ns = xmlSearchNs(node->doc, node, prefix);
6806 return(xmlSetNsProp(node, ns, nqname, value));
6808 return(xmlSetNsProp(node, NULL, name, value));
6814 * @ns: the namespace definition
6815 * @name: the attribute name
6816 * @value: the attribute value
6818 * Set (or reset) an attribute carried by a node.
6819 * The ns structure must be in scope, this is not checked
6821 * Returns the attribute pointer.
6824 xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6825 const xmlChar *value)
6829 if (ns && (ns->href == NULL))
6831 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6834 * Modify the attribute's value.
6836 if (prop->atype == XML_ATTRIBUTE_ID) {
6837 xmlRemoveID(node->doc, prop);
6838 prop->atype = XML_ATTRIBUTE_ID;
6840 if (prop->children != NULL)
6841 xmlFreeNodeList(prop->children);
6842 prop->children = NULL;
6845 if (value != NULL) {
6848 if(!xmlCheckUTF8(value)) {
6849 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc,
6851 if (node->doc != NULL)
6852 node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
6854 prop->children = xmlNewDocText(node->doc, value);
6856 tmp = prop->children;
6857 while (tmp != NULL) {
6858 tmp->parent = (xmlNodePtr) prop;
6859 if (tmp->next == NULL)
6864 if (prop->atype == XML_ATTRIBUTE_ID)
6865 xmlAddID(NULL, node->doc, value, prop);
6869 * No equal attr found; create a new one.
6871 return(xmlNewPropInternal(node, ns, name, value, 0));
6874 #endif /* LIBXML_TREE_ENABLED */
6880 * Is this node a Text node ?
6881 * Returns 1 yes, 0 no
6884 xmlNodeIsText(xmlNodePtr node) {
6885 if (node == NULL) return(0);
6887 if (node->type == XML_TEXT_NODE) return(1);
6895 * Checks whether this node is an empty or whitespace only
6896 * (and possibly ignorable) text-node.
6898 * Returns 1 yes, 0 no
6901 xmlIsBlankNode(xmlNodePtr node) {
6903 if (node == NULL) return(0);
6905 if ((node->type != XML_TEXT_NODE) &&
6906 (node->type != XML_CDATA_SECTION_NODE))
6908 if (node->content == NULL) return(1);
6909 cur = node->content;
6911 if (!IS_BLANK_CH(*cur)) return(0);
6921 * @content: the content
6922 * @len: @content length
6924 * Concat the given string at the end of the existing node content
6926 * Returns -1 in case of error, 0 otherwise
6930 xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
6931 if (node == NULL) return(-1);
6933 if ((node->type != XML_TEXT_NODE) &&
6934 (node->type != XML_CDATA_SECTION_NODE) &&
6935 (node->type != XML_COMMENT_NODE) &&
6936 (node->type != XML_PI_NODE)) {
6938 xmlGenericError(xmlGenericErrorContext,
6939 "xmlTextConcat: node is not text nor CDATA\n");
6943 /* need to check if content is currently in the dictionary */
6944 if ((node->content == (xmlChar *) &(node->properties)) ||
6945 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6946 xmlDictOwns(node->doc->dict, node->content))) {
6947 node->content = xmlStrncatNew(node->content, content, len);
6949 node->content = xmlStrncat(node->content, content, len);
6951 node->properties = NULL;
6952 if (node->content == NULL)
6957 /************************************************************************
6959 * Output : to a FILE or in memory *
6961 ************************************************************************/
6966 * routine to create an XML buffer.
6967 * returns the new structure.
6970 xmlBufferCreate(void) {
6973 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6975 xmlTreeErrMemory("creating buffer");
6979 ret->size = xmlDefaultBufferSize;
6980 ret->alloc = xmlBufferAllocScheme;
6981 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
6982 if (ret->content == NULL) {
6983 xmlTreeErrMemory("creating buffer");
6987 ret->content[0] = 0;
6988 ret->contentIO = NULL;
6993 * xmlBufferCreateSize:
6994 * @size: initial size of buffer
6996 * routine to create an XML buffer.
6997 * returns the new structure.
7000 xmlBufferCreateSize(size_t size) {
7003 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7005 xmlTreeErrMemory("creating buffer");
7009 ret->alloc = xmlBufferAllocScheme;
7010 ret->size = (size ? size+2 : 0); /* +1 for ending null */
7012 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
7013 if (ret->content == NULL) {
7014 xmlTreeErrMemory("creating buffer");
7018 ret->content[0] = 0;
7020 ret->content = NULL;
7021 ret->contentIO = NULL;
7029 * Remove the string contained in a buffer and gie it back to the
7030 * caller. The buffer is reset to an empty content.
7031 * This doesn't work with immutable buffers as they can't be reset.
7033 * Returns the previous string contained by the buffer.
7036 xmlBufferDetach(xmlBufferPtr buf) {
7041 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
7045 buf->content = NULL;
7054 * xmlBufferCreateStatic:
7055 * @mem: the memory area
7056 * @size: the size in byte
7058 * routine to create an XML buffer from an immutable memory area.
7059 * The area won't be modified nor copied, and is expected to be
7060 * present until the end of the buffer lifetime.
7062 * returns the new structure.
7065 xmlBufferCreateStatic(void *mem, size_t size) {
7068 if ((mem == NULL) || (size == 0))
7071 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7073 xmlTreeErrMemory("creating buffer");
7078 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
7079 ret->content = (xmlChar *) mem;
7084 * xmlBufferSetAllocationScheme:
7085 * @buf: the buffer to tune
7086 * @scheme: allocation scheme to use
7088 * Sets the allocation scheme for this buffer
7091 xmlBufferSetAllocationScheme(xmlBufferPtr buf,
7092 xmlBufferAllocationScheme scheme) {
7095 xmlGenericError(xmlGenericErrorContext,
7096 "xmlBufferSetAllocationScheme: buf == NULL\n");
7100 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7101 (buf->alloc == XML_BUFFER_ALLOC_IO)) return;
7102 if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
7103 (scheme == XML_BUFFER_ALLOC_EXACT) ||
7104 (scheme == XML_BUFFER_ALLOC_HYBRID) ||
7105 (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
7106 buf->alloc = scheme;
7111 * @buf: the buffer to free
7113 * Frees an XML buffer. It frees both the content and the structure which
7117 xmlBufferFree(xmlBufferPtr buf) {
7120 xmlGenericError(xmlGenericErrorContext,
7121 "xmlBufferFree: buf == NULL\n");
7126 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7127 (buf->contentIO != NULL)) {
7128 xmlFree(buf->contentIO);
7129 } else if ((buf->content != NULL) &&
7130 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
7131 xmlFree(buf->content);
7143 xmlBufferEmpty(xmlBufferPtr buf) {
7144 if (buf == NULL) return;
7145 if (buf->content == NULL) return;
7147 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
7148 buf->content = BAD_CAST "";
7149 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7150 (buf->contentIO != NULL)) {
7151 size_t start_buf = buf->content - buf->contentIO;
7153 buf->size += start_buf;
7154 buf->content = buf->contentIO;
7155 buf->content[0] = 0;
7157 buf->content[0] = 0;
7163 * @buf: the buffer to dump
7164 * @len: the number of xmlChar to remove
7166 * Remove the beginning of an XML buffer.
7168 * Returns the number of #xmlChar removed, or -1 in case of failure.
7171 xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
7172 if (buf == NULL) return(-1);
7173 if (len == 0) return(0);
7174 if (len > buf->use) return(-1);
7177 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7178 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
7180 * we just move the content pointer, but also make sure
7181 * the perceived buffer size has shrinked accordingly
7183 buf->content += len;
7187 * sometimes though it maybe be better to really shrink
7190 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7191 size_t start_buf = buf->content - buf->contentIO;
7192 if (start_buf >= buf->size) {
7193 memmove(buf->contentIO, &buf->content[0], buf->use);
7194 buf->content = buf->contentIO;
7195 buf->content[buf->use] = 0;
7196 buf->size += start_buf;
7200 memmove(buf->content, &buf->content[len], buf->use);
7201 buf->content[buf->use] = 0;
7209 * @len: the minimum free size to allocate
7211 * Grow the available space of an XML buffer.
7213 * Returns the new available space or -1 in case of error
7216 xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
7220 if (buf == NULL) return(-1);
7222 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7223 if (len + buf->use < buf->size) return(0);
7226 * Windows has a BIG problem on realloc timing, so we try to double
7227 * the buffer size (if that's enough) (bug 146697)
7228 * Apparently BSD too, and it's probably best for linux too
7229 * On an embedded system this may be something to change
7232 if (buf->size > len)
7233 size = buf->size * 2;
7235 size = buf->use + len + 100;
7237 size = buf->use + len + 100;
7240 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7241 size_t start_buf = buf->content - buf->contentIO;
7243 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
7244 if (newbuf == NULL) {
7245 xmlTreeErrMemory("growing buffer");
7248 buf->contentIO = newbuf;
7249 buf->content = newbuf + start_buf;
7251 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
7252 if (newbuf == NULL) {
7253 xmlTreeErrMemory("growing buffer");
7256 buf->content = newbuf;
7259 return(buf->size - buf->use);
7264 * @file: the file output
7265 * @buf: the buffer to dump
7267 * Dumps an XML buffer to a FILE *.
7268 * Returns the number of #xmlChar written
7271 xmlBufferDump(FILE *file, xmlBufferPtr buf) {
7276 xmlGenericError(xmlGenericErrorContext,
7277 "xmlBufferDump: buf == NULL\n");
7281 if (buf->content == NULL) {
7283 xmlGenericError(xmlGenericErrorContext,
7284 "xmlBufferDump: buf->content == NULL\n");
7290 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
7298 * Function to extract the content of a buffer
7300 * Returns the internal content
7304 xmlBufferContent(const xmlBufferPtr buf)
7309 return buf->content;
7316 * Function to get the length of a buffer
7318 * Returns the length of data in the internal content
7322 xmlBufferLength(const xmlBufferPtr buf)
7332 * @buf: the buffer to resize
7333 * @size: the desired size
7335 * Resize a buffer to accommodate minimum size of @size.
7337 * Returns 0 in case of problems, 1 otherwise
7340 xmlBufferResize(xmlBufferPtr buf, unsigned int size)
7342 unsigned int newSize;
7343 xmlChar* rebuf = NULL;
7349 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7351 /* Don't resize if we don't have to */
7352 if (size < buf->size)
7355 /* figure out new size */
7356 switch (buf->alloc){
7357 case XML_BUFFER_ALLOC_IO:
7358 case XML_BUFFER_ALLOC_DOUBLEIT:
7359 /*take care of empty case*/
7360 newSize = (buf->size ? buf->size*2 : size + 10);
7361 while (size > newSize) {
7362 if (newSize > UINT_MAX / 2) {
7363 xmlTreeErrMemory("growing buffer");
7369 case XML_BUFFER_ALLOC_EXACT:
7372 case XML_BUFFER_ALLOC_HYBRID:
7373 if (buf->use < BASE_BUFFER_SIZE)
7376 newSize = buf->size * 2;
7377 while (size > newSize) {
7378 if (newSize > UINT_MAX / 2) {
7379 xmlTreeErrMemory("growing buffer");
7392 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7393 start_buf = buf->content - buf->contentIO;
7395 if (start_buf > newSize) {
7396 /* move data back to start */
7397 memmove(buf->contentIO, buf->content, buf->use);
7398 buf->content = buf->contentIO;
7399 buf->content[buf->use] = 0;
7400 buf->size += start_buf;
7402 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
7403 if (rebuf == NULL) {
7404 xmlTreeErrMemory("growing buffer");
7407 buf->contentIO = rebuf;
7408 buf->content = rebuf + start_buf;
7411 if (buf->content == NULL) {
7412 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7413 } else if (buf->size - buf->use < 100) {
7414 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
7417 * if we are reallocating a buffer far from being full, it's
7418 * better to make a new allocation and copy only the used range
7419 * and free the old one.
7421 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7422 if (rebuf != NULL) {
7423 memcpy(rebuf, buf->content, buf->use);
7424 xmlFree(buf->content);
7425 rebuf[buf->use] = 0;
7428 if (rebuf == NULL) {
7429 xmlTreeErrMemory("growing buffer");
7432 buf->content = rebuf;
7434 buf->size = newSize;
7441 * @buf: the buffer to dump
7442 * @str: the #xmlChar string
7443 * @len: the number of #xmlChar to add
7445 * Add a string range to an XML buffer. if len == -1, the length of
7446 * str is recomputed.
7448 * Returns 0 successful, a positive error code number otherwise
7449 * and -1 in case of internal or API error.
7452 xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
7453 unsigned int needSize;
7455 if ((str == NULL) || (buf == NULL)) {
7458 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7461 xmlGenericError(xmlGenericErrorContext,
7462 "xmlBufferAdd: len < 0\n");
7466 if (len == 0) return 0;
7469 len = xmlStrlen(str);
7471 if (len < 0) return -1;
7472 if (len == 0) return 0;
7474 needSize = buf->use + len + 2;
7475 if (needSize > buf->size){
7476 if (!xmlBufferResize(buf, needSize)){
7477 xmlTreeErrMemory("growing buffer");
7478 return XML_ERR_NO_MEMORY;
7482 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
7484 buf->content[buf->use] = 0;
7491 * @str: the #xmlChar string
7492 * @len: the number of #xmlChar to add
7494 * Add a string range to the beginning of an XML buffer.
7495 * if len == -1, the length of @str is recomputed.
7497 * Returns 0 successful, a positive error code number otherwise
7498 * and -1 in case of internal or API error.
7501 xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7502 unsigned int needSize;
7506 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7509 xmlGenericError(xmlGenericErrorContext,
7510 "xmlBufferAddHead: str == NULL\n");
7516 xmlGenericError(xmlGenericErrorContext,
7517 "xmlBufferAddHead: len < 0\n");
7521 if (len == 0) return 0;
7524 len = xmlStrlen(str);
7526 if (len <= 0) return -1;
7528 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7529 size_t start_buf = buf->content - buf->contentIO;
7531 if (start_buf > (unsigned int) len) {
7533 * We can add it in the space previously shrinked
7535 buf->content -= len;
7536 memmove(&buf->content[0], str, len);
7542 needSize = buf->use + len + 2;
7543 if (needSize > buf->size){
7544 if (!xmlBufferResize(buf, needSize)){
7545 xmlTreeErrMemory("growing buffer");
7546 return XML_ERR_NO_MEMORY;
7550 memmove(&buf->content[len], &buf->content[0], buf->use);
7551 memmove(&buf->content[0], str, len);
7553 buf->content[buf->use] = 0;
7559 * @buf: the buffer to add to
7560 * @str: the #xmlChar string
7562 * Append a zero terminated string to an XML buffer.
7564 * Returns 0 successful, a positive error code number otherwise
7565 * and -1 in case of internal or API error.
7568 xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
7571 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7572 if (str == NULL) return -1;
7573 return xmlBufferAdd(buf, str, -1);
7578 * @buf: the buffer to dump
7579 * @str: the C char string
7581 * Append a zero terminated C string to an XML buffer.
7583 * Returns 0 successful, a positive error code number otherwise
7584 * and -1 in case of internal or API error.
7587 xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7592 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7595 xmlGenericError(xmlGenericErrorContext,
7596 "xmlBufferCCat: str == NULL\n");
7600 for (cur = str;*cur != 0;cur++) {
7601 if (buf->use + 10 >= buf->size) {
7602 if (!xmlBufferResize(buf, buf->use+10)){
7603 xmlTreeErrMemory("growing buffer");
7604 return XML_ERR_NO_MEMORY;
7607 buf->content[buf->use++] = *cur;
7609 buf->content[buf->use] = 0;
7614 * xmlBufferWriteCHAR:
7615 * @buf: the XML buffer
7616 * @string: the string to add
7618 * routine which manages and grows an output buffer. This one adds
7619 * xmlChars at the end of the buffer.
7622 xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
7625 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7626 xmlBufferCat(buf, string);
7630 * xmlBufferWriteChar:
7631 * @buf: the XML buffer output
7632 * @string: the string to add
7634 * routine which manage and grows an output buffer. This one add
7635 * C chars at the end of the array.
7638 xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
7641 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7642 xmlBufferCCat(buf, string);
7647 * xmlBufferWriteQuotedString:
7648 * @buf: the XML buffer output
7649 * @string: the string to add
7651 * routine which manage and grows an output buffer. This one writes
7652 * a quoted or double quoted #xmlChar string, checking first if it holds
7653 * quote or double-quotes internally
7656 xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
7657 const xmlChar *cur, *base;
7660 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7661 if (xmlStrchr(string, '\"')) {
7662 if (xmlStrchr(string, '\'')) {
7664 xmlGenericError(xmlGenericErrorContext,
7665 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7667 xmlBufferCCat(buf, "\"");
7668 base = cur = string;
7672 xmlBufferAdd(buf, base, cur - base);
7673 xmlBufferAdd(buf, BAD_CAST """, 6);
7682 xmlBufferAdd(buf, base, cur - base);
7683 xmlBufferCCat(buf, "\"");
7686 xmlBufferCCat(buf, "\'");
7687 xmlBufferCat(buf, string);
7688 xmlBufferCCat(buf, "\'");
7691 xmlBufferCCat(buf, "\"");
7692 xmlBufferCat(buf, string);
7693 xmlBufferCCat(buf, "\"");
7699 * xmlGetDocCompressMode:
7700 * @doc: the document
7702 * get the compression ratio for a document, ZLIB based
7703 * Returns 0 (uncompressed) to 9 (max compression)
7706 xmlGetDocCompressMode (xmlDocPtr doc) {
7707 if (doc == NULL) return(-1);
7708 return(doc->compression);
7712 * xmlSetDocCompressMode:
7713 * @doc: the document
7714 * @mode: the compression ratio
7716 * set the compression ratio for a document, ZLIB based
7717 * Correct values: 0 (uncompressed) to 9 (max compression)
7720 xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7721 if (doc == NULL) return;
7722 if (mode < 0) doc->compression = 0;
7723 else if (mode > 9) doc->compression = 9;
7724 else doc->compression = mode;
7728 * xmlGetCompressMode:
7730 * get the default compression mode used, ZLIB based.
7731 * Returns 0 (uncompressed) to 9 (max compression)
7734 xmlGetCompressMode(void)
7736 return (xmlCompressMode);
7740 * xmlSetCompressMode:
7741 * @mode: the compression ratio
7743 * set the default compression mode used, ZLIB based
7744 * Correct values: 0 (uncompressed) to 9 (max compression)
7747 xmlSetCompressMode(int mode) {
7748 if (mode < 0) xmlCompressMode = 0;
7749 else if (mode > 9) xmlCompressMode = 9;
7750 else xmlCompressMode = mode;
7753 #define XML_TREE_NSMAP_PARENT -1
7754 #define XML_TREE_NSMAP_XML -2
7755 #define XML_TREE_NSMAP_DOC -3
7756 #define XML_TREE_NSMAP_CUSTOM -4
7758 typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7759 struct xmlNsMapItem {
7760 xmlNsMapItemPtr next;
7761 xmlNsMapItemPtr prev;
7762 xmlNsPtr oldNs; /* old ns decl reference */
7763 xmlNsPtr newNs; /* new ns decl reference */
7764 int shadowDepth; /* Shadowed at this depth */
7767 * >= 0 == @node's ns-decls
7768 * -1 == @parent's ns-decls
7769 * -2 == the doc->oldNs XML ns-decl
7770 * -3 == the doc->oldNs storage ns-decls
7771 * -4 == ns-decls provided via custom ns-handling
7776 typedef struct xmlNsMap *xmlNsMapPtr;
7778 xmlNsMapItemPtr first;
7779 xmlNsMapItemPtr last;
7780 xmlNsMapItemPtr pool;
7783 #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7784 #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7785 #define XML_NSMAP_POP(m, i) \
7787 (m)->last = (i)->prev; \
7788 if ((m)->last == NULL) \
7789 (m)->first = NULL; \
7791 (m)->last->next = NULL; \
7792 (i)->next = (m)->pool; \
7796 * xmlDOMWrapNsMapFree:
7802 xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7804 xmlNsMapItemPtr cur, tmp;
7809 while (cur != NULL) {
7815 while (cur != NULL) {
7824 * xmlDOMWrapNsMapAddItem:
7826 * @oldNs: the old ns-struct
7827 * @newNs: the new ns-struct
7828 * @depth: depth and ns-kind information
7830 * Adds an ns-mapping item.
7832 static xmlNsMapItemPtr
7833 xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
7834 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
7836 xmlNsMapItemPtr ret;
7841 if ((position != -1) && (position != 0))
7847 * Create the ns-map.
7849 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7851 xmlTreeErrMemory("allocating namespace map");
7854 memset(map, 0, sizeof(struct xmlNsMap));
7858 if (map->pool != NULL) {
7860 * Reuse an item from the pool.
7863 map->pool = ret->next;
7864 memset(ret, 0, sizeof(struct xmlNsMapItem));
7867 * Create a new item.
7869 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7871 xmlTreeErrMemory("allocating namespace map item");
7874 memset(ret, 0, sizeof(struct xmlNsMapItem));
7877 if (map->first == NULL) {
7883 } else if (position == -1) {
7887 ret->prev = map->last;
7888 map->last->next = ret;
7890 } else if (position == 0) {
7892 * Set on first position.
7894 map->first->prev = ret;
7895 ret->next = map->first;
7902 ret->shadowDepth = -1;
7908 * xmlDOMWrapStoreNs:
7910 * @nsName: the namespace name
7911 * @prefix: the prefix
7913 * Creates or reuses an xmlNs struct on doc->oldNs with
7914 * the given prefix and namespace name.
7916 * Returns the aquired ns struct or NULL in case of an API
7917 * or internal error.
7920 xmlDOMWrapStoreNs(xmlDocPtr doc,
7921 const xmlChar *nsName,
7922 const xmlChar *prefix)
7928 ns = xmlTreeEnsureXMLDecl(doc);
7931 if (ns->next != NULL) {
7934 while (ns != NULL) {
7935 if (((ns->prefix == prefix) ||
7936 xmlStrEqual(ns->prefix, prefix)) &&
7937 xmlStrEqual(ns->href, nsName)) {
7940 if (ns->next == NULL)
7947 ns->next = xmlNewNs(NULL, nsName, prefix);
7954 * xmlDOMWrapNewCtxt:
7956 * Allocates and initializes a new DOM-wrapper context.
7958 * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror.
7961 xmlDOMWrapNewCtxt(void)
7963 xmlDOMWrapCtxtPtr ret;
7965 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
7967 xmlTreeErrMemory("allocating DOM-wrapper context");
7970 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
7975 * xmlDOMWrapFreeCtxt:
7976 * @ctxt: the DOM-wrapper context
7978 * Frees the DOM-wrapper context.
7981 xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
7985 if (ctxt->namespaceMap != NULL)
7986 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
7988 * TODO: Store the namespace map in the context.
7994 * xmlTreeLookupNsListByPrefix:
7995 * @nsList: a list of ns-structs
7996 * @prefix: the searched prefix
7998 * Searches for a ns-decl with the given prefix in @nsList.
8000 * Returns the ns-decl if found, NULL if not found and on
8004 xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
8012 if ((prefix == ns->prefix) ||
8013 xmlStrEqual(prefix, ns->prefix)) {
8017 } while (ns != NULL);
8024 * xmlDOMWrapNSNormGatherInScopeNs:
8025 * @map: the namespace map
8026 * @node: the node to start with
8028 * Puts in-scope namespaces into the ns-map.
8030 * Returns 0 on success, -1 on API or internal errors.
8033 xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
8041 if ((map == NULL) || (*map != NULL))
8043 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8046 * Get in-scope ns-decls of @parent.
8049 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
8050 if (cur->type == XML_ELEMENT_NODE) {
8051 if (cur->nsDef != NULL) {
8055 if (XML_NSMAP_NOTEMPTY(*map)) {
8057 * Skip shadowed prefixes.
8059 XML_NSMAP_FOREACH(*map, mi) {
8060 if ((ns->prefix == mi->newNs->prefix) ||
8061 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
8070 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
8071 ns, XML_TREE_NSMAP_PARENT);
8075 mi->shadowDepth = 0;
8077 } while (ns != NULL);
8086 * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
8087 * otherwise copy it, when it was in the source-dict.
8089 #define XML_TREE_ADOPT_STR(str) \
8090 if (adoptStr && (str != NULL)) { \
8091 if (destDoc->dict) { \
8092 const xmlChar *old = str; \
8093 str = xmlDictLookup(destDoc->dict, str, -1); \
8094 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
8095 (!xmlDictOwns(sourceDoc->dict, old))) \
8096 xmlFree((char *)old); \
8097 } else if ((sourceDoc) && (sourceDoc->dict) && \
8098 xmlDictOwns(sourceDoc->dict, str)) { \
8099 str = BAD_CAST xmlStrdup(str); \
8104 * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
8105 * put it in dest-dict or copy it.
8107 #define XML_TREE_ADOPT_STR_2(str) \
8108 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
8109 (sourceDoc->dict != NULL) && \
8110 xmlDictOwns(sourceDoc->dict, cur->content)) { \
8111 if (destDoc->dict) \
8112 cur->content = (xmlChar *) \
8113 xmlDictLookup(destDoc->dict, cur->content, -1); \
8115 cur->content = xmlStrdup(BAD_CAST cur->content); \
8119 * xmlDOMWrapNSNormAddNsMapItem2:
8121 * For internal use. Adds a ns-decl mapping.
8123 * Returns 0 on success, -1 on internal errors.
8126 xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
8127 xmlNsPtr oldNs, xmlNsPtr newNs)
8129 if (*list == NULL) {
8130 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
8131 if (*list == NULL) {
8132 xmlTreeErrMemory("alloc ns map item");
8137 } else if ((*number) >= (*size)) {
8139 *list = (xmlNsPtr *) xmlRealloc(*list,
8140 (*size) * 2 * sizeof(xmlNsPtr));
8141 if (*list == NULL) {
8142 xmlTreeErrMemory("realloc ns map item");
8146 (*list)[2 * (*number)] = oldNs;
8147 (*list)[2 * (*number) +1] = newNs;
8153 * xmlDOMWrapRemoveNode:
8154 * @ctxt: a DOM wrapper context
8156 * @node: the node to be removed.
8157 * @options: set of options, unused at the moment
8159 * Unlinks the given node from its owner.
8160 * This will substitute ns-references to node->nsDef for
8161 * ns-references to doc->oldNs, thus ensuring the removed
8162 * branch to be autark wrt ns-references.
8164 * NOTE: This function was not intensively tested.
8166 * Returns 0 on success, 1 if the node is not supported,
8167 * -1 on API and internal errors.
8170 xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
8171 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
8173 xmlNsPtr *list = NULL;
8174 int sizeList, nbList, i, j;
8177 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
8180 /* TODO: 0 or -1 ? */
8181 if (node->parent == NULL)
8184 switch (node->type) {
8186 case XML_CDATA_SECTION_NODE:
8187 case XML_ENTITY_REF_NODE:
8189 case XML_COMMENT_NODE:
8190 xmlUnlinkNode(node);
8192 case XML_ELEMENT_NODE:
8193 case XML_ATTRIBUTE_NODE:
8198 xmlUnlinkNode(node);
8200 * Save out-of-scope ns-references in doc->oldNs.
8203 switch (node->type) {
8204 case XML_ELEMENT_NODE:
8205 if ((ctxt == NULL) && (node->nsDef != NULL)) {
8208 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8209 &nbList, ns, ns) == -1)
8210 goto internal_error;
8212 } while (ns != NULL);
8214 /* No break on purpose. */
8215 case XML_ATTRIBUTE_NODE:
8216 if (node->ns != NULL) {
8221 for (i = 0, j = 0; i < nbList; i++, j += 2) {
8222 if (node->ns == list[j]) {
8223 node->ns = list[++j];
8235 * Add to doc's oldNs.
8237 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
8240 goto internal_error;
8246 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8247 &nbList, node->ns, ns) == -1)
8248 goto internal_error;
8252 if ((node->type == XML_ELEMENT_NODE) &&
8253 (node->properties != NULL)) {
8254 node = (xmlNodePtr) node->properties;
8262 if ((node->type == XML_ELEMENT_NODE) &&
8263 (node->children != NULL)) {
8264 node = node->children;
8270 if (node->next != NULL)
8273 node = node->parent;
8276 } while (node != NULL);
8289 * xmlSearchNsByNamespaceStrict:
8290 * @doc: the document
8291 * @node: the start node
8292 * @nsName: the searched namespace name
8293 * @retNs: the resulting ns-decl
8294 * @prefixed: if the found ns-decl must have a prefix (for attributes)
8296 * Dynamically searches for a ns-declaration which matches
8297 * the given @nsName in the ancestor-or-self axis of @node.
8299 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8300 * and internal errors.
8303 xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
8304 const xmlChar* nsName,
8305 xmlNsPtr *retNs, int prefixed)
8307 xmlNodePtr cur, prev = NULL, out = NULL;
8308 xmlNsPtr ns, prevns;
8310 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
8312 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8316 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
8317 *retNs = xmlTreeEnsureXMLDecl(doc);
8324 if (cur->type == XML_ELEMENT_NODE) {
8325 if (cur->nsDef != NULL) {
8326 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8327 if (prefixed && (ns->prefix == NULL))
8331 * Check the last level of ns-decls for a
8334 prevns = prev->nsDef;
8336 if ((prevns->prefix == ns->prefix) ||
8337 ((prevns->prefix != NULL) &&
8338 (ns->prefix != NULL) &&
8339 xmlStrEqual(prevns->prefix, ns->prefix))) {
8345 prevns = prevns->next;
8346 } while (prevns != NULL);
8351 * Ns-name comparison.
8353 if ((nsName == ns->href) ||
8354 xmlStrEqual(nsName, ns->href)) {
8356 * At this point the prefix can only be shadowed,
8357 * if we are the the (at least) 3rd level of
8363 ret = xmlNsInScope(doc, node, prev, ns->prefix);
8367 * TODO: Should we try to find a matching ns-name
8368 * only once? This here keeps on searching.
8369 * I think we should try further since, there might
8370 * be an other matching ns-decl with an unshadowed
8383 } else if ((cur->type == XML_ENTITY_NODE) ||
8384 (cur->type == XML_ENTITY_DECL))
8387 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8392 * xmlSearchNsByPrefixStrict:
8393 * @doc: the document
8394 * @node: the start node
8395 * @prefix: the searched namespace prefix
8396 * @retNs: the resulting ns-decl
8398 * Dynamically searches for a ns-declaration which matches
8399 * the given @nsName in the ancestor-or-self axis of @node.
8401 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8402 * and internal errors.
8405 xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
8406 const xmlChar* prefix,
8412 if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL))
8417 if (IS_STR_XML(prefix)) {
8419 *retNs = xmlTreeEnsureXMLDecl(doc);
8427 if (cur->type == XML_ELEMENT_NODE) {
8428 if (cur->nsDef != NULL) {
8431 if ((prefix == ns->prefix) ||
8432 xmlStrEqual(prefix, ns->prefix))
8435 * Disabled namespaces, e.g. xmlns:abc="".
8437 if (ns->href == NULL)
8444 } while (ns != NULL);
8446 } else if ((cur->type == XML_ENTITY_NODE) ||
8447 (cur->type == XML_ENTITY_DECL))
8450 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8455 * xmlDOMWrapNSNormDeclareNsForced:
8457 * @elem: the element-node to declare on
8458 * @nsName: the namespace-name of the ns-decl
8459 * @prefix: the preferred prefix of the ns-decl
8460 * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
8462 * Declares a new namespace on @elem. It tries to use the
8463 * given @prefix; if a ns-decl with the given prefix is already existent
8464 * on @elem, it will generate an other prefix.
8466 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8467 * and internal errors.
8470 xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
8472 const xmlChar *nsName,
8473 const xmlChar *prefix,
8479 const xmlChar *pref;
8482 if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
8485 * Create a ns-decl on @anchor.
8490 * Lookup whether the prefix is unused in elem's ns-decls.
8492 if ((elem->nsDef != NULL) &&
8493 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
8494 goto ns_next_prefix;
8495 if (checkShadow && elem->parent &&
8496 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8498 * Does it shadow ancestor ns-decls?
8500 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
8501 goto ns_next_prefix;
8503 ret = xmlNewNs(NULL, nsName, pref);
8506 if (elem->nsDef == NULL)
8509 xmlNsPtr ns2 = elem->nsDef;
8510 while (ns2->next != NULL)
8519 if (prefix == NULL) {
8520 snprintf((char *) buf, sizeof(buf),
8523 snprintf((char *) buf, sizeof(buf),
8524 "%.30s_%d", (char *)prefix, counter);
8525 pref = BAD_CAST buf;
8530 * xmlDOMWrapNSNormAquireNormalizedNs:
8532 * @elem: the element-node to declare namespaces on
8533 * @ns: the ns-struct to use for the search
8534 * @retNs: the found/created ns-struct
8535 * @nsMap: the ns-map
8536 * @depth: the current tree depth
8537 * @ancestorsOnly: search in ancestor ns-decls only
8538 * @prefixed: if the searched ns-decl must have a prefix (for attributes)
8540 * Searches for a matching ns-name in the ns-decls of @nsMap, if not
8541 * found it will either declare it on @elem, or store it in doc->oldNs.
8542 * If a new ns-decl needs to be declared on @elem, it tries to use the
8543 * @ns->prefix for it, if this prefix is already in use on @elem, it will
8544 * change the prefix or the new ns-decl.
8546 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8549 xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8561 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
8567 * Handle XML namespace.
8569 if (IS_STR_XML(ns->prefix)) {
8571 * Insert XML namespace mapping.
8573 *retNs = xmlTreeEnsureXMLDecl(doc);
8579 * If the search should be done in ancestors only and no
8580 * @elem (the first ancestor) was specified, then skip the search.
8582 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8583 (! (ancestorsOnly && (elem == NULL))))
8586 * Try to find an equal ns-name in in-scope ns-decls.
8588 XML_NSMAP_FOREACH(*nsMap, mi) {
8589 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8591 * ancestorsOnly: This should be turned on to gain speed,
8592 * if one knows that the branch itself was already
8593 * ns-wellformed and no stale references existed.
8594 * I.e. it searches in the ancestor axis only.
8596 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8597 /* Skip shadowed prefixes. */
8598 (mi->shadowDepth == -1) &&
8599 /* Skip xmlns="" or xmlns:foo="". */
8600 ((mi->newNs->href != NULL) &&
8601 (mi->newNs->href[0] != 0)) &&
8602 /* Ensure a prefix if wanted. */
8603 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8605 ((mi->newNs->href == ns->href) ||
8606 xmlStrEqual(mi->newNs->href, ns->href))) {
8607 /* Set the mapping. */
8615 * No luck, the namespace is out of scope or shadowed.
8621 * Store ns-decls in "oldNs" of the document-node.
8623 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8629 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
8630 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8638 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8643 if (*nsMap != NULL) {
8645 * Does it shadow ancestor ns-decls?
8647 XML_NSMAP_FOREACH(*nsMap, mi) {
8648 if ((mi->depth < depth) &&
8649 (mi->shadowDepth == -1) &&
8650 ((ns->prefix == mi->newNs->prefix) ||
8651 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8655 mi->shadowDepth = depth;
8660 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
8670 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
8671 } xmlDOMReconcileNSOptions;
8674 * xmlDOMWrapReconcileNamespaces:
8675 * @ctxt: DOM wrapper context, unused at the moment
8676 * @elem: the element-node
8677 * @options: option flags
8679 * Ensures that ns-references point to ns-decls hold on element-nodes.
8680 * Ensures that the tree is namespace wellformed by creating additional
8681 * ns-decls where needed. Note that, since prefixes of already existent
8682 * ns-decls can be shadowed by this process, it could break QNames in
8683 * attribute values or element content.
8685 * NOTE: This function was not intensively tested.
8687 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8691 xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8695 int depth = -1, adoptns = 0, parnsdone = 0;
8696 xmlNsPtr ns, prevns;
8698 xmlNodePtr cur, curElem = NULL;
8699 xmlNsMapPtr nsMap = NULL;
8700 xmlNsMapItemPtr /* topmi = NULL, */ mi;
8701 /* @ancestorsOnly should be set by an option flag. */
8702 int ancestorsOnly = 0;
8703 int optRemoveRedundantNS =
8704 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8705 xmlNsPtr *listRedund = NULL;
8706 int sizeRedund = 0, nbRedund = 0, ret, i, j;
8708 if ((elem == NULL) || (elem->doc == NULL) ||
8709 (elem->type != XML_ELEMENT_NODE))
8715 switch (cur->type) {
8716 case XML_ELEMENT_NODE:
8721 * Namespace declarations.
8723 if (cur->nsDef != NULL) {
8726 while (ns != NULL) {
8728 if ((elem->parent) &&
8729 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8731 * Gather ancestor in-scope ns-decls.
8733 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8734 elem->parent) == -1)
8735 goto internal_error;
8741 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8743 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
8744 XML_NSMAP_FOREACH(nsMap, mi) {
8745 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8746 (mi->shadowDepth == -1) &&
8747 ((ns->prefix == mi->newNs->prefix) ||
8748 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8749 ((ns->href == mi->newNs->href) ||
8750 xmlStrEqual(ns->href, mi->newNs->href)))
8753 * A redundant ns-decl was found.
8754 * Add it to the list of redundant ns-decls.
8756 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8757 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8758 goto internal_error;
8760 * Remove the ns-decl from the element-node.
8763 prevns->next = ns->next;
8765 cur->nsDef = ns->next;
8772 * Skip ns-references handling if the referenced
8773 * ns-decl is declared on the same element.
8775 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
8778 * Does it shadow any ns-decl?
8780 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8781 XML_NSMAP_FOREACH(nsMap, mi) {
8782 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8783 (mi->shadowDepth == -1) &&
8784 ((ns->prefix == mi->newNs->prefix) ||
8785 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8787 mi->shadowDepth = depth;
8794 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
8796 goto internal_error;
8805 /* No break on purpose. */
8806 case XML_ATTRIBUTE_NODE:
8807 /* No ns, no fun. */
8808 if (cur->ns == NULL)
8812 if ((elem->parent) &&
8813 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8814 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8815 elem->parent) == -1)
8816 goto internal_error;
8821 * Adjust the reference if this was a redundant ns-decl.
8824 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8825 if (cur->ns == listRedund[j]) {
8826 cur->ns = listRedund[++j];
8832 * Adopt ns-references.
8834 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8836 * Search for a mapping.
8838 XML_NSMAP_FOREACH(nsMap, mi) {
8839 if ((mi->shadowDepth == -1) &&
8840 (cur->ns == mi->oldNs)) {
8842 cur->ns = mi->newNs;
8848 * Aquire a normalized ns-decl and add it to the map.
8850 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8854 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8855 goto internal_error;
8859 if ((cur->type == XML_ELEMENT_NODE) &&
8860 (cur->properties != NULL)) {
8862 * Process attributes.
8864 cur = (xmlNodePtr) cur->properties;
8872 if ((cur->type == XML_ELEMENT_NODE) &&
8873 (cur->children != NULL)) {
8875 * Process content of element-nodes only.
8877 cur = cur->children;
8883 if (cur->type == XML_ELEMENT_NODE) {
8884 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8888 while ((nsMap->last != NULL) &&
8889 (nsMap->last->depth >= depth))
8891 XML_NSMAP_POP(nsMap, mi)
8896 XML_NSMAP_FOREACH(nsMap, mi) {
8897 if (mi->shadowDepth >= depth)
8898 mi->shadowDepth = -1;
8903 if (cur->next != NULL)
8906 if (cur->type == XML_ATTRIBUTE_NODE) {
8913 } while (cur != NULL);
8921 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8922 xmlFreeNs(listRedund[j]);
8924 xmlFree(listRedund);
8927 xmlDOMWrapNsMapFree(nsMap);
8932 * xmlDOMWrapAdoptBranch:
8933 * @ctxt: the optional context for custom processing
8934 * @sourceDoc: the optional sourceDoc
8935 * @node: the element-node to start with
8936 * @destDoc: the destination doc for adoption
8937 * @destParent: the optional new parent of @node in @destDoc
8938 * @options: option flags
8940 * Ensures that ns-references point to @destDoc: either to
8941 * elements->nsDef entries if @destParent is given, or to
8942 * @destDoc->oldNs otherwise.
8943 * If @destParent is given, it ensures that the tree is namespace
8944 * wellformed by creating additional ns-decls where needed.
8945 * Note that, since prefixes of already existent ns-decls can be
8946 * shadowed by this process, it could break QNames in attribute
8947 * values or element content.
8949 * NOTE: This function was not intensively tested.
8951 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8954 xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8955 xmlDocPtr sourceDoc,
8958 xmlNodePtr destParent,
8959 int options ATTRIBUTE_UNUSED)
8962 xmlNodePtr cur, curElem = NULL;
8963 xmlNsMapPtr nsMap = NULL;
8966 int depth = -1, adoptStr = 1;
8967 /* gather @parent's ns-decls. */
8969 /* @ancestorsOnly should be set per option. */
8970 int ancestorsOnly = 0;
8973 * Optimize string adoption for equal or none dicts.
8975 if ((sourceDoc != NULL) &&
8976 (sourceDoc->dict == destDoc->dict))
8982 * Get the ns-map from the context if available.
8985 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8987 * Disable search for ns-decls in the parent-axis of the
8988 * desination element, if:
8989 * 1) there's no destination parent
8990 * 2) custom ns-reference handling is used
8992 if ((destParent == NULL) ||
8993 (ctxt && ctxt->getNsForNodeFunc))
9000 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9001 goto internal_error;
9003 while (cur != NULL) {
9005 * Paranoid source-doc sanity check.
9007 if (cur->doc != sourceDoc) {
9009 * We'll assume XIncluded nodes if the doc differs.
9010 * TODO: Do we need to reconciliate XIncluded nodes?
9011 * This here skips XIncluded nodes and tries to handle
9014 if (cur->next == NULL)
9018 if ((cur->type == XML_XINCLUDE_END) ||
9019 (cur->doc == node->doc))
9021 } while (cur->next != NULL);
9023 if (cur->doc != node->doc)
9027 switch (cur->type) {
9028 case XML_XINCLUDE_START:
9029 case XML_XINCLUDE_END:
9034 case XML_ELEMENT_NODE:
9038 * Namespace declarations.
9039 * - ns->href and ns->prefix are never in the dict, so
9040 * we need not move the values over to the destination dict.
9041 * - Note that for custom handling of ns-references,
9042 * the ns-decls need not be stored in the ns-map,
9043 * since they won't be referenced by node->ns.
9046 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
9050 * Gather @parent's in-scope ns-decls.
9052 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9054 goto internal_error;
9057 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9059 * NOTE: ns->prefix and ns->href are never in the dict.
9060 * XML_TREE_ADOPT_STR(ns->prefix)
9061 * XML_TREE_ADOPT_STR(ns->href)
9064 * Does it shadow any ns-decl?
9066 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9067 XML_NSMAP_FOREACH(nsMap, mi) {
9068 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9069 (mi->shadowDepth == -1) &&
9070 ((ns->prefix == mi->newNs->prefix) ||
9071 xmlStrEqual(ns->prefix,
9072 mi->newNs->prefix))) {
9074 mi->shadowDepth = depth;
9081 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9082 ns, ns, depth) == NULL)
9083 goto internal_error;
9086 /* No break on purpose. */
9087 case XML_ATTRIBUTE_NODE:
9088 /* No namespace, no fun. */
9089 if (cur->ns == NULL)
9093 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9095 goto internal_error;
9099 * Adopt ns-references.
9101 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9103 * Search for a mapping.
9105 XML_NSMAP_FOREACH(nsMap, mi) {
9106 if ((mi->shadowDepth == -1) &&
9107 (cur->ns == mi->oldNs)) {
9109 cur->ns = mi->newNs;
9115 * No matching namespace in scope. We need a new one.
9117 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
9119 * User-defined behaviour.
9121 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9122 cur->ns->href, cur->ns->prefix);
9124 * Insert mapping if ns is available; it's the users fault
9127 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9128 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9129 goto internal_error;
9133 * Aquire a normalized ns-decl and add it to the map.
9135 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9136 /* ns-decls on curElem or on destDoc->oldNs */
9137 destParent ? curElem : NULL,
9141 /* ns-decls must be prefixed for attributes. */
9142 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9143 goto internal_error;
9148 * Further node properties.
9149 * TODO: Is this all?
9151 XML_TREE_ADOPT_STR(cur->name)
9152 if (cur->type == XML_ELEMENT_NODE) {
9159 if (cur->properties != NULL) {
9161 * Process first attribute node.
9163 cur = (xmlNodePtr) cur->properties;
9170 if ((sourceDoc != NULL) &&
9171 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
9173 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
9175 ((xmlAttrPtr) cur)->atype = 0;
9176 ((xmlAttrPtr) cur)->psvi = NULL;
9180 case XML_CDATA_SECTION_NODE:
9182 * This puts the content in the dest dict, only if
9183 * it was previously in the source dict.
9185 XML_TREE_ADOPT_STR_2(cur->content)
9187 case XML_ENTITY_REF_NODE:
9189 * Remove reference to the entitity-node.
9191 cur->content = NULL;
9192 cur->children = NULL;
9194 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9197 * Assign new entity-node if available.
9199 ent = xmlGetDocEntity(destDoc, cur->name);
9201 cur->content = ent->content;
9202 cur->children = (xmlNodePtr) ent;
9203 cur->last = (xmlNodePtr) ent;
9208 XML_TREE_ADOPT_STR(cur->name)
9209 XML_TREE_ADOPT_STR_2(cur->content)
9211 case XML_COMMENT_NODE:
9214 goto internal_error;
9219 if (cur->children != NULL) {
9220 cur = cur->children;
9227 if ((cur->type == XML_ELEMENT_NODE) ||
9228 (cur->type == XML_XINCLUDE_START) ||
9229 (cur->type == XML_XINCLUDE_END))
9232 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9234 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9238 while ((nsMap->last != NULL) &&
9239 (nsMap->last->depth >= depth))
9241 XML_NSMAP_POP(nsMap, mi)
9246 XML_NSMAP_FOREACH(nsMap, mi) {
9247 if (mi->shadowDepth >= depth)
9248 mi->shadowDepth = -1;
9253 if (cur->next != NULL)
9255 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
9256 (cur->parent->children != NULL))
9258 cur = cur->parent->children;
9274 if (nsMap != NULL) {
9275 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9277 * Just cleanup the map but don't free.
9281 nsMap->last->next = nsMap->pool;
9282 nsMap->pool = nsMap->first;
9283 nsMap->first = NULL;
9286 xmlDOMWrapNsMapFree(nsMap);
9292 * xmlDOMWrapCloneNode:
9293 * @ctxt: the optional context for custom processing
9294 * @sourceDoc: the optional sourceDoc
9295 * @node: the node to start with
9296 * @resNode: the clone of the given @node
9297 * @destDoc: the destination doc
9298 * @destParent: the optional new parent of @node in @destDoc
9299 * @deep: descend into child if set
9300 * @options: option flags
9302 * References of out-of scope ns-decls are remapped to point to @destDoc:
9303 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9304 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
9305 * This is the case when you don't know already where the cloned branch
9308 * If @destParent is given, it ensures that the tree is namespace
9309 * wellformed by creating additional ns-decls where needed.
9310 * Note that, since prefixes of already existent ns-decls can be
9311 * shadowed by this process, it could break QNames in attribute
9312 * values or element content.
9314 * 1) What to do with XInclude? Currently this returns an error for XInclude.
9316 * Returns 0 if the operation succeeded,
9317 * 1 if a node of unsupported (or not yet supported) type was given,
9318 * -1 on API/internal errors.
9322 xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
9323 xmlDocPtr sourceDoc,
9325 xmlNodePtr *resNode,
9327 xmlNodePtr destParent,
9329 int options ATTRIBUTE_UNUSED)
9332 xmlNodePtr cur, curElem = NULL;
9333 xmlNsMapPtr nsMap = NULL;
9337 /* int adoptStr = 1; */
9338 /* gather @parent's ns-decls. */
9342 * TODO: @ancestorsOnly should be set per option.
9345 int ancestorsOnly = 0;
9346 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
9347 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
9348 xmlDictPtr dict; /* The destination dict */
9350 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
9353 * TODO: Initially we support only element-nodes.
9355 if (node->type != XML_ELEMENT_NODE)
9358 * Check node->doc sanity.
9360 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9361 (node->doc != sourceDoc)) {
9363 * Might be an XIncluded node.
9367 if (sourceDoc == NULL)
9368 sourceDoc = node->doc;
9369 if (sourceDoc == NULL)
9372 dict = destDoc->dict;
9374 * Reuse the namespace map of the context.
9377 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9382 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9385 while (cur != NULL) {
9386 if (cur->doc != sourceDoc) {
9388 * We'll assume XIncluded nodes if the doc differs.
9389 * TODO: Do we need to reconciliate XIncluded nodes?
9390 * TODO: This here returns -1 in this case.
9392 goto internal_error;
9395 * Create a new node.
9397 switch (cur->type) {
9398 case XML_XINCLUDE_START:
9399 case XML_XINCLUDE_END:
9401 * TODO: What to do with XInclude?
9403 goto internal_error;
9405 case XML_ELEMENT_NODE:
9407 case XML_CDATA_SECTION_NODE:
9408 case XML_COMMENT_NODE:
9410 case XML_DOCUMENT_FRAG_NODE:
9411 case XML_ENTITY_REF_NODE:
9412 case XML_ENTITY_NODE:
9414 * Nodes of xmlNode structure.
9416 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
9417 if (clone == NULL) {
9418 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
9419 goto internal_error;
9421 memset(clone, 0, sizeof(xmlNode));
9423 * Set hierachical links.
9425 if (resultClone != NULL) {
9426 clone->parent = parentClone;
9428 prevClone->next = clone;
9429 clone->prev = prevClone;
9431 parentClone->children = clone;
9433 resultClone = clone;
9436 case XML_ATTRIBUTE_NODE:
9438 * Attributes (xmlAttr).
9440 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
9441 if (clone == NULL) {
9442 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
9443 goto internal_error;
9445 memset(clone, 0, sizeof(xmlAttr));
9447 * Set hierachical links.
9448 * TODO: Change this to add to the end of attributes.
9450 if (resultClone != NULL) {
9451 clone->parent = parentClone;
9453 prevClone->next = clone;
9454 clone->prev = prevClone;
9456 parentClone->properties = (xmlAttrPtr) clone;
9458 resultClone = clone;
9462 * TODO QUESTION: Any other nodes expected?
9464 goto internal_error;
9467 clone->type = cur->type;
9468 clone->doc = destDoc;
9471 * Clone the name of the node if any.
9473 if (cur->name == xmlStringText)
9474 clone->name = xmlStringText;
9475 else if (cur->name == xmlStringTextNoenc)
9477 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9478 * in tree.c, it might be set in Libxslt via
9479 * "xsl:disable-output-escaping".
9481 clone->name = xmlStringTextNoenc;
9482 else if (cur->name == xmlStringComment)
9483 clone->name = xmlStringComment;
9484 else if (cur->name != NULL) {
9485 DICT_CONST_COPY(cur->name, clone->name);
9488 switch (cur->type) {
9489 case XML_XINCLUDE_START:
9490 case XML_XINCLUDE_END:
9495 case XML_ELEMENT_NODE:
9499 * Namespace declarations.
9501 if (cur->nsDef != NULL) {
9503 if (destParent && (ctxt == NULL)) {
9505 * Gather @parent's in-scope ns-decls.
9507 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9509 goto internal_error;
9514 * Clone namespace declarations.
9516 cloneNsDefSlot = &(clone->nsDef);
9517 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9519 * Create a new xmlNs.
9521 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
9522 if (cloneNs == NULL) {
9523 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
9524 "allocating namespace");
9527 memset(cloneNs, 0, sizeof(xmlNs));
9528 cloneNs->type = XML_LOCAL_NAMESPACE;
9530 if (ns->href != NULL)
9531 cloneNs->href = xmlStrdup(ns->href);
9532 if (ns->prefix != NULL)
9533 cloneNs->prefix = xmlStrdup(ns->prefix);
9535 *cloneNsDefSlot = cloneNs;
9536 cloneNsDefSlot = &(cloneNs->next);
9539 * Note that for custom handling of ns-references,
9540 * the ns-decls need not be stored in the ns-map,
9541 * since they won't be referenced by node->ns.
9543 if ((ctxt == NULL) ||
9544 (ctxt->getNsForNodeFunc == NULL))
9547 * Does it shadow any ns-decl?
9549 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9550 XML_NSMAP_FOREACH(nsMap, mi) {
9551 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9552 (mi->shadowDepth == -1) &&
9553 ((ns->prefix == mi->newNs->prefix) ||
9554 xmlStrEqual(ns->prefix,
9555 mi->newNs->prefix))) {
9557 * Mark as shadowed at the current
9560 mi->shadowDepth = depth;
9567 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9568 ns, cloneNs, depth) == NULL)
9569 goto internal_error;
9573 /* cur->ns will be processed further down. */
9575 case XML_ATTRIBUTE_NODE:
9576 /* IDs will be processed further down. */
9577 /* cur->ns will be processed further down. */
9580 case XML_CDATA_SECTION_NODE:
9582 * Note that this will also cover the values of attributes.
9584 DICT_COPY(cur->content, clone->content);
9586 case XML_ENTITY_NODE:
9587 /* TODO: What to do here? */
9589 case XML_ENTITY_REF_NODE:
9590 if (sourceDoc != destDoc) {
9591 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9594 * Different doc: Assign new entity-node if available.
9596 ent = xmlGetDocEntity(destDoc, cur->name);
9598 clone->content = ent->content;
9599 clone->children = (xmlNodePtr) ent;
9600 clone->last = (xmlNodePtr) ent;
9605 * Same doc: Use the current node's entity declaration
9608 clone->content = cur->content;
9609 clone->children = cur->children;
9610 clone->last = cur->last;
9614 DICT_COPY(cur->content, clone->content);
9616 case XML_COMMENT_NODE:
9617 DICT_COPY(cur->content, clone->content);
9620 goto internal_error;
9623 if (cur->ns == NULL)
9624 goto end_ns_reference;
9626 /* handle_ns_reference: */
9628 ** The following will take care of references to ns-decls ********
9629 ** and is intended only for element- and attribute-nodes.
9633 if (destParent && (ctxt == NULL)) {
9634 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
9635 goto internal_error;
9640 * Adopt ns-references.
9642 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9644 * Search for a mapping.
9646 XML_NSMAP_FOREACH(nsMap, mi) {
9647 if ((mi->shadowDepth == -1) &&
9648 (cur->ns == mi->oldNs)) {
9650 * This is the nice case: a mapping was found.
9652 clone->ns = mi->newNs;
9653 goto end_ns_reference;
9658 * No matching namespace in scope. We need a new one.
9660 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
9662 * User-defined behaviour.
9664 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9665 cur->ns->href, cur->ns->prefix);
9667 * Add user's mapping.
9669 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9670 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9671 goto internal_error;
9675 * Aquire a normalized ns-decl and add it to the map.
9677 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9678 /* ns-decls on curElem or on destDoc->oldNs */
9679 destParent ? curElem : NULL,
9682 /* if we need to search only in the ancestor-axis */
9684 /* ns-decls must be prefixed for attributes. */
9685 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9686 goto internal_error;
9693 * Some post-processing.
9695 * Handle ID attributes.
9697 if ((clone->type == XML_ATTRIBUTE_NODE) &&
9698 (clone->parent != NULL))
9700 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
9704 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9705 if (idVal != NULL) {
9706 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9707 /* TODO: error message. */
9709 goto internal_error;
9717 ** The following will traverse the tree **************************
9720 * Walk the element's attributes before descending into child-nodes.
9722 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9724 parentClone = clone;
9725 cur = (xmlNodePtr) cur->properties;
9730 * Descend into child-nodes.
9732 if (cur->children != NULL) {
9733 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9735 parentClone = clone;
9736 cur = cur->children;
9743 * At this point we are done with the node, its content
9744 * and an element-nodes's attribute-nodes.
9748 if ((cur->type == XML_ELEMENT_NODE) ||
9749 (cur->type == XML_XINCLUDE_START) ||
9750 (cur->type == XML_XINCLUDE_END)) {
9752 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9754 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9758 while ((nsMap->last != NULL) &&
9759 (nsMap->last->depth >= depth))
9761 XML_NSMAP_POP(nsMap, mi)
9766 XML_NSMAP_FOREACH(nsMap, mi) {
9767 if (mi->shadowDepth >= depth)
9768 mi->shadowDepth = -1;
9773 if (cur->next != NULL) {
9776 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9780 if (clone->parent != NULL)
9781 clone->parent->last = clone;
9782 clone = clone->parent;
9783 parentClone = clone->parent;
9785 * Process parent --> next;
9790 /* This is for attributes only. */
9791 clone = clone->parent;
9792 parentClone = clone->parent;
9794 * Process parent-element --> children.
9809 if (nsMap != NULL) {
9810 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9812 * Just cleanup the map but don't free.
9816 nsMap->last->next = nsMap->pool;
9817 nsMap->pool = nsMap->first;
9818 nsMap->first = NULL;
9821 xmlDOMWrapNsMapFree(nsMap);
9824 * TODO: Should we try a cleanup of the cloned node in case of a
9827 *resNode = resultClone;
9832 * xmlDOMWrapAdoptAttr:
9833 * @ctxt: the optional context for custom processing
9834 * @sourceDoc: the optional source document of attr
9835 * @attr: the attribute-node to be adopted
9836 * @destDoc: the destination doc for adoption
9837 * @destParent: the optional new parent of @attr in @destDoc
9838 * @options: option flags
9840 * @attr is adopted by @destDoc.
9841 * Ensures that ns-references point to @destDoc: either to
9842 * elements->nsDef entries if @destParent is given, or to
9843 * @destDoc->oldNs otherwise.
9845 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9848 xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9849 xmlDocPtr sourceDoc,
9852 xmlNodePtr destParent,
9853 int options ATTRIBUTE_UNUSED)
9858 if ((attr == NULL) || (destDoc == NULL))
9861 attr->doc = destDoc;
9862 if (attr->ns != NULL) {
9866 /* TODO: User defined. */
9868 /* XML Namespace. */
9869 if (IS_STR_XML(attr->ns->prefix)) {
9870 ns = xmlTreeEnsureXMLDecl(destDoc);
9871 } else if (destParent == NULL) {
9873 * Store in @destDoc->oldNs.
9875 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9878 * Declare on @destParent.
9880 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
9882 goto internal_error;
9884 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9885 attr->ns->href, attr->ns->prefix, 1);
9889 goto internal_error;
9893 XML_TREE_ADOPT_STR(attr->name);
9899 if (attr->children == NULL)
9901 cur = attr->children;
9902 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9903 goto internal_error;
9904 while (cur != NULL) {
9906 switch (cur->type) {
9908 case XML_CDATA_SECTION_NODE:
9909 XML_TREE_ADOPT_STR_2(cur->content)
9911 case XML_ENTITY_REF_NODE:
9913 * Remove reference to the entitity-node.
9915 cur->content = NULL;
9916 cur->children = NULL;
9918 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9921 * Assign new entity-node if available.
9923 ent = xmlGetDocEntity(destDoc, cur->name);
9925 cur->content = ent->content;
9926 cur->children = (xmlNodePtr) ent;
9927 cur->last = (xmlNodePtr) ent;
9934 if (cur->children != NULL) {
9935 cur = cur->children;
9939 if (cur == (xmlNodePtr) attr)
9941 if (cur->next != NULL)
9954 * xmlDOMWrapAdoptNode:
9955 * @ctxt: the optional context for custom processing
9956 * @sourceDoc: the optional sourceDoc
9957 * @node: the node to start with
9958 * @destDoc: the destination doc
9959 * @destParent: the optional new parent of @node in @destDoc
9960 * @options: option flags
9962 * References of out-of scope ns-decls are remapped to point to @destDoc:
9963 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9964 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
9965 * This is the case when you have an unliked node and just want to move it
9968 * If @destParent is given, it ensures that the tree is namespace
9969 * wellformed by creating additional ns-decls where needed.
9970 * Note that, since prefixes of already existent ns-decls can be
9971 * shadowed by this process, it could break QNames in attribute
9972 * values or element content.
9973 * NOTE: This function was not intensively tested.
9975 * Returns 0 if the operation succeeded,
9976 * 1 if a node of unsupported type was given,
9977 * 2 if a node of not yet supported type was given and
9978 * -1 on API/internal errors.
9981 xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
9982 xmlDocPtr sourceDoc,
9985 xmlNodePtr destParent,
9988 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
9989 (destDoc == NULL) ||
9990 ((destParent != NULL) && (destParent->doc != destDoc)))
9993 * Check node->doc sanity.
9995 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9996 (node->doc != sourceDoc)) {
9998 * Might be an XIncluded node.
10002 if (sourceDoc == NULL)
10003 sourceDoc = node->doc;
10004 if (sourceDoc == destDoc)
10006 switch (node->type) {
10007 case XML_ELEMENT_NODE:
10008 case XML_ATTRIBUTE_NODE:
10009 case XML_TEXT_NODE:
10010 case XML_CDATA_SECTION_NODE:
10011 case XML_ENTITY_REF_NODE:
10013 case XML_COMMENT_NODE:
10015 case XML_DOCUMENT_FRAG_NODE:
10016 /* TODO: Support document-fragment-nodes. */
10022 * Unlink only if @node was not already added to @destParent.
10024 if ((node->parent != NULL) && (destParent != node->parent))
10025 xmlUnlinkNode(node);
10027 if (node->type == XML_ELEMENT_NODE) {
10028 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
10029 destDoc, destParent, options));
10030 } else if (node->type == XML_ATTRIBUTE_NODE) {
10031 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
10032 (xmlAttrPtr) node, destDoc, destParent, options));
10034 xmlNodePtr cur = node;
10037 cur->doc = destDoc;
10039 * Optimize string adoption.
10041 if ((sourceDoc != NULL) &&
10042 (sourceDoc->dict == destDoc->dict))
10044 switch (node->type) {
10045 case XML_TEXT_NODE:
10046 case XML_CDATA_SECTION_NODE:
10047 XML_TREE_ADOPT_STR_2(node->content)
10049 case XML_ENTITY_REF_NODE:
10051 * Remove reference to the entitity-node.
10053 node->content = NULL;
10054 node->children = NULL;
10056 if ((destDoc->intSubset) || (destDoc->extSubset)) {
10059 * Assign new entity-node if available.
10061 ent = xmlGetDocEntity(destDoc, node->name);
10063 node->content = ent->content;
10064 node->children = (xmlNodePtr) ent;
10065 node->last = (xmlNodePtr) ent;
10068 XML_TREE_ADOPT_STR(node->name)
10070 case XML_PI_NODE: {
10071 XML_TREE_ADOPT_STR(node->name)
10072 XML_TREE_ADOPT_STR_2(node->content)
10082 #define bottom_tree
10083 #include "elfgcchack.h"