2 * tree.c : implementation of access function for an XML tree.
5 * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
7 * See Copyright for the status of this software.
16 #include <string.h> /* for memset() only ! */
28 #include <libxml/xmlmemory.h>
29 #include <libxml/tree.h>
30 #include <libxml/parser.h>
31 #include <libxml/uri.h>
32 #include <libxml/entities.h>
33 #include <libxml/valid.h>
34 #include <libxml/xmlerror.h>
35 #include <libxml/parserInternals.h>
36 #include <libxml/globals.h>
37 #ifdef LIBXML_HTML_ENABLED
38 #include <libxml/HTMLtree.h>
40 #ifdef LIBXML_DEBUG_ENABLED
41 #include <libxml/debugXML.h>
44 int __xmlRegisterCallbacks = 0;
46 /************************************************************************
48 * Forward declarations *
50 ************************************************************************/
53 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
55 static xmlChar* xmlGetPropNodeValueInternal(xmlAttrPtr prop);
57 /************************************************************************
59 * Tree memory error handler *
61 ************************************************************************/
64 * @extra: extra informations
66 * Handle an out of memory condition
69 xmlTreeErrMemory(const char *extra)
71 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
76 * @code: the error number
77 * @extra: extra informations
79 * Handle an out of memory condition
82 xmlTreeErr(int code, xmlNodePtr node, const char *extra)
84 const char *msg = NULL;
87 case XML_TREE_INVALID_HEX:
88 msg = "invalid hexadecimal character value\n";
90 case XML_TREE_INVALID_DEC:
91 msg = "invalid decimal character value\n";
93 case XML_TREE_UNTERMINATED_ENTITY:
94 msg = "unterminated entity reference %15s\n";
96 case XML_TREE_NOT_UTF8:
97 msg = "string is not in UTF-8\n";
100 msg = "unexpected error number\n";
102 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
105 /************************************************************************
107 * A few static variables and macros *
109 ************************************************************************/
110 /* #undef xmlStringText */
111 const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
112 /* #undef xmlStringTextNoenc */
113 const xmlChar xmlStringTextNoenc[] =
114 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
115 /* #undef xmlStringComment */
116 const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
118 static int xmlCompressMode = 0;
119 static int xmlCheckDTD = 1;
121 #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
122 xmlNodePtr ulccur = (n)->children; \
123 if (ulccur == NULL) { \
126 while (ulccur->next != NULL) { \
127 ulccur->parent = (n); \
128 ulccur = ulccur->next; \
130 ulccur->parent = (n); \
131 (n)->last = ulccur; \
134 #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
135 (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
137 /* #define DEBUG_BUFFER */
138 /* #define DEBUG_TREE */
140 /************************************************************************
142 * Functions to move to entities.c once the *
143 * API freeze is smoothen and they can be made public. *
145 ************************************************************************/
146 #include <libxml/hash.h>
148 #ifdef LIBXML_TREE_ENABLED
150 * xmlGetEntityFromDtd:
151 * @dtd: A pointer to the DTD to search
152 * @name: The entity name
154 * Do an entity lookup in the DTD entity hash table and
155 * return the corresponding entity, if found.
157 * Returns A pointer to the entity structure or NULL if not found.
160 xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
161 xmlEntitiesTablePtr table;
163 if((dtd != NULL) && (dtd->entities != NULL)) {
164 table = (xmlEntitiesTablePtr) dtd->entities;
165 return((xmlEntityPtr) xmlHashLookup(table, name));
166 /* return(xmlGetEntityFromTable(table, name)); */
171 * xmlGetParameterEntityFromDtd:
172 * @dtd: A pointer to the DTD to search
173 * @name: The entity name
175 * Do an entity lookup in the DTD pararmeter entity hash table and
176 * return the corresponding entity, if found.
178 * Returns A pointer to the entity structure or NULL if not found.
181 xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
182 xmlEntitiesTablePtr table;
184 if ((dtd != NULL) && (dtd->pentities != NULL)) {
185 table = (xmlEntitiesTablePtr) dtd->pentities;
186 return((xmlEntityPtr) xmlHashLookup(table, name));
187 /* return(xmlGetEntityFromTable(table, name)); */
191 #endif /* LIBXML_TREE_ENABLED */
193 /************************************************************************
195 * QName handling helper *
197 ************************************************************************/
202 * @prefix: the prefix
203 * @memory: preallocated memory
204 * @len: preallocated memory length
206 * Builds the QName @prefix:@ncname in @memory if there is enough space
207 * and prefix is not NULL nor empty, otherwise allocate a new string.
208 * If prefix is NULL or empty it returns ncname.
210 * Returns the new string which must be freed by the caller if different from
211 * @memory and @ncname or NULL in case of error
214 xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
215 xmlChar *memory, int len) {
219 if (ncname == NULL) return(NULL);
220 if (prefix == NULL) return((xmlChar *) ncname);
222 lenn = strlen((char *) ncname);
223 lenp = strlen((char *) prefix);
225 if ((memory == NULL) || (len < lenn + lenp + 2)) {
226 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
228 xmlTreeErrMemory("building QName");
234 memcpy(&ret[0], prefix, lenp);
236 memcpy(&ret[lenp + 1], ncname, lenn);
237 ret[lenn + lenp + 1] = 0;
243 * @name: the full QName
244 * @prefix: a xmlChar **
246 * parse an XML qualified name string
248 * [NS 5] QName ::= (Prefix ':')? LocalPart
250 * [NS 6] Prefix ::= NCName
252 * [NS 7] LocalPart ::= NCName
254 * Returns NULL if not a QName, otherwise the local part, and prefix
255 * is updated to get the Prefix if any.
259 xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
263 if (prefix == NULL) return(NULL);
265 if (name == NULL) return(NULL);
267 #ifndef XML_XML_NAMESPACE
268 /* xml: prefix is not really a namespace */
269 if ((name[0] == 'x') && (name[1] == 'm') &&
270 (name[2] == 'l') && (name[3] == ':'))
274 /* nasty but valid */
279 * we are not trying to validate but just to cut, and yes it will
280 * work even if this is as set of UTF-8 encoded chars
282 while ((name[len] != 0) && (name[len] != ':'))
288 *prefix = xmlStrndup(name, len);
289 if (*prefix == NULL) {
290 xmlTreeErrMemory("QName split");
293 ret = xmlStrdup(&name[len + 1]);
295 xmlTreeErrMemory("QName split");
296 if (*prefix != NULL) {
308 * @name: the full QName
311 * parse an XML qualified name string,i
313 * returns NULL if it is not a Qualified Name, otherwise, update len
314 * with the lenght in byte of the prefix and return a pointer
315 * to the start of the name without the prefix
319 xmlSplitQName3(const xmlChar *name, int *len) {
322 if (name == NULL) return(NULL);
323 if (len == NULL) return(NULL);
325 /* nasty but valid */
330 * we are not trying to validate but just to cut, and yes it will
331 * work even if this is as set of UTF-8 encoded chars
333 while ((name[l] != 0) && (name[l] != ':'))
344 /************************************************************************
346 * Check Name, NCName and QName strings *
348 ************************************************************************/
350 #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
352 #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)
355 * @value: the value to check
356 * @space: allow spaces in front and end of the string
358 * Check that a value conforms to the lexical space of NCName
360 * Returns 0 if this validates, a positive error code number otherwise
361 * and -1 in case of internal or API error.
364 xmlValidateNCName(const xmlChar *value, int space) {
365 const xmlChar *cur = value;
372 * First quick algorithm for ASCII range
375 while (IS_BLANK_CH(*cur)) cur++;
376 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
381 while (((*cur >= 'a') && (*cur <= 'z')) ||
382 ((*cur >= 'A') && (*cur <= 'Z')) ||
383 ((*cur >= '0') && (*cur <= '9')) ||
384 (*cur == '_') || (*cur == '-') || (*cur == '.'))
387 while (IS_BLANK_CH(*cur)) cur++;
393 * Second check for chars outside the ASCII range
396 c = CUR_SCHAR(cur, l);
398 while (IS_BLANK(c)) {
400 c = CUR_SCHAR(cur, l);
403 if ((!IS_LETTER(c)) && (c != '_'))
406 c = CUR_SCHAR(cur, l);
407 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
408 (c == '-') || (c == '_') || IS_COMBINING(c) ||
411 c = CUR_SCHAR(cur, l);
414 while (IS_BLANK(c)) {
416 c = CUR_SCHAR(cur, l);
426 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
429 * @value: the value to check
430 * @space: allow spaces in front and end of the string
432 * Check that a value conforms to the lexical space of QName
434 * Returns 0 if this validates, a positive error code number otherwise
435 * and -1 in case of internal or API error.
438 xmlValidateQName(const xmlChar *value, int space) {
439 const xmlChar *cur = value;
445 * First quick algorithm for ASCII range
448 while (IS_BLANK_CH(*cur)) cur++;
449 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
454 while (((*cur >= 'a') && (*cur <= 'z')) ||
455 ((*cur >= 'A') && (*cur <= 'Z')) ||
456 ((*cur >= '0') && (*cur <= '9')) ||
457 (*cur == '_') || (*cur == '-') || (*cur == '.'))
461 if (((*cur >= 'a') && (*cur <= 'z')) ||
462 ((*cur >= 'A') && (*cur <= 'Z')) ||
467 while (((*cur >= 'a') && (*cur <= 'z')) ||
468 ((*cur >= 'A') && (*cur <= 'Z')) ||
469 ((*cur >= '0') && (*cur <= '9')) ||
470 (*cur == '_') || (*cur == '-') || (*cur == '.'))
474 while (IS_BLANK_CH(*cur)) cur++;
480 * Second check for chars outside the ASCII range
483 c = CUR_SCHAR(cur, l);
485 while (IS_BLANK(c)) {
487 c = CUR_SCHAR(cur, l);
490 if ((!IS_LETTER(c)) && (c != '_'))
493 c = CUR_SCHAR(cur, l);
494 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
495 (c == '-') || (c == '_') || IS_COMBINING(c) ||
498 c = CUR_SCHAR(cur, l);
502 c = CUR_SCHAR(cur, l);
503 if ((!IS_LETTER(c)) && (c != '_'))
506 c = CUR_SCHAR(cur, l);
507 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
508 (c == '-') || (c == '_') || IS_COMBINING(c) ||
511 c = CUR_SCHAR(cur, l);
515 while (IS_BLANK(c)) {
517 c = CUR_SCHAR(cur, l);
527 * @value: the value to check
528 * @space: allow spaces in front and end of the string
530 * Check that a value conforms to the lexical space of Name
532 * Returns 0 if this validates, a positive error code number otherwise
533 * and -1 in case of internal or API error.
536 xmlValidateName(const xmlChar *value, int space) {
537 const xmlChar *cur = value;
543 * First quick algorithm for ASCII range
546 while (IS_BLANK_CH(*cur)) cur++;
547 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
548 (*cur == '_') || (*cur == ':'))
552 while (((*cur >= 'a') && (*cur <= 'z')) ||
553 ((*cur >= 'A') && (*cur <= 'Z')) ||
554 ((*cur >= '0') && (*cur <= '9')) ||
555 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
558 while (IS_BLANK_CH(*cur)) cur++;
564 * Second check for chars outside the ASCII range
567 c = CUR_SCHAR(cur, l);
569 while (IS_BLANK(c)) {
571 c = CUR_SCHAR(cur, l);
574 if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
577 c = CUR_SCHAR(cur, l);
578 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
579 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
581 c = CUR_SCHAR(cur, l);
584 while (IS_BLANK(c)) {
586 c = CUR_SCHAR(cur, l);
595 * xmlValidateNMToken:
596 * @value: the value to check
597 * @space: allow spaces in front and end of the string
599 * Check that a value conforms to the lexical space of NMToken
601 * Returns 0 if this validates, a positive error code number otherwise
602 * and -1 in case of internal or API error.
605 xmlValidateNMToken(const xmlChar *value, int space) {
606 const xmlChar *cur = value;
612 * First quick algorithm for ASCII range
615 while (IS_BLANK_CH(*cur)) cur++;
616 if (((*cur >= 'a') && (*cur <= 'z')) ||
617 ((*cur >= 'A') && (*cur <= 'Z')) ||
618 ((*cur >= '0') && (*cur <= '9')) ||
619 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
623 while (((*cur >= 'a') && (*cur <= 'z')) ||
624 ((*cur >= 'A') && (*cur <= 'Z')) ||
625 ((*cur >= '0') && (*cur <= '9')) ||
626 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
629 while (IS_BLANK_CH(*cur)) cur++;
635 * Second check for chars outside the ASCII range
638 c = CUR_SCHAR(cur, l);
640 while (IS_BLANK(c)) {
642 c = CUR_SCHAR(cur, l);
645 if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
646 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
649 c = CUR_SCHAR(cur, l);
650 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
651 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
653 c = CUR_SCHAR(cur, l);
656 while (IS_BLANK(c)) {
658 c = CUR_SCHAR(cur, l);
665 #endif /* LIBXML_TREE_ENABLED */
667 /************************************************************************
669 * Allocation and deallocation of basic structures *
671 ************************************************************************/
674 * xmlSetBufferAllocationScheme:
675 * @scheme: allocation method to use
677 * Set the buffer allocation method. Types are
678 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
679 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
680 * improves performance
683 xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
684 if ((scheme == XML_BUFFER_ALLOC_EXACT) ||
685 (scheme == XML_BUFFER_ALLOC_DOUBLEIT))
686 xmlBufferAllocScheme = scheme;
690 * xmlGetBufferAllocationScheme:
693 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
694 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
695 * improves performance
697 * Returns the current allocation scheme
699 xmlBufferAllocationScheme
700 xmlGetBufferAllocationScheme(void) {
701 return(xmlBufferAllocScheme);
706 * @node: the element carrying the namespace
707 * @href: the URI associated
708 * @prefix: the prefix for the namespace
710 * Creation of a new Namespace. This function will refuse to create
711 * a namespace with a similar prefix than an existing one present on this
713 * We use href==NULL in the case of an element creation where the namespace
715 * Returns a new namespace pointer or NULL
718 xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
721 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
724 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
725 /* xml namespace is predefined, no need to add it */
726 if (xmlStrEqual(href, XML_XML_NAMESPACE))
730 * Problem, this is an attempt to bind xml prefix to a wrong
731 * namespace, which breaks
732 * Namespace constraint: Reserved Prefixes and Namespace Names
733 * from XML namespace. But documents authors may not care in
734 * their context so let's proceed.
739 * Allocate a new Namespace and fill the fields.
741 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
743 xmlTreeErrMemory("building namespace");
746 memset(cur, 0, sizeof(xmlNs));
747 cur->type = XML_LOCAL_NAMESPACE;
750 cur->href = xmlStrdup(href);
752 cur->prefix = xmlStrdup(prefix);
755 * Add it at the end to preserve parsing order ...
756 * and checks for existing use of the prefix
759 if (node->nsDef == NULL) {
762 xmlNsPtr prev = node->nsDef;
764 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
765 (xmlStrEqual(prev->prefix, cur->prefix))) {
769 while (prev->next != NULL) {
771 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
772 (xmlStrEqual(prev->prefix, cur->prefix))) {
785 * @node: a node in the document
786 * @ns: a namespace pointer
788 * Associate a namespace to a node, a posteriori.
791 xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
794 xmlGenericError(xmlGenericErrorContext,
795 "xmlSetNs: node == NULL\n");
804 * @cur: the namespace pointer
806 * Free up the structures associated to a namespace
809 xmlFreeNs(xmlNsPtr cur) {
812 xmlGenericError(xmlGenericErrorContext,
813 "xmlFreeNs : ns == NULL\n");
817 if (cur->href != NULL) xmlFree((char *) cur->href);
818 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
824 * @cur: the first namespace pointer
826 * Free up all the structures associated to the chained namespaces.
829 xmlFreeNsList(xmlNsPtr cur) {
833 xmlGenericError(xmlGenericErrorContext,
834 "xmlFreeNsList : ns == NULL\n");
838 while (cur != NULL) {
847 * @doc: the document pointer
848 * @name: the DTD name
849 * @ExternalID: the external ID
850 * @SystemID: the system ID
852 * Creation of a new DTD for the external subset. To create an
853 * internal subset, use xmlCreateIntSubset().
855 * Returns a pointer to the new DTD structure
858 xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
859 const xmlChar *ExternalID, const xmlChar *SystemID) {
862 if ((doc != NULL) && (doc->extSubset != NULL)) {
864 xmlGenericError(xmlGenericErrorContext,
865 "xmlNewDtd(%s): document %s already have a DTD %s\n",
866 /* !!! */ (char *) name, doc->name,
867 /* !!! */ (char *)doc->extSubset->name);
873 * Allocate a new DTD and fill the fields.
875 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
877 xmlTreeErrMemory("building DTD");
880 memset(cur, 0 , sizeof(xmlDtd));
881 cur->type = XML_DTD_NODE;
884 cur->name = xmlStrdup(name);
885 if (ExternalID != NULL)
886 cur->ExternalID = xmlStrdup(ExternalID);
887 if (SystemID != NULL)
888 cur->SystemID = xmlStrdup(SystemID);
890 doc->extSubset = cur;
893 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
894 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
900 * @doc: the document pointer
902 * Get the internal subset of a document
903 * Returns a pointer to the DTD structure or NULL if not found
907 xmlGetIntSubset(xmlDocPtr doc) {
913 while (cur != NULL) {
914 if (cur->type == XML_DTD_NODE)
915 return((xmlDtdPtr) cur);
918 return((xmlDtdPtr) doc->intSubset);
922 * xmlCreateIntSubset:
923 * @doc: the document pointer
924 * @name: the DTD name
925 * @ExternalID: the external (PUBLIC) ID
926 * @SystemID: the system ID
928 * Create the internal subset of a document
929 * Returns a pointer to the new DTD structure
932 xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
933 const xmlChar *ExternalID, const xmlChar *SystemID) {
936 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
938 xmlGenericError(xmlGenericErrorContext,
940 "xmlCreateIntSubset(): document %s already have an internal subset\n",
947 * Allocate a new DTD and fill the fields.
949 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
951 xmlTreeErrMemory("building internal subset");
954 memset(cur, 0, sizeof(xmlDtd));
955 cur->type = XML_DTD_NODE;
958 cur->name = xmlStrdup(name);
959 if (cur->name == NULL) {
960 xmlTreeErrMemory("building internal subset");
965 if (ExternalID != NULL) {
966 cur->ExternalID = xmlStrdup(ExternalID);
967 if (cur->ExternalID == NULL) {
968 xmlTreeErrMemory("building internal subset");
969 if (cur->name != NULL)
970 xmlFree((char *)cur->name);
975 if (SystemID != NULL) {
976 cur->SystemID = xmlStrdup(SystemID);
977 if (cur->SystemID == NULL) {
978 xmlTreeErrMemory("building internal subset");
979 if (cur->name != NULL)
980 xmlFree((char *)cur->name);
981 if (cur->ExternalID != NULL)
982 xmlFree((char *)cur->ExternalID);
988 doc->intSubset = cur;
991 if (doc->children == NULL) {
992 doc->children = (xmlNodePtr) cur;
993 doc->last = (xmlNodePtr) cur;
995 if (doc->type == XML_HTML_DOCUMENT_NODE) {
998 prev = doc->children;
999 prev->prev = (xmlNodePtr) cur;
1001 doc->children = (xmlNodePtr) cur;
1005 next = doc->children;
1006 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
1009 cur->prev = doc->last;
1010 cur->prev->next = (xmlNodePtr) cur;
1012 doc->last = (xmlNodePtr) cur;
1015 cur->prev = next->prev;
1016 if (cur->prev == NULL)
1017 doc->children = (xmlNodePtr) cur;
1019 cur->prev->next = (xmlNodePtr) cur;
1020 next->prev = (xmlNodePtr) cur;
1026 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1027 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1035 * Free a string if it is not owned by the "dict" dictionnary in the
1038 #define DICT_FREE(str) \
1039 if ((str) && ((!dict) || \
1040 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1041 xmlFree((char *)(str));
1048 * Copy a string using a "dict" dictionnary in the current scope,
1051 #define DICT_COPY(str, cpy) \
1054 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1055 cpy = (xmlChar *) (str); \
1057 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1059 cpy = xmlStrdup((const xmlChar *)(str)); }
1065 * Copy a string using a "dict" dictionnary in the current scope,
1068 #define DICT_CONST_COPY(str, cpy) \
1071 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1072 cpy = (const xmlChar *) (str); \
1074 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1076 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1081 * @cur: the DTD structure to free up
1083 * Free a DTD structure.
1086 xmlFreeDtd(xmlDtdPtr cur) {
1087 xmlDictPtr dict = NULL;
1092 if (cur->doc != NULL) dict = cur->doc->dict;
1094 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1095 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1097 if (cur->children != NULL) {
1098 xmlNodePtr next, c = cur->children;
1101 * Cleanup all nodes which are not part of the specific lists
1102 * of notations, elements, attributes and entities.
1106 if ((c->type != XML_NOTATION_NODE) &&
1107 (c->type != XML_ELEMENT_DECL) &&
1108 (c->type != XML_ATTRIBUTE_DECL) &&
1109 (c->type != XML_ENTITY_DECL)) {
1116 DICT_FREE(cur->name)
1117 DICT_FREE(cur->SystemID)
1118 DICT_FREE(cur->ExternalID)
1120 if (cur->notations != NULL)
1121 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
1123 if (cur->elements != NULL)
1124 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1125 if (cur->attributes != NULL)
1126 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1127 if (cur->entities != NULL)
1128 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1129 if (cur->pentities != NULL)
1130 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1137 * @version: xmlChar string giving the version of XML "1.0"
1139 * Creates a new XML document
1141 * Returns a new document
1144 xmlNewDoc(const xmlChar *version) {
1147 if (version == NULL)
1148 version = (const xmlChar *) "1.0";
1151 * Allocate a new document and fill the fields.
1153 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1155 xmlTreeErrMemory("building doc");
1158 memset(cur, 0, sizeof(xmlDoc));
1159 cur->type = XML_DOCUMENT_NODE;
1161 cur->version = xmlStrdup(version);
1162 if (cur->version == NULL) {
1163 xmlTreeErrMemory("building doc");
1167 cur->standalone = -1;
1168 cur->compression = -1; /* not initialized */
1170 cur->parseFlags = 0;
1171 cur->properties = XML_DOC_USERBUILT;
1173 * The in memory encoding is always UTF8
1174 * This field will never change and would
1175 * be obsolete if not for binary compatibility.
1177 cur->charset = XML_CHAR_ENCODING_UTF8;
1179 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1180 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
1186 * @cur: pointer to the document
1188 * Free up all the structures used by a document, tree included.
1191 xmlFreeDoc(xmlDocPtr cur) {
1192 xmlDtdPtr extSubset, intSubset;
1193 xmlDictPtr dict = NULL;
1197 xmlGenericError(xmlGenericErrorContext,
1198 "xmlFreeDoc : document == NULL\n");
1202 #ifdef LIBXML_DEBUG_RUNTIME
1203 #ifdef LIBXML_DEBUG_ENABLED
1204 xmlDebugCheckDocument(stderr, cur);
1208 if (cur != NULL) dict = cur->dict;
1210 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
1211 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1214 * Do this before freeing the children list to avoid ID lookups
1216 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1218 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1220 extSubset = cur->extSubset;
1221 intSubset = cur->intSubset;
1222 if (intSubset == extSubset)
1224 if (extSubset != NULL) {
1225 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
1226 cur->extSubset = NULL;
1227 xmlFreeDtd(extSubset);
1229 if (intSubset != NULL) {
1230 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
1231 cur->intSubset = NULL;
1232 xmlFreeDtd(intSubset);
1235 if (cur->children != NULL) xmlFreeNodeList(cur->children);
1236 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
1238 DICT_FREE(cur->version)
1239 DICT_FREE(cur->name)
1240 DICT_FREE(cur->encoding)
1243 if (dict) xmlDictFree(dict);
1247 * xmlStringLenGetNodeList:
1248 * @doc: the document
1249 * @value: the value of the text
1250 * @len: the length of the string value
1252 * Parse the value string and build the node list associated. Should
1253 * produce a flat tree with only TEXTs and ENTITY_REFs.
1254 * Returns a pointer to the first child
1257 xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
1258 xmlNodePtr ret = NULL, last = NULL;
1261 const xmlChar *cur = value, *end = cur + len;
1265 if (value == NULL) return(NULL);
1268 while ((cur < end) && (*cur != 0)) {
1269 if (cur[0] == '&') {
1274 * Save the current text.
1277 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1278 xmlNodeAddContentLen(last, q, cur - q);
1280 node = xmlNewDocTextLen(doc, q, cur - q);
1281 if (node == NULL) return(ret);
1292 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1298 while (tmp != ';') { /* Non input consuming loop */
1299 if ((tmp >= '0') && (tmp <= '9'))
1300 charval = charval * 16 + (tmp - '0');
1301 else if ((tmp >= 'a') && (tmp <= 'f'))
1302 charval = charval * 16 + (tmp - 'a') + 10;
1303 else if ((tmp >= 'A') && (tmp <= 'F'))
1304 charval = charval * 16 + (tmp - 'A') + 10;
1306 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1320 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1326 while (tmp != ';') { /* Non input consuming loops */
1327 if ((tmp >= '0') && (tmp <= '9'))
1328 charval = charval * 10 + (tmp - '0');
1330 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1346 * Read the entity string
1350 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1351 if ((cur >= end) || (*cur == 0)) {
1352 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1358 * Predefined entities don't generate nodes
1360 val = xmlStrndup(q, cur - q);
1361 ent = xmlGetDocEntity(doc, val);
1362 if ((ent != NULL) &&
1363 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1365 node = xmlNewDocText(doc, ent->content);
1367 } else if (last->type != XML_TEXT_NODE) {
1368 node = xmlNewDocText(doc, ent->content);
1369 last = xmlAddNextSibling(last, node);
1371 xmlNodeAddContent(last, ent->content);
1375 * Create a new REFERENCE_REF node
1377 node = xmlNewReference(doc, val);
1379 if (val != NULL) xmlFree(val);
1382 else if ((ent != NULL) && (ent->children == NULL)) {
1385 ent->children = xmlStringGetNodeList(doc,
1386 (const xmlChar*)node->content);
1388 temp = ent->children;
1390 temp->parent = (xmlNodePtr)ent;
1398 last = xmlAddNextSibling(last, node);
1410 l = xmlCopyCharMultiByte(buf, charval);
1412 node = xmlNewDocText(doc, buf);
1417 last = xmlAddNextSibling(last, node);
1425 if ((cur != q) || (ret == NULL)) {
1427 * Handle the last piece of text.
1429 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1430 xmlNodeAddContentLen(last, q, cur - q);
1432 node = xmlNewDocTextLen(doc, q, cur - q);
1433 if (node == NULL) return(ret);
1437 xmlAddNextSibling(last, node);
1445 * xmlStringGetNodeList:
1446 * @doc: the document
1447 * @value: the value of the attribute
1449 * Parse the value string and build the node list associated. Should
1450 * produce a flat tree with only TEXTs and ENTITY_REFs.
1451 * Returns a pointer to the first child
1454 xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
1455 xmlNodePtr ret = NULL, last = NULL;
1458 const xmlChar *cur = value;
1462 if (value == NULL) return(NULL);
1466 if (cur[0] == '&') {
1471 * Save the current text.
1474 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1475 xmlNodeAddContentLen(last, q, cur - q);
1477 node = xmlNewDocTextLen(doc, q, cur - q);
1478 if (node == NULL) return(ret);
1489 if ((cur[1] == '#') && (cur[2] == 'x')) {
1492 while (tmp != ';') { /* Non input consuming loop */
1493 if ((tmp >= '0') && (tmp <= '9'))
1494 charval = charval * 16 + (tmp - '0');
1495 else if ((tmp >= 'a') && (tmp <= 'f'))
1496 charval = charval * 16 + (tmp - 'a') + 10;
1497 else if ((tmp >= 'A') && (tmp <= 'F'))
1498 charval = charval * 16 + (tmp - 'A') + 10;
1500 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1511 } else if (cur[1] == '#') {
1514 while (tmp != ';') { /* Non input consuming loops */
1515 if ((tmp >= '0') && (tmp <= '9'))
1516 charval = charval * 10 + (tmp - '0');
1518 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1531 * Read the entity string
1535 while ((*cur != 0) && (*cur != ';')) cur++;
1537 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1538 (xmlNodePtr) doc, (const char *) q);
1543 * Predefined entities don't generate nodes
1545 val = xmlStrndup(q, cur - q);
1546 ent = xmlGetDocEntity(doc, val);
1547 if ((ent != NULL) &&
1548 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1550 node = xmlNewDocText(doc, ent->content);
1552 } else if (last->type != XML_TEXT_NODE) {
1553 node = xmlNewDocText(doc, ent->content);
1554 last = xmlAddNextSibling(last, node);
1556 xmlNodeAddContent(last, ent->content);
1560 * Create a new REFERENCE_REF node
1562 node = xmlNewReference(doc, val);
1564 if (val != NULL) xmlFree(val);
1567 else if ((ent != NULL) && (ent->children == NULL)) {
1570 ent->children = xmlStringGetNodeList(doc,
1571 (const xmlChar*)node->content);
1573 temp = ent->children;
1575 temp->parent = (xmlNodePtr)ent;
1582 last = xmlAddNextSibling(last, node);
1594 len = xmlCopyCharMultiByte(buf, charval);
1596 node = xmlNewDocText(doc, buf);
1601 last = xmlAddNextSibling(last, node);
1608 if ((cur != q) || (ret == NULL)) {
1610 * Handle the last piece of text.
1612 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1613 xmlNodeAddContentLen(last, q, cur - q);
1615 node = xmlNewDocTextLen(doc, q, cur - q);
1616 if (node == NULL) return(ret);
1620 last = xmlAddNextSibling(last, node);
1628 * xmlNodeListGetString:
1629 * @doc: the document
1630 * @list: a Node list
1631 * @inLine: should we replace entity contents or show their external form
1633 * Build the string equivalent to the text contained in the Node list
1634 * made of TEXTs and ENTITY_REFs
1636 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1639 xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1641 xmlNodePtr node = list;
1642 xmlChar *ret = NULL;
1648 while (node != NULL) {
1649 if ((node->type == XML_TEXT_NODE) ||
1650 (node->type == XML_CDATA_SECTION_NODE)) {
1652 ret = xmlStrcat(ret, node->content);
1656 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1657 if (buffer != NULL) {
1658 ret = xmlStrcat(ret, buffer);
1662 } else if (node->type == XML_ENTITY_REF_NODE) {
1664 ent = xmlGetDocEntity(doc, node->name);
1668 /* an entity content can be any "well balanced chunk",
1669 * i.e. the result of the content [43] production:
1670 * http://www.w3.org/TR/REC-xml#NT-content.
1671 * So it can contain text, CDATA section or nested
1672 * entity reference nodes (among others).
1673 * -> we recursive call xmlNodeListGetString()
1674 * which handles these types */
1675 buffer = xmlNodeListGetString(doc, ent->children, 1);
1676 if (buffer != NULL) {
1677 ret = xmlStrcat(ret, buffer);
1681 ret = xmlStrcat(ret, node->content);
1688 ret = xmlStrncat(ret, buf, 1);
1689 ret = xmlStrcat(ret, node->name);
1692 ret = xmlStrncat(ret, buf, 1);
1697 xmlGenericError(xmlGenericErrorContext,
1698 "xmlGetNodeListString : invalid node type %d\n",
1707 #ifdef LIBXML_TREE_ENABLED
1709 * xmlNodeListGetRawString:
1710 * @doc: the document
1711 * @list: a Node list
1712 * @inLine: should we replace entity contents or show their external form
1714 * Builds the string equivalent to the text contained in the Node list
1715 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1716 * this function doesn't do any character encoding handling.
1718 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1721 xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1723 xmlNodePtr node = list;
1724 xmlChar *ret = NULL;
1730 while (node != NULL) {
1731 if ((node->type == XML_TEXT_NODE) ||
1732 (node->type == XML_CDATA_SECTION_NODE)) {
1734 ret = xmlStrcat(ret, node->content);
1738 buffer = xmlEncodeSpecialChars(doc, node->content);
1739 if (buffer != NULL) {
1740 ret = xmlStrcat(ret, buffer);
1744 } else if (node->type == XML_ENTITY_REF_NODE) {
1746 ent = xmlGetDocEntity(doc, node->name);
1750 /* an entity content can be any "well balanced chunk",
1751 * i.e. the result of the content [43] production:
1752 * http://www.w3.org/TR/REC-xml#NT-content.
1753 * So it can contain text, CDATA section or nested
1754 * entity reference nodes (among others).
1755 * -> we recursive call xmlNodeListGetRawString()
1756 * which handles these types */
1758 xmlNodeListGetRawString(doc, ent->children, 1);
1759 if (buffer != NULL) {
1760 ret = xmlStrcat(ret, buffer);
1764 ret = xmlStrcat(ret, node->content);
1771 ret = xmlStrncat(ret, buf, 1);
1772 ret = xmlStrcat(ret, node->name);
1775 ret = xmlStrncat(ret, buf, 1);
1780 xmlGenericError(xmlGenericErrorContext,
1781 "xmlGetNodeListString : invalid node type %d\n",
1789 #endif /* LIBXML_TREE_ENABLED */
1792 xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1793 const xmlChar * name, const xmlChar * value,
1797 xmlDocPtr doc = NULL;
1799 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
1800 if ((eatname == 1) &&
1801 ((node->doc == NULL) ||
1802 (!(xmlDictOwns(node->doc->dict, name)))))
1803 xmlFree((xmlChar *) name);
1808 * Allocate a new property and fill the fields.
1810 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1812 if ((eatname == 1) &&
1813 ((node == NULL) || (node->doc == NULL) ||
1814 (!(xmlDictOwns(node->doc->dict, name)))))
1815 xmlFree((xmlChar *) name);
1816 xmlTreeErrMemory("building attribute");
1819 memset(cur, 0, sizeof(xmlAttr));
1820 cur->type = XML_ATTRIBUTE_NODE;
1830 if ((doc != NULL) && (doc->dict != NULL))
1831 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1833 cur->name = xmlStrdup(name);
1837 if (value != NULL) {
1840 if(!xmlCheckUTF8(value)) {
1841 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc,
1844 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1846 cur->children = xmlNewDocText(doc, value);
1848 tmp = cur->children;
1849 while (tmp != NULL) {
1850 tmp->parent = (xmlNodePtr) cur;
1851 if (tmp->next == NULL)
1858 * Add it at the end to preserve parsing order ...
1861 if (node->properties == NULL) {
1862 node->properties = cur;
1864 xmlAttrPtr prev = node->properties;
1866 while (prev->next != NULL)
1873 if ((value != NULL) && (node != NULL) &&
1874 (xmlIsID(node->doc, node, cur) == 1))
1875 xmlAddID(NULL, node->doc, value, cur);
1877 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1878 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1882 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1883 defined(LIBXML_SCHEMAS_ENABLED)
1886 * @node: the holding node
1887 * @name: the name of the attribute
1888 * @value: the value of the attribute
1890 * Create a new property carried by a node.
1891 * Returns a pointer to the attribute
1894 xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1898 xmlGenericError(xmlGenericErrorContext,
1899 "xmlNewProp : name == NULL\n");
1904 return xmlNewPropInternal(node, NULL, name, value, 0);
1906 #endif /* LIBXML_TREE_ENABLED */
1910 * @node: the holding node
1911 * @ns: the namespace
1912 * @name: the name of the attribute
1913 * @value: the value of the attribute
1915 * Create a new property tagged with a namespace and carried by a node.
1916 * Returns a pointer to the attribute
1919 xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1920 const xmlChar *value) {
1924 xmlGenericError(xmlGenericErrorContext,
1925 "xmlNewNsProp : name == NULL\n");
1930 return xmlNewPropInternal(node, ns, name, value, 0);
1934 * xmlNewNsPropEatName:
1935 * @node: the holding node
1936 * @ns: the namespace
1937 * @name: the name of the attribute
1938 * @value: the value of the attribute
1940 * Create a new property tagged with a namespace and carried by a node.
1941 * Returns a pointer to the attribute
1944 xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1945 const xmlChar *value) {
1949 xmlGenericError(xmlGenericErrorContext,
1950 "xmlNewNsPropEatName : name == NULL\n");
1955 return xmlNewPropInternal(node, ns, name, value, 1);
1960 * @doc: the document
1961 * @name: the name of the attribute
1962 * @value: the value of the attribute
1964 * Create a new property carried by a document.
1965 * Returns a pointer to the attribute
1968 xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1973 xmlGenericError(xmlGenericErrorContext,
1974 "xmlNewDocProp : name == NULL\n");
1980 * Allocate a new property and fill the fields.
1982 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1984 xmlTreeErrMemory("building attribute");
1987 memset(cur, 0, sizeof(xmlAttr));
1988 cur->type = XML_ATTRIBUTE_NODE;
1990 if ((doc != NULL) && (doc->dict != NULL))
1991 cur->name = xmlDictLookup(doc->dict, name, -1);
1993 cur->name = xmlStrdup(name);
1995 if (value != NULL) {
1998 cur->children = xmlStringGetNodeList(doc, value);
2001 tmp = cur->children;
2002 while (tmp != NULL) {
2003 tmp->parent = (xmlNodePtr) cur;
2004 if (tmp->next == NULL)
2010 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2011 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2017 * @cur: the first property in the list
2019 * Free a property and all its siblings, all the children are freed too.
2022 xmlFreePropList(xmlAttrPtr cur) {
2024 if (cur == NULL) return;
2025 while (cur != NULL) {
2034 * @cur: an attribute
2036 * Free one attribute, all the content is freed too
2039 xmlFreeProp(xmlAttrPtr cur) {
2040 xmlDictPtr dict = NULL;
2041 if (cur == NULL) return;
2043 if (cur->doc != NULL) dict = cur->doc->dict;
2045 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
2046 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2048 /* Check for ID removal -> leading to invalid references ! */
2049 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2050 xmlRemoveID(cur->doc, cur);
2052 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2053 DICT_FREE(cur->name)
2059 * @cur: an attribute
2061 * Unlink and free one attribute, all the content is freed too
2062 * Note this doesn't work for namespace definition attributes
2064 * Returns 0 if success and -1 in case of error.
2067 xmlRemoveProp(xmlAttrPtr cur) {
2071 xmlGenericError(xmlGenericErrorContext,
2072 "xmlRemoveProp : cur == NULL\n");
2076 if (cur->parent == NULL) {
2078 xmlGenericError(xmlGenericErrorContext,
2079 "xmlRemoveProp : cur->parent == NULL\n");
2083 tmp = cur->parent->properties;
2085 cur->parent->properties = cur->next;
2086 if (cur->next != NULL)
2087 cur->next->prev = NULL;
2091 while (tmp != NULL) {
2092 if (tmp->next == cur) {
2093 tmp->next = cur->next;
2094 if (tmp->next != NULL)
2095 tmp->next->prev = tmp;
2102 xmlGenericError(xmlGenericErrorContext,
2103 "xmlRemoveProp : attribute not owned by its node\n");
2110 * @doc: the target document
2111 * @name: the processing instruction name
2112 * @content: the PI content
2114 * Creation of a processing instruction element.
2115 * Returns a pointer to the new node object.
2118 xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
2123 xmlGenericError(xmlGenericErrorContext,
2124 "xmlNewPI : name == NULL\n");
2130 * Allocate a new node and fill the fields.
2132 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2134 xmlTreeErrMemory("building PI");
2137 memset(cur, 0, sizeof(xmlNode));
2138 cur->type = XML_PI_NODE;
2140 if ((doc != NULL) && (doc->dict != NULL))
2141 cur->name = xmlDictLookup(doc->dict, name, -1);
2143 cur->name = xmlStrdup(name);
2144 if (content != NULL) {
2145 cur->content = xmlStrdup(content);
2149 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2150 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2156 * @name: the processing instruction name
2157 * @content: the PI content
2159 * Creation of a processing instruction element.
2160 * Use xmlDocNewPI preferably to get string interning
2162 * Returns a pointer to the new node object.
2165 xmlNewPI(const xmlChar *name, const xmlChar *content) {
2166 return(xmlNewDocPI(NULL, name, content));
2171 * @ns: namespace if any
2172 * @name: the node name
2174 * Creation of a new node element. @ns is optional (NULL).
2176 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2180 xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2185 xmlGenericError(xmlGenericErrorContext,
2186 "xmlNewNode : name == NULL\n");
2192 * Allocate a new node and fill the fields.
2194 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2196 xmlTreeErrMemory("building node");
2199 memset(cur, 0, sizeof(xmlNode));
2200 cur->type = XML_ELEMENT_NODE;
2202 cur->name = xmlStrdup(name);
2205 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2206 xmlRegisterNodeDefaultValue(cur);
2211 * xmlNewNodeEatName:
2212 * @ns: namespace if any
2213 * @name: the node name
2215 * Creation of a new node element. @ns is optional (NULL).
2217 * Returns a pointer to the new node object, with pointer @name as
2218 * new node's name. Use xmlNewNode() if a copy of @name string is
2219 * is needed as new node's name.
2222 xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2227 xmlGenericError(xmlGenericErrorContext,
2228 "xmlNewNode : name == NULL\n");
2234 * Allocate a new node and fill the fields.
2236 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2238 xmlTreeErrMemory("building node");
2239 /* we can't check here that name comes from the doc dictionnary */
2242 memset(cur, 0, sizeof(xmlNode));
2243 cur->type = XML_ELEMENT_NODE;
2248 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2249 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2255 * @doc: the document
2256 * @ns: namespace if any
2257 * @name: the node name
2258 * @content: the XML text content if any
2260 * Creation of a new node element within a document. @ns and @content
2261 * are optional (NULL).
2262 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2263 * references, but XML special chars need to be escaped first by using
2264 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2265 * need entities support.
2267 * Returns a pointer to the new node object.
2270 xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2271 const xmlChar *name, const xmlChar *content) {
2274 if ((doc != NULL) && (doc->dict != NULL))
2275 cur = xmlNewNodeEatName(ns, (xmlChar *)
2276 xmlDictLookup(doc->dict, name, -1));
2278 cur = xmlNewNode(ns, name);
2281 if (content != NULL) {
2282 cur->children = xmlStringGetNodeList(doc, content);
2283 UPDATE_LAST_CHILD_AND_PARENT(cur)
2291 * xmlNewDocNodeEatName:
2292 * @doc: the document
2293 * @ns: namespace if any
2294 * @name: the node name
2295 * @content: the XML text content if any
2297 * Creation of a new node element within a document. @ns and @content
2298 * are optional (NULL).
2299 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2300 * references, but XML special chars need to be escaped first by using
2301 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2302 * need entities support.
2304 * Returns a pointer to the new node object.
2307 xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2308 xmlChar *name, const xmlChar *content) {
2311 cur = xmlNewNodeEatName(ns, name);
2314 if (content != NULL) {
2315 cur->children = xmlStringGetNodeList(doc, content);
2316 UPDATE_LAST_CHILD_AND_PARENT(cur)
2319 /* if name don't come from the doc dictionnary free it here */
2320 if ((name != NULL) && (doc != NULL) &&
2321 (!(xmlDictOwns(doc->dict, name))))
2327 #ifdef LIBXML_TREE_ENABLED
2330 * @doc: the document
2331 * @ns: namespace if any
2332 * @name: the node name
2333 * @content: the text content if any
2335 * Creation of a new node element within a document. @ns and @content
2336 * are optional (NULL).
2338 * Returns a pointer to the new node object.
2341 xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2342 const xmlChar *name, const xmlChar *content) {
2345 cur = xmlNewDocNode(doc, ns, name, NULL);
2348 if (content != NULL) {
2349 cur->children = xmlNewDocText(doc, content);
2350 UPDATE_LAST_CHILD_AND_PARENT(cur)
2357 * xmlNewDocFragment:
2358 * @doc: the document owning the fragment
2360 * Creation of a new Fragment node.
2361 * Returns a pointer to the new node object.
2364 xmlNewDocFragment(xmlDocPtr doc) {
2368 * Allocate a new DocumentFragment node and fill the fields.
2370 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2372 xmlTreeErrMemory("building fragment");
2375 memset(cur, 0, sizeof(xmlNode));
2376 cur->type = XML_DOCUMENT_FRAG_NODE;
2380 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2381 xmlRegisterNodeDefaultValue(cur);
2384 #endif /* LIBXML_TREE_ENABLED */
2388 * @content: the text content
2390 * Creation of a new text node.
2391 * Returns a pointer to the new node object.
2394 xmlNewText(const xmlChar *content) {
2398 * Allocate a new node and fill the fields.
2400 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2402 xmlTreeErrMemory("building text");
2405 memset(cur, 0, sizeof(xmlNode));
2406 cur->type = XML_TEXT_NODE;
2408 cur->name = xmlStringText;
2409 if (content != NULL) {
2410 cur->content = xmlStrdup(content);
2413 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2414 xmlRegisterNodeDefaultValue(cur);
2418 #ifdef LIBXML_TREE_ENABLED
2421 * @parent: the parent node
2422 * @ns: a namespace if any
2423 * @name: the name of the child
2424 * @content: the text content of the child if any.
2426 * Creation of a new child element, added at the end of @parent children list.
2427 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2428 * created element inherits the namespace of @parent. If @content is non NULL,
2429 * a child TEXT node will be created containing the string @content.
2430 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2431 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
2432 * reserved XML chars that might appear in @content, such as the ampersand,
2433 * greater-than or less-than signs, are automatically replaced by their XML
2434 * escaped entity representations.
2436 * Returns a pointer to the new node object.
2439 xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2440 const xmlChar *name, const xmlChar *content) {
2441 xmlNodePtr cur, prev;
2443 if (parent == NULL) {
2445 xmlGenericError(xmlGenericErrorContext,
2446 "xmlNewTextChild : parent == NULL\n");
2453 xmlGenericError(xmlGenericErrorContext,
2454 "xmlNewTextChild : name == NULL\n");
2460 * Allocate a new node
2462 if (parent->type == XML_ELEMENT_NODE) {
2464 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2466 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2467 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2468 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2470 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2472 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2473 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2474 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2478 if (cur == NULL) return(NULL);
2481 * add the new element at the end of the children list.
2483 cur->type = XML_ELEMENT_NODE;
2484 cur->parent = parent;
2485 cur->doc = parent->doc;
2486 if (parent->children == NULL) {
2487 parent->children = cur;
2490 prev = parent->last;
2498 #endif /* LIBXML_TREE_ENABLED */
2502 * @doc: the document
2503 * @name: the char ref string, starting with # or "&# ... ;"
2505 * Creation of a new character reference node.
2506 * Returns a pointer to the new node object.
2509 xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2516 * Allocate a new node and fill the fields.
2518 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2520 xmlTreeErrMemory("building character reference");
2523 memset(cur, 0, sizeof(xmlNode));
2524 cur->type = XML_ENTITY_REF_NODE;
2527 if (name[0] == '&') {
2530 len = xmlStrlen(name);
2531 if (name[len - 1] == ';')
2532 cur->name = xmlStrndup(name, len - 1);
2534 cur->name = xmlStrndup(name, len);
2536 cur->name = xmlStrdup(name);
2538 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2539 xmlRegisterNodeDefaultValue(cur);
2545 * @doc: the document
2546 * @name: the reference name, or the reference string with & and ;
2548 * Creation of a new reference node.
2549 * Returns a pointer to the new node object.
2552 xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
2560 * Allocate a new node and fill the fields.
2562 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2564 xmlTreeErrMemory("building reference");
2567 memset(cur, 0, sizeof(xmlNode));
2568 cur->type = XML_ENTITY_REF_NODE;
2571 if (name[0] == '&') {
2574 len = xmlStrlen(name);
2575 if (name[len - 1] == ';')
2576 cur->name = xmlStrndup(name, len - 1);
2578 cur->name = xmlStrndup(name, len);
2580 cur->name = xmlStrdup(name);
2582 ent = xmlGetDocEntity(doc, cur->name);
2584 cur->content = ent->content;
2586 * The parent pointer in entity is a DTD pointer and thus is NOT
2587 * updated. Not sure if this is 100% correct.
2590 cur->children = (xmlNodePtr) ent;
2591 cur->last = (xmlNodePtr) ent;
2594 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2595 xmlRegisterNodeDefaultValue(cur);
2601 * @doc: the document
2602 * @content: the text content
2604 * Creation of a new text node within a document.
2605 * Returns a pointer to the new node object.
2608 xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
2611 cur = xmlNewText(content);
2612 if (cur != NULL) cur->doc = doc;
2618 * @content: the text content
2619 * @len: the text len.
2621 * Creation of a new text node with an extra parameter for the content's length
2622 * Returns a pointer to the new node object.
2625 xmlNewTextLen(const xmlChar *content, int len) {
2629 * Allocate a new node and fill the fields.
2631 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2633 xmlTreeErrMemory("building text");
2636 memset(cur, 0, sizeof(xmlNode));
2637 cur->type = XML_TEXT_NODE;
2639 cur->name = xmlStringText;
2640 if (content != NULL) {
2641 cur->content = xmlStrndup(content, len);
2644 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2645 xmlRegisterNodeDefaultValue(cur);
2651 * @doc: the document
2652 * @content: the text content
2653 * @len: the text len.
2655 * Creation of a new text node with an extra content length parameter. The
2656 * text node pertain to a given document.
2657 * Returns a pointer to the new node object.
2660 xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2663 cur = xmlNewTextLen(content, len);
2664 if (cur != NULL) cur->doc = doc;
2670 * @content: the comment content
2672 * Creation of a new node containing a comment.
2673 * Returns a pointer to the new node object.
2676 xmlNewComment(const xmlChar *content) {
2680 * Allocate a new node and fill the fields.
2682 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2684 xmlTreeErrMemory("building comment");
2687 memset(cur, 0, sizeof(xmlNode));
2688 cur->type = XML_COMMENT_NODE;
2690 cur->name = xmlStringComment;
2691 if (content != NULL) {
2692 cur->content = xmlStrdup(content);
2695 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2696 xmlRegisterNodeDefaultValue(cur);
2702 * @doc: the document
2703 * @content: the CDATA block content content
2704 * @len: the length of the block
2706 * Creation of a new node containing a CDATA block.
2707 * Returns a pointer to the new node object.
2710 xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2714 * Allocate a new node and fill the fields.
2716 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2718 xmlTreeErrMemory("building CDATA");
2721 memset(cur, 0, sizeof(xmlNode));
2722 cur->type = XML_CDATA_SECTION_NODE;
2725 if (content != NULL) {
2726 cur->content = xmlStrndup(content, len);
2729 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2730 xmlRegisterNodeDefaultValue(cur);
2736 * @doc: the document
2737 * @content: the comment content
2739 * Creation of a new node containing a comment within a document.
2740 * Returns a pointer to the new node object.
2743 xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2746 cur = xmlNewComment(content);
2747 if (cur != NULL) cur->doc = doc;
2753 * @tree: the top element
2754 * @doc: the document
2756 * update all nodes under the tree to point to the right document
2759 xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
2764 if (tree->doc != doc) {
2765 if(tree->type == XML_ELEMENT_NODE) {
2766 prop = tree->properties;
2767 while (prop != NULL) {
2769 xmlSetListDoc(prop->children, doc);
2773 if (tree->children != NULL)
2774 xmlSetListDoc(tree->children, doc);
2781 * @list: the first element
2782 * @doc: the document
2784 * update all nodes in the list to point to the right document
2787 xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2793 while (cur != NULL) {
2794 if (cur->doc != doc)
2795 xmlSetTreeDoc(cur, doc);
2800 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
2803 * @parent: the parent node
2804 * @ns: a namespace if any
2805 * @name: the name of the child
2806 * @content: the XML content of the child if any.
2808 * Creation of a new child element, added at the end of @parent children list.
2809 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2810 * created element inherits the namespace of @parent. If @content is non NULL,
2811 * a child list containing the TEXTs and ENTITY_REFs node will be created.
2812 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2813 * references. XML special chars must be escaped first by using
2814 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
2816 * Returns a pointer to the new node object.
2819 xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2820 const xmlChar *name, const xmlChar *content) {
2821 xmlNodePtr cur, prev;
2823 if (parent == NULL) {
2825 xmlGenericError(xmlGenericErrorContext,
2826 "xmlNewChild : parent == NULL\n");
2833 xmlGenericError(xmlGenericErrorContext,
2834 "xmlNewChild : name == NULL\n");
2840 * Allocate a new node
2842 if (parent->type == XML_ELEMENT_NODE) {
2844 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2846 cur = xmlNewDocNode(parent->doc, ns, name, content);
2847 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2848 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2850 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2852 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
2853 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2854 cur = xmlNewDocNode( parent->doc, ns, name, content);
2858 if (cur == NULL) return(NULL);
2861 * add the new element at the end of the children list.
2863 cur->type = XML_ELEMENT_NODE;
2864 cur->parent = parent;
2865 cur->doc = parent->doc;
2866 if (parent->children == NULL) {
2867 parent->children = cur;
2870 prev = parent->last;
2878 #endif /* LIBXML_TREE_ENABLED */
2881 * xmlAddPropSibling:
2882 * @prev: the attribute to which @prop is added after
2883 * @cur: the base attribute passed to calling function
2884 * @prop: the new attribute
2886 * Add a new attribute after @prev using @cur as base attribute.
2887 * When inserting before @cur, @prev is passed as @cur->prev.
2888 * When inserting after @cur, @prev is passed as @cur.
2889 * If an existing attribute is found it is detroyed prior to adding @prop.
2891 * Returns the attribute being inserted or NULL in case of error.
2894 xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2897 if (cur->type != XML_ATTRIBUTE_NODE)
2900 /* check if an attribute with the same name exists */
2901 if (prop->ns == NULL)
2902 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2904 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2906 if (prop->doc != cur->doc) {
2907 xmlSetTreeDoc(prop, cur->doc);
2909 prop->parent = cur->parent;
2912 prop->next = prev->next;
2915 prop->next->prev = prop;
2920 if (prop->prev == NULL && prop->parent != NULL)
2921 prop->parent->properties = (xmlAttrPtr) prop;
2922 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2923 /* different instance, destroy it (attributes must be unique) */
2924 xmlRemoveProp((xmlAttrPtr) attr);
2930 * xmlAddNextSibling:
2931 * @cur: the child node
2932 * @elem: the new node
2934 * Add a new node @elem as the next sibling of @cur
2935 * If the new node was already inserted in a document it is
2936 * first unlinked from its existing context.
2937 * As a result of text merging @elem may be freed.
2938 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2939 * If there is an attribute with equal name, it is first destroyed.
2941 * Returns the new node or NULL in case of error.
2944 xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2947 xmlGenericError(xmlGenericErrorContext,
2948 "xmlAddNextSibling : cur == NULL\n");
2954 xmlGenericError(xmlGenericErrorContext,
2955 "xmlAddNextSibling : elem == NULL\n");
2962 xmlGenericError(xmlGenericErrorContext,
2963 "xmlAddNextSibling : cur == elem\n");
2968 xmlUnlinkNode(elem);
2970 if (elem->type == XML_TEXT_NODE) {
2971 if (cur->type == XML_TEXT_NODE) {
2972 xmlNodeAddContent(cur, elem->content);
2976 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
2977 (cur->name == cur->next->name)) {
2980 tmp = xmlStrdup(elem->content);
2981 tmp = xmlStrcat(tmp, cur->next->content);
2982 xmlNodeSetContent(cur->next, tmp);
2987 } else if (elem->type == XML_ATTRIBUTE_NODE) {
2988 return xmlAddPropSibling(cur, cur, elem);
2991 if (elem->doc != cur->doc) {
2992 xmlSetTreeDoc(elem, cur->doc);
2994 elem->parent = cur->parent;
2996 elem->next = cur->next;
2998 if (elem->next != NULL)
2999 elem->next->prev = elem;
3000 if ((elem->parent != NULL) && (elem->parent->last == cur))
3001 elem->parent->last = elem;
3005 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
3006 defined(LIBXML_SCHEMAS_ENABLED)
3008 * xmlAddPrevSibling:
3009 * @cur: the child node
3010 * @elem: the new node
3012 * Add a new node @elem as the previous sibling of @cur
3013 * merging adjacent TEXT nodes (@elem may be freed)
3014 * If the new node was already inserted in a document it is
3015 * first unlinked from its existing context.
3016 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3017 * If there is an attribute with equal name, it is first destroyed.
3019 * Returns the new node or NULL in case of error.
3022 xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
3025 xmlGenericError(xmlGenericErrorContext,
3026 "xmlAddPrevSibling : cur == NULL\n");
3032 xmlGenericError(xmlGenericErrorContext,
3033 "xmlAddPrevSibling : elem == NULL\n");
3040 xmlGenericError(xmlGenericErrorContext,
3041 "xmlAddPrevSibling : cur == elem\n");
3046 xmlUnlinkNode(elem);
3048 if (elem->type == XML_TEXT_NODE) {
3049 if (cur->type == XML_TEXT_NODE) {
3052 tmp = xmlStrdup(elem->content);
3053 tmp = xmlStrcat(tmp, cur->content);
3054 xmlNodeSetContent(cur, tmp);
3059 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3060 (cur->name == cur->prev->name)) {
3061 xmlNodeAddContent(cur->prev, elem->content);
3065 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3066 return xmlAddPropSibling(cur->prev, cur, elem);
3069 if (elem->doc != cur->doc) {
3070 xmlSetTreeDoc(elem, cur->doc);
3072 elem->parent = cur->parent;
3074 elem->prev = cur->prev;
3076 if (elem->prev != NULL)
3077 elem->prev->next = elem;
3078 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
3079 elem->parent->children = elem;
3083 #endif /* LIBXML_TREE_ENABLED */
3087 * @cur: the child node
3088 * @elem: the new node
3090 * Add a new element @elem to the list of siblings of @cur
3091 * merging adjacent TEXT nodes (@elem may be freed)
3092 * If the new element was already inserted in a document it is
3093 * first unlinked from its existing context.
3095 * Returns the new element or NULL in case of error.
3098 xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3103 xmlGenericError(xmlGenericErrorContext,
3104 "xmlAddSibling : cur == NULL\n");
3111 xmlGenericError(xmlGenericErrorContext,
3112 "xmlAddSibling : elem == NULL\n");
3119 xmlGenericError(xmlGenericErrorContext,
3120 "xmlAddSibling : cur == elem\n");
3126 * Constant time is we can rely on the ->parent->last to find
3129 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
3130 (cur->parent->children != NULL) &&
3131 (cur->parent->last != NULL) &&
3132 (cur->parent->last->next == NULL)) {
3133 cur = cur->parent->last;
3135 while (cur->next != NULL) cur = cur->next;
3138 xmlUnlinkNode(elem);
3140 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3141 (cur->name == elem->name)) {
3142 xmlNodeAddContent(cur, elem->content);
3145 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3146 return xmlAddPropSibling(cur, cur, elem);
3149 if (elem->doc != cur->doc) {
3150 xmlSetTreeDoc(elem, cur->doc);
3152 parent = cur->parent;
3155 elem->parent = parent;
3158 parent->last = elem;
3165 * @parent: the parent node
3166 * @cur: the first node in the list
3168 * Add a list of node at the end of the child list of the parent
3169 * merging adjacent TEXT nodes (@cur may be freed)
3171 * Returns the last child or NULL in case of error.
3174 xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3177 if (parent == NULL) {
3179 xmlGenericError(xmlGenericErrorContext,
3180 "xmlAddChildList : parent == NULL\n");
3187 xmlGenericError(xmlGenericErrorContext,
3188 "xmlAddChildList : child == NULL\n");
3193 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3194 (cur->doc != parent->doc)) {
3196 xmlGenericError(xmlGenericErrorContext,
3197 "Elements moved to a different document\n");
3202 * add the first element at the end of the children list.
3205 if (parent->children == NULL) {
3206 parent->children = cur;
3209 * If cur and parent->last both are TEXT nodes, then merge them.
3211 if ((cur->type == XML_TEXT_NODE) &&
3212 (parent->last->type == XML_TEXT_NODE) &&
3213 (cur->name == parent->last->name)) {
3214 xmlNodeAddContent(parent->last, cur->content);
3216 * if it's the only child, nothing more to be done.
3218 if (cur->next == NULL) {
3220 return(parent->last);
3226 prev = parent->last;
3230 while (cur->next != NULL) {
3231 cur->parent = parent;
3232 if (cur->doc != parent->doc) {
3233 xmlSetTreeDoc(cur, parent->doc);
3237 cur->parent = parent;
3238 /* the parent may not be linked to a doc ! */
3239 if (cur->doc != parent->doc) {
3240 xmlSetTreeDoc(cur, parent->doc);
3249 * @parent: the parent node
3250 * @cur: the child node
3252 * Add a new node to @parent, at the end of the child (or property) list
3253 * merging adjacent TEXT nodes (in which case @cur is freed)
3254 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3255 * If there is an attribute with equal name, it is first destroyed.
3257 * Returns the child or NULL in case of error.
3260 xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3263 if (parent == NULL) {
3265 xmlGenericError(xmlGenericErrorContext,
3266 "xmlAddChild : parent == NULL\n");
3273 xmlGenericError(xmlGenericErrorContext,
3274 "xmlAddChild : child == NULL\n");
3279 if (parent == cur) {
3281 xmlGenericError(xmlGenericErrorContext,
3282 "xmlAddChild : parent == cur\n");
3287 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
3288 * cur is then freed.
3290 if (cur->type == XML_TEXT_NODE) {
3291 if ((parent->type == XML_TEXT_NODE) &&
3292 (parent->content != NULL) &&
3293 (parent->name == cur->name)) {
3294 xmlNodeAddContent(parent, cur->content);
3298 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
3299 (parent->last->name == cur->name) &&
3300 (parent->last != cur)) {
3301 xmlNodeAddContent(parent->last, cur->content);
3303 return(parent->last);
3308 * add the new element at the end of the children list.
3311 cur->parent = parent;
3312 if (cur->doc != parent->doc) {
3313 xmlSetTreeDoc(cur, parent->doc);
3315 /* this check prevents a loop on tree-traversions if a developer
3316 * tries to add a node to its parent multiple times
3324 if ((parent->type == XML_TEXT_NODE) &&
3325 (parent->content != NULL) &&
3327 xmlNodeAddContent(parent, cur->content);
3331 if (cur->type == XML_ATTRIBUTE_NODE) {
3332 if (parent->type != XML_ELEMENT_NODE)
3334 if (parent->properties != NULL) {
3335 /* check if an attribute with the same name exists */
3336 xmlAttrPtr lastattr;
3338 if (cur->ns == NULL)
3339 lastattr = xmlHasNsProp(parent, cur->name, NULL);
3341 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
3342 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
3343 /* different instance, destroy it (attributes must be unique) */
3344 xmlUnlinkNode((xmlNodePtr) lastattr);
3345 xmlFreeProp(lastattr);
3347 if (lastattr == (xmlAttrPtr) cur)
3351 if (parent->properties == NULL) {
3352 parent->properties = (xmlAttrPtr) cur;
3355 xmlAttrPtr lastattr = parent->properties;
3356 while (lastattr->next != NULL) {
3357 lastattr = lastattr->next;
3359 lastattr->next = (xmlAttrPtr) cur;
3360 ((xmlAttrPtr) cur)->prev = lastattr;
3363 if (parent->children == NULL) {
3364 parent->children = cur;
3367 prev = parent->last;
3378 * @parent: the parent node
3380 * Search the last child of a node.
3381 * Returns the last child or NULL if none.
3384 xmlGetLastChild(xmlNodePtr parent) {
3385 if (parent == NULL) {
3387 xmlGenericError(xmlGenericErrorContext,
3388 "xmlGetLastChild : parent == NULL\n");
3392 return(parent->last);
3395 #ifdef LIBXML_TREE_ENABLED
3397 * 5 interfaces from DOM ElementTraversal
3401 * xmlChildElementCount:
3402 * @parent: the parent node
3404 * Finds the current number of child nodes of that element which are
3406 * Note the handling of entities references is different than in
3407 * the W3C DOM element traversal spec since we don't have back reference
3408 * from entities content to entities references.
3410 * Returns the count of element child or 0 if not available
3413 xmlChildElementCount(xmlNodePtr parent) {
3414 unsigned long ret = 0;
3415 xmlNodePtr cur = NULL;
3419 switch (parent->type) {
3420 case XML_ELEMENT_NODE:
3421 case XML_ENTITY_NODE:
3422 case XML_DOCUMENT_NODE:
3423 case XML_HTML_DOCUMENT_NODE:
3424 cur = parent->children;
3429 while (cur != NULL) {
3430 if (cur->type == XML_ELEMENT_NODE)
3438 * xmlFirstElementChild:
3439 * @parent: the parent node
3441 * Finds the first child node of that element which is a Element node
3442 * Note the handling of entities references is different than in
3443 * the W3C DOM element traversal spec since we don't have back reference
3444 * from entities content to entities references.
3446 * Returns the first element child or NULL if not available
3449 xmlFirstElementChild(xmlNodePtr parent) {
3450 xmlNodePtr cur = NULL;
3454 switch (parent->type) {
3455 case XML_ELEMENT_NODE:
3456 case XML_ENTITY_NODE:
3457 case XML_DOCUMENT_NODE:
3458 case XML_HTML_DOCUMENT_NODE:
3459 cur = parent->children;
3464 while (cur != NULL) {
3465 if (cur->type == XML_ELEMENT_NODE)
3473 * xmlLastElementChild:
3474 * @parent: the parent node
3476 * Finds the last child node of that element which is a Element node
3477 * Note the handling of entities references is different than in
3478 * the W3C DOM element traversal spec since we don't have back reference
3479 * from entities content to entities references.
3481 * Returns the last element child or NULL if not available
3484 xmlLastElementChild(xmlNodePtr parent) {
3485 xmlNodePtr cur = NULL;
3489 switch (parent->type) {
3490 case XML_ELEMENT_NODE:
3491 case XML_ENTITY_NODE:
3492 case XML_DOCUMENT_NODE:
3493 case XML_HTML_DOCUMENT_NODE:
3499 while (cur != NULL) {
3500 if (cur->type == XML_ELEMENT_NODE)
3508 * xmlPreviousElementSibling:
3509 * @node: the current node
3511 * Finds the first closest previous sibling of the node which is an
3513 * Note the handling of entities references is different than in
3514 * the W3C DOM element traversal spec since we don't have back reference
3515 * from entities content to entities references.
3517 * Returns the previous element sibling or NULL if not available
3520 xmlPreviousElementSibling(xmlNodePtr node) {
3523 switch (node->type) {
3524 case XML_ELEMENT_NODE:
3526 case XML_CDATA_SECTION_NODE:
3527 case XML_ENTITY_REF_NODE:
3528 case XML_ENTITY_NODE:
3530 case XML_COMMENT_NODE:
3531 case XML_XINCLUDE_START:
3532 case XML_XINCLUDE_END:
3538 while (node != NULL) {
3539 if (node->type == XML_ELEMENT_NODE)
3547 * xmlNextElementSibling:
3548 * @node: the current node
3550 * Finds the first closest next sibling of the node which is an
3552 * Note the handling of entities references is different than in
3553 * the W3C DOM element traversal spec since we don't have back reference
3554 * from entities content to entities references.
3556 * Returns the next element sibling or NULL if not available
3559 xmlNextElementSibling(xmlNodePtr node) {
3562 switch (node->type) {
3563 case XML_ELEMENT_NODE:
3565 case XML_CDATA_SECTION_NODE:
3566 case XML_ENTITY_REF_NODE:
3567 case XML_ENTITY_NODE:
3569 case XML_COMMENT_NODE:
3571 case XML_XINCLUDE_START:
3572 case XML_XINCLUDE_END:
3578 while (node != NULL) {
3579 if (node->type == XML_ELEMENT_NODE)
3586 #endif /* LIBXML_TREE_ENABLED */
3590 * @cur: the first node in the list
3592 * Free a node and all its siblings, this is a recursive behaviour, all
3593 * the children are freed too.
3596 xmlFreeNodeList(xmlNodePtr cur) {
3598 xmlDictPtr dict = NULL;
3600 if (cur == NULL) return;
3601 if (cur->type == XML_NAMESPACE_DECL) {
3602 xmlFreeNsList((xmlNsPtr) cur);
3605 if ((cur->type == XML_DOCUMENT_NODE) ||
3606 #ifdef LIBXML_DOCB_ENABLED
3607 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
3609 (cur->type == XML_HTML_DOCUMENT_NODE)) {
3610 xmlFreeDoc((xmlDocPtr) cur);
3613 if (cur->doc != NULL) dict = cur->doc->dict;
3614 while (cur != NULL) {
3616 if (cur->type != XML_DTD_NODE) {
3618 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3619 xmlDeregisterNodeDefaultValue(cur);
3621 if ((cur->children != NULL) &&
3622 (cur->type != XML_ENTITY_REF_NODE))
3623 xmlFreeNodeList(cur->children);
3624 if (((cur->type == XML_ELEMENT_NODE) ||
3625 (cur->type == XML_XINCLUDE_START) ||
3626 (cur->type == XML_XINCLUDE_END)) &&
3627 (cur->properties != NULL))
3628 xmlFreePropList(cur->properties);
3629 if ((cur->type != XML_ELEMENT_NODE) &&
3630 (cur->type != XML_XINCLUDE_START) &&
3631 (cur->type != XML_XINCLUDE_END) &&
3632 (cur->type != XML_ENTITY_REF_NODE) &&
3633 (cur->content != (xmlChar *) &(cur->properties))) {
3634 DICT_FREE(cur->content)
3636 if (((cur->type == XML_ELEMENT_NODE) ||
3637 (cur->type == XML_XINCLUDE_START) ||
3638 (cur->type == XML_XINCLUDE_END)) &&
3639 (cur->nsDef != NULL))
3640 xmlFreeNsList(cur->nsDef);
3643 * When a node is a text node or a comment, it uses a global static
3644 * variable for the name of the node.
3645 * Otherwise the node name might come from the document's
3648 if ((cur->name != NULL) &&
3649 (cur->type != XML_TEXT_NODE) &&
3650 (cur->type != XML_COMMENT_NODE))
3651 DICT_FREE(cur->name)
3662 * Free a node, this is a recursive behaviour, all the children are freed too.
3663 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3666 xmlFreeNode(xmlNodePtr cur) {
3667 xmlDictPtr dict = NULL;
3669 if (cur == NULL) return;
3671 /* use xmlFreeDtd for DTD nodes */
3672 if (cur->type == XML_DTD_NODE) {
3673 xmlFreeDtd((xmlDtdPtr) cur);
3676 if (cur->type == XML_NAMESPACE_DECL) {
3677 xmlFreeNs((xmlNsPtr) cur);
3680 if (cur->type == XML_ATTRIBUTE_NODE) {
3681 xmlFreeProp((xmlAttrPtr) cur);
3685 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
3686 xmlDeregisterNodeDefaultValue(cur);
3688 if (cur->doc != NULL) dict = cur->doc->dict;
3690 if (cur->type == XML_ENTITY_DECL) {
3691 xmlEntityPtr ent = (xmlEntityPtr) cur;
3692 DICT_FREE(ent->SystemID);
3693 DICT_FREE(ent->ExternalID);
3695 if ((cur->children != NULL) &&
3696 (cur->type != XML_ENTITY_REF_NODE))
3697 xmlFreeNodeList(cur->children);
3698 if (((cur->type == XML_ELEMENT_NODE) ||
3699 (cur->type == XML_XINCLUDE_START) ||
3700 (cur->type == XML_XINCLUDE_END)) &&
3701 (cur->properties != NULL))
3702 xmlFreePropList(cur->properties);
3703 if ((cur->type != XML_ELEMENT_NODE) &&
3704 (cur->content != NULL) &&
3705 (cur->type != XML_ENTITY_REF_NODE) &&
3706 (cur->type != XML_XINCLUDE_END) &&
3707 (cur->type != XML_XINCLUDE_START) &&
3708 (cur->content != (xmlChar *) &(cur->properties))) {
3709 DICT_FREE(cur->content)
3713 * When a node is a text node or a comment, it uses a global static
3714 * variable for the name of the node.
3715 * Otherwise the node name might come from the document's dictionnary
3717 if ((cur->name != NULL) &&
3718 (cur->type != XML_TEXT_NODE) &&
3719 (cur->type != XML_COMMENT_NODE))
3720 DICT_FREE(cur->name)
3722 if (((cur->type == XML_ELEMENT_NODE) ||
3723 (cur->type == XML_XINCLUDE_START) ||
3724 (cur->type == XML_XINCLUDE_END)) &&
3725 (cur->nsDef != NULL))
3726 xmlFreeNsList(cur->nsDef);
3734 * Unlink a node from it's current context, the node is not freed
3737 xmlUnlinkNode(xmlNodePtr cur) {
3740 xmlGenericError(xmlGenericErrorContext,
3741 "xmlUnlinkNode : node == NULL\n");
3745 if (cur->type == XML_DTD_NODE) {
3749 if (doc->intSubset == (xmlDtdPtr) cur)
3750 doc->intSubset = NULL;
3751 if (doc->extSubset == (xmlDtdPtr) cur)
3752 doc->extSubset = NULL;
3755 if (cur->type == XML_ENTITY_DECL) {
3759 if (doc->intSubset != NULL) {
3760 if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur)
3761 xmlHashRemoveEntry(doc->intSubset->entities, cur->name,
3763 if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur)
3764 xmlHashRemoveEntry(doc->intSubset->pentities, cur->name,
3767 if (doc->extSubset != NULL) {
3768 if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur)
3769 xmlHashRemoveEntry(doc->extSubset->entities, cur->name,
3771 if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur)
3772 xmlHashRemoveEntry(doc->extSubset->pentities, cur->name,
3777 if (cur->parent != NULL) {
3779 parent = cur->parent;
3780 if (cur->type == XML_ATTRIBUTE_NODE) {
3781 if (parent->properties == (xmlAttrPtr) cur)
3782 parent->properties = ((xmlAttrPtr) cur)->next;
3784 if (parent->children == cur)
3785 parent->children = cur->next;
3786 if (parent->last == cur)
3787 parent->last = cur->prev;
3791 if (cur->next != NULL)
3792 cur->next->prev = cur->prev;
3793 if (cur->prev != NULL)
3794 cur->prev->next = cur->next;
3795 cur->next = cur->prev = NULL;
3798 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
3801 * @old: the old node
3804 * Unlink the old node from its current context, prune the new one
3805 * at the same place. If @cur was already inserted in a document it is
3806 * first unlinked from its existing context.
3808 * Returns the @old node
3811 xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
3812 if (old == cur) return(NULL);
3813 if ((old == NULL) || (old->parent == NULL)) {
3815 xmlGenericError(xmlGenericErrorContext,
3816 "xmlReplaceNode : old == NULL or without parent\n");
3827 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3829 xmlGenericError(xmlGenericErrorContext,
3830 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3834 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3836 xmlGenericError(xmlGenericErrorContext,
3837 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3842 xmlSetTreeDoc(cur, old->doc);
3843 cur->parent = old->parent;
3844 cur->next = old->next;
3845 if (cur->next != NULL)
3846 cur->next->prev = cur;
3847 cur->prev = old->prev;
3848 if (cur->prev != NULL)
3849 cur->prev->next = cur;
3850 if (cur->parent != NULL) {
3851 if (cur->type == XML_ATTRIBUTE_NODE) {
3852 if (cur->parent->properties == (xmlAttrPtr)old)
3853 cur->parent->properties = ((xmlAttrPtr) cur);
3855 if (cur->parent->children == old)
3856 cur->parent->children = cur;
3857 if (cur->parent->last == old)
3858 cur->parent->last = cur;
3861 old->next = old->prev = NULL;
3865 #endif /* LIBXML_TREE_ENABLED */
3867 /************************************************************************
3871 ************************************************************************/
3875 * @cur: the namespace
3877 * Do a copy of the namespace.
3879 * Returns: a new #xmlNsPtr, or NULL in case of error.
3882 xmlCopyNamespace(xmlNsPtr cur) {
3885 if (cur == NULL) return(NULL);
3886 switch (cur->type) {
3887 case XML_LOCAL_NAMESPACE:
3888 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3892 xmlGenericError(xmlGenericErrorContext,
3893 "xmlCopyNamespace: invalid type %d\n", cur->type);
3901 * xmlCopyNamespaceList:
3902 * @cur: the first namespace
3904 * Do a copy of an namespace list.
3906 * Returns: a new #xmlNsPtr, or NULL in case of error.
3909 xmlCopyNamespaceList(xmlNsPtr cur) {
3910 xmlNsPtr ret = NULL;
3911 xmlNsPtr p = NULL,q;
3913 while (cur != NULL) {
3914 q = xmlCopyNamespace(cur);
3927 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
3930 xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
3933 if (cur == NULL) return(NULL);
3935 ret = xmlNewDocProp(target->doc, cur->name, NULL);
3936 else if (doc != NULL)
3937 ret = xmlNewDocProp(doc, cur->name, NULL);
3938 else if (cur->parent != NULL)
3939 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3940 else if (cur->children != NULL)
3941 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3943 ret = xmlNewDocProp(NULL, cur->name, NULL);
3944 if (ret == NULL) return(NULL);
3945 ret->parent = target;
3947 if ((cur->ns != NULL) && (target != NULL)) {
3950 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3953 * Humm, we are copying an element whose namespace is defined
3954 * out of the new tree scope. Search it in the original tree
3955 * and add it at the top of the new tree
3957 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
3959 xmlNodePtr root = target;
3960 xmlNodePtr pred = NULL;
3962 while (root->parent != NULL) {
3964 root = root->parent;
3966 if (root == (xmlNodePtr) target->doc) {
3967 /* correct possibly cycling above the document elt */
3970 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3974 * we have to find something appropriate here since
3975 * we cant be sure, that the namespce we found is identified
3978 if (xmlStrEqual(ns->href, cur->ns->href)) {
3979 /* this is the nice case */
3983 * we are in trouble: we need a new reconcilied namespace.
3986 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
3993 if (cur->children != NULL) {
3996 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
3998 tmp = ret->children;
3999 while (tmp != NULL) {
4000 /* tmp->parent = (xmlNodePtr)ret; */
4001 if (tmp->next == NULL)
4009 if ((target!= NULL) && (cur!= NULL) &&
4010 (target->doc != NULL) && (cur->doc != NULL) &&
4011 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
4012 if (xmlIsID(cur->doc, cur->parent, cur)) {
4015 id = xmlNodeListGetString(cur->doc, cur->children, 1);
4017 xmlAddID(NULL, target->doc, id, ret);
4027 * @target: the element where the attribute will be grafted
4028 * @cur: the attribute
4030 * Do a copy of the attribute.
4032 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4035 xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
4036 return xmlCopyPropInternal(NULL, target, cur);
4041 * @target: the element where the attributes will be grafted
4042 * @cur: the first attribute
4044 * Do a copy of an attribute list.
4046 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4049 xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
4050 xmlAttrPtr ret = NULL;
4051 xmlAttrPtr p = NULL,q;
4053 while (cur != NULL) {
4054 q = xmlCopyProp(target, cur);
4070 * NOTE about the CopyNode operations !
4072 * They are split into external and internal parts for one
4073 * tricky reason: namespaces. Doing a direct copy of a node
4074 * say RPM:Copyright without changing the namespace pointer to
4075 * something else can produce stale links. One way to do it is
4076 * to keep a reference counter but this doesn't work as soon
4077 * as one move the element or the subtree out of the scope of
4078 * the existing namespace. The actual solution seems to add
4079 * a copy of the namespace at the top of the copied tree if
4080 * not available in the subtree.
4081 * Hence two functions, the public front-end call the inner ones
4082 * The argument "recursive" normally indicates a recursive copy
4083 * of the node with values 0 (no) and 1 (yes). For XInclude,
4084 * however, we allow a value of 2 to indicate copy properties and
4085 * namespace info, but don't recurse on children.
4089 xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
4093 if (node == NULL) return(NULL);
4094 switch (node->type) {
4096 case XML_CDATA_SECTION_NODE:
4097 case XML_ELEMENT_NODE:
4098 case XML_DOCUMENT_FRAG_NODE:
4099 case XML_ENTITY_REF_NODE:
4100 case XML_ENTITY_NODE:
4102 case XML_COMMENT_NODE:
4103 case XML_XINCLUDE_START:
4104 case XML_XINCLUDE_END:
4106 case XML_ATTRIBUTE_NODE:
4107 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
4108 case XML_NAMESPACE_DECL:
4109 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
4111 case XML_DOCUMENT_NODE:
4112 case XML_HTML_DOCUMENT_NODE:
4113 #ifdef LIBXML_DOCB_ENABLED
4114 case XML_DOCB_DOCUMENT_NODE:
4116 #ifdef LIBXML_TREE_ENABLED
4117 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
4118 #endif /* LIBXML_TREE_ENABLED */
4119 case XML_DOCUMENT_TYPE_NODE:
4120 case XML_NOTATION_NODE:
4122 case XML_ELEMENT_DECL:
4123 case XML_ATTRIBUTE_DECL:
4124 case XML_ENTITY_DECL:
4129 * Allocate a new node and fill the fields.
4131 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4133 xmlTreeErrMemory("copying node");
4136 memset(ret, 0, sizeof(xmlNode));
4137 ret->type = node->type;
4140 ret->parent = parent;
4141 if (node->name == xmlStringText)
4142 ret->name = xmlStringText;
4143 else if (node->name == xmlStringTextNoenc)
4144 ret->name = xmlStringTextNoenc;
4145 else if (node->name == xmlStringComment)
4146 ret->name = xmlStringComment;
4147 else if (node->name != NULL) {
4148 if ((doc != NULL) && (doc->dict != NULL))
4149 ret->name = xmlDictLookup(doc->dict, node->name, -1);
4151 ret->name = xmlStrdup(node->name);
4153 if ((node->type != XML_ELEMENT_NODE) &&
4154 (node->content != NULL) &&
4155 (node->type != XML_ENTITY_REF_NODE) &&
4156 (node->type != XML_XINCLUDE_END) &&
4157 (node->type != XML_XINCLUDE_START)) {
4158 ret->content = xmlStrdup(node->content);
4160 if (node->type == XML_ELEMENT_NODE)
4161 ret->line = node->line;
4163 if (parent != NULL) {
4167 * this is a tricky part for the node register thing:
4168 * in case ret does get coalesced in xmlAddChild
4169 * the deregister-node callback is called; so we register ret now already
4171 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
4172 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4174 tmp = xmlAddChild(parent, ret);
4175 /* node could have coalesced */
4182 if (((node->type == XML_ELEMENT_NODE) ||
4183 (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
4184 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
4186 if (node->ns != NULL) {
4189 ns = xmlSearchNs(doc, ret, node->ns->prefix);
4192 * Humm, we are copying an element whose namespace is defined
4193 * out of the new tree scope. Search it in the original tree
4194 * and add it at the top of the new tree
4196 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
4198 xmlNodePtr root = ret;
4200 while (root->parent != NULL) root = root->parent;
4201 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4203 ret->ns = xmlNewReconciliedNs(doc, ret, node->ns);
4207 * reference the existing namespace definition in our own tree.
4212 if (((node->type == XML_ELEMENT_NODE) ||
4213 (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
4214 ret->properties = xmlCopyPropList(ret, node->properties);
4215 if (node->type == XML_ENTITY_REF_NODE) {
4216 if ((doc == NULL) || (node->doc != doc)) {
4218 * The copied node will go into a separate document, so
4219 * to avoid dangling references to the ENTITY_DECL node
4220 * we cannot keep the reference. Try to find it in the
4223 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
4225 ret->children = node->children;
4227 ret->last = ret->children;
4228 } else if ((node->children != NULL) && (extended != 2)) {
4229 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
4230 UPDATE_LAST_CHILD_AND_PARENT(ret)
4234 /* if parent != NULL we already registered the node above */
4235 if ((parent == NULL) &&
4236 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
4237 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4242 xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
4243 xmlNodePtr ret = NULL;
4244 xmlNodePtr p = NULL,q;
4246 while (node != NULL) {
4247 #ifdef LIBXML_TREE_ENABLED
4248 if (node->type == XML_DTD_NODE ) {
4253 if (doc->intSubset == NULL) {
4254 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
4257 doc->intSubset = (xmlDtdPtr) q;
4258 xmlAddChild(parent, q);
4260 q = (xmlNodePtr) doc->intSubset;
4261 xmlAddChild(parent, q);
4264 #endif /* LIBXML_TREE_ENABLED */
4265 q = xmlStaticCopyNode(node, doc, parent, 1);
4269 } else if (p != q) {
4270 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
4283 * @extended: if 1 do a recursive copy (properties, namespaces and children
4285 * if 2 copy properties and namespaces (when applicable)
4287 * Do a copy of the node.
4289 * Returns: a new #xmlNodePtr, or NULL in case of error.
4292 xmlCopyNode(const xmlNodePtr node, int extended) {
4295 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
4302 * @doc: the document
4303 * @extended: if 1 do a recursive copy (properties, namespaces and children
4305 * if 2 copy properties and namespaces (when applicable)
4307 * Do a copy of the node to a given document.
4309 * Returns: a new #xmlNodePtr, or NULL in case of error.
4312 xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) {
4315 ret = xmlStaticCopyNode(node, doc, NULL, extended);
4320 * xmlDocCopyNodeList:
4321 * @doc: the target document
4322 * @node: the first node in the list.
4324 * Do a recursive copy of the node list.
4326 * Returns: a new #xmlNodePtr, or NULL in case of error.
4328 xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) {
4329 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4335 * @node: the first node in the list.
4337 * Do a recursive copy of the node list.
4338 * Use xmlDocCopyNodeList() if possible to ensure string interning.
4340 * Returns: a new #xmlNodePtr, or NULL in case of error.
4342 xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
4343 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4347 #if defined(LIBXML_TREE_ENABLED)
4352 * Do a copy of the dtd.
4354 * Returns: a new #xmlDtdPtr, or NULL in case of error.
4357 xmlCopyDtd(xmlDtdPtr dtd) {
4359 xmlNodePtr cur, p = NULL, q;
4361 if (dtd == NULL) return(NULL);
4362 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4363 if (ret == NULL) return(NULL);
4364 if (dtd->entities != NULL)
4365 ret->entities = (void *) xmlCopyEntitiesTable(
4366 (xmlEntitiesTablePtr) dtd->entities);
4367 if (dtd->notations != NULL)
4368 ret->notations = (void *) xmlCopyNotationTable(
4369 (xmlNotationTablePtr) dtd->notations);
4370 if (dtd->elements != NULL)
4371 ret->elements = (void *) xmlCopyElementTable(
4372 (xmlElementTablePtr) dtd->elements);
4373 if (dtd->attributes != NULL)
4374 ret->attributes = (void *) xmlCopyAttributeTable(
4375 (xmlAttributeTablePtr) dtd->attributes);
4376 if (dtd->pentities != NULL)
4377 ret->pentities = (void *) xmlCopyEntitiesTable(
4378 (xmlEntitiesTablePtr) dtd->pentities);
4380 cur = dtd->children;
4381 while (cur != NULL) {
4384 if (cur->type == XML_ENTITY_DECL) {
4385 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4386 switch (tmp->etype) {
4387 case XML_INTERNAL_GENERAL_ENTITY:
4388 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4389 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4390 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4392 case XML_INTERNAL_PARAMETER_ENTITY:
4393 case XML_EXTERNAL_PARAMETER_ENTITY:
4395 xmlGetParameterEntityFromDtd(ret, tmp->name);
4397 case XML_INTERNAL_PREDEFINED_ENTITY:
4400 } else if (cur->type == XML_ELEMENT_DECL) {
4401 xmlElementPtr tmp = (xmlElementPtr) cur;
4403 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4404 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4405 xmlAttributePtr tmp = (xmlAttributePtr) cur;
4407 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4408 } else if (cur->type == XML_COMMENT_NODE) {
4409 q = xmlCopyNode(cur, 0);
4423 q->parent = (xmlNodePtr) ret;
4434 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
4437 * @doc: the document
4438 * @recursive: if not zero do a recursive copy.
4440 * Do a copy of the document info. If recursive, the content tree will
4441 * be copied too as well as DTD, namespaces and entities.
4443 * Returns: a new #xmlDocPtr, or NULL in case of error.
4446 xmlCopyDoc(xmlDocPtr doc, int recursive) {
4449 if (doc == NULL) return(NULL);
4450 ret = xmlNewDoc(doc->version);
4451 if (ret == NULL) return(NULL);
4452 if (doc->name != NULL)
4453 ret->name = xmlMemStrdup(doc->name);
4454 if (doc->encoding != NULL)
4455 ret->encoding = xmlStrdup(doc->encoding);
4456 if (doc->URL != NULL)
4457 ret->URL = xmlStrdup(doc->URL);
4458 ret->charset = doc->charset;
4459 ret->compression = doc->compression;
4460 ret->standalone = doc->standalone;
4461 if (!recursive) return(ret);
4464 ret->children = NULL;
4465 #ifdef LIBXML_TREE_ENABLED
4466 if (doc->intSubset != NULL) {
4467 ret->intSubset = xmlCopyDtd(doc->intSubset);
4468 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
4469 ret->intSubset->parent = ret;
4472 if (doc->oldNs != NULL)
4473 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4474 if (doc->children != NULL) {
4477 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4480 tmp = ret->children;
4481 while (tmp != NULL) {
4482 if (tmp->next == NULL)
4489 #endif /* LIBXML_TREE_ENABLED */
4491 /************************************************************************
4493 * Content access functions *
4495 ************************************************************************/
4501 * Get line number of @node. This requires activation of this option
4502 * before invoking the parser by calling xmlLineNumbersDefault(1)
4504 * Returns the line number if successful, -1 otherwise
4507 xmlGetLineNo(xmlNodePtr node)
4513 if ((node->type == XML_ELEMENT_NODE) ||
4514 (node->type == XML_TEXT_NODE) ||
4515 (node->type == XML_COMMENT_NODE) ||
4516 (node->type == XML_PI_NODE))
4517 result = (long) node->line;
4518 else if ((node->prev != NULL) &&
4519 ((node->prev->type == XML_ELEMENT_NODE) ||
4520 (node->prev->type == XML_TEXT_NODE) ||
4521 (node->prev->type == XML_COMMENT_NODE) ||
4522 (node->prev->type == XML_PI_NODE)))
4523 result = xmlGetLineNo(node->prev);
4524 else if ((node->parent != NULL) &&
4525 (node->parent->type == XML_ELEMENT_NODE))
4526 result = xmlGetLineNo(node->parent);
4531 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
4536 * Build a structure based Path for the given node
4538 * Returns the new path or NULL in case of error. The caller must free
4539 * the returned string
4542 xmlGetNodePath(xmlNodePtr node)
4544 xmlNodePtr cur, tmp, next;
4545 xmlChar *buffer = NULL, *temp;
4551 int occur = 0, generic;
4557 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4558 if (buffer == NULL) {
4559 xmlTreeErrMemory("getting node path");
4562 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
4564 xmlTreeErrMemory("getting node path");
4575 if ((cur->type == XML_DOCUMENT_NODE) ||
4576 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4577 if (buffer[0] == '/')
4581 } else if (cur->type == XML_ELEMENT_NODE) {
4584 name = (const char *) cur->name;
4586 if (cur->ns->prefix != NULL) {
4587 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4588 (char *)cur->ns->prefix, (char *)cur->name);
4589 nametemp[sizeof(nametemp) - 1] = 0;
4593 * We cannot express named elements in the default
4594 * namespace, so use "*".
4603 * Thumbler index computation
4604 * TODO: the ocurence test seems bogus for namespaced names
4607 while (tmp != NULL) {
4608 if ((tmp->type == XML_ELEMENT_NODE) &&
4610 (xmlStrEqual(cur->name, tmp->name) &&
4611 ((tmp->ns == cur->ns) ||
4612 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4613 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4619 while (tmp != NULL && occur == 0) {
4620 if ((tmp->type == XML_ELEMENT_NODE) &&
4622 (xmlStrEqual(cur->name, tmp->name) &&
4623 ((tmp->ns == cur->ns) ||
4624 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4625 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
4633 } else if (cur->type == XML_COMMENT_NODE) {
4639 * Thumbler index computation
4642 while (tmp != NULL) {
4643 if (tmp->type == XML_COMMENT_NODE)
4649 while (tmp != NULL && occur == 0) {
4650 if (tmp->type == XML_COMMENT_NODE)
4658 } else if ((cur->type == XML_TEXT_NODE) ||
4659 (cur->type == XML_CDATA_SECTION_NODE)) {
4665 * Thumbler index computation
4668 while (tmp != NULL) {
4669 if ((tmp->type == XML_TEXT_NODE) ||
4670 (tmp->type == XML_CDATA_SECTION_NODE))
4675 * Evaluate if this is the only text- or CDATA-section-node;
4676 * if yes, then we'll get "text()", otherwise "text()[1]".
4680 while (tmp != NULL) {
4681 if ((tmp->type == XML_TEXT_NODE) ||
4682 (tmp->type == XML_CDATA_SECTION_NODE))
4691 } else if (cur->type == XML_PI_NODE) {
4693 snprintf(nametemp, sizeof(nametemp) - 1,
4694 "processing-instruction('%s')", (char *)cur->name);
4695 nametemp[sizeof(nametemp) - 1] = 0;
4701 * Thumbler index computation
4704 while (tmp != NULL) {
4705 if ((tmp->type == XML_PI_NODE) &&
4706 (xmlStrEqual(cur->name, tmp->name)))
4712 while (tmp != NULL && occur == 0) {
4713 if ((tmp->type == XML_PI_NODE) &&
4714 (xmlStrEqual(cur->name, tmp->name)))
4723 } else if (cur->type == XML_ATTRIBUTE_NODE) {
4725 name = (const char *) (((xmlAttrPtr) cur)->name);
4727 if (cur->ns->prefix != NULL)
4728 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4729 (char *)cur->ns->prefix, (char *)cur->name);
4731 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4733 nametemp[sizeof(nametemp) - 1] = 0;
4736 next = ((xmlAttrPtr) cur)->parent;
4742 * Make sure there is enough room
4744 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4746 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4747 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4749 xmlTreeErrMemory("getting node path");
4755 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4757 xmlTreeErrMemory("getting node path");
4765 snprintf((char *) buf, buf_len, "%s%s%s",
4766 sep, name, (char *) buffer);
4768 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
4769 sep, name, occur, (char *) buffer);
4770 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
4772 } while (cur != NULL);
4776 #endif /* LIBXML_TREE_ENABLED */
4779 * xmlDocGetRootElement:
4780 * @doc: the document
4782 * Get the root element of the document (doc->children is a list
4783 * containing possibly comments, PIs, etc ...).
4785 * Returns the #xmlNodePtr for the root or NULL
4788 xmlDocGetRootElement(xmlDocPtr doc) {
4791 if (doc == NULL) return(NULL);
4792 ret = doc->children;
4793 while (ret != NULL) {
4794 if (ret->type == XML_ELEMENT_NODE)
4801 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
4803 * xmlDocSetRootElement:
4804 * @doc: the document
4805 * @root: the new document root element, if root is NULL no action is taken,
4806 * to remove a node from a document use xmlUnlinkNode(root) instead.
4808 * Set the root element of the document (doc->children is a list
4809 * containing possibly comments, PIs, etc ...).
4811 * Returns the old root element if any was found, NULL if root was NULL
4814 xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4815 xmlNodePtr old = NULL;
4817 if (doc == NULL) return(NULL);
4820 xmlUnlinkNode(root);
4821 xmlSetTreeDoc(root, doc);
4822 root->parent = (xmlNodePtr) doc;
4823 old = doc->children;
4824 while (old != NULL) {
4825 if (old->type == XML_ELEMENT_NODE)
4830 if (doc->children == NULL) {
4831 doc->children = root;
4834 xmlAddSibling(doc->children, root);
4837 xmlReplaceNode(old, root);
4843 #if defined(LIBXML_TREE_ENABLED)
4846 * @cur: the node being changed
4847 * @lang: the language description
4849 * Set the language of a node, i.e. the values of the xml:lang
4853 xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
4856 if (cur == NULL) return;
4859 case XML_CDATA_SECTION_NODE:
4860 case XML_COMMENT_NODE:
4861 case XML_DOCUMENT_NODE:
4862 case XML_DOCUMENT_TYPE_NODE:
4863 case XML_DOCUMENT_FRAG_NODE:
4864 case XML_NOTATION_NODE:
4865 case XML_HTML_DOCUMENT_NODE:
4867 case XML_ELEMENT_DECL:
4868 case XML_ATTRIBUTE_DECL:
4869 case XML_ENTITY_DECL:
4871 case XML_ENTITY_REF_NODE:
4872 case XML_ENTITY_NODE:
4873 case XML_NAMESPACE_DECL:
4874 #ifdef LIBXML_DOCB_ENABLED
4875 case XML_DOCB_DOCUMENT_NODE:
4877 case XML_XINCLUDE_START:
4878 case XML_XINCLUDE_END:
4880 case XML_ELEMENT_NODE:
4881 case XML_ATTRIBUTE_NODE:
4884 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4887 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
4889 #endif /* LIBXML_TREE_ENABLED */
4893 * @cur: the node being checked
4895 * Searches the language of a node, i.e. the values of the xml:lang
4896 * attribute or the one carried by the nearest ancestor.
4898 * Returns a pointer to the lang value, or NULL if not found
4899 * It's up to the caller to free the memory with xmlFree().
4902 xmlNodeGetLang(xmlNodePtr cur) {
4905 while (cur != NULL) {
4906 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
4915 #ifdef LIBXML_TREE_ENABLED
4917 * xmlNodeSetSpacePreserve:
4918 * @cur: the node being changed
4919 * @val: the xml:space value ("0": default, 1: "preserve")
4921 * Set (or reset) the space preserving behaviour of a node, i.e. the
4922 * value of the xml:space attribute.
4925 xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
4928 if (cur == NULL) return;
4931 case XML_CDATA_SECTION_NODE:
4932 case XML_COMMENT_NODE:
4933 case XML_DOCUMENT_NODE:
4934 case XML_DOCUMENT_TYPE_NODE:
4935 case XML_DOCUMENT_FRAG_NODE:
4936 case XML_NOTATION_NODE:
4937 case XML_HTML_DOCUMENT_NODE:
4939 case XML_ELEMENT_DECL:
4940 case XML_ATTRIBUTE_DECL:
4941 case XML_ENTITY_DECL:
4943 case XML_ENTITY_REF_NODE:
4944 case XML_ENTITY_NODE:
4945 case XML_NAMESPACE_DECL:
4946 case XML_XINCLUDE_START:
4947 case XML_XINCLUDE_END:
4948 #ifdef LIBXML_DOCB_ENABLED
4949 case XML_DOCB_DOCUMENT_NODE:
4952 case XML_ELEMENT_NODE:
4953 case XML_ATTRIBUTE_NODE:
4956 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4961 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
4964 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
4968 #endif /* LIBXML_TREE_ENABLED */
4971 * xmlNodeGetSpacePreserve:
4972 * @cur: the node being checked
4974 * Searches the space preserving behaviour of a node, i.e. the values
4975 * of the xml:space attribute or the one carried by the nearest
4978 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
4981 xmlNodeGetSpacePreserve(xmlNodePtr cur) {
4984 while (cur != NULL) {
4985 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
4986 if (space != NULL) {
4987 if (xmlStrEqual(space, BAD_CAST "preserve")) {
4991 if (xmlStrEqual(space, BAD_CAST "default")) {
5002 #ifdef LIBXML_TREE_ENABLED
5005 * @cur: the node being changed
5006 * @name: the new tag name
5008 * Set (or reset) the name of a node.
5011 xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
5015 if (cur == NULL) return;
5016 if (name == NULL) return;
5019 case XML_CDATA_SECTION_NODE:
5020 case XML_COMMENT_NODE:
5021 case XML_DOCUMENT_TYPE_NODE:
5022 case XML_DOCUMENT_FRAG_NODE:
5023 case XML_NOTATION_NODE:
5024 case XML_HTML_DOCUMENT_NODE:
5025 case XML_NAMESPACE_DECL:
5026 case XML_XINCLUDE_START:
5027 case XML_XINCLUDE_END:
5028 #ifdef LIBXML_DOCB_ENABLED
5029 case XML_DOCB_DOCUMENT_NODE:
5032 case XML_ELEMENT_NODE:
5033 case XML_ATTRIBUTE_NODE:
5035 case XML_ENTITY_REF_NODE:
5036 case XML_ENTITY_NODE:
5038 case XML_DOCUMENT_NODE:
5039 case XML_ELEMENT_DECL:
5040 case XML_ATTRIBUTE_DECL:
5041 case XML_ENTITY_DECL:
5050 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
5051 xmlFree((xmlChar *) cur->name);
5052 cur->name = xmlDictLookup(dict, name, -1);
5054 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
5055 cur->name = xmlStrdup(name);
5060 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
5063 * @cur: the node being changed
5064 * @uri: the new base URI
5066 * Set (or reset) the base URI of a node, i.e. the value of the
5067 * xml:base attribute.
5070 xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
5074 if (cur == NULL) return;
5077 case XML_CDATA_SECTION_NODE:
5078 case XML_COMMENT_NODE:
5079 case XML_DOCUMENT_TYPE_NODE:
5080 case XML_DOCUMENT_FRAG_NODE:
5081 case XML_NOTATION_NODE:
5083 case XML_ELEMENT_DECL:
5084 case XML_ATTRIBUTE_DECL:
5085 case XML_ENTITY_DECL:
5087 case XML_ENTITY_REF_NODE:
5088 case XML_ENTITY_NODE:
5089 case XML_NAMESPACE_DECL:
5090 case XML_XINCLUDE_START:
5091 case XML_XINCLUDE_END:
5093 case XML_ELEMENT_NODE:
5094 case XML_ATTRIBUTE_NODE:
5096 case XML_DOCUMENT_NODE:
5097 #ifdef LIBXML_DOCB_ENABLED
5098 case XML_DOCB_DOCUMENT_NODE:
5100 case XML_HTML_DOCUMENT_NODE: {
5101 xmlDocPtr doc = (xmlDocPtr) cur;
5103 if (doc->URL != NULL)
5104 xmlFree((xmlChar *) doc->URL);
5108 doc->URL = xmlPathToURI(uri);
5113 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5116 fixed = xmlPathToURI(uri);
5117 if (fixed != NULL) {
5118 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
5121 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
5124 #endif /* LIBXML_TREE_ENABLED */
5128 * @doc: the document the node pertains to
5129 * @cur: the node being checked
5131 * Searches for the BASE URL. The code should work on both XML
5132 * and HTML document even if base mechanisms are completely different.
5133 * It returns the base as defined in RFC 2396 sections
5134 * 5.1.1. Base URI within Document Content
5136 * 5.1.2. Base URI from the Encapsulating Entity
5137 * However it does not return the document base (5.1.3), use
5138 * doc->URL in this case
5140 * Returns a pointer to the base URL, or NULL if not found
5141 * It's up to the caller to free the memory with xmlFree().
5144 xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
5145 xmlChar *oldbase = NULL;
5146 xmlChar *base, *newbase;
5148 if ((cur == NULL) && (doc == NULL))
5150 if (doc == NULL) doc = cur->doc;
5151 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
5152 cur = doc->children;
5153 while ((cur != NULL) && (cur->name != NULL)) {
5154 if (cur->type != XML_ELEMENT_NODE) {
5158 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5159 cur = cur->children;
5162 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5163 cur = cur->children;
5166 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5167 return(xmlGetProp(cur, BAD_CAST "href"));
5173 while (cur != NULL) {
5174 if (cur->type == XML_ENTITY_DECL) {
5175 xmlEntityPtr ent = (xmlEntityPtr) cur;
5176 return(xmlStrdup(ent->URI));
5178 if (cur->type == XML_ELEMENT_NODE) {
5179 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
5181 if (oldbase != NULL) {
5182 newbase = xmlBuildURI(oldbase, base);
5183 if (newbase != NULL) {
5195 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
5196 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
5197 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
5203 if ((doc != NULL) && (doc->URL != NULL)) {
5204 if (oldbase == NULL)
5205 return(xmlStrdup(doc->URL));
5206 newbase = xmlBuildURI(oldbase, doc->URL);
5214 * xmlNodeBufGetContent:
5216 * @cur: the node being read
5218 * Read the value of a node @cur, this can be either the text carried
5219 * directly by this node if it's a TEXT node or the aggregate string
5220 * of the values carried by this node child's (TEXT and ENTITY_REF).
5221 * Entity references are substituted.
5222 * Fills up the buffer @buffer with this value
5224 * Returns 0 in case of success and -1 in case of error.
5227 xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
5229 if ((cur == NULL) || (buffer == NULL)) return(-1);
5230 switch (cur->type) {
5231 case XML_CDATA_SECTION_NODE:
5233 xmlBufferCat(buffer, cur->content);
5235 case XML_DOCUMENT_FRAG_NODE:
5236 case XML_ELEMENT_NODE:{
5237 xmlNodePtr tmp = cur;
5239 while (tmp != NULL) {
5240 switch (tmp->type) {
5241 case XML_CDATA_SECTION_NODE:
5243 if (tmp->content != NULL)
5244 xmlBufferCat(buffer, tmp->content);
5246 case XML_ENTITY_REF_NODE:
5247 xmlNodeBufGetContent(buffer, tmp);
5255 if (tmp->children != NULL) {
5256 if (tmp->children->type != XML_ENTITY_DECL) {
5257 tmp = tmp->children;
5264 if (tmp->next != NULL) {
5277 if (tmp->next != NULL) {
5281 } while (tmp != NULL);
5285 case XML_ATTRIBUTE_NODE:{
5286 xmlAttrPtr attr = (xmlAttrPtr) cur;
5287 xmlNodePtr tmp = attr->children;
5289 while (tmp != NULL) {
5290 if (tmp->type == XML_TEXT_NODE)
5291 xmlBufferCat(buffer, tmp->content);
5293 xmlNodeBufGetContent(buffer, tmp);
5298 case XML_COMMENT_NODE:
5300 xmlBufferCat(buffer, cur->content);
5302 case XML_ENTITY_REF_NODE:{
5306 /* lookup entity declaration */
5307 ent = xmlGetDocEntity(cur->doc, cur->name);
5311 /* an entity content can be any "well balanced chunk",
5312 * i.e. the result of the content [43] production:
5313 * http://www.w3.org/TR/REC-xml#NT-content
5314 * -> we iterate through child nodes and recursive call
5315 * xmlNodeGetContent() which handles all possible node types */
5316 tmp = ent->children;
5318 xmlNodeBufGetContent(buffer, tmp);
5323 case XML_ENTITY_NODE:
5324 case XML_DOCUMENT_TYPE_NODE:
5325 case XML_NOTATION_NODE:
5327 case XML_XINCLUDE_START:
5328 case XML_XINCLUDE_END:
5330 case XML_DOCUMENT_NODE:
5331 #ifdef LIBXML_DOCB_ENABLED
5332 case XML_DOCB_DOCUMENT_NODE:
5334 case XML_HTML_DOCUMENT_NODE:
5335 cur = cur->children;
5336 while (cur!= NULL) {
5337 if ((cur->type == XML_ELEMENT_NODE) ||
5338 (cur->type == XML_TEXT_NODE) ||
5339 (cur->type == XML_CDATA_SECTION_NODE)) {
5340 xmlNodeBufGetContent(buffer, cur);
5345 case XML_NAMESPACE_DECL:
5346 xmlBufferCat(buffer, ((xmlNsPtr) cur)->href);
5348 case XML_ELEMENT_DECL:
5349 case XML_ATTRIBUTE_DECL:
5350 case XML_ENTITY_DECL:
5356 * xmlNodeGetContent:
5357 * @cur: the node being read
5359 * Read the value of a node, this can be either the text carried
5360 * directly by this node if it's a TEXT node or the aggregate string
5361 * of the values carried by this node child's (TEXT and ENTITY_REF).
5362 * Entity references are substituted.
5363 * Returns a new #xmlChar * or NULL if no content is available.
5364 * It's up to the caller to free the memory with xmlFree().
5367 xmlNodeGetContent(xmlNodePtr cur)
5371 switch (cur->type) {
5372 case XML_DOCUMENT_FRAG_NODE:
5373 case XML_ELEMENT_NODE:{
5374 xmlBufferPtr buffer;
5377 buffer = xmlBufferCreateSize(64);
5380 xmlNodeBufGetContent(buffer, cur);
5381 ret = buffer->content;
5382 buffer->content = NULL;
5383 xmlBufferFree(buffer);
5386 case XML_ATTRIBUTE_NODE:
5387 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
5388 case XML_COMMENT_NODE:
5390 if (cur->content != NULL)
5391 return (xmlStrdup(cur->content));
5393 case XML_ENTITY_REF_NODE:{
5395 xmlBufferPtr buffer;
5398 /* lookup entity declaration */
5399 ent = xmlGetDocEntity(cur->doc, cur->name);
5403 buffer = xmlBufferCreate();
5407 xmlNodeBufGetContent(buffer, cur);
5409 ret = buffer->content;
5410 buffer->content = NULL;
5411 xmlBufferFree(buffer);
5414 case XML_ENTITY_NODE:
5415 case XML_DOCUMENT_TYPE_NODE:
5416 case XML_NOTATION_NODE:
5418 case XML_XINCLUDE_START:
5419 case XML_XINCLUDE_END:
5421 case XML_DOCUMENT_NODE:
5422 #ifdef LIBXML_DOCB_ENABLED
5423 case XML_DOCB_DOCUMENT_NODE:
5425 case XML_HTML_DOCUMENT_NODE: {
5426 xmlBufferPtr buffer;
5429 buffer = xmlBufferCreate();
5433 xmlNodeBufGetContent(buffer, (xmlNodePtr) cur);
5435 ret = buffer->content;
5436 buffer->content = NULL;
5437 xmlBufferFree(buffer);
5440 case XML_NAMESPACE_DECL: {
5443 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5446 case XML_ELEMENT_DECL:
5449 case XML_ATTRIBUTE_DECL:
5452 case XML_ENTITY_DECL:
5455 case XML_CDATA_SECTION_NODE:
5457 if (cur->content != NULL)
5458 return (xmlStrdup(cur->content));
5465 * xmlNodeSetContent:
5466 * @cur: the node being modified
5467 * @content: the new value of the content
5469 * Replace the content of a node.
5470 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5471 * references, but XML special chars need to be escaped first by using
5472 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5475 xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5478 xmlGenericError(xmlGenericErrorContext,
5479 "xmlNodeSetContent : node == NULL\n");
5483 switch (cur->type) {
5484 case XML_DOCUMENT_FRAG_NODE:
5485 case XML_ELEMENT_NODE:
5486 case XML_ATTRIBUTE_NODE:
5487 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5488 cur->children = xmlStringGetNodeList(cur->doc, content);
5489 UPDATE_LAST_CHILD_AND_PARENT(cur)
5492 case XML_CDATA_SECTION_NODE:
5493 case XML_ENTITY_REF_NODE:
5494 case XML_ENTITY_NODE:
5496 case XML_COMMENT_NODE:
5497 if ((cur->content != NULL) &&
5498 (cur->content != (xmlChar *) &(cur->properties))) {
5499 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5500 (xmlDictOwns(cur->doc->dict, cur->content))))
5501 xmlFree(cur->content);
5503 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5504 cur->last = cur->children = NULL;
5505 if (content != NULL) {
5506 cur->content = xmlStrdup(content);
5508 cur->content = NULL;
5509 cur->properties = NULL;
5512 case XML_DOCUMENT_NODE:
5513 case XML_HTML_DOCUMENT_NODE:
5514 case XML_DOCUMENT_TYPE_NODE:
5515 case XML_XINCLUDE_START:
5516 case XML_XINCLUDE_END:
5517 #ifdef LIBXML_DOCB_ENABLED
5518 case XML_DOCB_DOCUMENT_NODE:
5521 case XML_NOTATION_NODE:
5525 case XML_NAMESPACE_DECL:
5527 case XML_ELEMENT_DECL:
5530 case XML_ATTRIBUTE_DECL:
5533 case XML_ENTITY_DECL:
5539 #ifdef LIBXML_TREE_ENABLED
5541 * xmlNodeSetContentLen:
5542 * @cur: the node being modified
5543 * @content: the new value of the content
5544 * @len: the size of @content
5546 * Replace the content of a node.
5547 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5548 * references, but XML special chars need to be escaped first by using
5549 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5552 xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5555 xmlGenericError(xmlGenericErrorContext,
5556 "xmlNodeSetContentLen : node == NULL\n");
5560 switch (cur->type) {
5561 case XML_DOCUMENT_FRAG_NODE:
5562 case XML_ELEMENT_NODE:
5563 case XML_ATTRIBUTE_NODE:
5564 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5565 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5566 UPDATE_LAST_CHILD_AND_PARENT(cur)
5569 case XML_CDATA_SECTION_NODE:
5570 case XML_ENTITY_REF_NODE:
5571 case XML_ENTITY_NODE:
5573 case XML_COMMENT_NODE:
5574 case XML_NOTATION_NODE:
5575 if ((cur->content != NULL) &&
5576 (cur->content != (xmlChar *) &(cur->properties))) {
5577 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5578 (xmlDictOwns(cur->doc->dict, cur->content))))
5579 xmlFree(cur->content);
5581 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5582 cur->children = cur->last = NULL;
5583 if (content != NULL) {
5584 cur->content = xmlStrndup(content, len);
5586 cur->content = NULL;
5587 cur->properties = NULL;
5590 case XML_DOCUMENT_NODE:
5592 case XML_HTML_DOCUMENT_NODE:
5593 case XML_DOCUMENT_TYPE_NODE:
5594 case XML_NAMESPACE_DECL:
5595 case XML_XINCLUDE_START:
5596 case XML_XINCLUDE_END:
5597 #ifdef LIBXML_DOCB_ENABLED
5598 case XML_DOCB_DOCUMENT_NODE:
5601 case XML_ELEMENT_DECL:
5604 case XML_ATTRIBUTE_DECL:
5607 case XML_ENTITY_DECL:
5612 #endif /* LIBXML_TREE_ENABLED */
5615 * xmlNodeAddContentLen:
5616 * @cur: the node being modified
5617 * @content: extra content
5618 * @len: the size of @content
5620 * Append the extra substring to the node content.
5621 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5622 * raw text, so unescaped XML special chars are allowed, entity
5623 * references are not supported.
5626 xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5629 xmlGenericError(xmlGenericErrorContext,
5630 "xmlNodeAddContentLen : node == NULL\n");
5634 if (len <= 0) return;
5635 switch (cur->type) {
5636 case XML_DOCUMENT_FRAG_NODE:
5637 case XML_ELEMENT_NODE: {
5638 xmlNodePtr last, newNode, tmp;
5641 newNode = xmlNewTextLen(content, len);
5642 if (newNode != NULL) {
5643 tmp = xmlAddChild(cur, newNode);
5646 if ((last != NULL) && (last->next == newNode)) {
5647 xmlTextMerge(last, newNode);
5652 case XML_ATTRIBUTE_NODE:
5655 case XML_CDATA_SECTION_NODE:
5656 case XML_ENTITY_REF_NODE:
5657 case XML_ENTITY_NODE:
5659 case XML_COMMENT_NODE:
5660 case XML_NOTATION_NODE:
5661 if (content != NULL) {
5662 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5663 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5664 xmlDictOwns(cur->doc->dict, cur->content))) {
5665 cur->content = xmlStrncatNew(cur->content, content, len);
5666 cur->properties = NULL;
5670 cur->content = xmlStrncat(cur->content, content, len);
5672 case XML_DOCUMENT_NODE:
5674 case XML_HTML_DOCUMENT_NODE:
5675 case XML_DOCUMENT_TYPE_NODE:
5676 case XML_NAMESPACE_DECL:
5677 case XML_XINCLUDE_START:
5678 case XML_XINCLUDE_END:
5679 #ifdef LIBXML_DOCB_ENABLED
5680 case XML_DOCB_DOCUMENT_NODE:
5683 case XML_ELEMENT_DECL:
5684 case XML_ATTRIBUTE_DECL:
5685 case XML_ENTITY_DECL:
5691 * xmlNodeAddContent:
5692 * @cur: the node being modified
5693 * @content: extra content
5695 * Append the extra substring to the node content.
5696 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5697 * raw text, so unescaped XML special chars are allowed, entity
5698 * references are not supported.
5701 xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5706 xmlGenericError(xmlGenericErrorContext,
5707 "xmlNodeAddContent : node == NULL\n");
5711 if (content == NULL) return;
5712 len = xmlStrlen(content);
5713 xmlNodeAddContentLen(cur, content, len);
5718 * @first: the first text node
5719 * @second: the second text node being merged
5721 * Merge two text nodes into one
5722 * Returns the first text node augmented
5725 xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5726 if (first == NULL) return(second);
5727 if (second == NULL) return(first);
5728 if (first->type != XML_TEXT_NODE) return(first);
5729 if (second->type != XML_TEXT_NODE) return(first);
5730 if (second->name != first->name)
5732 xmlNodeAddContent(first, second->content);
5733 xmlUnlinkNode(second);
5734 xmlFreeNode(second);
5738 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
5741 * @doc: the document
5742 * @node: the current node
5744 * Search all the namespace applying to a given element.
5745 * Returns an NULL terminated array of all the #xmlNsPtr found
5746 * that need to be freed by the caller or NULL if no
5747 * namespace if defined
5750 xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5753 xmlNsPtr *ret = NULL;
5758 while (node != NULL) {
5759 if (node->type == XML_ELEMENT_NODE) {
5761 while (cur != NULL) {
5764 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5767 xmlTreeErrMemory("getting namespace list");
5772 for (i = 0; i < nbns; i++) {
5773 if ((cur->prefix == ret[i]->prefix) ||
5774 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5778 if (nbns >= maxns) {
5780 ret = (xmlNsPtr *) xmlRealloc(ret,
5785 xmlTreeErrMemory("getting namespace list");
5796 node = node->parent;
5800 #endif /* LIBXML_TREE_ENABLED */
5803 * xmlTreeEnsureXMLDecl:
5806 * Ensures that there is an XML namespace declaration on the doc.
5808 * Returns the XML ns-struct or NULL on API and internal errors.
5811 xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5815 if (doc->oldNs != NULL)
5816 return (doc->oldNs);
5819 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5822 "allocating the XML namespace");
5825 memset(ns, 0, sizeof(xmlNs));
5826 ns->type = XML_LOCAL_NAMESPACE;
5827 ns->href = xmlStrdup(XML_XML_NAMESPACE);
5828 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5836 * @doc: the document
5837 * @node: the current node
5838 * @nameSpace: the namespace prefix
5840 * Search a Ns registered under a given name space for a document.
5841 * recurse on the parents until it finds the defined namespace
5842 * or return NULL otherwise.
5843 * @nameSpace can be NULL, this is a search for the default namespace.
5844 * We don't allow to cross entities boundaries. If you don't declare
5845 * the namespace within those you will be in troubles !!! A warning
5846 * is generated to cover this case.
5848 * Returns the namespace pointer or NULL.
5851 xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
5854 xmlNodePtr orig = node;
5856 if (node == NULL) return(NULL);
5857 if ((nameSpace != NULL) &&
5858 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
5859 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5861 * The XML-1.0 namespace is normally held on the root
5862 * element. In this case exceptionally create it on the
5865 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5867 xmlTreeErrMemory("searching namespace");
5870 memset(cur, 0, sizeof(xmlNs));
5871 cur->type = XML_LOCAL_NAMESPACE;
5872 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5873 cur->prefix = xmlStrdup((const xmlChar *)"xml");
5874 cur->next = node->nsDef;
5884 * Return the XML namespace declaration held by the doc.
5886 if (doc->oldNs == NULL)
5887 return(xmlTreeEnsureXMLDecl(doc));
5891 while (node != NULL) {
5892 if ((node->type == XML_ENTITY_REF_NODE) ||
5893 (node->type == XML_ENTITY_NODE) ||
5894 (node->type == XML_ENTITY_DECL))
5896 if (node->type == XML_ELEMENT_NODE) {
5898 while (cur != NULL) {
5899 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5900 (cur->href != NULL))
5902 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5903 (cur->href != NULL) &&
5904 (xmlStrEqual(cur->prefix, nameSpace)))
5911 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5912 (cur->href != NULL))
5914 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5915 (cur->href != NULL) &&
5916 (xmlStrEqual(cur->prefix, nameSpace)))
5921 node = node->parent;
5928 * @doc: the document
5929 * @node: the current node
5930 * @ancestor: the ancestor carrying the namespace
5931 * @prefix: the namespace prefix
5933 * Verify that the given namespace held on @ancestor is still in scope
5936 * Returns 1 if true, 0 if false and -1 in case of error.
5939 xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5940 xmlNodePtr ancestor, const xmlChar * prefix)
5944 while ((node != NULL) && (node != ancestor)) {
5945 if ((node->type == XML_ENTITY_REF_NODE) ||
5946 (node->type == XML_ENTITY_NODE) ||
5947 (node->type == XML_ENTITY_DECL))
5949 if (node->type == XML_ELEMENT_NODE) {
5951 while (tst != NULL) {
5952 if ((tst->prefix == NULL)
5953 && (prefix == NULL))
5955 if ((tst->prefix != NULL)
5957 && (xmlStrEqual(tst->prefix, prefix)))
5962 node = node->parent;
5964 if (node != ancestor)
5970 * xmlSearchNsByHref:
5971 * @doc: the document
5972 * @node: the current node
5973 * @href: the namespace value
5975 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5976 * the defined namespace or return NULL otherwise.
5977 * Returns the namespace pointer or NULL.
5980 xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
5983 xmlNodePtr orig = node;
5986 if ((node == NULL) || (href == NULL))
5988 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
5990 * Only the document can hold the XML spec namespace.
5992 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5994 * The XML-1.0 namespace is normally held on the root
5995 * element. In this case exceptionally create it on the
5998 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6000 xmlTreeErrMemory("searching namespace");
6003 memset(cur, 0, sizeof(xmlNs));
6004 cur->type = XML_LOCAL_NAMESPACE;
6005 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6006 cur->prefix = xmlStrdup((const xmlChar *) "xml");
6007 cur->next = node->nsDef;
6017 * Return the XML namespace declaration held by the doc.
6019 if (doc->oldNs == NULL)
6020 return(xmlTreeEnsureXMLDecl(doc));
6024 is_attr = (node->type == XML_ATTRIBUTE_NODE);
6025 while (node != NULL) {
6026 if ((node->type == XML_ENTITY_REF_NODE) ||
6027 (node->type == XML_ENTITY_NODE) ||
6028 (node->type == XML_ENTITY_DECL))
6030 if (node->type == XML_ELEMENT_NODE) {
6032 while (cur != NULL) {
6033 if ((cur->href != NULL) && (href != NULL) &&
6034 (xmlStrEqual(cur->href, href))) {
6035 if (((!is_attr) || (cur->prefix != NULL)) &&
6036 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6044 if ((cur->href != NULL) && (href != NULL) &&
6045 (xmlStrEqual(cur->href, href))) {
6046 if (((!is_attr) || (cur->prefix != NULL)) &&
6047 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
6053 node = node->parent;
6059 * xmlNewReconciliedNs:
6060 * @doc: the document
6061 * @tree: a node expected to hold the new namespace
6062 * @ns: the original namespace
6064 * This function tries to locate a namespace definition in a tree
6065 * ancestors, or create a new namespace definition node similar to
6066 * @ns trying to reuse the same prefix. However if the given prefix is
6067 * null (default namespace) or reused within the subtree defined by
6068 * @tree or on one of its ancestors then a new prefix is generated.
6069 * Returns the (new) namespace definition or NULL in case of error
6072 xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
6079 xmlGenericError(xmlGenericErrorContext,
6080 "xmlNewReconciliedNs : tree == NULL\n");
6084 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
6086 xmlGenericError(xmlGenericErrorContext,
6087 "xmlNewReconciliedNs : ns == NULL\n");
6092 * Search an existing namespace definition inherited.
6094 def = xmlSearchNsByHref(doc, tree, ns->href);
6099 * Find a close prefix which is not already in use.
6100 * Let's strip namespace prefixes longer than 20 chars !
6102 if (ns->prefix == NULL)
6103 snprintf((char *) prefix, sizeof(prefix), "default");
6105 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
6107 def = xmlSearchNs(doc, tree, prefix);
6108 while (def != NULL) {
6109 if (counter > 1000) return(NULL);
6110 if (ns->prefix == NULL)
6111 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
6113 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
6114 (char *)ns->prefix, counter++);
6115 def = xmlSearchNs(doc, tree, prefix);
6119 * OK, now we are ready to create a new one.
6121 def = xmlNewNs(tree, ns->href, prefix);
6125 #ifdef LIBXML_TREE_ENABLED
6127 * xmlReconciliateNs:
6128 * @doc: the document
6129 * @tree: a node defining the subtree to reconciliate
6131 * This function checks that all the namespaces declared within the given
6132 * tree are properly declared. This is needed for example after Copy or Cut
6133 * and then paste operations. The subtree may still hold pointers to
6134 * namespace declarations outside the subtree or invalid/masked. As much
6135 * as possible the function try to reuse the existing namespaces found in
6136 * the new environment. If not possible the new namespaces are redeclared
6137 * on @tree at the top of the given subtree.
6138 * Returns the number of namespace declarations created or -1 in case of error.
6141 xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
6142 xmlNsPtr *oldNs = NULL;
6143 xmlNsPtr *newNs = NULL;
6148 xmlNodePtr node = tree;
6152 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6153 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
6154 if (node->doc != doc) return(-1);
6155 while (node != NULL) {
6157 * Reconciliate the node namespace
6159 if (node->ns != NULL) {
6161 * initialize the cache if needed
6163 if (sizeCache == 0) {
6165 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6167 if (oldNs == NULL) {
6168 xmlTreeErrMemory("fixing namespaces");
6171 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6173 if (newNs == NULL) {
6174 xmlTreeErrMemory("fixing namespaces");
6179 for (i = 0;i < nbCache;i++) {
6180 if (oldNs[i] == node->ns) {
6181 node->ns = newNs[i];
6187 * OK we need to recreate a new namespace definition
6189 n = xmlNewReconciliedNs(doc, tree, node->ns);
6190 if (n != NULL) { /* :-( what if else ??? */
6192 * check if we need to grow the cache buffers.
6194 if (sizeCache <= nbCache) {
6196 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
6198 if (oldNs == NULL) {
6199 xmlTreeErrMemory("fixing namespaces");
6203 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
6205 if (newNs == NULL) {
6206 xmlTreeErrMemory("fixing namespaces");
6212 oldNs[nbCache++] = node->ns;
6218 * now check for namespace hold by attributes on the node.
6220 if (node->type == XML_ELEMENT_NODE) {
6221 attr = node->properties;
6222 while (attr != NULL) {
6223 if (attr->ns != NULL) {
6225 * initialize the cache if needed
6227 if (sizeCache == 0) {
6229 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6231 if (oldNs == NULL) {
6232 xmlTreeErrMemory("fixing namespaces");
6235 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6237 if (newNs == NULL) {
6238 xmlTreeErrMemory("fixing namespaces");
6243 for (i = 0;i < nbCache;i++) {
6244 if (oldNs[i] == attr->ns) {
6245 attr->ns = newNs[i];
6251 * OK we need to recreate a new namespace definition
6253 n = xmlNewReconciliedNs(doc, tree, attr->ns);
6254 if (n != NULL) { /* :-( what if else ??? */
6256 * check if we need to grow the cache buffers.
6258 if (sizeCache <= nbCache) {
6260 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
6261 sizeCache * sizeof(xmlNsPtr));
6262 if (oldNs == NULL) {
6263 xmlTreeErrMemory("fixing namespaces");
6267 newNs = (xmlNsPtr *) xmlRealloc(newNs,
6268 sizeCache * sizeof(xmlNsPtr));
6269 if (newNs == NULL) {
6270 xmlTreeErrMemory("fixing namespaces");
6276 oldNs[nbCache++] = attr->ns;
6286 * Browse the full subtree, deep first
6288 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
6290 node = node->children;
6291 } else if ((node != tree) && (node->next != NULL)) {
6294 } else if (node != tree) {
6295 /* go up to parents->next if needed */
6296 while (node != tree) {
6297 if (node->parent != NULL)
6298 node = node->parent;
6299 if ((node != tree) && (node->next != NULL)) {
6303 if (node->parent == NULL) {
6308 /* exit condition */
6320 #endif /* LIBXML_TREE_ENABLED */
6323 xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name,
6324 const xmlChar *nsName, int useDTD)
6328 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6331 if (node->properties != NULL) {
6332 prop = node->properties;
6333 if (nsName == NULL) {
6335 * We want the attr to be in no namespace.
6338 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6342 } while (prop != NULL);
6345 * We want the attr to be in the specified namespace.
6348 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6349 ((prop->ns->href == nsName) ||
6350 xmlStrEqual(prop->ns->href, nsName)))
6355 } while (prop != NULL);
6359 #ifdef LIBXML_TREE_ENABLED
6363 * Check if there is a default/fixed attribute declaration in
6364 * the internal or external subset.
6366 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6367 xmlDocPtr doc = node->doc;
6368 xmlAttributePtr attrDecl = NULL;
6369 xmlChar *elemQName, *tmpstr = NULL;
6372 * We need the QName of the element for the DTD-lookup.
6374 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6375 tmpstr = xmlStrdup(node->ns->prefix);
6376 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6377 tmpstr = xmlStrcat(tmpstr, node->name);
6382 elemQName = (xmlChar *) node->name;
6383 if (nsName == NULL) {
6385 * The common and nice case: Attr in no namespace.
6387 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6388 elemQName, name, NULL);
6389 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6390 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6391 elemQName, name, NULL);
6394 xmlNsPtr *nsList, *cur;
6397 * The ugly case: Search using the prefixes of in-scope
6398 * ns-decls corresponding to @nsName.
6400 nsList = xmlGetNsList(node->doc, node);
6401 if (nsList == NULL) {
6407 while (*cur != NULL) {
6408 if (xmlStrEqual((*cur)->href, nsName)) {
6409 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6410 name, (*cur)->prefix);
6413 if (doc->extSubset != NULL) {
6414 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6415 name, (*cur)->prefix);
6427 * Only default/fixed attrs are relevant.
6429 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6430 return((xmlAttrPtr) attrDecl);
6432 #endif /* LIBXML_TREE_ENABLED */
6437 xmlGetPropNodeValueInternal(xmlAttrPtr prop)
6441 if (prop->type == XML_ATTRIBUTE_NODE) {
6443 * Note that we return at least the empty string.
6444 * TODO: Do we really always want that?
6446 if (prop->children != NULL) {
6447 if ((prop->children->next == NULL) &&
6448 ((prop->children->type == XML_TEXT_NODE) ||
6449 (prop->children->type == XML_CDATA_SECTION_NODE)))
6452 * Optimization for the common case: only 1 text node.
6454 return(xmlStrdup(prop->children->content));
6458 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6463 return(xmlStrdup((xmlChar *)""));
6464 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6465 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6473 * @name: the attribute name
6475 * Search an attribute associated to a node
6476 * This function also looks in DTD attribute declaration for #FIXED or
6477 * default declaration values unless DTD use has been turned off.
6479 * Returns the attribute or the attribute declaration or NULL if
6480 * neither was found.
6483 xmlHasProp(xmlNodePtr node, const xmlChar *name) {
6487 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6490 * Check on the properties attached to the node
6492 prop = node->properties;
6493 while (prop != NULL) {
6494 if (xmlStrEqual(prop->name, name)) {
6499 if (!xmlCheckDTD) return(NULL);
6502 * Check if there is a default declaration in the internal
6503 * or external subsets
6507 xmlAttributePtr attrDecl;
6508 if (doc->intSubset != NULL) {
6509 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6510 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6511 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6512 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6513 /* return attribute declaration only if a default value is given
6514 (that includes #FIXED declarations) */
6515 return((xmlAttrPtr) attrDecl);
6524 * @name: the attribute name
6525 * @nameSpace: the URI of the namespace
6527 * Search for an attribute associated to a node
6528 * This attribute has to be anchored in the namespace specified.
6529 * This does the entity substitution.
6530 * This function looks in DTD attribute declaration for #FIXED or
6531 * default declaration values unless DTD use has been turned off.
6532 * Note that a namespace of NULL indicates to use the default namespace.
6534 * Returns the attribute or the attribute declaration or NULL
6535 * if neither was found.
6538 xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
6540 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
6546 * @name: the attribute name
6548 * Search and get the value of an attribute associated to a node
6549 * This does the entity substitution.
6550 * This function looks in DTD attribute declaration for #FIXED or
6551 * default declaration values unless DTD use has been turned off.
6552 * NOTE: this function acts independently of namespaces associated
6553 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6554 * for namespace aware processing.
6556 * Returns the attribute value or NULL if not found.
6557 * It's up to the caller to free the memory with xmlFree().
6560 xmlGetProp(xmlNodePtr node, const xmlChar *name) {
6563 prop = xmlHasProp(node, name);
6566 return(xmlGetPropNodeValueInternal(prop));
6572 * @name: the attribute name
6574 * Search and get the value of an attribute associated to a node
6575 * This does the entity substitution.
6576 * This function looks in DTD attribute declaration for #FIXED or
6577 * default declaration values unless DTD use has been turned off.
6578 * This function is similar to xmlGetProp except it will accept only
6579 * an attribute in no namespace.
6581 * Returns the attribute value or NULL if not found.
6582 * It's up to the caller to free the memory with xmlFree().
6585 xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
6588 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6591 return(xmlGetPropNodeValueInternal(prop));
6597 * @name: the attribute name
6598 * @nameSpace: the URI of the namespace
6600 * Search and get the value of an attribute associated to a node
6601 * This attribute has to be anchored in the namespace specified.
6602 * This does the entity substitution.
6603 * This function looks in DTD attribute declaration for #FIXED or
6604 * default declaration values unless DTD use has been turned off.
6606 * Returns the attribute value or NULL if not found.
6607 * It's up to the caller to free the memory with xmlFree().
6610 xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
6613 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6616 return(xmlGetPropNodeValueInternal(prop));
6619 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6623 * @name: the attribute name
6625 * Remove an attribute carried by a node.
6626 * This handles only attributes in no namespace.
6627 * Returns 0 if successful, -1 if not found
6630 xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
6633 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6636 xmlUnlinkNode((xmlNodePtr) prop);
6644 * @ns: the namespace definition
6645 * @name: the attribute name
6647 * Remove an attribute carried by a node.
6648 * Returns 0 if successful, -1 if not found
6651 xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
6654 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6657 xmlUnlinkNode((xmlNodePtr) prop);
6663 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
6667 * @name: the attribute name (a QName)
6668 * @value: the attribute value
6670 * Set (or reset) an attribute carried by a node.
6671 * If @name has a prefix, then the corresponding
6672 * namespace-binding will be used, if in scope; it is an
6673 * error it there's no such ns-binding for the prefix in
6675 * Returns the attribute pointer.
6679 xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
6681 const xmlChar *nqname;
6683 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
6689 nqname = xmlSplitQName3(name, &len);
6690 if (nqname != NULL) {
6692 xmlChar *prefix = xmlStrndup(name, len);
6693 ns = xmlSearchNs(node->doc, node, prefix);
6697 return(xmlSetNsProp(node, ns, nqname, value));
6699 return(xmlSetNsProp(node, NULL, name, value));
6705 * @ns: the namespace definition
6706 * @name: the attribute name
6707 * @value: the attribute value
6709 * Set (or reset) an attribute carried by a node.
6710 * The ns structure must be in scope, this is not checked
6712 * Returns the attribute pointer.
6715 xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6716 const xmlChar *value)
6720 if (ns && (ns->href == NULL))
6722 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6725 * Modify the attribute's value.
6727 if (prop->atype == XML_ATTRIBUTE_ID) {
6728 xmlRemoveID(node->doc, prop);
6729 prop->atype = XML_ATTRIBUTE_ID;
6731 if (prop->children != NULL)
6732 xmlFreeNodeList(prop->children);
6733 prop->children = NULL;
6736 if (value != NULL) {
6739 if(!xmlCheckUTF8(value)) {
6740 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc,
6742 if (node->doc != NULL)
6743 node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
6745 prop->children = xmlNewDocText(node->doc, value);
6747 tmp = prop->children;
6748 while (tmp != NULL) {
6749 tmp->parent = (xmlNodePtr) prop;
6750 if (tmp->next == NULL)
6755 if (prop->atype == XML_ATTRIBUTE_ID)
6756 xmlAddID(NULL, node->doc, value, prop);
6760 * No equal attr found; create a new one.
6762 return(xmlNewPropInternal(node, ns, name, value, 0));
6765 #endif /* LIBXML_TREE_ENABLED */
6771 * Is this node a Text node ?
6772 * Returns 1 yes, 0 no
6775 xmlNodeIsText(xmlNodePtr node) {
6776 if (node == NULL) return(0);
6778 if (node->type == XML_TEXT_NODE) return(1);
6786 * Checks whether this node is an empty or whitespace only
6787 * (and possibly ignorable) text-node.
6789 * Returns 1 yes, 0 no
6792 xmlIsBlankNode(xmlNodePtr node) {
6794 if (node == NULL) return(0);
6796 if ((node->type != XML_TEXT_NODE) &&
6797 (node->type != XML_CDATA_SECTION_NODE))
6799 if (node->content == NULL) return(1);
6800 cur = node->content;
6802 if (!IS_BLANK_CH(*cur)) return(0);
6812 * @content: the content
6813 * @len: @content length
6815 * Concat the given string at the end of the existing node content
6817 * Returns -1 in case of error, 0 otherwise
6821 xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
6822 if (node == NULL) return(-1);
6824 if ((node->type != XML_TEXT_NODE) &&
6825 (node->type != XML_CDATA_SECTION_NODE) &&
6826 (node->type != XML_COMMENT_NODE) &&
6827 (node->type != XML_PI_NODE)) {
6829 xmlGenericError(xmlGenericErrorContext,
6830 "xmlTextConcat: node is not text nor CDATA\n");
6834 /* need to check if content is currently in the dictionary */
6835 if ((node->content == (xmlChar *) &(node->properties)) ||
6836 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6837 xmlDictOwns(node->doc->dict, node->content))) {
6838 node->content = xmlStrncatNew(node->content, content, len);
6840 node->content = xmlStrncat(node->content, content, len);
6842 node->properties = NULL;
6843 if (node->content == NULL)
6848 /************************************************************************
6850 * Output : to a FILE or in memory *
6852 ************************************************************************/
6857 * routine to create an XML buffer.
6858 * returns the new structure.
6861 xmlBufferCreate(void) {
6864 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6866 xmlTreeErrMemory("creating buffer");
6870 ret->size = xmlDefaultBufferSize;
6871 ret->alloc = xmlBufferAllocScheme;
6872 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
6873 if (ret->content == NULL) {
6874 xmlTreeErrMemory("creating buffer");
6878 ret->content[0] = 0;
6879 ret->contentIO = NULL;
6884 * xmlBufferCreateSize:
6885 * @size: initial size of buffer
6887 * routine to create an XML buffer.
6888 * returns the new structure.
6891 xmlBufferCreateSize(size_t size) {
6894 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6896 xmlTreeErrMemory("creating buffer");
6900 ret->alloc = xmlBufferAllocScheme;
6901 ret->size = (size ? size+2 : 0); /* +1 for ending null */
6903 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
6904 if (ret->content == NULL) {
6905 xmlTreeErrMemory("creating buffer");
6909 ret->content[0] = 0;
6911 ret->content = NULL;
6912 ret->contentIO = NULL;
6917 * xmlBufferCreateStatic:
6918 * @mem: the memory area
6919 * @size: the size in byte
6921 * routine to create an XML buffer from an immutable memory area.
6922 * The area won't be modified nor copied, and is expected to be
6923 * present until the end of the buffer lifetime.
6925 * returns the new structure.
6928 xmlBufferCreateStatic(void *mem, size_t size) {
6931 if ((mem == NULL) || (size == 0))
6934 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6936 xmlTreeErrMemory("creating buffer");
6941 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
6942 ret->content = (xmlChar *) mem;
6947 * xmlBufferSetAllocationScheme:
6948 * @buf: the buffer to tune
6949 * @scheme: allocation scheme to use
6951 * Sets the allocation scheme for this buffer
6954 xmlBufferSetAllocationScheme(xmlBufferPtr buf,
6955 xmlBufferAllocationScheme scheme) {
6958 xmlGenericError(xmlGenericErrorContext,
6959 "xmlBufferSetAllocationScheme: buf == NULL\n");
6963 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
6964 (buf->alloc == XML_BUFFER_ALLOC_IO)) return;
6965 if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
6966 (scheme == XML_BUFFER_ALLOC_EXACT) ||
6967 (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
6968 buf->alloc = scheme;
6973 * @buf: the buffer to free
6975 * Frees an XML buffer. It frees both the content and the structure which
6979 xmlBufferFree(xmlBufferPtr buf) {
6982 xmlGenericError(xmlGenericErrorContext,
6983 "xmlBufferFree: buf == NULL\n");
6988 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
6989 (buf->contentIO != NULL)) {
6990 xmlFree(buf->contentIO);
6991 } else if ((buf->content != NULL) &&
6992 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
6993 xmlFree(buf->content);
7005 xmlBufferEmpty(xmlBufferPtr buf) {
7006 if (buf == NULL) return;
7007 if (buf->content == NULL) return;
7009 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
7010 buf->content = BAD_CAST "";
7011 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7012 (buf->contentIO != NULL)) {
7013 size_t start_buf = buf->content - buf->contentIO;
7015 buf->size += start_buf;
7016 buf->content = buf->contentIO;
7017 buf->content[0] = 0;
7019 buf->content[0] = 0;
7025 * @buf: the buffer to dump
7026 * @len: the number of xmlChar to remove
7028 * Remove the beginning of an XML buffer.
7030 * Returns the number of #xmlChar removed, or -1 in case of failure.
7033 xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
7034 if (buf == NULL) return(-1);
7035 if (len == 0) return(0);
7036 if (len > buf->use) return(-1);
7039 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7040 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
7042 * we just move the content pointer, but also make sure
7043 * the perceived buffer size has shrinked accordingly
7045 buf->content += len;
7049 * sometimes though it maybe be better to really shrink
7052 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7053 size_t start_buf = buf->content - buf->contentIO;
7054 if (start_buf >= buf->size) {
7055 memmove(buf->contentIO, &buf->content[0], buf->use);
7056 buf->content = buf->contentIO;
7057 buf->content[buf->use] = 0;
7058 buf->size += start_buf;
7062 memmove(buf->content, &buf->content[len], buf->use);
7063 buf->content[buf->use] = 0;
7071 * @len: the minimum free size to allocate
7073 * Grow the available space of an XML buffer.
7075 * Returns the new available space or -1 in case of error
7078 xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
7082 if (buf == NULL) return(-1);
7084 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7085 if (len + buf->use < buf->size) return(0);
7088 * Windows has a BIG problem on realloc timing, so we try to double
7089 * the buffer size (if that's enough) (bug 146697)
7090 * Apparently BSD too, and it's probably best for linux too
7091 * On an embedded system this may be something to change
7094 if (buf->size > len)
7095 size = buf->size * 2;
7097 size = buf->use + len + 100;
7099 size = buf->use + len + 100;
7102 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7103 size_t start_buf = buf->content - buf->contentIO;
7105 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
7106 if (newbuf == NULL) {
7107 xmlTreeErrMemory("growing buffer");
7110 buf->contentIO = newbuf;
7111 buf->content = newbuf + start_buf;
7113 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
7114 if (newbuf == NULL) {
7115 xmlTreeErrMemory("growing buffer");
7118 buf->content = newbuf;
7121 return(buf->size - buf->use);
7126 * @file: the file output
7127 * @buf: the buffer to dump
7129 * Dumps an XML buffer to a FILE *.
7130 * Returns the number of #xmlChar written
7133 xmlBufferDump(FILE *file, xmlBufferPtr buf) {
7138 xmlGenericError(xmlGenericErrorContext,
7139 "xmlBufferDump: buf == NULL\n");
7143 if (buf->content == NULL) {
7145 xmlGenericError(xmlGenericErrorContext,
7146 "xmlBufferDump: buf->content == NULL\n");
7152 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
7160 * Function to extract the content of a buffer
7162 * Returns the internal content
7166 xmlBufferContent(const xmlBufferPtr buf)
7171 return buf->content;
7178 * Function to get the length of a buffer
7180 * Returns the length of data in the internal content
7184 xmlBufferLength(const xmlBufferPtr buf)
7194 * @buf: the buffer to resize
7195 * @size: the desired size
7197 * Resize a buffer to accommodate minimum size of @size.
7199 * Returns 0 in case of problems, 1 otherwise
7202 xmlBufferResize(xmlBufferPtr buf, unsigned int size)
7204 unsigned int newSize;
7205 xmlChar* rebuf = NULL;
7211 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7213 /* Don't resize if we don't have to */
7214 if (size < buf->size)
7217 /* figure out new size */
7218 switch (buf->alloc){
7219 case XML_BUFFER_ALLOC_IO:
7220 case XML_BUFFER_ALLOC_DOUBLEIT:
7221 /*take care of empty case*/
7222 newSize = (buf->size ? buf->size*2 : size + 10);
7223 while (size > newSize) {
7224 if (newSize > UINT_MAX / 2) {
7225 xmlTreeErrMemory("growing buffer");
7231 case XML_BUFFER_ALLOC_EXACT:
7239 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7240 start_buf = buf->content - buf->contentIO;
7242 if (start_buf > newSize) {
7243 /* move data back to start */
7244 memmove(buf->contentIO, buf->content, buf->use);
7245 buf->content = buf->contentIO;
7246 buf->content[buf->use] = 0;
7247 buf->size += start_buf;
7249 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
7250 if (rebuf == NULL) {
7251 xmlTreeErrMemory("growing buffer");
7254 buf->contentIO = rebuf;
7255 buf->content = rebuf + start_buf;
7258 if (buf->content == NULL) {
7259 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7260 } else if (buf->size - buf->use < 100) {
7261 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
7264 * if we are reallocating a buffer far from being full, it's
7265 * better to make a new allocation and copy only the used range
7266 * and free the old one.
7268 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7269 if (rebuf != NULL) {
7270 memcpy(rebuf, buf->content, buf->use);
7271 xmlFree(buf->content);
7272 rebuf[buf->use] = 0;
7275 if (rebuf == NULL) {
7276 xmlTreeErrMemory("growing buffer");
7279 buf->content = rebuf;
7281 buf->size = newSize;
7288 * @buf: the buffer to dump
7289 * @str: the #xmlChar string
7290 * @len: the number of #xmlChar to add
7292 * Add a string range to an XML buffer. if len == -1, the length of
7293 * str is recomputed.
7295 * Returns 0 successful, a positive error code number otherwise
7296 * and -1 in case of internal or API error.
7299 xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
7300 unsigned int needSize;
7302 if ((str == NULL) || (buf == NULL)) {
7305 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7308 xmlGenericError(xmlGenericErrorContext,
7309 "xmlBufferAdd: len < 0\n");
7313 if (len == 0) return 0;
7316 len = xmlStrlen(str);
7318 if (len < 0) return -1;
7319 if (len == 0) return 0;
7321 needSize = buf->use + len + 2;
7322 if (needSize > buf->size){
7323 if (!xmlBufferResize(buf, needSize)){
7324 xmlTreeErrMemory("growing buffer");
7325 return XML_ERR_NO_MEMORY;
7329 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
7331 buf->content[buf->use] = 0;
7338 * @str: the #xmlChar string
7339 * @len: the number of #xmlChar to add
7341 * Add a string range to the beginning of an XML buffer.
7342 * if len == -1, the length of @str is recomputed.
7344 * Returns 0 successful, a positive error code number otherwise
7345 * and -1 in case of internal or API error.
7348 xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7349 unsigned int needSize;
7353 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7356 xmlGenericError(xmlGenericErrorContext,
7357 "xmlBufferAddHead: str == NULL\n");
7363 xmlGenericError(xmlGenericErrorContext,
7364 "xmlBufferAddHead: len < 0\n");
7368 if (len == 0) return 0;
7371 len = xmlStrlen(str);
7373 if (len <= 0) return -1;
7375 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7376 size_t start_buf = buf->content - buf->contentIO;
7378 if (start_buf > (unsigned int) len) {
7380 * We can add it in the space previously shrinked
7382 buf->content -= len;
7383 memmove(&buf->content[0], str, len);
7389 needSize = buf->use + len + 2;
7390 if (needSize > buf->size){
7391 if (!xmlBufferResize(buf, needSize)){
7392 xmlTreeErrMemory("growing buffer");
7393 return XML_ERR_NO_MEMORY;
7397 memmove(&buf->content[len], &buf->content[0], buf->use);
7398 memmove(&buf->content[0], str, len);
7400 buf->content[buf->use] = 0;
7406 * @buf: the buffer to add to
7407 * @str: the #xmlChar string
7409 * Append a zero terminated string to an XML buffer.
7411 * Returns 0 successful, a positive error code number otherwise
7412 * and -1 in case of internal or API error.
7415 xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
7418 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7419 if (str == NULL) return -1;
7420 return xmlBufferAdd(buf, str, -1);
7425 * @buf: the buffer to dump
7426 * @str: the C char string
7428 * Append a zero terminated C string to an XML buffer.
7430 * Returns 0 successful, a positive error code number otherwise
7431 * and -1 in case of internal or API error.
7434 xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7439 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7442 xmlGenericError(xmlGenericErrorContext,
7443 "xmlBufferCCat: str == NULL\n");
7447 for (cur = str;*cur != 0;cur++) {
7448 if (buf->use + 10 >= buf->size) {
7449 if (!xmlBufferResize(buf, buf->use+10)){
7450 xmlTreeErrMemory("growing buffer");
7451 return XML_ERR_NO_MEMORY;
7454 buf->content[buf->use++] = *cur;
7456 buf->content[buf->use] = 0;
7461 * xmlBufferWriteCHAR:
7462 * @buf: the XML buffer
7463 * @string: the string to add
7465 * routine which manages and grows an output buffer. This one adds
7466 * xmlChars at the end of the buffer.
7469 xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
7472 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7473 xmlBufferCat(buf, string);
7477 * xmlBufferWriteChar:
7478 * @buf: the XML buffer output
7479 * @string: the string to add
7481 * routine which manage and grows an output buffer. This one add
7482 * C chars at the end of the array.
7485 xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
7488 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7489 xmlBufferCCat(buf, string);
7494 * xmlBufferWriteQuotedString:
7495 * @buf: the XML buffer output
7496 * @string: the string to add
7498 * routine which manage and grows an output buffer. This one writes
7499 * a quoted or double quoted #xmlChar string, checking first if it holds
7500 * quote or double-quotes internally
7503 xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
7504 const xmlChar *cur, *base;
7507 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
7508 if (xmlStrchr(string, '\"')) {
7509 if (xmlStrchr(string, '\'')) {
7511 xmlGenericError(xmlGenericErrorContext,
7512 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7514 xmlBufferCCat(buf, "\"");
7515 base = cur = string;
7519 xmlBufferAdd(buf, base, cur - base);
7520 xmlBufferAdd(buf, BAD_CAST """, 6);
7529 xmlBufferAdd(buf, base, cur - base);
7530 xmlBufferCCat(buf, "\"");
7533 xmlBufferCCat(buf, "\'");
7534 xmlBufferCat(buf, string);
7535 xmlBufferCCat(buf, "\'");
7538 xmlBufferCCat(buf, "\"");
7539 xmlBufferCat(buf, string);
7540 xmlBufferCCat(buf, "\"");
7546 * xmlGetDocCompressMode:
7547 * @doc: the document
7549 * get the compression ratio for a document, ZLIB based
7550 * Returns 0 (uncompressed) to 9 (max compression)
7553 xmlGetDocCompressMode (xmlDocPtr doc) {
7554 if (doc == NULL) return(-1);
7555 return(doc->compression);
7559 * xmlSetDocCompressMode:
7560 * @doc: the document
7561 * @mode: the compression ratio
7563 * set the compression ratio for a document, ZLIB based
7564 * Correct values: 0 (uncompressed) to 9 (max compression)
7567 xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7568 if (doc == NULL) return;
7569 if (mode < 0) doc->compression = 0;
7570 else if (mode > 9) doc->compression = 9;
7571 else doc->compression = mode;
7575 * xmlGetCompressMode:
7577 * get the default compression mode used, ZLIB based.
7578 * Returns 0 (uncompressed) to 9 (max compression)
7581 xmlGetCompressMode(void)
7583 return (xmlCompressMode);
7587 * xmlSetCompressMode:
7588 * @mode: the compression ratio
7590 * set the default compression mode used, ZLIB based
7591 * Correct values: 0 (uncompressed) to 9 (max compression)
7594 xmlSetCompressMode(int mode) {
7595 if (mode < 0) xmlCompressMode = 0;
7596 else if (mode > 9) xmlCompressMode = 9;
7597 else xmlCompressMode = mode;
7600 #define XML_TREE_NSMAP_PARENT -1
7601 #define XML_TREE_NSMAP_XML -2
7602 #define XML_TREE_NSMAP_DOC -3
7603 #define XML_TREE_NSMAP_CUSTOM -4
7605 typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7606 struct xmlNsMapItem {
7607 xmlNsMapItemPtr next;
7608 xmlNsMapItemPtr prev;
7609 xmlNsPtr oldNs; /* old ns decl reference */
7610 xmlNsPtr newNs; /* new ns decl reference */
7611 int shadowDepth; /* Shadowed at this depth */
7614 * >= 0 == @node's ns-decls
7615 * -1 == @parent's ns-decls
7616 * -2 == the doc->oldNs XML ns-decl
7617 * -3 == the doc->oldNs storage ns-decls
7618 * -4 == ns-decls provided via custom ns-handling
7623 typedef struct xmlNsMap *xmlNsMapPtr;
7625 xmlNsMapItemPtr first;
7626 xmlNsMapItemPtr last;
7627 xmlNsMapItemPtr pool;
7630 #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7631 #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7632 #define XML_NSMAP_POP(m, i) \
7634 (m)->last = (i)->prev; \
7635 if ((m)->last == NULL) \
7636 (m)->first = NULL; \
7638 (m)->last->next = NULL; \
7639 (i)->next = (m)->pool; \
7643 * xmlDOMWrapNsMapFree:
7649 xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7651 xmlNsMapItemPtr cur, tmp;
7656 while (cur != NULL) {
7662 while (cur != NULL) {
7671 * xmlDOMWrapNsMapAddItem:
7673 * @oldNs: the old ns-struct
7674 * @newNs: the new ns-struct
7675 * @depth: depth and ns-kind information
7677 * Adds an ns-mapping item.
7679 static xmlNsMapItemPtr
7680 xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
7681 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
7683 xmlNsMapItemPtr ret;
7688 if ((position != -1) && (position != 0))
7694 * Create the ns-map.
7696 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7698 xmlTreeErrMemory("allocating namespace map");
7701 memset(map, 0, sizeof(struct xmlNsMap));
7705 if (map->pool != NULL) {
7707 * Reuse an item from the pool.
7710 map->pool = ret->next;
7711 memset(ret, 0, sizeof(struct xmlNsMapItem));
7714 * Create a new item.
7716 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7718 xmlTreeErrMemory("allocating namespace map item");
7721 memset(ret, 0, sizeof(struct xmlNsMapItem));
7724 if (map->first == NULL) {
7730 } else if (position == -1) {
7734 ret->prev = map->last;
7735 map->last->next = ret;
7737 } else if (position == 0) {
7739 * Set on first position.
7741 map->first->prev = ret;
7742 ret->next = map->first;
7749 ret->shadowDepth = -1;
7755 * xmlDOMWrapStoreNs:
7757 * @nsName: the namespace name
7758 * @prefix: the prefix
7760 * Creates or reuses an xmlNs struct on doc->oldNs with
7761 * the given prefix and namespace name.
7763 * Returns the aquired ns struct or NULL in case of an API
7764 * or internal error.
7767 xmlDOMWrapStoreNs(xmlDocPtr doc,
7768 const xmlChar *nsName,
7769 const xmlChar *prefix)
7775 ns = xmlTreeEnsureXMLDecl(doc);
7778 if (ns->next != NULL) {
7781 while (ns != NULL) {
7782 if (((ns->prefix == prefix) ||
7783 xmlStrEqual(ns->prefix, prefix)) &&
7784 xmlStrEqual(ns->href, nsName)) {
7787 if (ns->next == NULL)
7794 ns->next = xmlNewNs(NULL, nsName, prefix);
7801 * xmlDOMWrapNewCtxt:
7803 * Allocates and initializes a new DOM-wrapper context.
7805 * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror.
7808 xmlDOMWrapNewCtxt(void)
7810 xmlDOMWrapCtxtPtr ret;
7812 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
7814 xmlTreeErrMemory("allocating DOM-wrapper context");
7817 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
7822 * xmlDOMWrapFreeCtxt:
7823 * @ctxt: the DOM-wrapper context
7825 * Frees the DOM-wrapper context.
7828 xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
7832 if (ctxt->namespaceMap != NULL)
7833 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
7835 * TODO: Store the namespace map in the context.
7841 * xmlTreeLookupNsListByPrefix:
7842 * @nsList: a list of ns-structs
7843 * @prefix: the searched prefix
7845 * Searches for a ns-decl with the given prefix in @nsList.
7847 * Returns the ns-decl if found, NULL if not found and on
7851 xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
7859 if ((prefix == ns->prefix) ||
7860 xmlStrEqual(prefix, ns->prefix)) {
7864 } while (ns != NULL);
7871 * xmlDOMWrapNSNormGatherInScopeNs:
7872 * @map: the namespace map
7873 * @node: the node to start with
7875 * Puts in-scope namespaces into the ns-map.
7877 * Returns 0 on success, -1 on API or internal errors.
7880 xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
7888 if ((map == NULL) || (*map != NULL))
7891 * Get in-scope ns-decls of @parent.
7894 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
7895 if (cur->type == XML_ELEMENT_NODE) {
7896 if (cur->nsDef != NULL) {
7900 if (XML_NSMAP_NOTEMPTY(*map)) {
7902 * Skip shadowed prefixes.
7904 XML_NSMAP_FOREACH(*map, mi) {
7905 if ((ns->prefix == mi->newNs->prefix) ||
7906 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
7915 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
7916 ns, XML_TREE_NSMAP_PARENT);
7920 mi->shadowDepth = 0;
7922 } while (ns != NULL);
7931 * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
7932 * otherwise copy it, when it was in the source-dict.
7934 #define XML_TREE_ADOPT_STR(str) \
7935 if (adoptStr && (str != NULL)) { \
7936 if (destDoc->dict) { \
7937 const xmlChar *old = str; \
7938 str = xmlDictLookup(destDoc->dict, str, -1); \
7939 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
7940 (!xmlDictOwns(sourceDoc->dict, old))) \
7941 xmlFree((char *)old); \
7942 } else if ((sourceDoc) && (sourceDoc->dict) && \
7943 xmlDictOwns(sourceDoc->dict, str)) { \
7944 str = BAD_CAST xmlStrdup(str); \
7949 * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
7950 * put it in dest-dict or copy it.
7952 #define XML_TREE_ADOPT_STR_2(str) \
7953 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
7954 (sourceDoc->dict != NULL) && \
7955 xmlDictOwns(sourceDoc->dict, cur->content)) { \
7956 if (destDoc->dict) \
7957 cur->content = (xmlChar *) \
7958 xmlDictLookup(destDoc->dict, cur->content, -1); \
7960 cur->content = xmlStrdup(BAD_CAST cur->content); \
7964 * xmlDOMWrapNSNormAddNsMapItem2:
7966 * For internal use. Adds a ns-decl mapping.
7968 * Returns 0 on success, -1 on internal errors.
7971 xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
7972 xmlNsPtr oldNs, xmlNsPtr newNs)
7974 if (*list == NULL) {
7975 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
7976 if (*list == NULL) {
7977 xmlTreeErrMemory("alloc ns map item");
7982 } else if ((*number) >= (*size)) {
7984 *list = (xmlNsPtr *) xmlRealloc(*list,
7985 (*size) * 2 * sizeof(xmlNsPtr));
7986 if (*list == NULL) {
7987 xmlTreeErrMemory("realloc ns map item");
7991 (*list)[2 * (*number)] = oldNs;
7992 (*list)[2 * (*number) +1] = newNs;
7998 * xmlDOMWrapRemoveNode:
7999 * @ctxt: a DOM wrapper context
8001 * @node: the node to be removed.
8002 * @options: set of options, unused at the moment
8004 * Unlinks the given node from its owner.
8005 * This will substitute ns-references to node->nsDef for
8006 * ns-references to doc->oldNs, thus ensuring the removed
8007 * branch to be autark wrt ns-references.
8009 * NOTE: This function was not intensively tested.
8011 * Returns 0 on success, 1 if the node is not supported,
8012 * -1 on API and internal errors.
8015 xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
8016 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
8018 xmlNsPtr *list = NULL;
8019 int sizeList, nbList, i, j;
8022 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
8025 /* TODO: 0 or -1 ? */
8026 if (node->parent == NULL)
8029 switch (node->type) {
8031 case XML_CDATA_SECTION_NODE:
8032 case XML_ENTITY_REF_NODE:
8034 case XML_COMMENT_NODE:
8035 xmlUnlinkNode(node);
8037 case XML_ELEMENT_NODE:
8038 case XML_ATTRIBUTE_NODE:
8043 xmlUnlinkNode(node);
8045 * Save out-of-scope ns-references in doc->oldNs.
8048 switch (node->type) {
8049 case XML_ELEMENT_NODE:
8050 if ((ctxt == NULL) && (node->nsDef != NULL)) {
8053 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8054 &nbList, ns, ns) == -1)
8055 goto internal_error;
8057 } while (ns != NULL);
8059 /* No break on purpose. */
8060 case XML_ATTRIBUTE_NODE:
8061 if (node->ns != NULL) {
8066 for (i = 0, j = 0; i < nbList; i++, j += 2) {
8067 if (node->ns == list[j]) {
8068 node->ns = list[++j];
8080 * Add to doc's oldNs.
8082 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
8085 goto internal_error;
8091 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8092 &nbList, node->ns, ns) == -1)
8093 goto internal_error;
8097 if ((node->type == XML_ELEMENT_NODE) &&
8098 (node->properties != NULL)) {
8099 node = (xmlNodePtr) node->properties;
8107 if ((node->type == XML_ELEMENT_NODE) &&
8108 (node->children != NULL)) {
8109 node = node->children;
8115 if (node->next != NULL)
8118 node = node->parent;
8121 } while (node != NULL);
8134 * xmlSearchNsByNamespaceStrict:
8135 * @doc: the document
8136 * @node: the start node
8137 * @nsName: the searched namespace name
8138 * @retNs: the resulting ns-decl
8139 * @prefixed: if the found ns-decl must have a prefix (for attributes)
8141 * Dynamically searches for a ns-declaration which matches
8142 * the given @nsName in the ancestor-or-self axis of @node.
8144 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8145 * and internal errors.
8148 xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
8149 const xmlChar* nsName,
8150 xmlNsPtr *retNs, int prefixed)
8152 xmlNodePtr cur, prev = NULL, out = NULL;
8153 xmlNsPtr ns, prevns;
8155 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
8159 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
8160 *retNs = xmlTreeEnsureXMLDecl(doc);
8167 if (cur->type == XML_ELEMENT_NODE) {
8168 if (cur->nsDef != NULL) {
8169 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8170 if (prefixed && (ns->prefix == NULL))
8174 * Check the last level of ns-decls for a
8177 prevns = prev->nsDef;
8179 if ((prevns->prefix == ns->prefix) ||
8180 ((prevns->prefix != NULL) &&
8181 (ns->prefix != NULL) &&
8182 xmlStrEqual(prevns->prefix, ns->prefix))) {
8188 prevns = prevns->next;
8189 } while (prevns != NULL);
8194 * Ns-name comparison.
8196 if ((nsName == ns->href) ||
8197 xmlStrEqual(nsName, ns->href)) {
8199 * At this point the prefix can only be shadowed,
8200 * if we are the the (at least) 3rd level of
8206 ret = xmlNsInScope(doc, node, prev, ns->prefix);
8210 * TODO: Should we try to find a matching ns-name
8211 * only once? This here keeps on searching.
8212 * I think we should try further since, there might
8213 * be an other matching ns-decl with an unshadowed
8226 } else if ((cur->type == XML_ENTITY_NODE) ||
8227 (cur->type == XML_ENTITY_DECL))
8230 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8235 * xmlSearchNsByPrefixStrict:
8236 * @doc: the document
8237 * @node: the start node
8238 * @prefix: the searched namespace prefix
8239 * @retNs: the resulting ns-decl
8241 * Dynamically searches for a ns-declaration which matches
8242 * the given @nsName in the ancestor-or-self axis of @node.
8244 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8245 * and internal errors.
8248 xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
8249 const xmlChar* prefix,
8255 if ((doc == NULL) || (node == NULL))
8260 if (IS_STR_XML(prefix)) {
8262 *retNs = xmlTreeEnsureXMLDecl(doc);
8270 if (cur->type == XML_ELEMENT_NODE) {
8271 if (cur->nsDef != NULL) {
8274 if ((prefix == ns->prefix) ||
8275 xmlStrEqual(prefix, ns->prefix))
8278 * Disabled namespaces, e.g. xmlns:abc="".
8280 if (ns->href == NULL)
8287 } while (ns != NULL);
8289 } else if ((cur->type == XML_ENTITY_NODE) ||
8290 (cur->type == XML_ENTITY_DECL))
8293 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8298 * xmlDOMWrapNSNormDeclareNsForced:
8300 * @elem: the element-node to declare on
8301 * @nsName: the namespace-name of the ns-decl
8302 * @prefix: the preferred prefix of the ns-decl
8303 * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
8305 * Declares a new namespace on @elem. It tries to use the
8306 * given @prefix; if a ns-decl with the given prefix is already existent
8307 * on @elem, it will generate an other prefix.
8309 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8310 * and internal errors.
8313 xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
8315 const xmlChar *nsName,
8316 const xmlChar *prefix,
8322 const xmlChar *pref;
8325 * Create a ns-decl on @anchor.
8330 * Lookup whether the prefix is unused in elem's ns-decls.
8332 if ((elem->nsDef != NULL) &&
8333 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
8334 goto ns_next_prefix;
8335 if (checkShadow && elem->parent &&
8336 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8338 * Does it shadow ancestor ns-decls?
8340 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
8341 goto ns_next_prefix;
8343 ret = xmlNewNs(NULL, nsName, pref);
8346 if (elem->nsDef == NULL)
8349 xmlNsPtr ns2 = elem->nsDef;
8350 while (ns2->next != NULL)
8359 if (prefix == NULL) {
8360 snprintf((char *) buf, sizeof(buf),
8363 snprintf((char *) buf, sizeof(buf),
8364 "%.30s_%d", (char *)prefix, counter);
8365 pref = BAD_CAST buf;
8370 * xmlDOMWrapNSNormAquireNormalizedNs:
8372 * @elem: the element-node to declare namespaces on
8373 * @ns: the ns-struct to use for the search
8374 * @retNs: the found/created ns-struct
8375 * @nsMap: the ns-map
8376 * @depth: the current tree depth
8377 * @ancestorsOnly: search in ancestor ns-decls only
8378 * @prefixed: if the searched ns-decl must have a prefix (for attributes)
8380 * Searches for a matching ns-name in the ns-decls of @nsMap, if not
8381 * found it will either declare it on @elem, or store it in doc->oldNs.
8382 * If a new ns-decl needs to be declared on @elem, it tries to use the
8383 * @ns->prefix for it, if this prefix is already in use on @elem, it will
8384 * change the prefix or the new ns-decl.
8386 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8389 xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8401 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
8407 * Handle XML namespace.
8409 if (IS_STR_XML(ns->prefix)) {
8411 * Insert XML namespace mapping.
8413 *retNs = xmlTreeEnsureXMLDecl(doc);
8419 * If the search should be done in ancestors only and no
8420 * @elem (the first ancestor) was specified, then skip the search.
8422 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8423 (! (ancestorsOnly && (elem == NULL))))
8426 * Try to find an equal ns-name in in-scope ns-decls.
8428 XML_NSMAP_FOREACH(*nsMap, mi) {
8429 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8431 * ancestorsOnly: This should be turned on to gain speed,
8432 * if one knows that the branch itself was already
8433 * ns-wellformed and no stale references existed.
8434 * I.e. it searches in the ancestor axis only.
8436 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8437 /* Skip shadowed prefixes. */
8438 (mi->shadowDepth == -1) &&
8439 /* Skip xmlns="" or xmlns:foo="". */
8440 ((mi->newNs->href != NULL) &&
8441 (mi->newNs->href[0] != 0)) &&
8442 /* Ensure a prefix if wanted. */
8443 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8445 ((mi->newNs->href == ns->href) ||
8446 xmlStrEqual(mi->newNs->href, ns->href))) {
8447 /* Set the mapping. */
8455 * No luck, the namespace is out of scope or shadowed.
8461 * Store ns-decls in "oldNs" of the document-node.
8463 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8469 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
8470 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8478 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8483 if (*nsMap != NULL) {
8485 * Does it shadow ancestor ns-decls?
8487 XML_NSMAP_FOREACH(*nsMap, mi) {
8488 if ((mi->depth < depth) &&
8489 (mi->shadowDepth == -1) &&
8490 ((ns->prefix == mi->newNs->prefix) ||
8491 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8495 mi->shadowDepth = depth;
8500 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
8510 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
8511 } xmlDOMReconcileNSOptions;
8514 * xmlDOMWrapReconcileNamespaces:
8515 * @ctxt: DOM wrapper context, unused at the moment
8516 * @elem: the element-node
8517 * @options: option flags
8519 * Ensures that ns-references point to ns-decls hold on element-nodes.
8520 * Ensures that the tree is namespace wellformed by creating additional
8521 * ns-decls where needed. Note that, since prefixes of already existent
8522 * ns-decls can be shadowed by this process, it could break QNames in
8523 * attribute values or element content.
8525 * NOTE: This function was not intensively tested.
8527 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8531 xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8535 int depth = -1, adoptns = 0, parnsdone = 0;
8536 xmlNsPtr ns, prevns;
8538 xmlNodePtr cur, curElem = NULL;
8539 xmlNsMapPtr nsMap = NULL;
8540 xmlNsMapItemPtr /* topmi = NULL, */ mi;
8541 /* @ancestorsOnly should be set by an option flag. */
8542 int ancestorsOnly = 0;
8543 int optRemoveRedundantNS =
8544 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8545 xmlNsPtr *listRedund = NULL;
8546 int sizeRedund = 0, nbRedund = 0, ret, i, j;
8548 if ((elem == NULL) || (elem->doc == NULL) ||
8549 (elem->type != XML_ELEMENT_NODE))
8555 switch (cur->type) {
8556 case XML_ELEMENT_NODE:
8561 * Namespace declarations.
8563 if (cur->nsDef != NULL) {
8566 while (ns != NULL) {
8568 if ((elem->parent) &&
8569 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8571 * Gather ancestor in-scope ns-decls.
8573 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8574 elem->parent) == -1)
8575 goto internal_error;
8581 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8583 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
8584 XML_NSMAP_FOREACH(nsMap, mi) {
8585 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8586 (mi->shadowDepth == -1) &&
8587 ((ns->prefix == mi->newNs->prefix) ||
8588 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8589 ((ns->href == mi->newNs->href) ||
8590 xmlStrEqual(ns->href, mi->newNs->href)))
8593 * A redundant ns-decl was found.
8594 * Add it to the list of redundant ns-decls.
8596 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8597 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8598 goto internal_error;
8600 * Remove the ns-decl from the element-node.
8603 prevns->next = ns->next;
8605 cur->nsDef = ns->next;
8612 * Skip ns-references handling if the referenced
8613 * ns-decl is declared on the same element.
8615 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
8618 * Does it shadow any ns-decl?
8620 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8621 XML_NSMAP_FOREACH(nsMap, mi) {
8622 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8623 (mi->shadowDepth == -1) &&
8624 ((ns->prefix == mi->newNs->prefix) ||
8625 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8627 mi->shadowDepth = depth;
8634 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
8636 goto internal_error;
8645 /* No break on purpose. */
8646 case XML_ATTRIBUTE_NODE:
8647 /* No ns, no fun. */
8648 if (cur->ns == NULL)
8652 if ((elem->parent) &&
8653 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8654 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8655 elem->parent) == -1)
8656 goto internal_error;
8661 * Adjust the reference if this was a redundant ns-decl.
8664 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8665 if (cur->ns == listRedund[j]) {
8666 cur->ns = listRedund[++j];
8672 * Adopt ns-references.
8674 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8676 * Search for a mapping.
8678 XML_NSMAP_FOREACH(nsMap, mi) {
8679 if ((mi->shadowDepth == -1) &&
8680 (cur->ns == mi->oldNs)) {
8682 cur->ns = mi->newNs;
8688 * Aquire a normalized ns-decl and add it to the map.
8690 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8694 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8695 goto internal_error;
8699 if ((cur->type == XML_ELEMENT_NODE) &&
8700 (cur->properties != NULL)) {
8702 * Process attributes.
8704 cur = (xmlNodePtr) cur->properties;
8712 if ((cur->type == XML_ELEMENT_NODE) &&
8713 (cur->children != NULL)) {
8715 * Process content of element-nodes only.
8717 cur = cur->children;
8723 if (cur->type == XML_ELEMENT_NODE) {
8724 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8728 while ((nsMap->last != NULL) &&
8729 (nsMap->last->depth >= depth))
8731 XML_NSMAP_POP(nsMap, mi)
8736 XML_NSMAP_FOREACH(nsMap, mi) {
8737 if (mi->shadowDepth >= depth)
8738 mi->shadowDepth = -1;
8743 if (cur->next != NULL)
8746 if (cur->type == XML_ATTRIBUTE_NODE) {
8753 } while (cur != NULL);
8761 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8762 xmlFreeNs(listRedund[j]);
8764 xmlFree(listRedund);
8767 xmlDOMWrapNsMapFree(nsMap);
8772 * xmlDOMWrapAdoptBranch:
8773 * @ctxt: the optional context for custom processing
8774 * @sourceDoc: the optional sourceDoc
8775 * @node: the element-node to start with
8776 * @destDoc: the destination doc for adoption
8777 * @destParent: the optional new parent of @node in @destDoc
8778 * @options: option flags
8780 * Ensures that ns-references point to @destDoc: either to
8781 * elements->nsDef entries if @destParent is given, or to
8782 * @destDoc->oldNs otherwise.
8783 * If @destParent is given, it ensures that the tree is namespace
8784 * wellformed by creating additional ns-decls where needed.
8785 * Note that, since prefixes of already existent ns-decls can be
8786 * shadowed by this process, it could break QNames in attribute
8787 * values or element content.
8789 * NOTE: This function was not intensively tested.
8791 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8794 xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8795 xmlDocPtr sourceDoc,
8798 xmlNodePtr destParent,
8799 int options ATTRIBUTE_UNUSED)
8802 xmlNodePtr cur, curElem = NULL;
8803 xmlNsMapPtr nsMap = NULL;
8806 int depth = -1, adoptStr = 1;
8807 /* gather @parent's ns-decls. */
8809 /* @ancestorsOnly should be set per option. */
8810 int ancestorsOnly = 0;
8813 * Optimize string adoption for equal or none dicts.
8815 if ((sourceDoc != NULL) &&
8816 (sourceDoc->dict == destDoc->dict))
8822 * Get the ns-map from the context if available.
8825 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8827 * Disable search for ns-decls in the parent-axis of the
8828 * desination element, if:
8829 * 1) there's no destination parent
8830 * 2) custom ns-reference handling is used
8832 if ((destParent == NULL) ||
8833 (ctxt && ctxt->getNsForNodeFunc))
8840 while (cur != NULL) {
8842 * Paranoid source-doc sanity check.
8844 if (cur->doc != sourceDoc) {
8846 * We'll assume XIncluded nodes if the doc differs.
8847 * TODO: Do we need to reconciliate XIncluded nodes?
8848 * This here skips XIncluded nodes and tries to handle
8851 if (cur->next == NULL)
8855 if ((cur->type == XML_XINCLUDE_END) ||
8856 (cur->doc == node->doc))
8858 } while (cur->next != NULL);
8860 if (cur->doc != node->doc)
8864 switch (cur->type) {
8865 case XML_XINCLUDE_START:
8866 case XML_XINCLUDE_END:
8871 case XML_ELEMENT_NODE:
8875 * Namespace declarations.
8876 * - ns->href and ns->prefix are never in the dict, so
8877 * we need not move the values over to the destination dict.
8878 * - Note that for custom handling of ns-references,
8879 * the ns-decls need not be stored in the ns-map,
8880 * since they won't be referenced by node->ns.
8883 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
8887 * Gather @parent's in-scope ns-decls.
8889 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8891 goto internal_error;
8894 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8896 * NOTE: ns->prefix and ns->href are never in the dict.
8897 * XML_TREE_ADOPT_STR(ns->prefix)
8898 * XML_TREE_ADOPT_STR(ns->href)
8901 * Does it shadow any ns-decl?
8903 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8904 XML_NSMAP_FOREACH(nsMap, mi) {
8905 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8906 (mi->shadowDepth == -1) &&
8907 ((ns->prefix == mi->newNs->prefix) ||
8908 xmlStrEqual(ns->prefix,
8909 mi->newNs->prefix))) {
8911 mi->shadowDepth = depth;
8918 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
8919 ns, ns, depth) == NULL)
8920 goto internal_error;
8923 /* No break on purpose. */
8924 case XML_ATTRIBUTE_NODE:
8925 /* No namespace, no fun. */
8926 if (cur->ns == NULL)
8930 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8932 goto internal_error;
8936 * Adopt ns-references.
8938 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8940 * Search for a mapping.
8942 XML_NSMAP_FOREACH(nsMap, mi) {
8943 if ((mi->shadowDepth == -1) &&
8944 (cur->ns == mi->oldNs)) {
8946 cur->ns = mi->newNs;
8952 * No matching namespace in scope. We need a new one.
8954 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
8956 * User-defined behaviour.
8958 ns = ctxt->getNsForNodeFunc(ctxt, cur,
8959 cur->ns->href, cur->ns->prefix);
8961 * Insert mapping if ns is available; it's the users fault
8964 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
8965 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
8966 goto internal_error;
8970 * Aquire a normalized ns-decl and add it to the map.
8972 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
8973 /* ns-decls on curElem or on destDoc->oldNs */
8974 destParent ? curElem : NULL,
8978 /* ns-decls must be prefixed for attributes. */
8979 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8980 goto internal_error;
8985 * Further node properties.
8986 * TODO: Is this all?
8988 XML_TREE_ADOPT_STR(cur->name)
8989 if (cur->type == XML_ELEMENT_NODE) {
8996 if (cur->properties != NULL) {
8998 * Process first attribute node.
9000 cur = (xmlNodePtr) cur->properties;
9007 if ((sourceDoc != NULL) &&
9008 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
9010 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
9012 ((xmlAttrPtr) cur)->atype = 0;
9013 ((xmlAttrPtr) cur)->psvi = NULL;
9017 case XML_CDATA_SECTION_NODE:
9019 * This puts the content in the dest dict, only if
9020 * it was previously in the source dict.
9022 XML_TREE_ADOPT_STR_2(cur->content)
9024 case XML_ENTITY_REF_NODE:
9026 * Remove reference to the entitity-node.
9028 cur->content = NULL;
9029 cur->children = NULL;
9031 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9034 * Assign new entity-node if available.
9036 ent = xmlGetDocEntity(destDoc, cur->name);
9038 cur->content = ent->content;
9039 cur->children = (xmlNodePtr) ent;
9040 cur->last = (xmlNodePtr) ent;
9045 XML_TREE_ADOPT_STR(cur->name)
9046 XML_TREE_ADOPT_STR_2(cur->content)
9048 case XML_COMMENT_NODE:
9051 goto internal_error;
9056 if (cur->children != NULL) {
9057 cur = cur->children;
9064 if ((cur->type == XML_ELEMENT_NODE) ||
9065 (cur->type == XML_XINCLUDE_START) ||
9066 (cur->type == XML_XINCLUDE_END))
9069 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9071 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9075 while ((nsMap->last != NULL) &&
9076 (nsMap->last->depth >= depth))
9078 XML_NSMAP_POP(nsMap, mi)
9083 XML_NSMAP_FOREACH(nsMap, mi) {
9084 if (mi->shadowDepth >= depth)
9085 mi->shadowDepth = -1;
9090 if (cur->next != NULL)
9092 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
9093 (cur->parent->children != NULL))
9095 cur = cur->parent->children;
9111 if (nsMap != NULL) {
9112 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9114 * Just cleanup the map but don't free.
9118 nsMap->last->next = nsMap->pool;
9119 nsMap->pool = nsMap->first;
9120 nsMap->first = NULL;
9123 xmlDOMWrapNsMapFree(nsMap);
9129 * xmlDOMWrapCloneNode:
9130 * @ctxt: the optional context for custom processing
9131 * @sourceDoc: the optional sourceDoc
9132 * @node: the node to start with
9133 * @resNode: the clone of the given @node
9134 * @destDoc: the destination doc
9135 * @destParent: the optional new parent of @node in @destDoc
9136 * @deep: descend into child if set
9137 * @options: option flags
9139 * References of out-of scope ns-decls are remapped to point to @destDoc:
9140 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9141 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
9142 * This is the case when you don't know already where the cloned branch
9145 * If @destParent is given, it ensures that the tree is namespace
9146 * wellformed by creating additional ns-decls where needed.
9147 * Note that, since prefixes of already existent ns-decls can be
9148 * shadowed by this process, it could break QNames in attribute
9149 * values or element content.
9151 * 1) What to do with XInclude? Currently this returns an error for XInclude.
9153 * Returns 0 if the operation succeeded,
9154 * 1 if a node of unsupported (or not yet supported) type was given,
9155 * -1 on API/internal errors.
9159 xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
9160 xmlDocPtr sourceDoc,
9162 xmlNodePtr *resNode,
9164 xmlNodePtr destParent,
9166 int options ATTRIBUTE_UNUSED)
9169 xmlNodePtr cur, curElem = NULL;
9170 xmlNsMapPtr nsMap = NULL;
9174 /* int adoptStr = 1; */
9175 /* gather @parent's ns-decls. */
9179 * TODO: @ancestorsOnly should be set per option.
9182 int ancestorsOnly = 0;
9183 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
9184 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
9185 xmlDictPtr dict; /* The destination dict */
9187 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
9190 * TODO: Initially we support only element-nodes.
9192 if (node->type != XML_ELEMENT_NODE)
9195 * Check node->doc sanity.
9197 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9198 (node->doc != sourceDoc)) {
9200 * Might be an XIncluded node.
9204 if (sourceDoc == NULL)
9205 sourceDoc = node->doc;
9206 if (sourceDoc == NULL)
9209 dict = destDoc->dict;
9211 * Reuse the namespace map of the context.
9214 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9219 while (cur != NULL) {
9220 if (cur->doc != sourceDoc) {
9222 * We'll assume XIncluded nodes if the doc differs.
9223 * TODO: Do we need to reconciliate XIncluded nodes?
9224 * TODO: This here returns -1 in this case.
9226 goto internal_error;
9229 * Create a new node.
9231 switch (cur->type) {
9232 case XML_XINCLUDE_START:
9233 case XML_XINCLUDE_END:
9235 * TODO: What to do with XInclude?
9237 goto internal_error;
9239 case XML_ELEMENT_NODE:
9241 case XML_CDATA_SECTION_NODE:
9242 case XML_COMMENT_NODE:
9244 case XML_DOCUMENT_FRAG_NODE:
9245 case XML_ENTITY_REF_NODE:
9246 case XML_ENTITY_NODE:
9248 * Nodes of xmlNode structure.
9250 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
9251 if (clone == NULL) {
9252 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
9253 goto internal_error;
9255 memset(clone, 0, sizeof(xmlNode));
9257 * Set hierachical links.
9259 if (resultClone != NULL) {
9260 clone->parent = parentClone;
9262 prevClone->next = clone;
9263 clone->prev = prevClone;
9265 parentClone->children = clone;
9267 resultClone = clone;
9270 case XML_ATTRIBUTE_NODE:
9272 * Attributes (xmlAttr).
9274 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
9275 if (clone == NULL) {
9276 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
9277 goto internal_error;
9279 memset(clone, 0, sizeof(xmlAttr));
9281 * Set hierachical links.
9282 * TODO: Change this to add to the end of attributes.
9284 if (resultClone != NULL) {
9285 clone->parent = parentClone;
9287 prevClone->next = clone;
9288 clone->prev = prevClone;
9290 parentClone->properties = (xmlAttrPtr) clone;
9292 resultClone = clone;
9296 * TODO QUESTION: Any other nodes expected?
9298 goto internal_error;
9301 clone->type = cur->type;
9302 clone->doc = destDoc;
9305 * Clone the name of the node if any.
9307 if (cur->name == xmlStringText)
9308 clone->name = xmlStringText;
9309 else if (cur->name == xmlStringTextNoenc)
9311 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9312 * in tree.c, it might be set in Libxslt via
9313 * "xsl:disable-output-escaping".
9315 clone->name = xmlStringTextNoenc;
9316 else if (cur->name == xmlStringComment)
9317 clone->name = xmlStringComment;
9318 else if (cur->name != NULL) {
9319 DICT_CONST_COPY(cur->name, clone->name);
9322 switch (cur->type) {
9323 case XML_XINCLUDE_START:
9324 case XML_XINCLUDE_END:
9329 case XML_ELEMENT_NODE:
9333 * Namespace declarations.
9335 if (cur->nsDef != NULL) {
9337 if (destParent && (ctxt == NULL)) {
9339 * Gather @parent's in-scope ns-decls.
9341 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9343 goto internal_error;
9348 * Clone namespace declarations.
9350 cloneNsDefSlot = &(clone->nsDef);
9351 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9353 * Create a new xmlNs.
9355 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
9356 if (cloneNs == NULL) {
9357 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
9358 "allocating namespace");
9361 memset(cloneNs, 0, sizeof(xmlNs));
9362 cloneNs->type = XML_LOCAL_NAMESPACE;
9364 if (ns->href != NULL)
9365 cloneNs->href = xmlStrdup(ns->href);
9366 if (ns->prefix != NULL)
9367 cloneNs->prefix = xmlStrdup(ns->prefix);
9369 *cloneNsDefSlot = cloneNs;
9370 cloneNsDefSlot = &(cloneNs->next);
9373 * Note that for custom handling of ns-references,
9374 * the ns-decls need not be stored in the ns-map,
9375 * since they won't be referenced by node->ns.
9377 if ((ctxt == NULL) ||
9378 (ctxt->getNsForNodeFunc == NULL))
9381 * Does it shadow any ns-decl?
9383 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9384 XML_NSMAP_FOREACH(nsMap, mi) {
9385 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9386 (mi->shadowDepth == -1) &&
9387 ((ns->prefix == mi->newNs->prefix) ||
9388 xmlStrEqual(ns->prefix,
9389 mi->newNs->prefix))) {
9391 * Mark as shadowed at the current
9394 mi->shadowDepth = depth;
9401 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9402 ns, cloneNs, depth) == NULL)
9403 goto internal_error;
9407 /* cur->ns will be processed further down. */
9409 case XML_ATTRIBUTE_NODE:
9410 /* IDs will be processed further down. */
9411 /* cur->ns will be processed further down. */
9414 case XML_CDATA_SECTION_NODE:
9416 * Note that this will also cover the values of attributes.
9418 DICT_COPY(cur->content, clone->content);
9420 case XML_ENTITY_NODE:
9421 /* TODO: What to do here? */
9423 case XML_ENTITY_REF_NODE:
9424 if (sourceDoc != destDoc) {
9425 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9428 * Different doc: Assign new entity-node if available.
9430 ent = xmlGetDocEntity(destDoc, cur->name);
9432 clone->content = ent->content;
9433 clone->children = (xmlNodePtr) ent;
9434 clone->last = (xmlNodePtr) ent;
9439 * Same doc: Use the current node's entity declaration
9442 clone->content = cur->content;
9443 clone->children = cur->children;
9444 clone->last = cur->last;
9448 DICT_COPY(cur->content, clone->content);
9450 case XML_COMMENT_NODE:
9451 DICT_COPY(cur->content, clone->content);
9454 goto internal_error;
9457 if (cur->ns == NULL)
9458 goto end_ns_reference;
9460 /* handle_ns_reference: */
9462 ** The following will take care of references to ns-decls ********
9463 ** and is intended only for element- and attribute-nodes.
9467 if (destParent && (ctxt == NULL)) {
9468 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
9469 goto internal_error;
9474 * Adopt ns-references.
9476 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9478 * Search for a mapping.
9480 XML_NSMAP_FOREACH(nsMap, mi) {
9481 if ((mi->shadowDepth == -1) &&
9482 (cur->ns == mi->oldNs)) {
9484 * This is the nice case: a mapping was found.
9486 clone->ns = mi->newNs;
9487 goto end_ns_reference;
9492 * No matching namespace in scope. We need a new one.
9494 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
9496 * User-defined behaviour.
9498 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9499 cur->ns->href, cur->ns->prefix);
9501 * Add user's mapping.
9503 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9504 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9505 goto internal_error;
9509 * Aquire a normalized ns-decl and add it to the map.
9511 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9512 /* ns-decls on curElem or on destDoc->oldNs */
9513 destParent ? curElem : NULL,
9516 /* if we need to search only in the ancestor-axis */
9518 /* ns-decls must be prefixed for attributes. */
9519 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9520 goto internal_error;
9527 * Some post-processing.
9529 * Handle ID attributes.
9531 if ((clone->type == XML_ATTRIBUTE_NODE) &&
9532 (clone->parent != NULL))
9534 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
9538 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9539 if (idVal != NULL) {
9540 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9541 /* TODO: error message. */
9543 goto internal_error;
9551 ** The following will traverse the tree **************************
9554 * Walk the element's attributes before descending into child-nodes.
9556 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9558 parentClone = clone;
9559 cur = (xmlNodePtr) cur->properties;
9564 * Descend into child-nodes.
9566 if (cur->children != NULL) {
9567 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9569 parentClone = clone;
9570 cur = cur->children;
9577 * At this point we are done with the node, its content
9578 * and an element-nodes's attribute-nodes.
9582 if ((cur->type == XML_ELEMENT_NODE) ||
9583 (cur->type == XML_XINCLUDE_START) ||
9584 (cur->type == XML_XINCLUDE_END)) {
9586 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9588 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9592 while ((nsMap->last != NULL) &&
9593 (nsMap->last->depth >= depth))
9595 XML_NSMAP_POP(nsMap, mi)
9600 XML_NSMAP_FOREACH(nsMap, mi) {
9601 if (mi->shadowDepth >= depth)
9602 mi->shadowDepth = -1;
9607 if (cur->next != NULL) {
9610 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9614 if (clone->parent != NULL)
9615 clone->parent->last = clone;
9616 clone = clone->parent;
9617 parentClone = clone->parent;
9619 * Process parent --> next;
9624 /* This is for attributes only. */
9625 clone = clone->parent;
9626 parentClone = clone->parent;
9628 * Process parent-element --> children.
9643 if (nsMap != NULL) {
9644 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9646 * Just cleanup the map but don't free.
9650 nsMap->last->next = nsMap->pool;
9651 nsMap->pool = nsMap->first;
9652 nsMap->first = NULL;
9655 xmlDOMWrapNsMapFree(nsMap);
9658 * TODO: Should we try a cleanup of the cloned node in case of a
9661 *resNode = resultClone;
9666 * xmlDOMWrapAdoptAttr:
9667 * @ctxt: the optional context for custom processing
9668 * @sourceDoc: the optional source document of attr
9669 * @attr: the attribute-node to be adopted
9670 * @destDoc: the destination doc for adoption
9671 * @destParent: the optional new parent of @attr in @destDoc
9672 * @options: option flags
9674 * @attr is adopted by @destDoc.
9675 * Ensures that ns-references point to @destDoc: either to
9676 * elements->nsDef entries if @destParent is given, or to
9677 * @destDoc->oldNs otherwise.
9679 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9682 xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9683 xmlDocPtr sourceDoc,
9686 xmlNodePtr destParent,
9687 int options ATTRIBUTE_UNUSED)
9692 if ((attr == NULL) || (destDoc == NULL))
9695 attr->doc = destDoc;
9696 if (attr->ns != NULL) {
9700 /* TODO: User defined. */
9702 /* XML Namespace. */
9703 if (IS_STR_XML(attr->ns->prefix)) {
9704 ns = xmlTreeEnsureXMLDecl(destDoc);
9705 } else if (destParent == NULL) {
9707 * Store in @destDoc->oldNs.
9709 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9712 * Declare on @destParent.
9714 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
9716 goto internal_error;
9718 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9719 attr->ns->href, attr->ns->prefix, 1);
9723 goto internal_error;
9727 XML_TREE_ADOPT_STR(attr->name);
9733 if (attr->children == NULL)
9735 cur = attr->children;
9736 while (cur != NULL) {
9738 switch (cur->type) {
9740 case XML_CDATA_SECTION_NODE:
9741 XML_TREE_ADOPT_STR_2(cur->content)
9743 case XML_ENTITY_REF_NODE:
9745 * Remove reference to the entitity-node.
9747 cur->content = NULL;
9748 cur->children = NULL;
9750 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9753 * Assign new entity-node if available.
9755 ent = xmlGetDocEntity(destDoc, cur->name);
9757 cur->content = ent->content;
9758 cur->children = (xmlNodePtr) ent;
9759 cur->last = (xmlNodePtr) ent;
9766 if (cur->children != NULL) {
9767 cur = cur->children;
9771 if (cur == (xmlNodePtr) attr)
9773 if (cur->next != NULL)
9786 * xmlDOMWrapAdoptNode:
9787 * @ctxt: the optional context for custom processing
9788 * @sourceDoc: the optional sourceDoc
9789 * @node: the node to start with
9790 * @destDoc: the destination doc
9791 * @destParent: the optional new parent of @node in @destDoc
9792 * @options: option flags
9794 * References of out-of scope ns-decls are remapped to point to @destDoc:
9795 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9796 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
9797 * This is the case when you have an unliked node and just want to move it
9800 * If @destParent is given, it ensures that the tree is namespace
9801 * wellformed by creating additional ns-decls where needed.
9802 * Note that, since prefixes of already existent ns-decls can be
9803 * shadowed by this process, it could break QNames in attribute
9804 * values or element content.
9805 * NOTE: This function was not intensively tested.
9807 * Returns 0 if the operation succeeded,
9808 * 1 if a node of unsupported type was given,
9809 * 2 if a node of not yet supported type was given and
9810 * -1 on API/internal errors.
9813 xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
9814 xmlDocPtr sourceDoc,
9817 xmlNodePtr destParent,
9820 if ((node == NULL) || (destDoc == NULL) ||
9821 ((destParent != NULL) && (destParent->doc != destDoc)))
9824 * Check node->doc sanity.
9826 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9827 (node->doc != sourceDoc)) {
9829 * Might be an XIncluded node.
9833 if (sourceDoc == NULL)
9834 sourceDoc = node->doc;
9835 if (sourceDoc == destDoc)
9837 switch (node->type) {
9838 case XML_ELEMENT_NODE:
9839 case XML_ATTRIBUTE_NODE:
9841 case XML_CDATA_SECTION_NODE:
9842 case XML_ENTITY_REF_NODE:
9844 case XML_COMMENT_NODE:
9846 case XML_DOCUMENT_FRAG_NODE:
9847 /* TODO: Support document-fragment-nodes. */
9853 * Unlink only if @node was not already added to @destParent.
9855 if ((node->parent != NULL) && (destParent != node->parent))
9856 xmlUnlinkNode(node);
9858 if (node->type == XML_ELEMENT_NODE) {
9859 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
9860 destDoc, destParent, options));
9861 } else if (node->type == XML_ATTRIBUTE_NODE) {
9862 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
9863 (xmlAttrPtr) node, destDoc, destParent, options));
9865 xmlNodePtr cur = node;
9870 * Optimize string adoption.
9872 if ((sourceDoc != NULL) &&
9873 (sourceDoc->dict == destDoc->dict))
9875 switch (node->type) {
9877 case XML_CDATA_SECTION_NODE:
9878 XML_TREE_ADOPT_STR_2(node->content)
9880 case XML_ENTITY_REF_NODE:
9882 * Remove reference to the entitity-node.
9884 node->content = NULL;
9885 node->children = NULL;
9887 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9890 * Assign new entity-node if available.
9892 ent = xmlGetDocEntity(destDoc, node->name);
9894 node->content = ent->content;
9895 node->children = (xmlNodePtr) ent;
9896 node->last = (xmlNodePtr) ent;
9899 XML_TREE_ADOPT_STR(node->name)
9902 XML_TREE_ADOPT_STR(node->name)
9903 XML_TREE_ADOPT_STR_2(node->content)
9914 #include "elfgcchack.h"