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")))
728 * Allocate a new Namespace and fill the fields.
730 cur
= (xmlNsPtr
) xmlMalloc(sizeof(xmlNs
));
732 xmlTreeErrMemory("building namespace");
735 memset(cur
, 0, sizeof(xmlNs
));
736 cur
->type
= XML_LOCAL_NAMESPACE
;
739 cur
->href
= xmlStrdup(href
);
741 cur
->prefix
= xmlStrdup(prefix
);
744 * Add it at the end to preserve parsing order ...
745 * and checks for existing use of the prefix
748 if (node
->nsDef
== NULL
) {
751 xmlNsPtr prev
= node
->nsDef
;
753 if (((prev
->prefix
== NULL
) && (cur
->prefix
== NULL
)) ||
754 (xmlStrEqual(prev
->prefix
, cur
->prefix
))) {
758 while (prev
->next
!= NULL
) {
760 if (((prev
->prefix
== NULL
) && (cur
->prefix
== NULL
)) ||
761 (xmlStrEqual(prev
->prefix
, cur
->prefix
))) {
774 * @node: a node in the document
775 * @ns: a namespace pointer
777 * Associate a namespace to a node, a posteriori.
780 xmlSetNs(xmlNodePtr node
, xmlNsPtr ns
) {
783 xmlGenericError(xmlGenericErrorContext
,
784 "xmlSetNs: node == NULL\n");
793 * @cur: the namespace pointer
795 * Free up the structures associated to a namespace
798 xmlFreeNs(xmlNsPtr cur
) {
801 xmlGenericError(xmlGenericErrorContext
,
802 "xmlFreeNs : ns == NULL\n");
806 if (cur
->href
!= NULL
) xmlFree((char *) cur
->href
);
807 if (cur
->prefix
!= NULL
) xmlFree((char *) cur
->prefix
);
813 * @cur: the first namespace pointer
815 * Free up all the structures associated to the chained namespaces.
818 xmlFreeNsList(xmlNsPtr cur
) {
822 xmlGenericError(xmlGenericErrorContext
,
823 "xmlFreeNsList : ns == NULL\n");
827 while (cur
!= NULL
) {
836 * @doc: the document pointer
837 * @name: the DTD name
838 * @ExternalID: the external ID
839 * @SystemID: the system ID
841 * Creation of a new DTD for the external subset. To create an
842 * internal subset, use xmlCreateIntSubset().
844 * Returns a pointer to the new DTD structure
847 xmlNewDtd(xmlDocPtr doc
, const xmlChar
*name
,
848 const xmlChar
*ExternalID
, const xmlChar
*SystemID
) {
851 if ((doc
!= NULL
) && (doc
->extSubset
!= NULL
)) {
853 xmlGenericError(xmlGenericErrorContext
,
854 "xmlNewDtd(%s): document %s already have a DTD %s\n",
855 /* !!! */ (char *) name
, doc
->name
,
856 /* !!! */ (char *)doc
->extSubset
->name
);
862 * Allocate a new DTD and fill the fields.
864 cur
= (xmlDtdPtr
) xmlMalloc(sizeof(xmlDtd
));
866 xmlTreeErrMemory("building DTD");
869 memset(cur
, 0 , sizeof(xmlDtd
));
870 cur
->type
= XML_DTD_NODE
;
873 cur
->name
= xmlStrdup(name
);
874 if (ExternalID
!= NULL
)
875 cur
->ExternalID
= xmlStrdup(ExternalID
);
876 if (SystemID
!= NULL
)
877 cur
->SystemID
= xmlStrdup(SystemID
);
879 doc
->extSubset
= cur
;
882 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
883 xmlRegisterNodeDefaultValue((xmlNodePtr
)cur
);
889 * @doc: the document pointer
891 * Get the internal subset of a document
892 * Returns a pointer to the DTD structure or NULL if not found
896 xmlGetIntSubset(xmlDocPtr doc
) {
902 while (cur
!= NULL
) {
903 if (cur
->type
== XML_DTD_NODE
)
904 return((xmlDtdPtr
) cur
);
907 return((xmlDtdPtr
) doc
->intSubset
);
911 * xmlCreateIntSubset:
912 * @doc: the document pointer
913 * @name: the DTD name
914 * @ExternalID: the external (PUBLIC) ID
915 * @SystemID: the system ID
917 * Create the internal subset of a document
918 * Returns a pointer to the new DTD structure
921 xmlCreateIntSubset(xmlDocPtr doc
, const xmlChar
*name
,
922 const xmlChar
*ExternalID
, const xmlChar
*SystemID
) {
925 if ((doc
!= NULL
) && (xmlGetIntSubset(doc
) != NULL
)) {
927 xmlGenericError(xmlGenericErrorContext
,
929 "xmlCreateIntSubset(): document %s already have an internal subset\n",
936 * Allocate a new DTD and fill the fields.
938 cur
= (xmlDtdPtr
) xmlMalloc(sizeof(xmlDtd
));
940 xmlTreeErrMemory("building internal subset");
943 memset(cur
, 0, sizeof(xmlDtd
));
944 cur
->type
= XML_DTD_NODE
;
947 cur
->name
= xmlStrdup(name
);
948 if (cur
->name
== NULL
) {
949 xmlTreeErrMemory("building internal subset");
954 if (ExternalID
!= NULL
) {
955 cur
->ExternalID
= xmlStrdup(ExternalID
);
956 if (cur
->ExternalID
== NULL
) {
957 xmlTreeErrMemory("building internal subset");
958 if (cur
->name
!= NULL
)
959 xmlFree((char *)cur
->name
);
964 if (SystemID
!= NULL
) {
965 cur
->SystemID
= xmlStrdup(SystemID
);
966 if (cur
->SystemID
== NULL
) {
967 xmlTreeErrMemory("building internal subset");
968 if (cur
->name
!= NULL
)
969 xmlFree((char *)cur
->name
);
970 if (cur
->ExternalID
!= NULL
)
971 xmlFree((char *)cur
->ExternalID
);
977 doc
->intSubset
= cur
;
980 if (doc
->children
== NULL
) {
981 doc
->children
= (xmlNodePtr
) cur
;
982 doc
->last
= (xmlNodePtr
) cur
;
984 if (doc
->type
== XML_HTML_DOCUMENT_NODE
) {
987 prev
= doc
->children
;
988 prev
->prev
= (xmlNodePtr
) cur
;
990 doc
->children
= (xmlNodePtr
) cur
;
994 next
= doc
->children
;
995 while ((next
!= NULL
) && (next
->type
!= XML_ELEMENT_NODE
))
998 cur
->prev
= doc
->last
;
999 cur
->prev
->next
= (xmlNodePtr
) cur
;
1001 doc
->last
= (xmlNodePtr
) cur
;
1004 cur
->prev
= next
->prev
;
1005 if (cur
->prev
== NULL
)
1006 doc
->children
= (xmlNodePtr
) cur
;
1008 cur
->prev
->next
= (xmlNodePtr
) cur
;
1009 next
->prev
= (xmlNodePtr
) cur
;
1015 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
1016 xmlRegisterNodeDefaultValue((xmlNodePtr
)cur
);
1024 * Free a string if it is not owned by the "dict" dictionnary in the
1027 #define DICT_FREE(str) \
1028 if ((str) && ((!dict) || \
1029 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1030 xmlFree((char *)(str));
1037 * Copy a string using a "dict" dictionnary in the current scope,
1040 #define DICT_COPY(str, cpy) \
1043 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1044 cpy = (xmlChar *) (str); \
1046 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1048 cpy = xmlStrdup((const xmlChar *)(str)); }
1054 * Copy a string using a "dict" dictionnary in the current scope,
1057 #define DICT_CONST_COPY(str, cpy) \
1060 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1061 cpy = (const xmlChar *) (str); \
1063 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1065 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1070 * @cur: the DTD structure to free up
1072 * Free a DTD structure.
1075 xmlFreeDtd(xmlDtdPtr cur
) {
1076 xmlDictPtr dict
= NULL
;
1081 if (cur
->doc
!= NULL
) dict
= cur
->doc
->dict
;
1083 if ((__xmlRegisterCallbacks
) && (xmlDeregisterNodeDefaultValue
))
1084 xmlDeregisterNodeDefaultValue((xmlNodePtr
)cur
);
1086 if (cur
->children
!= NULL
) {
1087 xmlNodePtr next
, c
= cur
->children
;
1090 * Cleanup all nodes which are not part of the specific lists
1091 * of notations, elements, attributes and entities.
1095 if ((c
->type
!= XML_NOTATION_NODE
) &&
1096 (c
->type
!= XML_ELEMENT_DECL
) &&
1097 (c
->type
!= XML_ATTRIBUTE_DECL
) &&
1098 (c
->type
!= XML_ENTITY_DECL
)) {
1105 DICT_FREE(cur
->name
)
1106 DICT_FREE(cur
->SystemID
)
1107 DICT_FREE(cur
->ExternalID
)
1109 if (cur
->notations
!= NULL
)
1110 xmlFreeNotationTable((xmlNotationTablePtr
) cur
->notations
);
1112 if (cur
->elements
!= NULL
)
1113 xmlFreeElementTable((xmlElementTablePtr
) cur
->elements
);
1114 if (cur
->attributes
!= NULL
)
1115 xmlFreeAttributeTable((xmlAttributeTablePtr
) cur
->attributes
);
1116 if (cur
->entities
!= NULL
)
1117 xmlFreeEntitiesTable((xmlEntitiesTablePtr
) cur
->entities
);
1118 if (cur
->pentities
!= NULL
)
1119 xmlFreeEntitiesTable((xmlEntitiesTablePtr
) cur
->pentities
);
1126 * @version: xmlChar string giving the version of XML "1.0"
1128 * Creates a new XML document
1130 * Returns a new document
1133 xmlNewDoc(const xmlChar
*version
) {
1136 if (version
== NULL
)
1137 version
= (const xmlChar
*) "1.0";
1140 * Allocate a new document and fill the fields.
1142 cur
= (xmlDocPtr
) xmlMalloc(sizeof(xmlDoc
));
1144 xmlTreeErrMemory("building doc");
1147 memset(cur
, 0, sizeof(xmlDoc
));
1148 cur
->type
= XML_DOCUMENT_NODE
;
1150 cur
->version
= xmlStrdup(version
);
1151 if (cur
->version
== NULL
) {
1152 xmlTreeErrMemory("building doc");
1156 cur
->standalone
= -1;
1157 cur
->compression
= -1; /* not initialized */
1159 cur
->parseFlags
= 0;
1160 cur
->properties
= XML_DOC_USERBUILT
;
1162 * The in memory encoding is always UTF8
1163 * This field will never change and would
1164 * be obsolete if not for binary compatibility.
1166 cur
->charset
= XML_CHAR_ENCODING_UTF8
;
1168 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
1169 xmlRegisterNodeDefaultValue((xmlNodePtr
)cur
);
1175 * @cur: pointer to the document
1177 * Free up all the structures used by a document, tree included.
1180 xmlFreeDoc(xmlDocPtr cur
) {
1181 xmlDtdPtr extSubset
, intSubset
;
1182 xmlDictPtr dict
= NULL
;
1186 xmlGenericError(xmlGenericErrorContext
,
1187 "xmlFreeDoc : document == NULL\n");
1191 #ifdef LIBXML_DEBUG_RUNTIME
1192 #ifdef LIBXML_DEBUG_ENABLED
1193 xmlDebugCheckDocument(stderr
, cur
);
1197 if (cur
!= NULL
) dict
= cur
->dict
;
1199 if ((__xmlRegisterCallbacks
) && (xmlDeregisterNodeDefaultValue
))
1200 xmlDeregisterNodeDefaultValue((xmlNodePtr
)cur
);
1203 * Do this before freeing the children list to avoid ID lookups
1205 if (cur
->ids
!= NULL
) xmlFreeIDTable((xmlIDTablePtr
) cur
->ids
);
1207 if (cur
->refs
!= NULL
) xmlFreeRefTable((xmlRefTablePtr
) cur
->refs
);
1209 extSubset
= cur
->extSubset
;
1210 intSubset
= cur
->intSubset
;
1211 if (intSubset
== extSubset
)
1213 if (extSubset
!= NULL
) {
1214 xmlUnlinkNode((xmlNodePtr
) cur
->extSubset
);
1215 cur
->extSubset
= NULL
;
1216 xmlFreeDtd(extSubset
);
1218 if (intSubset
!= NULL
) {
1219 xmlUnlinkNode((xmlNodePtr
) cur
->intSubset
);
1220 cur
->intSubset
= NULL
;
1221 xmlFreeDtd(intSubset
);
1224 if (cur
->children
!= NULL
) xmlFreeNodeList(cur
->children
);
1225 if (cur
->oldNs
!= NULL
) xmlFreeNsList(cur
->oldNs
);
1227 DICT_FREE(cur
->version
)
1228 DICT_FREE(cur
->name
)
1229 DICT_FREE(cur
->encoding
)
1232 if (dict
) xmlDictFree(dict
);
1236 * xmlStringLenGetNodeList:
1237 * @doc: the document
1238 * @value: the value of the text
1239 * @len: the length of the string value
1241 * Parse the value string and build the node list associated. Should
1242 * produce a flat tree with only TEXTs and ENTITY_REFs.
1243 * Returns a pointer to the first child
1246 xmlStringLenGetNodeList(xmlDocPtr doc
, const xmlChar
*value
, int len
) {
1247 xmlNodePtr ret
= NULL
, last
= NULL
;
1250 const xmlChar
*cur
= value
, *end
= cur
+ len
;
1254 if (value
== NULL
) return(NULL
);
1257 while ((cur
< end
) && (*cur
!= 0)) {
1258 if (cur
[0] == '&') {
1263 * Save the current text.
1266 if ((last
!= NULL
) && (last
->type
== XML_TEXT_NODE
)) {
1267 xmlNodeAddContentLen(last
, q
, cur
- q
);
1269 node
= xmlNewDocTextLen(doc
, q
, cur
- q
);
1270 if (node
== NULL
) return(ret
);
1281 if ((cur
+ 2 < end
) && (cur
[1] == '#') && (cur
[2] == 'x')) {
1287 while (tmp
!= ';') { /* Non input consuming loop */
1288 if ((tmp
>= '0') && (tmp
<= '9'))
1289 charval
= charval
* 16 + (tmp
- '0');
1290 else if ((tmp
>= 'a') && (tmp
<= 'f'))
1291 charval
= charval
* 16 + (tmp
- 'a') + 10;
1292 else if ((tmp
>= 'A') && (tmp
<= 'F'))
1293 charval
= charval
* 16 + (tmp
- 'A') + 10;
1295 xmlTreeErr(XML_TREE_INVALID_HEX
, (xmlNodePtr
) doc
,
1309 } else if ((cur
+ 1 < end
) && (cur
[1] == '#')) {
1315 while (tmp
!= ';') { /* Non input consuming loops */
1316 if ((tmp
>= '0') && (tmp
<= '9'))
1317 charval
= charval
* 10 + (tmp
- '0');
1319 xmlTreeErr(XML_TREE_INVALID_DEC
, (xmlNodePtr
) doc
,
1335 * Read the entity string
1339 while ((cur
< end
) && (*cur
!= 0) && (*cur
!= ';')) cur
++;
1340 if ((cur
>= end
) || (*cur
== 0)) {
1341 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY
, (xmlNodePtr
) doc
,
1347 * Predefined entities don't generate nodes
1349 val
= xmlStrndup(q
, cur
- q
);
1350 ent
= xmlGetDocEntity(doc
, val
);
1351 if ((ent
!= NULL
) &&
1352 (ent
->etype
== XML_INTERNAL_PREDEFINED_ENTITY
)) {
1354 node
= xmlNewDocText(doc
, ent
->content
);
1356 } else if (last
->type
!= XML_TEXT_NODE
) {
1357 node
= xmlNewDocText(doc
, ent
->content
);
1358 last
= xmlAddNextSibling(last
, node
);
1360 xmlNodeAddContent(last
, ent
->content
);
1364 * Create a new REFERENCE_REF node
1366 node
= xmlNewReference(doc
, val
);
1368 if (val
!= NULL
) xmlFree(val
);
1371 else if ((ent
!= NULL
) && (ent
->children
== NULL
)) {
1374 ent
->children
= xmlStringGetNodeList(doc
,
1375 (const xmlChar
*)node
->content
);
1377 temp
= ent
->children
;
1379 temp
->parent
= (xmlNodePtr
)ent
;
1387 last
= xmlAddNextSibling(last
, node
);
1399 l
= xmlCopyCharMultiByte(buf
, charval
);
1401 node
= xmlNewDocText(doc
, buf
);
1406 last
= xmlAddNextSibling(last
, node
);
1414 if ((cur
!= q
) || (ret
== NULL
)) {
1416 * Handle the last piece of text.
1418 if ((last
!= NULL
) && (last
->type
== XML_TEXT_NODE
)) {
1419 xmlNodeAddContentLen(last
, q
, cur
- q
);
1421 node
= xmlNewDocTextLen(doc
, q
, cur
- q
);
1422 if (node
== NULL
) return(ret
);
1426 xmlAddNextSibling(last
, node
);
1434 * xmlStringGetNodeList:
1435 * @doc: the document
1436 * @value: the value of the attribute
1438 * Parse the value string and build the node list associated. Should
1439 * produce a flat tree with only TEXTs and ENTITY_REFs.
1440 * Returns a pointer to the first child
1443 xmlStringGetNodeList(xmlDocPtr doc
, const xmlChar
*value
) {
1444 xmlNodePtr ret
= NULL
, last
= NULL
;
1447 const xmlChar
*cur
= value
;
1451 if (value
== NULL
) return(NULL
);
1455 if (cur
[0] == '&') {
1460 * Save the current text.
1463 if ((last
!= NULL
) && (last
->type
== XML_TEXT_NODE
)) {
1464 xmlNodeAddContentLen(last
, q
, cur
- q
);
1466 node
= xmlNewDocTextLen(doc
, q
, cur
- q
);
1467 if (node
== NULL
) return(ret
);
1478 if ((cur
[1] == '#') && (cur
[2] == 'x')) {
1481 while (tmp
!= ';') { /* Non input consuming loop */
1482 if ((tmp
>= '0') && (tmp
<= '9'))
1483 charval
= charval
* 16 + (tmp
- '0');
1484 else if ((tmp
>= 'a') && (tmp
<= 'f'))
1485 charval
= charval
* 16 + (tmp
- 'a') + 10;
1486 else if ((tmp
>= 'A') && (tmp
<= 'F'))
1487 charval
= charval
* 16 + (tmp
- 'A') + 10;
1489 xmlTreeErr(XML_TREE_INVALID_HEX
, (xmlNodePtr
) doc
,
1500 } else if (cur
[1] == '#') {
1503 while (tmp
!= ';') { /* Non input consuming loops */
1504 if ((tmp
>= '0') && (tmp
<= '9'))
1505 charval
= charval
* 10 + (tmp
- '0');
1507 xmlTreeErr(XML_TREE_INVALID_DEC
, (xmlNodePtr
) doc
,
1520 * Read the entity string
1524 while ((*cur
!= 0) && (*cur
!= ';')) cur
++;
1526 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY
,
1527 (xmlNodePtr
) doc
, (const char *) q
);
1532 * Predefined entities don't generate nodes
1534 val
= xmlStrndup(q
, cur
- q
);
1535 ent
= xmlGetDocEntity(doc
, val
);
1536 if ((ent
!= NULL
) &&
1537 (ent
->etype
== XML_INTERNAL_PREDEFINED_ENTITY
)) {
1539 node
= xmlNewDocText(doc
, ent
->content
);
1541 } else if (last
->type
!= XML_TEXT_NODE
) {
1542 node
= xmlNewDocText(doc
, ent
->content
);
1543 last
= xmlAddNextSibling(last
, node
);
1545 xmlNodeAddContent(last
, ent
->content
);
1549 * Create a new REFERENCE_REF node
1551 node
= xmlNewReference(doc
, val
);
1553 if (val
!= NULL
) xmlFree(val
);
1556 else if ((ent
!= NULL
) && (ent
->children
== NULL
)) {
1559 ent
->children
= xmlStringGetNodeList(doc
,
1560 (const xmlChar
*)node
->content
);
1562 temp
= ent
->children
;
1564 temp
->parent
= (xmlNodePtr
)ent
;
1571 last
= xmlAddNextSibling(last
, node
);
1583 len
= xmlCopyCharMultiByte(buf
, charval
);
1585 node
= xmlNewDocText(doc
, buf
);
1590 last
= xmlAddNextSibling(last
, node
);
1597 if ((cur
!= q
) || (ret
== NULL
)) {
1599 * Handle the last piece of text.
1601 if ((last
!= NULL
) && (last
->type
== XML_TEXT_NODE
)) {
1602 xmlNodeAddContentLen(last
, q
, cur
- q
);
1604 node
= xmlNewDocTextLen(doc
, q
, cur
- q
);
1605 if (node
== NULL
) return(ret
);
1609 last
= xmlAddNextSibling(last
, node
);
1617 * xmlNodeListGetString:
1618 * @doc: the document
1619 * @list: a Node list
1620 * @inLine: should we replace entity contents or show their external form
1622 * Build the string equivalent to the text contained in the Node list
1623 * made of TEXTs and ENTITY_REFs
1625 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1628 xmlNodeListGetString(xmlDocPtr doc
, xmlNodePtr list
, int inLine
)
1630 xmlNodePtr node
= list
;
1631 xmlChar
*ret
= NULL
;
1637 while (node
!= NULL
) {
1638 if ((node
->type
== XML_TEXT_NODE
) ||
1639 (node
->type
== XML_CDATA_SECTION_NODE
)) {
1641 ret
= xmlStrcat(ret
, node
->content
);
1645 buffer
= xmlEncodeEntitiesReentrant(doc
, node
->content
);
1646 if (buffer
!= NULL
) {
1647 ret
= xmlStrcat(ret
, buffer
);
1651 } else if (node
->type
== XML_ENTITY_REF_NODE
) {
1653 ent
= xmlGetDocEntity(doc
, node
->name
);
1657 /* an entity content can be any "well balanced chunk",
1658 * i.e. the result of the content [43] production:
1659 * http://www.w3.org/TR/REC-xml#NT-content.
1660 * So it can contain text, CDATA section or nested
1661 * entity reference nodes (among others).
1662 * -> we recursive call xmlNodeListGetString()
1663 * which handles these types */
1664 buffer
= xmlNodeListGetString(doc
, ent
->children
, 1);
1665 if (buffer
!= NULL
) {
1666 ret
= xmlStrcat(ret
, buffer
);
1670 ret
= xmlStrcat(ret
, node
->content
);
1677 ret
= xmlStrncat(ret
, buf
, 1);
1678 ret
= xmlStrcat(ret
, node
->name
);
1681 ret
= xmlStrncat(ret
, buf
, 1);
1686 xmlGenericError(xmlGenericErrorContext
,
1687 "xmlGetNodeListString : invalid node type %d\n",
1696 #ifdef LIBXML_TREE_ENABLED
1698 * xmlNodeListGetRawString:
1699 * @doc: the document
1700 * @list: a Node list
1701 * @inLine: should we replace entity contents or show their external form
1703 * Builds the string equivalent to the text contained in the Node list
1704 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1705 * this function doesn't do any character encoding handling.
1707 * Returns a pointer to the string copy, the caller must free it with xmlFree().
1710 xmlNodeListGetRawString(xmlDocPtr doc
, xmlNodePtr list
, int inLine
)
1712 xmlNodePtr node
= list
;
1713 xmlChar
*ret
= NULL
;
1719 while (node
!= NULL
) {
1720 if ((node
->type
== XML_TEXT_NODE
) ||
1721 (node
->type
== XML_CDATA_SECTION_NODE
)) {
1723 ret
= xmlStrcat(ret
, node
->content
);
1727 buffer
= xmlEncodeSpecialChars(doc
, node
->content
);
1728 if (buffer
!= NULL
) {
1729 ret
= xmlStrcat(ret
, buffer
);
1733 } else if (node
->type
== XML_ENTITY_REF_NODE
) {
1735 ent
= xmlGetDocEntity(doc
, node
->name
);
1739 /* an entity content can be any "well balanced chunk",
1740 * i.e. the result of the content [43] production:
1741 * http://www.w3.org/TR/REC-xml#NT-content.
1742 * So it can contain text, CDATA section or nested
1743 * entity reference nodes (among others).
1744 * -> we recursive call xmlNodeListGetRawString()
1745 * which handles these types */
1747 xmlNodeListGetRawString(doc
, ent
->children
, 1);
1748 if (buffer
!= NULL
) {
1749 ret
= xmlStrcat(ret
, buffer
);
1753 ret
= xmlStrcat(ret
, node
->content
);
1760 ret
= xmlStrncat(ret
, buf
, 1);
1761 ret
= xmlStrcat(ret
, node
->name
);
1764 ret
= xmlStrncat(ret
, buf
, 1);
1769 xmlGenericError(xmlGenericErrorContext
,
1770 "xmlGetNodeListString : invalid node type %d\n",
1778 #endif /* LIBXML_TREE_ENABLED */
1781 xmlNewPropInternal(xmlNodePtr node
, xmlNsPtr ns
,
1782 const xmlChar
* name
, const xmlChar
* value
,
1786 xmlDocPtr doc
= NULL
;
1788 if ((node
!= NULL
) && (node
->type
!= XML_ELEMENT_NODE
)) {
1789 if ((eatname
== 1) &&
1790 ((node
->doc
== NULL
) ||
1791 (!(xmlDictOwns(node
->doc
->dict
, name
)))))
1792 xmlFree((xmlChar
*) name
);
1797 * Allocate a new property and fill the fields.
1799 cur
= (xmlAttrPtr
) xmlMalloc(sizeof(xmlAttr
));
1801 if ((eatname
== 1) &&
1802 ((node
== NULL
) || (node
->doc
== NULL
) ||
1803 (!(xmlDictOwns(node
->doc
->dict
, name
)))))
1804 xmlFree((xmlChar
*) name
);
1805 xmlTreeErrMemory("building attribute");
1808 memset(cur
, 0, sizeof(xmlAttr
));
1809 cur
->type
= XML_ATTRIBUTE_NODE
;
1819 if ((doc
!= NULL
) && (doc
->dict
!= NULL
))
1820 cur
->name
= (xmlChar
*) xmlDictLookup(doc
->dict
, name
, -1);
1822 cur
->name
= xmlStrdup(name
);
1826 if (value
!= NULL
) {
1829 if(!xmlCheckUTF8(value
)) {
1830 xmlTreeErr(XML_TREE_NOT_UTF8
, (xmlNodePtr
) doc
,
1833 doc
->encoding
= xmlStrdup(BAD_CAST
"ISO-8859-1");
1835 cur
->children
= xmlNewDocText(doc
, value
);
1837 tmp
= cur
->children
;
1838 while (tmp
!= NULL
) {
1839 tmp
->parent
= (xmlNodePtr
) cur
;
1840 if (tmp
->next
== NULL
)
1847 * Add it at the end to preserve parsing order ...
1850 if (node
->properties
== NULL
) {
1851 node
->properties
= cur
;
1853 xmlAttrPtr prev
= node
->properties
;
1855 while (prev
->next
!= NULL
)
1862 if ((value
!= NULL
) && (node
!= NULL
) &&
1863 (xmlIsID(node
->doc
, node
, cur
) == 1))
1864 xmlAddID(NULL
, node
->doc
, value
, cur
);
1866 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
1867 xmlRegisterNodeDefaultValue((xmlNodePtr
) cur
);
1871 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1872 defined(LIBXML_SCHEMAS_ENABLED)
1875 * @node: the holding node
1876 * @name: the name of the attribute
1877 * @value: the value of the attribute
1879 * Create a new property carried by a node.
1880 * Returns a pointer to the attribute
1883 xmlNewProp(xmlNodePtr node
, const xmlChar
*name
, const xmlChar
*value
) {
1887 xmlGenericError(xmlGenericErrorContext
,
1888 "xmlNewProp : name == NULL\n");
1893 return xmlNewPropInternal(node
, NULL
, name
, value
, 0);
1895 #endif /* LIBXML_TREE_ENABLED */
1899 * @node: the holding node
1900 * @ns: the namespace
1901 * @name: the name of the attribute
1902 * @value: the value of the attribute
1904 * Create a new property tagged with a namespace and carried by a node.
1905 * Returns a pointer to the attribute
1908 xmlNewNsProp(xmlNodePtr node
, xmlNsPtr ns
, const xmlChar
*name
,
1909 const xmlChar
*value
) {
1913 xmlGenericError(xmlGenericErrorContext
,
1914 "xmlNewNsProp : name == NULL\n");
1919 return xmlNewPropInternal(node
, ns
, name
, value
, 0);
1923 * xmlNewNsPropEatName:
1924 * @node: the holding node
1925 * @ns: the namespace
1926 * @name: the name of the attribute
1927 * @value: the value of the attribute
1929 * Create a new property tagged with a namespace and carried by a node.
1930 * Returns a pointer to the attribute
1933 xmlNewNsPropEatName(xmlNodePtr node
, xmlNsPtr ns
, xmlChar
*name
,
1934 const xmlChar
*value
) {
1938 xmlGenericError(xmlGenericErrorContext
,
1939 "xmlNewNsPropEatName : name == NULL\n");
1944 return xmlNewPropInternal(node
, ns
, name
, value
, 1);
1949 * @doc: the document
1950 * @name: the name of the attribute
1951 * @value: the value of the attribute
1953 * Create a new property carried by a document.
1954 * Returns a pointer to the attribute
1957 xmlNewDocProp(xmlDocPtr doc
, const xmlChar
*name
, const xmlChar
*value
) {
1962 xmlGenericError(xmlGenericErrorContext
,
1963 "xmlNewDocProp : name == NULL\n");
1969 * Allocate a new property and fill the fields.
1971 cur
= (xmlAttrPtr
) xmlMalloc(sizeof(xmlAttr
));
1973 xmlTreeErrMemory("building attribute");
1976 memset(cur
, 0, sizeof(xmlAttr
));
1977 cur
->type
= XML_ATTRIBUTE_NODE
;
1979 if ((doc
!= NULL
) && (doc
->dict
!= NULL
))
1980 cur
->name
= xmlDictLookup(doc
->dict
, name
, -1);
1982 cur
->name
= xmlStrdup(name
);
1984 if (value
!= NULL
) {
1987 cur
->children
= xmlStringGetNodeList(doc
, value
);
1990 tmp
= cur
->children
;
1991 while (tmp
!= NULL
) {
1992 tmp
->parent
= (xmlNodePtr
) cur
;
1993 if (tmp
->next
== NULL
)
1999 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
2000 xmlRegisterNodeDefaultValue((xmlNodePtr
)cur
);
2006 * @cur: the first property in the list
2008 * Free a property and all its siblings, all the children are freed too.
2011 xmlFreePropList(xmlAttrPtr cur
) {
2013 if (cur
== NULL
) return;
2014 while (cur
!= NULL
) {
2023 * @cur: an attribute
2025 * Free one attribute, all the content is freed too
2028 xmlFreeProp(xmlAttrPtr cur
) {
2029 xmlDictPtr dict
= NULL
;
2030 if (cur
== NULL
) return;
2032 if (cur
->doc
!= NULL
) dict
= cur
->doc
->dict
;
2034 if ((__xmlRegisterCallbacks
) && (xmlDeregisterNodeDefaultValue
))
2035 xmlDeregisterNodeDefaultValue((xmlNodePtr
)cur
);
2037 /* Check for ID removal -> leading to invalid references ! */
2038 if ((cur
->doc
!= NULL
) && (cur
->atype
== XML_ATTRIBUTE_ID
)) {
2039 xmlRemoveID(cur
->doc
, cur
);
2041 if (cur
->children
!= NULL
) xmlFreeNodeList(cur
->children
);
2042 DICT_FREE(cur
->name
)
2048 * @cur: an attribute
2050 * Unlink and free one attribute, all the content is freed too
2051 * Note this doesn't work for namespace definition attributes
2053 * Returns 0 if success and -1 in case of error.
2056 xmlRemoveProp(xmlAttrPtr cur
) {
2060 xmlGenericError(xmlGenericErrorContext
,
2061 "xmlRemoveProp : cur == NULL\n");
2065 if (cur
->parent
== NULL
) {
2067 xmlGenericError(xmlGenericErrorContext
,
2068 "xmlRemoveProp : cur->parent == NULL\n");
2072 tmp
= cur
->parent
->properties
;
2074 cur
->parent
->properties
= cur
->next
;
2075 if (cur
->next
!= NULL
)
2076 cur
->next
->prev
= NULL
;
2080 while (tmp
!= NULL
) {
2081 if (tmp
->next
== cur
) {
2082 tmp
->next
= cur
->next
;
2083 if (tmp
->next
!= NULL
)
2084 tmp
->next
->prev
= tmp
;
2091 xmlGenericError(xmlGenericErrorContext
,
2092 "xmlRemoveProp : attribute not owned by its node\n");
2099 * @doc: the target document
2100 * @name: the processing instruction name
2101 * @content: the PI content
2103 * Creation of a processing instruction element.
2104 * Returns a pointer to the new node object.
2107 xmlNewDocPI(xmlDocPtr doc
, const xmlChar
*name
, const xmlChar
*content
) {
2112 xmlGenericError(xmlGenericErrorContext
,
2113 "xmlNewPI : name == NULL\n");
2119 * Allocate a new node and fill the fields.
2121 cur
= (xmlNodePtr
) xmlMalloc(sizeof(xmlNode
));
2123 xmlTreeErrMemory("building PI");
2126 memset(cur
, 0, sizeof(xmlNode
));
2127 cur
->type
= XML_PI_NODE
;
2129 if ((doc
!= NULL
) && (doc
->dict
!= NULL
))
2130 cur
->name
= xmlDictLookup(doc
->dict
, name
, -1);
2132 cur
->name
= xmlStrdup(name
);
2133 if (content
!= NULL
) {
2134 cur
->content
= xmlStrdup(content
);
2138 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
2139 xmlRegisterNodeDefaultValue((xmlNodePtr
)cur
);
2145 * @name: the processing instruction name
2146 * @content: the PI content
2148 * Creation of a processing instruction element.
2149 * Use xmlDocNewPI preferably to get string interning
2151 * Returns a pointer to the new node object.
2154 xmlNewPI(const xmlChar
*name
, const xmlChar
*content
) {
2155 return(xmlNewDocPI(NULL
, name
, content
));
2160 * @ns: namespace if any
2161 * @name: the node name
2163 * Creation of a new node element. @ns is optional (NULL).
2165 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2169 xmlNewNode(xmlNsPtr ns
, const xmlChar
*name
) {
2174 xmlGenericError(xmlGenericErrorContext
,
2175 "xmlNewNode : name == NULL\n");
2181 * Allocate a new node and fill the fields.
2183 cur
= (xmlNodePtr
) xmlMalloc(sizeof(xmlNode
));
2185 xmlTreeErrMemory("building node");
2188 memset(cur
, 0, sizeof(xmlNode
));
2189 cur
->type
= XML_ELEMENT_NODE
;
2191 cur
->name
= xmlStrdup(name
);
2194 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
2195 xmlRegisterNodeDefaultValue(cur
);
2200 * xmlNewNodeEatName:
2201 * @ns: namespace if any
2202 * @name: the node name
2204 * Creation of a new node element. @ns is optional (NULL).
2206 * Returns a pointer to the new node object, with pointer @name as
2207 * new node's name. Use xmlNewNode() if a copy of @name string is
2208 * is needed as new node's name.
2211 xmlNewNodeEatName(xmlNsPtr ns
, xmlChar
*name
) {
2216 xmlGenericError(xmlGenericErrorContext
,
2217 "xmlNewNode : name == NULL\n");
2223 * Allocate a new node and fill the fields.
2225 cur
= (xmlNodePtr
) xmlMalloc(sizeof(xmlNode
));
2227 xmlTreeErrMemory("building node");
2228 /* we can't check here that name comes from the doc dictionnary */
2231 memset(cur
, 0, sizeof(xmlNode
));
2232 cur
->type
= XML_ELEMENT_NODE
;
2237 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
2238 xmlRegisterNodeDefaultValue((xmlNodePtr
)cur
);
2244 * @doc: the document
2245 * @ns: namespace if any
2246 * @name: the node name
2247 * @content: the XML text content if any
2249 * Creation of a new node element within a document. @ns and @content
2250 * are optional (NULL).
2251 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2252 * references, but XML special chars need to be escaped first by using
2253 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2254 * need entities support.
2256 * Returns a pointer to the new node object.
2259 xmlNewDocNode(xmlDocPtr doc
, xmlNsPtr ns
,
2260 const xmlChar
*name
, const xmlChar
*content
) {
2263 if ((doc
!= NULL
) && (doc
->dict
!= NULL
))
2264 cur
= xmlNewNodeEatName(ns
, (xmlChar
*)
2265 xmlDictLookup(doc
->dict
, name
, -1));
2267 cur
= xmlNewNode(ns
, name
);
2270 if (content
!= NULL
) {
2271 cur
->children
= xmlStringGetNodeList(doc
, content
);
2272 UPDATE_LAST_CHILD_AND_PARENT(cur
)
2280 * xmlNewDocNodeEatName:
2281 * @doc: the document
2282 * @ns: namespace if any
2283 * @name: the node name
2284 * @content: the XML text content if any
2286 * Creation of a new node element within a document. @ns and @content
2287 * are optional (NULL).
2288 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2289 * references, but XML special chars need to be escaped first by using
2290 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2291 * need entities support.
2293 * Returns a pointer to the new node object.
2296 xmlNewDocNodeEatName(xmlDocPtr doc
, xmlNsPtr ns
,
2297 xmlChar
*name
, const xmlChar
*content
) {
2300 cur
= xmlNewNodeEatName(ns
, name
);
2303 if (content
!= NULL
) {
2304 cur
->children
= xmlStringGetNodeList(doc
, content
);
2305 UPDATE_LAST_CHILD_AND_PARENT(cur
)
2308 /* if name don't come from the doc dictionnary free it here */
2309 if ((name
!= NULL
) && (doc
!= NULL
) &&
2310 (!(xmlDictOwns(doc
->dict
, name
))))
2316 #ifdef LIBXML_TREE_ENABLED
2319 * @doc: the document
2320 * @ns: namespace if any
2321 * @name: the node name
2322 * @content: the text content if any
2324 * Creation of a new node element within a document. @ns and @content
2325 * are optional (NULL).
2327 * Returns a pointer to the new node object.
2330 xmlNewDocRawNode(xmlDocPtr doc
, xmlNsPtr ns
,
2331 const xmlChar
*name
, const xmlChar
*content
) {
2334 cur
= xmlNewDocNode(doc
, ns
, name
, NULL
);
2337 if (content
!= NULL
) {
2338 cur
->children
= xmlNewDocText(doc
, content
);
2339 UPDATE_LAST_CHILD_AND_PARENT(cur
)
2346 * xmlNewDocFragment:
2347 * @doc: the document owning the fragment
2349 * Creation of a new Fragment node.
2350 * Returns a pointer to the new node object.
2353 xmlNewDocFragment(xmlDocPtr doc
) {
2357 * Allocate a new DocumentFragment node and fill the fields.
2359 cur
= (xmlNodePtr
) xmlMalloc(sizeof(xmlNode
));
2361 xmlTreeErrMemory("building fragment");
2364 memset(cur
, 0, sizeof(xmlNode
));
2365 cur
->type
= XML_DOCUMENT_FRAG_NODE
;
2369 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
2370 xmlRegisterNodeDefaultValue(cur
);
2373 #endif /* LIBXML_TREE_ENABLED */
2377 * @content: the text content
2379 * Creation of a new text node.
2380 * Returns a pointer to the new node object.
2383 xmlNewText(const xmlChar
*content
) {
2387 * Allocate a new node and fill the fields.
2389 cur
= (xmlNodePtr
) xmlMalloc(sizeof(xmlNode
));
2391 xmlTreeErrMemory("building text");
2394 memset(cur
, 0, sizeof(xmlNode
));
2395 cur
->type
= XML_TEXT_NODE
;
2397 cur
->name
= xmlStringText
;
2398 if (content
!= NULL
) {
2399 cur
->content
= xmlStrdup(content
);
2402 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
2403 xmlRegisterNodeDefaultValue(cur
);
2407 #ifdef LIBXML_TREE_ENABLED
2410 * @parent: the parent node
2411 * @ns: a namespace if any
2412 * @name: the name of the child
2413 * @content: the text content of the child if any.
2415 * Creation of a new child element, added at the end of @parent children list.
2416 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2417 * created element inherits the namespace of @parent. If @content is non NULL,
2418 * a child TEXT node will be created containing the string @content.
2419 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2420 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
2421 * reserved XML chars that might appear in @content, such as the ampersand,
2422 * greater-than or less-than signs, are automatically replaced by their XML
2423 * escaped entity representations.
2425 * Returns a pointer to the new node object.
2428 xmlNewTextChild(xmlNodePtr parent
, xmlNsPtr ns
,
2429 const xmlChar
*name
, const xmlChar
*content
) {
2430 xmlNodePtr cur
, prev
;
2432 if (parent
== NULL
) {
2434 xmlGenericError(xmlGenericErrorContext
,
2435 "xmlNewTextChild : parent == NULL\n");
2442 xmlGenericError(xmlGenericErrorContext
,
2443 "xmlNewTextChild : name == NULL\n");
2449 * Allocate a new node
2451 if (parent
->type
== XML_ELEMENT_NODE
) {
2453 cur
= xmlNewDocRawNode(parent
->doc
, parent
->ns
, name
, content
);
2455 cur
= xmlNewDocRawNode(parent
->doc
, ns
, name
, content
);
2456 } else if ((parent
->type
== XML_DOCUMENT_NODE
) ||
2457 (parent
->type
== XML_HTML_DOCUMENT_NODE
)) {
2459 cur
= xmlNewDocRawNode((xmlDocPtr
) parent
, NULL
, name
, content
);
2461 cur
= xmlNewDocRawNode((xmlDocPtr
) parent
, ns
, name
, content
);
2462 } else if (parent
->type
== XML_DOCUMENT_FRAG_NODE
) {
2463 cur
= xmlNewDocRawNode( parent
->doc
, ns
, name
, content
);
2467 if (cur
== NULL
) return(NULL
);
2470 * add the new element at the end of the children list.
2472 cur
->type
= XML_ELEMENT_NODE
;
2473 cur
->parent
= parent
;
2474 cur
->doc
= parent
->doc
;
2475 if (parent
->children
== NULL
) {
2476 parent
->children
= cur
;
2479 prev
= parent
->last
;
2487 #endif /* LIBXML_TREE_ENABLED */
2491 * @doc: the document
2492 * @name: the char ref string, starting with # or "&# ... ;"
2494 * Creation of a new character reference node.
2495 * Returns a pointer to the new node object.
2498 xmlNewCharRef(xmlDocPtr doc
, const xmlChar
*name
) {
2505 * Allocate a new node and fill the fields.
2507 cur
= (xmlNodePtr
) xmlMalloc(sizeof(xmlNode
));
2509 xmlTreeErrMemory("building character reference");
2512 memset(cur
, 0, sizeof(xmlNode
));
2513 cur
->type
= XML_ENTITY_REF_NODE
;
2516 if (name
[0] == '&') {
2519 len
= xmlStrlen(name
);
2520 if (name
[len
- 1] == ';')
2521 cur
->name
= xmlStrndup(name
, len
- 1);
2523 cur
->name
= xmlStrndup(name
, len
);
2525 cur
->name
= xmlStrdup(name
);
2527 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
2528 xmlRegisterNodeDefaultValue(cur
);
2534 * @doc: the document
2535 * @name: the reference name, or the reference string with & and ;
2537 * Creation of a new reference node.
2538 * Returns a pointer to the new node object.
2541 xmlNewReference(xmlDocPtr doc
, const xmlChar
*name
) {
2549 * Allocate a new node and fill the fields.
2551 cur
= (xmlNodePtr
) xmlMalloc(sizeof(xmlNode
));
2553 xmlTreeErrMemory("building reference");
2556 memset(cur
, 0, sizeof(xmlNode
));
2557 cur
->type
= XML_ENTITY_REF_NODE
;
2560 if (name
[0] == '&') {
2563 len
= xmlStrlen(name
);
2564 if (name
[len
- 1] == ';')
2565 cur
->name
= xmlStrndup(name
, len
- 1);
2567 cur
->name
= xmlStrndup(name
, len
);
2569 cur
->name
= xmlStrdup(name
);
2571 ent
= xmlGetDocEntity(doc
, cur
->name
);
2573 cur
->content
= ent
->content
;
2575 * The parent pointer in entity is a DTD pointer and thus is NOT
2576 * updated. Not sure if this is 100% correct.
2579 cur
->children
= (xmlNodePtr
) ent
;
2580 cur
->last
= (xmlNodePtr
) ent
;
2583 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
2584 xmlRegisterNodeDefaultValue(cur
);
2590 * @doc: the document
2591 * @content: the text content
2593 * Creation of a new text node within a document.
2594 * Returns a pointer to the new node object.
2597 xmlNewDocText(xmlDocPtr doc
, const xmlChar
*content
) {
2600 cur
= xmlNewText(content
);
2601 if (cur
!= NULL
) cur
->doc
= doc
;
2607 * @content: the text content
2608 * @len: the text len.
2610 * Creation of a new text node with an extra parameter for the content's length
2611 * Returns a pointer to the new node object.
2614 xmlNewTextLen(const xmlChar
*content
, int len
) {
2618 * Allocate a new node and fill the fields.
2620 cur
= (xmlNodePtr
) xmlMalloc(sizeof(xmlNode
));
2622 xmlTreeErrMemory("building text");
2625 memset(cur
, 0, sizeof(xmlNode
));
2626 cur
->type
= XML_TEXT_NODE
;
2628 cur
->name
= xmlStringText
;
2629 if (content
!= NULL
) {
2630 cur
->content
= xmlStrndup(content
, len
);
2633 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
2634 xmlRegisterNodeDefaultValue(cur
);
2640 * @doc: the document
2641 * @content: the text content
2642 * @len: the text len.
2644 * Creation of a new text node with an extra content length parameter. The
2645 * text node pertain to a given document.
2646 * Returns a pointer to the new node object.
2649 xmlNewDocTextLen(xmlDocPtr doc
, const xmlChar
*content
, int len
) {
2652 cur
= xmlNewTextLen(content
, len
);
2653 if (cur
!= NULL
) cur
->doc
= doc
;
2659 * @content: the comment content
2661 * Creation of a new node containing a comment.
2662 * Returns a pointer to the new node object.
2665 xmlNewComment(const xmlChar
*content
) {
2669 * Allocate a new node and fill the fields.
2671 cur
= (xmlNodePtr
) xmlMalloc(sizeof(xmlNode
));
2673 xmlTreeErrMemory("building comment");
2676 memset(cur
, 0, sizeof(xmlNode
));
2677 cur
->type
= XML_COMMENT_NODE
;
2679 cur
->name
= xmlStringComment
;
2680 if (content
!= NULL
) {
2681 cur
->content
= xmlStrdup(content
);
2684 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
2685 xmlRegisterNodeDefaultValue(cur
);
2691 * @doc: the document
2692 * @content: the CDATA block content content
2693 * @len: the length of the block
2695 * Creation of a new node containing a CDATA block.
2696 * Returns a pointer to the new node object.
2699 xmlNewCDataBlock(xmlDocPtr doc
, const xmlChar
*content
, int len
) {
2703 * Allocate a new node and fill the fields.
2705 cur
= (xmlNodePtr
) xmlMalloc(sizeof(xmlNode
));
2707 xmlTreeErrMemory("building CDATA");
2710 memset(cur
, 0, sizeof(xmlNode
));
2711 cur
->type
= XML_CDATA_SECTION_NODE
;
2714 if (content
!= NULL
) {
2715 cur
->content
= xmlStrndup(content
, len
);
2718 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
2719 xmlRegisterNodeDefaultValue(cur
);
2725 * @doc: the document
2726 * @content: the comment content
2728 * Creation of a new node containing a comment within a document.
2729 * Returns a pointer to the new node object.
2732 xmlNewDocComment(xmlDocPtr doc
, const xmlChar
*content
) {
2735 cur
= xmlNewComment(content
);
2736 if (cur
!= NULL
) cur
->doc
= doc
;
2742 * @tree: the top element
2743 * @doc: the document
2745 * update all nodes under the tree to point to the right document
2748 xmlSetTreeDoc(xmlNodePtr tree
, xmlDocPtr doc
) {
2753 if (tree
->doc
!= doc
) {
2754 if(tree
->type
== XML_ELEMENT_NODE
) {
2755 prop
= tree
->properties
;
2756 while (prop
!= NULL
) {
2758 xmlSetListDoc(prop
->children
, doc
);
2762 if (tree
->children
!= NULL
)
2763 xmlSetListDoc(tree
->children
, doc
);
2770 * @list: the first element
2771 * @doc: the document
2773 * update all nodes in the list to point to the right document
2776 xmlSetListDoc(xmlNodePtr list
, xmlDocPtr doc
) {
2782 while (cur
!= NULL
) {
2783 if (cur
->doc
!= doc
)
2784 xmlSetTreeDoc(cur
, doc
);
2789 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
2792 * @parent: the parent node
2793 * @ns: a namespace if any
2794 * @name: the name of the child
2795 * @content: the XML content of the child if any.
2797 * Creation of a new child element, added at the end of @parent children list.
2798 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2799 * created element inherits the namespace of @parent. If @content is non NULL,
2800 * a child list containing the TEXTs and ENTITY_REFs node will be created.
2801 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2802 * references. XML special chars must be escaped first by using
2803 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
2805 * Returns a pointer to the new node object.
2808 xmlNewChild(xmlNodePtr parent
, xmlNsPtr ns
,
2809 const xmlChar
*name
, const xmlChar
*content
) {
2810 xmlNodePtr cur
, prev
;
2812 if (parent
== NULL
) {
2814 xmlGenericError(xmlGenericErrorContext
,
2815 "xmlNewChild : parent == NULL\n");
2822 xmlGenericError(xmlGenericErrorContext
,
2823 "xmlNewChild : name == NULL\n");
2829 * Allocate a new node
2831 if (parent
->type
== XML_ELEMENT_NODE
) {
2833 cur
= xmlNewDocNode(parent
->doc
, parent
->ns
, name
, content
);
2835 cur
= xmlNewDocNode(parent
->doc
, ns
, name
, content
);
2836 } else if ((parent
->type
== XML_DOCUMENT_NODE
) ||
2837 (parent
->type
== XML_HTML_DOCUMENT_NODE
)) {
2839 cur
= xmlNewDocNode((xmlDocPtr
) parent
, NULL
, name
, content
);
2841 cur
= xmlNewDocNode((xmlDocPtr
) parent
, ns
, name
, content
);
2842 } else if (parent
->type
== XML_DOCUMENT_FRAG_NODE
) {
2843 cur
= xmlNewDocNode( parent
->doc
, ns
, name
, content
);
2847 if (cur
== NULL
) return(NULL
);
2850 * add the new element at the end of the children list.
2852 cur
->type
= XML_ELEMENT_NODE
;
2853 cur
->parent
= parent
;
2854 cur
->doc
= parent
->doc
;
2855 if (parent
->children
== NULL
) {
2856 parent
->children
= cur
;
2859 prev
= parent
->last
;
2867 #endif /* LIBXML_TREE_ENABLED */
2870 * xmlAddPropSibling:
2871 * @prev: the attribute to which @prop is added after
2872 * @cur: the base attribute passed to calling function
2873 * @prop: the new attribute
2875 * Add a new attribute after @prev using @cur as base attribute.
2876 * When inserting before @cur, @prev is passed as @cur->prev.
2877 * When inserting after @cur, @prev is passed as @cur.
2878 * If an existing attribute is found it is detroyed prior to adding @prop.
2880 * Returns the attribute being inserted or NULL in case of error.
2883 xmlAddPropSibling(xmlNodePtr prev
, xmlNodePtr cur
, xmlNodePtr prop
) {
2886 if (cur
->type
!= XML_ATTRIBUTE_NODE
)
2889 /* check if an attribute with the same name exists */
2890 if (prop
->ns
== NULL
)
2891 attr
= xmlHasNsProp(cur
->parent
, prop
->name
, NULL
);
2893 attr
= xmlHasNsProp(cur
->parent
, prop
->name
, prop
->ns
->href
);
2895 if (prop
->doc
!= cur
->doc
) {
2896 xmlSetTreeDoc(prop
, cur
->doc
);
2898 prop
->parent
= cur
->parent
;
2901 prop
->next
= prev
->next
;
2904 prop
->next
->prev
= prop
;
2909 if (prop
->prev
== NULL
&& prop
->parent
!= NULL
)
2910 prop
->parent
->properties
= (xmlAttrPtr
) prop
;
2911 if ((attr
!= NULL
) && (attr
->type
!= XML_ATTRIBUTE_DECL
)) {
2912 /* different instance, destroy it (attributes must be unique) */
2913 xmlRemoveProp((xmlAttrPtr
) attr
);
2919 * xmlAddNextSibling:
2920 * @cur: the child node
2921 * @elem: the new node
2923 * Add a new node @elem as the next sibling of @cur
2924 * If the new node was already inserted in a document it is
2925 * first unlinked from its existing context.
2926 * As a result of text merging @elem may be freed.
2927 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2928 * If there is an attribute with equal name, it is first destroyed.
2930 * Returns the new node or NULL in case of error.
2933 xmlAddNextSibling(xmlNodePtr cur
, xmlNodePtr elem
) {
2936 xmlGenericError(xmlGenericErrorContext
,
2937 "xmlAddNextSibling : cur == NULL\n");
2943 xmlGenericError(xmlGenericErrorContext
,
2944 "xmlAddNextSibling : elem == NULL\n");
2951 xmlGenericError(xmlGenericErrorContext
,
2952 "xmlAddNextSibling : cur == elem\n");
2957 xmlUnlinkNode(elem
);
2959 if (elem
->type
== XML_TEXT_NODE
) {
2960 if (cur
->type
== XML_TEXT_NODE
) {
2961 xmlNodeAddContent(cur
, elem
->content
);
2965 if ((cur
->next
!= NULL
) && (cur
->next
->type
== XML_TEXT_NODE
) &&
2966 (cur
->name
== cur
->next
->name
)) {
2969 tmp
= xmlStrdup(elem
->content
);
2970 tmp
= xmlStrcat(tmp
, cur
->next
->content
);
2971 xmlNodeSetContent(cur
->next
, tmp
);
2976 } else if (elem
->type
== XML_ATTRIBUTE_NODE
) {
2977 return xmlAddPropSibling(cur
, cur
, elem
);
2980 if (elem
->doc
!= cur
->doc
) {
2981 xmlSetTreeDoc(elem
, cur
->doc
);
2983 elem
->parent
= cur
->parent
;
2985 elem
->next
= cur
->next
;
2987 if (elem
->next
!= NULL
)
2988 elem
->next
->prev
= elem
;
2989 if ((elem
->parent
!= NULL
) && (elem
->parent
->last
== cur
))
2990 elem
->parent
->last
= elem
;
2994 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
2995 defined(LIBXML_SCHEMAS_ENABLED)
2997 * xmlAddPrevSibling:
2998 * @cur: the child node
2999 * @elem: the new node
3001 * Add a new node @elem as the previous sibling of @cur
3002 * merging adjacent TEXT nodes (@elem may be freed)
3003 * If the new node was already inserted in a document it is
3004 * first unlinked from its existing context.
3005 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3006 * If there is an attribute with equal name, it is first destroyed.
3008 * Returns the new node or NULL in case of error.
3011 xmlAddPrevSibling(xmlNodePtr cur
, xmlNodePtr elem
) {
3014 xmlGenericError(xmlGenericErrorContext
,
3015 "xmlAddPrevSibling : cur == NULL\n");
3021 xmlGenericError(xmlGenericErrorContext
,
3022 "xmlAddPrevSibling : elem == NULL\n");
3029 xmlGenericError(xmlGenericErrorContext
,
3030 "xmlAddPrevSibling : cur == elem\n");
3035 xmlUnlinkNode(elem
);
3037 if (elem
->type
== XML_TEXT_NODE
) {
3038 if (cur
->type
== XML_TEXT_NODE
) {
3041 tmp
= xmlStrdup(elem
->content
);
3042 tmp
= xmlStrcat(tmp
, cur
->content
);
3043 xmlNodeSetContent(cur
, tmp
);
3048 if ((cur
->prev
!= NULL
) && (cur
->prev
->type
== XML_TEXT_NODE
) &&
3049 (cur
->name
== cur
->prev
->name
)) {
3050 xmlNodeAddContent(cur
->prev
, elem
->content
);
3054 } else if (elem
->type
== XML_ATTRIBUTE_NODE
) {
3055 return xmlAddPropSibling(cur
->prev
, cur
, elem
);
3058 if (elem
->doc
!= cur
->doc
) {
3059 xmlSetTreeDoc(elem
, cur
->doc
);
3061 elem
->parent
= cur
->parent
;
3063 elem
->prev
= cur
->prev
;
3065 if (elem
->prev
!= NULL
)
3066 elem
->prev
->next
= elem
;
3067 if ((elem
->parent
!= NULL
) && (elem
->parent
->children
== cur
)) {
3068 elem
->parent
->children
= elem
;
3072 #endif /* LIBXML_TREE_ENABLED */
3076 * @cur: the child node
3077 * @elem: the new node
3079 * Add a new element @elem to the list of siblings of @cur
3080 * merging adjacent TEXT nodes (@elem may be freed)
3081 * If the new element was already inserted in a document it is
3082 * first unlinked from its existing context.
3084 * Returns the new element or NULL in case of error.
3087 xmlAddSibling(xmlNodePtr cur
, xmlNodePtr elem
) {
3092 xmlGenericError(xmlGenericErrorContext
,
3093 "xmlAddSibling : cur == NULL\n");
3100 xmlGenericError(xmlGenericErrorContext
,
3101 "xmlAddSibling : elem == NULL\n");
3108 xmlGenericError(xmlGenericErrorContext
,
3109 "xmlAddSibling : cur == elem\n");
3115 * Constant time is we can rely on the ->parent->last to find
3118 if ((cur
->type
!= XML_ATTRIBUTE_NODE
) && (cur
->parent
!= NULL
) &&
3119 (cur
->parent
->children
!= NULL
) &&
3120 (cur
->parent
->last
!= NULL
) &&
3121 (cur
->parent
->last
->next
== NULL
)) {
3122 cur
= cur
->parent
->last
;
3124 while (cur
->next
!= NULL
) cur
= cur
->next
;
3127 xmlUnlinkNode(elem
);
3129 if ((cur
->type
== XML_TEXT_NODE
) && (elem
->type
== XML_TEXT_NODE
) &&
3130 (cur
->name
== elem
->name
)) {
3131 xmlNodeAddContent(cur
, elem
->content
);
3134 } else if (elem
->type
== XML_ATTRIBUTE_NODE
) {
3135 return xmlAddPropSibling(cur
, cur
, elem
);
3138 if (elem
->doc
!= cur
->doc
) {
3139 xmlSetTreeDoc(elem
, cur
->doc
);
3141 parent
= cur
->parent
;
3144 elem
->parent
= parent
;
3147 parent
->last
= elem
;
3154 * @parent: the parent node
3155 * @cur: the first node in the list
3157 * Add a list of node at the end of the child list of the parent
3158 * merging adjacent TEXT nodes (@cur may be freed)
3160 * Returns the last child or NULL in case of error.
3163 xmlAddChildList(xmlNodePtr parent
, xmlNodePtr cur
) {
3166 if (parent
== NULL
) {
3168 xmlGenericError(xmlGenericErrorContext
,
3169 "xmlAddChildList : parent == NULL\n");
3176 xmlGenericError(xmlGenericErrorContext
,
3177 "xmlAddChildList : child == NULL\n");
3182 if ((cur
->doc
!= NULL
) && (parent
->doc
!= NULL
) &&
3183 (cur
->doc
!= parent
->doc
)) {
3185 xmlGenericError(xmlGenericErrorContext
,
3186 "Elements moved to a different document\n");
3191 * add the first element at the end of the children list.
3194 if (parent
->children
== NULL
) {
3195 parent
->children
= cur
;
3198 * If cur and parent->last both are TEXT nodes, then merge them.
3200 if ((cur
->type
== XML_TEXT_NODE
) &&
3201 (parent
->last
->type
== XML_TEXT_NODE
) &&
3202 (cur
->name
== parent
->last
->name
)) {
3203 xmlNodeAddContent(parent
->last
, cur
->content
);
3205 * if it's the only child, nothing more to be done.
3207 if (cur
->next
== NULL
) {
3209 return(parent
->last
);
3215 prev
= parent
->last
;
3219 while (cur
->next
!= NULL
) {
3220 cur
->parent
= parent
;
3221 if (cur
->doc
!= parent
->doc
) {
3222 xmlSetTreeDoc(cur
, parent
->doc
);
3226 cur
->parent
= parent
;
3227 /* the parent may not be linked to a doc ! */
3228 if (cur
->doc
!= parent
->doc
) {
3229 xmlSetTreeDoc(cur
, parent
->doc
);
3238 * @parent: the parent node
3239 * @cur: the child node
3241 * Add a new node to @parent, at the end of the child (or property) list
3242 * merging adjacent TEXT nodes (in which case @cur is freed)
3243 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3244 * If there is an attribute with equal name, it is first destroyed.
3246 * Returns the child or NULL in case of error.
3249 xmlAddChild(xmlNodePtr parent
, xmlNodePtr cur
) {
3252 if (parent
== NULL
) {
3254 xmlGenericError(xmlGenericErrorContext
,
3255 "xmlAddChild : parent == NULL\n");
3262 xmlGenericError(xmlGenericErrorContext
,
3263 "xmlAddChild : child == NULL\n");
3268 if (parent
== cur
) {
3270 xmlGenericError(xmlGenericErrorContext
,
3271 "xmlAddChild : parent == cur\n");
3276 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
3277 * cur is then freed.
3279 if (cur
->type
== XML_TEXT_NODE
) {
3280 if ((parent
->type
== XML_TEXT_NODE
) &&
3281 (parent
->content
!= NULL
) &&
3282 (parent
->name
== cur
->name
)) {
3283 xmlNodeAddContent(parent
, cur
->content
);
3287 if ((parent
->last
!= NULL
) && (parent
->last
->type
== XML_TEXT_NODE
) &&
3288 (parent
->last
->name
== cur
->name
) &&
3289 (parent
->last
!= cur
)) {
3290 xmlNodeAddContent(parent
->last
, cur
->content
);
3292 return(parent
->last
);
3297 * add the new element at the end of the children list.
3300 cur
->parent
= parent
;
3301 if (cur
->doc
!= parent
->doc
) {
3302 xmlSetTreeDoc(cur
, parent
->doc
);
3304 /* this check prevents a loop on tree-traversions if a developer
3305 * tries to add a node to its parent multiple times
3313 if ((parent
->type
== XML_TEXT_NODE
) &&
3314 (parent
->content
!= NULL
) &&
3316 xmlNodeAddContent(parent
, cur
->content
);
3320 if (cur
->type
== XML_ATTRIBUTE_NODE
) {
3321 if (parent
->type
!= XML_ELEMENT_NODE
)
3323 if (parent
->properties
!= NULL
) {
3324 /* check if an attribute with the same name exists */
3325 xmlAttrPtr lastattr
;
3327 if (cur
->ns
== NULL
)
3328 lastattr
= xmlHasNsProp(parent
, cur
->name
, NULL
);
3330 lastattr
= xmlHasNsProp(parent
, cur
->name
, cur
->ns
->href
);
3331 if ((lastattr
!= NULL
) && (lastattr
!= (xmlAttrPtr
) cur
) && (lastattr
->type
!= XML_ATTRIBUTE_DECL
)) {
3332 /* different instance, destroy it (attributes must be unique) */
3333 xmlUnlinkNode((xmlNodePtr
) lastattr
);
3334 xmlFreeProp(lastattr
);
3336 if (lastattr
== (xmlAttrPtr
) cur
)
3340 if (parent
->properties
== NULL
) {
3341 parent
->properties
= (xmlAttrPtr
) cur
;
3344 xmlAttrPtr lastattr
= parent
->properties
;
3345 while (lastattr
->next
!= NULL
) {
3346 lastattr
= lastattr
->next
;
3348 lastattr
->next
= (xmlAttrPtr
) cur
;
3349 ((xmlAttrPtr
) cur
)->prev
= lastattr
;
3352 if (parent
->children
== NULL
) {
3353 parent
->children
= cur
;
3356 prev
= parent
->last
;
3367 * @parent: the parent node
3369 * Search the last child of a node.
3370 * Returns the last child or NULL if none.
3373 xmlGetLastChild(xmlNodePtr parent
) {
3374 if (parent
== NULL
) {
3376 xmlGenericError(xmlGenericErrorContext
,
3377 "xmlGetLastChild : parent == NULL\n");
3381 return(parent
->last
);
3384 #ifdef LIBXML_TREE_ENABLED
3386 * 5 interfaces from DOM ElementTraversal
3390 * xmlChildElementCount:
3391 * @parent: the parent node
3393 * Finds the current number of child nodes of that element which are
3395 * Note the handling of entities references is different than in
3396 * the W3C DOM element traversal spec since we don't have back reference
3397 * from entities content to entities references.
3399 * Returns the count of element child or 0 if not available
3402 xmlChildElementCount(xmlNodePtr parent
) {
3403 unsigned long ret
= 0;
3404 xmlNodePtr cur
= NULL
;
3408 switch (parent
->type
) {
3409 case XML_ELEMENT_NODE
:
3410 case XML_ENTITY_NODE
:
3411 case XML_DOCUMENT_NODE
:
3412 case XML_HTML_DOCUMENT_NODE
:
3413 cur
= parent
->children
;
3418 while (cur
!= NULL
) {
3419 if (cur
->type
== XML_ELEMENT_NODE
)
3427 * xmlFirstElementChild:
3428 * @parent: the parent node
3430 * Finds the first child node of that element which is a Element node
3431 * Note the handling of entities references is different than in
3432 * the W3C DOM element traversal spec since we don't have back reference
3433 * from entities content to entities references.
3435 * Returns the first element child or NULL if not available
3438 xmlFirstElementChild(xmlNodePtr parent
) {
3439 xmlNodePtr cur
= NULL
;
3443 switch (parent
->type
) {
3444 case XML_ELEMENT_NODE
:
3445 case XML_ENTITY_NODE
:
3446 case XML_DOCUMENT_NODE
:
3447 case XML_HTML_DOCUMENT_NODE
:
3448 cur
= parent
->children
;
3453 while (cur
!= NULL
) {
3454 if (cur
->type
== XML_ELEMENT_NODE
)
3462 * xmlLastElementChild:
3463 * @parent: the parent node
3465 * Finds the last child node of that element which is a Element node
3466 * Note the handling of entities references is different than in
3467 * the W3C DOM element traversal spec since we don't have back reference
3468 * from entities content to entities references.
3470 * Returns the last element child or NULL if not available
3473 xmlLastElementChild(xmlNodePtr parent
) {
3474 xmlNodePtr cur
= NULL
;
3478 switch (parent
->type
) {
3479 case XML_ELEMENT_NODE
:
3480 case XML_ENTITY_NODE
:
3481 case XML_DOCUMENT_NODE
:
3482 case XML_HTML_DOCUMENT_NODE
:
3488 while (cur
!= NULL
) {
3489 if (cur
->type
== XML_ELEMENT_NODE
)
3497 * xmlPreviousElementSibling:
3498 * @node: the current node
3500 * Finds the first closest previous sibling of the node which is an
3502 * Note the handling of entities references is different than in
3503 * the W3C DOM element traversal spec since we don't have back reference
3504 * from entities content to entities references.
3506 * Returns the previous element sibling or NULL if not available
3509 xmlPreviousElementSibling(xmlNodePtr node
) {
3512 switch (node
->type
) {
3513 case XML_ELEMENT_NODE
:
3515 case XML_CDATA_SECTION_NODE
:
3516 case XML_ENTITY_REF_NODE
:
3517 case XML_ENTITY_NODE
:
3519 case XML_COMMENT_NODE
:
3520 case XML_XINCLUDE_START
:
3521 case XML_XINCLUDE_END
:
3527 while (node
!= NULL
) {
3528 if (node
->type
== XML_ELEMENT_NODE
)
3536 * xmlNextElementSibling:
3537 * @node: the current node
3539 * Finds the first closest next sibling of the node which is an
3541 * Note the handling of entities references is different than in
3542 * the W3C DOM element traversal spec since we don't have back reference
3543 * from entities content to entities references.
3545 * Returns the next element sibling or NULL if not available
3548 xmlNextElementSibling(xmlNodePtr node
) {
3551 switch (node
->type
) {
3552 case XML_ELEMENT_NODE
:
3554 case XML_CDATA_SECTION_NODE
:
3555 case XML_ENTITY_REF_NODE
:
3556 case XML_ENTITY_NODE
:
3558 case XML_COMMENT_NODE
:
3560 case XML_XINCLUDE_START
:
3561 case XML_XINCLUDE_END
:
3567 while (node
!= NULL
) {
3568 if (node
->type
== XML_ELEMENT_NODE
)
3575 #endif /* LIBXML_TREE_ENABLED */
3579 * @cur: the first node in the list
3581 * Free a node and all its siblings, this is a recursive behaviour, all
3582 * the children are freed too.
3585 xmlFreeNodeList(xmlNodePtr cur
) {
3587 xmlDictPtr dict
= NULL
;
3589 if (cur
== NULL
) return;
3590 if (cur
->type
== XML_NAMESPACE_DECL
) {
3591 xmlFreeNsList((xmlNsPtr
) cur
);
3594 if ((cur
->type
== XML_DOCUMENT_NODE
) ||
3595 #ifdef LIBXML_DOCB_ENABLED
3596 (cur
->type
== XML_DOCB_DOCUMENT_NODE
) ||
3598 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
3599 xmlFreeDoc((xmlDocPtr
) cur
);
3602 if (cur
->doc
!= NULL
) dict
= cur
->doc
->dict
;
3603 while (cur
!= NULL
) {
3605 if (cur
->type
!= XML_DTD_NODE
) {
3607 if ((__xmlRegisterCallbacks
) && (xmlDeregisterNodeDefaultValue
))
3608 xmlDeregisterNodeDefaultValue(cur
);
3610 if ((cur
->children
!= NULL
) &&
3611 (cur
->type
!= XML_ENTITY_REF_NODE
))
3612 xmlFreeNodeList(cur
->children
);
3613 if (((cur
->type
== XML_ELEMENT_NODE
) ||
3614 (cur
->type
== XML_XINCLUDE_START
) ||
3615 (cur
->type
== XML_XINCLUDE_END
)) &&
3616 (cur
->properties
!= NULL
))
3617 xmlFreePropList(cur
->properties
);
3618 if ((cur
->type
!= XML_ELEMENT_NODE
) &&
3619 (cur
->type
!= XML_XINCLUDE_START
) &&
3620 (cur
->type
!= XML_XINCLUDE_END
) &&
3621 (cur
->type
!= XML_ENTITY_REF_NODE
) &&
3622 (cur
->content
!= (xmlChar
*) &(cur
->properties
))) {
3623 DICT_FREE(cur
->content
)
3625 if (((cur
->type
== XML_ELEMENT_NODE
) ||
3626 (cur
->type
== XML_XINCLUDE_START
) ||
3627 (cur
->type
== XML_XINCLUDE_END
)) &&
3628 (cur
->nsDef
!= NULL
))
3629 xmlFreeNsList(cur
->nsDef
);
3632 * When a node is a text node or a comment, it uses a global static
3633 * variable for the name of the node.
3634 * Otherwise the node name might come from the document's
3637 if ((cur
->name
!= NULL
) &&
3638 (cur
->type
!= XML_TEXT_NODE
) &&
3639 (cur
->type
!= XML_COMMENT_NODE
))
3640 DICT_FREE(cur
->name
)
3651 * Free a node, this is a recursive behaviour, all the children are freed too.
3652 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3655 xmlFreeNode(xmlNodePtr cur
) {
3656 xmlDictPtr dict
= NULL
;
3658 if (cur
== NULL
) return;
3660 /* use xmlFreeDtd for DTD nodes */
3661 if (cur
->type
== XML_DTD_NODE
) {
3662 xmlFreeDtd((xmlDtdPtr
) cur
);
3665 if (cur
->type
== XML_NAMESPACE_DECL
) {
3666 xmlFreeNs((xmlNsPtr
) cur
);
3669 if (cur
->type
== XML_ATTRIBUTE_NODE
) {
3670 xmlFreeProp((xmlAttrPtr
) cur
);
3674 if ((__xmlRegisterCallbacks
) && (xmlDeregisterNodeDefaultValue
))
3675 xmlDeregisterNodeDefaultValue(cur
);
3677 if (cur
->doc
!= NULL
) dict
= cur
->doc
->dict
;
3679 if (cur
->type
== XML_ENTITY_DECL
) {
3680 xmlEntityPtr ent
= (xmlEntityPtr
) cur
;
3681 DICT_FREE(ent
->SystemID
);
3682 DICT_FREE(ent
->ExternalID
);
3684 if ((cur
->children
!= NULL
) &&
3685 (cur
->type
!= XML_ENTITY_REF_NODE
))
3686 xmlFreeNodeList(cur
->children
);
3687 if (((cur
->type
== XML_ELEMENT_NODE
) ||
3688 (cur
->type
== XML_XINCLUDE_START
) ||
3689 (cur
->type
== XML_XINCLUDE_END
)) &&
3690 (cur
->properties
!= NULL
))
3691 xmlFreePropList(cur
->properties
);
3692 if ((cur
->type
!= XML_ELEMENT_NODE
) &&
3693 (cur
->content
!= NULL
) &&
3694 (cur
->type
!= XML_ENTITY_REF_NODE
) &&
3695 (cur
->type
!= XML_XINCLUDE_END
) &&
3696 (cur
->type
!= XML_XINCLUDE_START
) &&
3697 (cur
->content
!= (xmlChar
*) &(cur
->properties
))) {
3698 DICT_FREE(cur
->content
)
3702 * When a node is a text node or a comment, it uses a global static
3703 * variable for the name of the node.
3704 * Otherwise the node name might come from the document's dictionnary
3706 if ((cur
->name
!= NULL
) &&
3707 (cur
->type
!= XML_TEXT_NODE
) &&
3708 (cur
->type
!= XML_COMMENT_NODE
))
3709 DICT_FREE(cur
->name
)
3711 if (((cur
->type
== XML_ELEMENT_NODE
) ||
3712 (cur
->type
== XML_XINCLUDE_START
) ||
3713 (cur
->type
== XML_XINCLUDE_END
)) &&
3714 (cur
->nsDef
!= NULL
))
3715 xmlFreeNsList(cur
->nsDef
);
3723 * Unlink a node from it's current context, the node is not freed
3726 xmlUnlinkNode(xmlNodePtr cur
) {
3729 xmlGenericError(xmlGenericErrorContext
,
3730 "xmlUnlinkNode : node == NULL\n");
3734 if (cur
->type
== XML_DTD_NODE
) {
3738 if (doc
->intSubset
== (xmlDtdPtr
) cur
)
3739 doc
->intSubset
= NULL
;
3740 if (doc
->extSubset
== (xmlDtdPtr
) cur
)
3741 doc
->extSubset
= NULL
;
3744 if (cur
->type
== XML_ENTITY_DECL
) {
3748 if (doc
->intSubset
!= NULL
) {
3749 if (xmlHashLookup(doc
->intSubset
->entities
, cur
->name
) == cur
)
3750 xmlHashRemoveEntry(doc
->intSubset
->entities
, cur
->name
,
3752 if (xmlHashLookup(doc
->intSubset
->pentities
, cur
->name
) == cur
)
3753 xmlHashRemoveEntry(doc
->intSubset
->pentities
, cur
->name
,
3756 if (doc
->extSubset
!= NULL
) {
3757 if (xmlHashLookup(doc
->extSubset
->entities
, cur
->name
) == cur
)
3758 xmlHashRemoveEntry(doc
->extSubset
->entities
, cur
->name
,
3760 if (xmlHashLookup(doc
->extSubset
->pentities
, cur
->name
) == cur
)
3761 xmlHashRemoveEntry(doc
->extSubset
->pentities
, cur
->name
,
3766 if (cur
->parent
!= NULL
) {
3768 parent
= cur
->parent
;
3769 if (cur
->type
== XML_ATTRIBUTE_NODE
) {
3770 if (parent
->properties
== (xmlAttrPtr
) cur
)
3771 parent
->properties
= ((xmlAttrPtr
) cur
)->next
;
3773 if (parent
->children
== cur
)
3774 parent
->children
= cur
->next
;
3775 if (parent
->last
== cur
)
3776 parent
->last
= cur
->prev
;
3780 if (cur
->next
!= NULL
)
3781 cur
->next
->prev
= cur
->prev
;
3782 if (cur
->prev
!= NULL
)
3783 cur
->prev
->next
= cur
->next
;
3784 cur
->next
= cur
->prev
= NULL
;
3787 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
3790 * @old: the old node
3793 * Unlink the old node from its current context, prune the new one
3794 * at the same place. If @cur was already inserted in a document it is
3795 * first unlinked from its existing context.
3797 * Returns the @old node
3800 xmlReplaceNode(xmlNodePtr old
, xmlNodePtr cur
) {
3801 if (old
== cur
) return(NULL
);
3802 if ((old
== NULL
) || (old
->parent
== NULL
)) {
3804 xmlGenericError(xmlGenericErrorContext
,
3805 "xmlReplaceNode : old == NULL or without parent\n");
3816 if ((old
->type
==XML_ATTRIBUTE_NODE
) && (cur
->type
!=XML_ATTRIBUTE_NODE
)) {
3818 xmlGenericError(xmlGenericErrorContext
,
3819 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3823 if ((cur
->type
==XML_ATTRIBUTE_NODE
) && (old
->type
!=XML_ATTRIBUTE_NODE
)) {
3825 xmlGenericError(xmlGenericErrorContext
,
3826 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3831 xmlSetTreeDoc(cur
, old
->doc
);
3832 cur
->parent
= old
->parent
;
3833 cur
->next
= old
->next
;
3834 if (cur
->next
!= NULL
)
3835 cur
->next
->prev
= cur
;
3836 cur
->prev
= old
->prev
;
3837 if (cur
->prev
!= NULL
)
3838 cur
->prev
->next
= cur
;
3839 if (cur
->parent
!= NULL
) {
3840 if (cur
->type
== XML_ATTRIBUTE_NODE
) {
3841 if (cur
->parent
->properties
== (xmlAttrPtr
)old
)
3842 cur
->parent
->properties
= ((xmlAttrPtr
) cur
);
3844 if (cur
->parent
->children
== old
)
3845 cur
->parent
->children
= cur
;
3846 if (cur
->parent
->last
== old
)
3847 cur
->parent
->last
= cur
;
3850 old
->next
= old
->prev
= NULL
;
3854 #endif /* LIBXML_TREE_ENABLED */
3856 /************************************************************************
3860 ************************************************************************/
3864 * @cur: the namespace
3866 * Do a copy of the namespace.
3868 * Returns: a new #xmlNsPtr, or NULL in case of error.
3871 xmlCopyNamespace(xmlNsPtr cur
) {
3874 if (cur
== NULL
) return(NULL
);
3875 switch (cur
->type
) {
3876 case XML_LOCAL_NAMESPACE
:
3877 ret
= xmlNewNs(NULL
, cur
->href
, cur
->prefix
);
3881 xmlGenericError(xmlGenericErrorContext
,
3882 "xmlCopyNamespace: invalid type %d\n", cur
->type
);
3890 * xmlCopyNamespaceList:
3891 * @cur: the first namespace
3893 * Do a copy of an namespace list.
3895 * Returns: a new #xmlNsPtr, or NULL in case of error.
3898 xmlCopyNamespaceList(xmlNsPtr cur
) {
3899 xmlNsPtr ret
= NULL
;
3900 xmlNsPtr p
= NULL
,q
;
3902 while (cur
!= NULL
) {
3903 q
= xmlCopyNamespace(cur
);
3916 xmlStaticCopyNodeList(xmlNodePtr node
, xmlDocPtr doc
, xmlNodePtr parent
);
3919 xmlCopyPropInternal(xmlDocPtr doc
, xmlNodePtr target
, xmlAttrPtr cur
) {
3922 if (cur
== NULL
) return(NULL
);
3924 ret
= xmlNewDocProp(target
->doc
, cur
->name
, NULL
);
3925 else if (doc
!= NULL
)
3926 ret
= xmlNewDocProp(doc
, cur
->name
, NULL
);
3927 else if (cur
->parent
!= NULL
)
3928 ret
= xmlNewDocProp(cur
->parent
->doc
, cur
->name
, NULL
);
3929 else if (cur
->children
!= NULL
)
3930 ret
= xmlNewDocProp(cur
->children
->doc
, cur
->name
, NULL
);
3932 ret
= xmlNewDocProp(NULL
, cur
->name
, NULL
);
3933 if (ret
== NULL
) return(NULL
);
3934 ret
->parent
= target
;
3936 if ((cur
->ns
!= NULL
) && (target
!= NULL
)) {
3939 ns
= xmlSearchNs(target
->doc
, target
, cur
->ns
->prefix
);
3942 * Humm, we are copying an element whose namespace is defined
3943 * out of the new tree scope. Search it in the original tree
3944 * and add it at the top of the new tree
3946 ns
= xmlSearchNs(cur
->doc
, cur
->parent
, cur
->ns
->prefix
);
3948 xmlNodePtr root
= target
;
3949 xmlNodePtr pred
= NULL
;
3951 while (root
->parent
!= NULL
) {
3953 root
= root
->parent
;
3955 if (root
== (xmlNodePtr
) target
->doc
) {
3956 /* correct possibly cycling above the document elt */
3959 ret
->ns
= xmlNewNs(root
, ns
->href
, ns
->prefix
);
3963 * we have to find something appropriate here since
3964 * we cant be sure, that the namespce we found is identified
3967 if (xmlStrEqual(ns
->href
, cur
->ns
->href
)) {
3968 /* this is the nice case */
3972 * we are in trouble: we need a new reconcilied namespace.
3975 ret
->ns
= xmlNewReconciliedNs(target
->doc
, target
, cur
->ns
);
3982 if (cur
->children
!= NULL
) {
3985 ret
->children
= xmlStaticCopyNodeList(cur
->children
, ret
->doc
, (xmlNodePtr
) ret
);
3987 tmp
= ret
->children
;
3988 while (tmp
!= NULL
) {
3989 /* tmp->parent = (xmlNodePtr)ret; */
3990 if (tmp
->next
== NULL
)
3998 if ((target
!= NULL
) && (cur
!= NULL
) &&
3999 (target
->doc
!= NULL
) && (cur
->doc
!= NULL
) &&
4000 (cur
->doc
->ids
!= NULL
) && (cur
->parent
!= NULL
)) {
4001 if (xmlIsID(cur
->doc
, cur
->parent
, cur
)) {
4004 id
= xmlNodeListGetString(cur
->doc
, cur
->children
, 1);
4006 xmlAddID(NULL
, target
->doc
, id
, ret
);
4016 * @target: the element where the attribute will be grafted
4017 * @cur: the attribute
4019 * Do a copy of the attribute.
4021 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4024 xmlCopyProp(xmlNodePtr target
, xmlAttrPtr cur
) {
4025 return xmlCopyPropInternal(NULL
, target
, cur
);
4030 * @target: the element where the attributes will be grafted
4031 * @cur: the first attribute
4033 * Do a copy of an attribute list.
4035 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4038 xmlCopyPropList(xmlNodePtr target
, xmlAttrPtr cur
) {
4039 xmlAttrPtr ret
= NULL
;
4040 xmlAttrPtr p
= NULL
,q
;
4042 while (cur
!= NULL
) {
4043 q
= xmlCopyProp(target
, cur
);
4059 * NOTE about the CopyNode operations !
4061 * They are split into external and internal parts for one
4062 * tricky reason: namespaces. Doing a direct copy of a node
4063 * say RPM:Copyright without changing the namespace pointer to
4064 * something else can produce stale links. One way to do it is
4065 * to keep a reference counter but this doesn't work as soon
4066 * as one move the element or the subtree out of the scope of
4067 * the existing namespace. The actual solution seems to add
4068 * a copy of the namespace at the top of the copied tree if
4069 * not available in the subtree.
4070 * Hence two functions, the public front-end call the inner ones
4071 * The argument "recursive" normally indicates a recursive copy
4072 * of the node with values 0 (no) and 1 (yes). For XInclude,
4073 * however, we allow a value of 2 to indicate copy properties and
4074 * namespace info, but don't recurse on children.
4078 xmlStaticCopyNode(const xmlNodePtr node
, xmlDocPtr doc
, xmlNodePtr parent
,
4082 if (node
== NULL
) return(NULL
);
4083 switch (node
->type
) {
4085 case XML_CDATA_SECTION_NODE
:
4086 case XML_ELEMENT_NODE
:
4087 case XML_DOCUMENT_FRAG_NODE
:
4088 case XML_ENTITY_REF_NODE
:
4089 case XML_ENTITY_NODE
:
4091 case XML_COMMENT_NODE
:
4092 case XML_XINCLUDE_START
:
4093 case XML_XINCLUDE_END
:
4095 case XML_ATTRIBUTE_NODE
:
4096 return((xmlNodePtr
) xmlCopyPropInternal(doc
, parent
, (xmlAttrPtr
) node
));
4097 case XML_NAMESPACE_DECL
:
4098 return((xmlNodePtr
) xmlCopyNamespaceList((xmlNsPtr
) node
));
4100 case XML_DOCUMENT_NODE
:
4101 case XML_HTML_DOCUMENT_NODE
:
4102 #ifdef LIBXML_DOCB_ENABLED
4103 case XML_DOCB_DOCUMENT_NODE
:
4105 #ifdef LIBXML_TREE_ENABLED
4106 return((xmlNodePtr
) xmlCopyDoc((xmlDocPtr
) node
, extended
));
4107 #endif /* LIBXML_TREE_ENABLED */
4108 case XML_DOCUMENT_TYPE_NODE
:
4109 case XML_NOTATION_NODE
:
4111 case XML_ELEMENT_DECL
:
4112 case XML_ATTRIBUTE_DECL
:
4113 case XML_ENTITY_DECL
:
4118 * Allocate a new node and fill the fields.
4120 ret
= (xmlNodePtr
) xmlMalloc(sizeof(xmlNode
));
4122 xmlTreeErrMemory("copying node");
4125 memset(ret
, 0, sizeof(xmlNode
));
4126 ret
->type
= node
->type
;
4129 ret
->parent
= parent
;
4130 if (node
->name
== xmlStringText
)
4131 ret
->name
= xmlStringText
;
4132 else if (node
->name
== xmlStringTextNoenc
)
4133 ret
->name
= xmlStringTextNoenc
;
4134 else if (node
->name
== xmlStringComment
)
4135 ret
->name
= xmlStringComment
;
4136 else if (node
->name
!= NULL
) {
4137 if ((doc
!= NULL
) && (doc
->dict
!= NULL
))
4138 ret
->name
= xmlDictLookup(doc
->dict
, node
->name
, -1);
4140 ret
->name
= xmlStrdup(node
->name
);
4142 if ((node
->type
!= XML_ELEMENT_NODE
) &&
4143 (node
->content
!= NULL
) &&
4144 (node
->type
!= XML_ENTITY_REF_NODE
) &&
4145 (node
->type
!= XML_XINCLUDE_END
) &&
4146 (node
->type
!= XML_XINCLUDE_START
)) {
4147 ret
->content
= xmlStrdup(node
->content
);
4149 if (node
->type
== XML_ELEMENT_NODE
)
4150 ret
->line
= node
->line
;
4152 if (parent
!= NULL
) {
4156 * this is a tricky part for the node register thing:
4157 * in case ret does get coalesced in xmlAddChild
4158 * the deregister-node callback is called; so we register ret now already
4160 if ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
))
4161 xmlRegisterNodeDefaultValue((xmlNodePtr
)ret
);
4163 tmp
= xmlAddChild(parent
, ret
);
4164 /* node could have coalesced */
4171 if (((node
->type
== XML_ELEMENT_NODE
) ||
4172 (node
->type
== XML_XINCLUDE_START
)) && (node
->nsDef
!= NULL
))
4173 ret
->nsDef
= xmlCopyNamespaceList(node
->nsDef
);
4175 if (node
->ns
!= NULL
) {
4178 ns
= xmlSearchNs(doc
, ret
, node
->ns
->prefix
);
4181 * Humm, we are copying an element whose namespace is defined
4182 * out of the new tree scope. Search it in the original tree
4183 * and add it at the top of the new tree
4185 ns
= xmlSearchNs(node
->doc
, node
, node
->ns
->prefix
);
4187 xmlNodePtr root
= ret
;
4189 while (root
->parent
!= NULL
) root
= root
->parent
;
4190 ret
->ns
= xmlNewNs(root
, ns
->href
, ns
->prefix
);
4192 ret
->ns
= xmlNewReconciliedNs(doc
, ret
, node
->ns
);
4196 * reference the existing namespace definition in our own tree.
4201 if (((node
->type
== XML_ELEMENT_NODE
) ||
4202 (node
->type
== XML_XINCLUDE_START
)) && (node
->properties
!= NULL
))
4203 ret
->properties
= xmlCopyPropList(ret
, node
->properties
);
4204 if (node
->type
== XML_ENTITY_REF_NODE
) {
4205 if ((doc
== NULL
) || (node
->doc
!= doc
)) {
4207 * The copied node will go into a separate document, so
4208 * to avoid dangling references to the ENTITY_DECL node
4209 * we cannot keep the reference. Try to find it in the
4212 ret
->children
= (xmlNodePtr
) xmlGetDocEntity(doc
, ret
->name
);
4214 ret
->children
= node
->children
;
4216 ret
->last
= ret
->children
;
4217 } else if ((node
->children
!= NULL
) && (extended
!= 2)) {
4218 ret
->children
= xmlStaticCopyNodeList(node
->children
, doc
, ret
);
4219 UPDATE_LAST_CHILD_AND_PARENT(ret
)
4223 /* if parent != NULL we already registered the node above */
4224 if ((parent
== NULL
) &&
4225 ((__xmlRegisterCallbacks
) && (xmlRegisterNodeDefaultValue
)))
4226 xmlRegisterNodeDefaultValue((xmlNodePtr
)ret
);
4231 xmlStaticCopyNodeList(xmlNodePtr node
, xmlDocPtr doc
, xmlNodePtr parent
) {
4232 xmlNodePtr ret
= NULL
;
4233 xmlNodePtr p
= NULL
,q
;
4235 while (node
!= NULL
) {
4236 #ifdef LIBXML_TREE_ENABLED
4237 if (node
->type
== XML_DTD_NODE
) {
4242 if (doc
->intSubset
== NULL
) {
4243 q
= (xmlNodePtr
) xmlCopyDtd( (xmlDtdPtr
) node
);
4246 doc
->intSubset
= (xmlDtdPtr
) q
;
4247 xmlAddChild(parent
, q
);
4249 q
= (xmlNodePtr
) doc
->intSubset
;
4250 xmlAddChild(parent
, q
);
4253 #endif /* LIBXML_TREE_ENABLED */
4254 q
= xmlStaticCopyNode(node
, doc
, parent
, 1);
4258 } else if (p
!= q
) {
4259 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
4272 * @extended: if 1 do a recursive copy (properties, namespaces and children
4274 * if 2 copy properties and namespaces (when applicable)
4276 * Do a copy of the node.
4278 * Returns: a new #xmlNodePtr, or NULL in case of error.
4281 xmlCopyNode(const xmlNodePtr node
, int extended
) {
4284 ret
= xmlStaticCopyNode(node
, NULL
, NULL
, extended
);
4291 * @doc: the document
4292 * @extended: if 1 do a recursive copy (properties, namespaces and children
4294 * if 2 copy properties and namespaces (when applicable)
4296 * Do a copy of the node to a given document.
4298 * Returns: a new #xmlNodePtr, or NULL in case of error.
4301 xmlDocCopyNode(const xmlNodePtr node
, xmlDocPtr doc
, int extended
) {
4304 ret
= xmlStaticCopyNode(node
, doc
, NULL
, extended
);
4309 * xmlDocCopyNodeList:
4310 * @doc: the target document
4311 * @node: the first node in the list.
4313 * Do a recursive copy of the node list.
4315 * Returns: a new #xmlNodePtr, or NULL in case of error.
4317 xmlNodePtr
xmlDocCopyNodeList(xmlDocPtr doc
, const xmlNodePtr node
) {
4318 xmlNodePtr ret
= xmlStaticCopyNodeList(node
, doc
, NULL
);
4324 * @node: the first node in the list.
4326 * Do a recursive copy of the node list.
4327 * Use xmlDocCopyNodeList() if possible to ensure string interning.
4329 * Returns: a new #xmlNodePtr, or NULL in case of error.
4331 xmlNodePtr
xmlCopyNodeList(const xmlNodePtr node
) {
4332 xmlNodePtr ret
= xmlStaticCopyNodeList(node
, NULL
, NULL
);
4336 #if defined(LIBXML_TREE_ENABLED)
4341 * Do a copy of the dtd.
4343 * Returns: a new #xmlDtdPtr, or NULL in case of error.
4346 xmlCopyDtd(xmlDtdPtr dtd
) {
4348 xmlNodePtr cur
, p
= NULL
, q
;
4350 if (dtd
== NULL
) return(NULL
);
4351 ret
= xmlNewDtd(NULL
, dtd
->name
, dtd
->ExternalID
, dtd
->SystemID
);
4352 if (ret
== NULL
) return(NULL
);
4353 if (dtd
->entities
!= NULL
)
4354 ret
->entities
= (void *) xmlCopyEntitiesTable(
4355 (xmlEntitiesTablePtr
) dtd
->entities
);
4356 if (dtd
->notations
!= NULL
)
4357 ret
->notations
= (void *) xmlCopyNotationTable(
4358 (xmlNotationTablePtr
) dtd
->notations
);
4359 if (dtd
->elements
!= NULL
)
4360 ret
->elements
= (void *) xmlCopyElementTable(
4361 (xmlElementTablePtr
) dtd
->elements
);
4362 if (dtd
->attributes
!= NULL
)
4363 ret
->attributes
= (void *) xmlCopyAttributeTable(
4364 (xmlAttributeTablePtr
) dtd
->attributes
);
4365 if (dtd
->pentities
!= NULL
)
4366 ret
->pentities
= (void *) xmlCopyEntitiesTable(
4367 (xmlEntitiesTablePtr
) dtd
->pentities
);
4369 cur
= dtd
->children
;
4370 while (cur
!= NULL
) {
4373 if (cur
->type
== XML_ENTITY_DECL
) {
4374 xmlEntityPtr tmp
= (xmlEntityPtr
) cur
;
4375 switch (tmp
->etype
) {
4376 case XML_INTERNAL_GENERAL_ENTITY
:
4377 case XML_EXTERNAL_GENERAL_PARSED_ENTITY
:
4378 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY
:
4379 q
= (xmlNodePtr
) xmlGetEntityFromDtd(ret
, tmp
->name
);
4381 case XML_INTERNAL_PARAMETER_ENTITY
:
4382 case XML_EXTERNAL_PARAMETER_ENTITY
:
4384 xmlGetParameterEntityFromDtd(ret
, tmp
->name
);
4386 case XML_INTERNAL_PREDEFINED_ENTITY
:
4389 } else if (cur
->type
== XML_ELEMENT_DECL
) {
4390 xmlElementPtr tmp
= (xmlElementPtr
) cur
;
4392 xmlGetDtdQElementDesc(ret
, tmp
->name
, tmp
->prefix
);
4393 } else if (cur
->type
== XML_ATTRIBUTE_DECL
) {
4394 xmlAttributePtr tmp
= (xmlAttributePtr
) cur
;
4396 xmlGetDtdQAttrDesc(ret
, tmp
->elem
, tmp
->name
, tmp
->prefix
);
4397 } else if (cur
->type
== XML_COMMENT_NODE
) {
4398 q
= xmlCopyNode(cur
, 0);
4412 q
->parent
= (xmlNodePtr
) ret
;
4423 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
4426 * @doc: the document
4427 * @recursive: if not zero do a recursive copy.
4429 * Do a copy of the document info. If recursive, the content tree will
4430 * be copied too as well as DTD, namespaces and entities.
4432 * Returns: a new #xmlDocPtr, or NULL in case of error.
4435 xmlCopyDoc(xmlDocPtr doc
, int recursive
) {
4438 if (doc
== NULL
) return(NULL
);
4439 ret
= xmlNewDoc(doc
->version
);
4440 if (ret
== NULL
) return(NULL
);
4441 if (doc
->name
!= NULL
)
4442 ret
->name
= xmlMemStrdup(doc
->name
);
4443 if (doc
->encoding
!= NULL
)
4444 ret
->encoding
= xmlStrdup(doc
->encoding
);
4445 if (doc
->URL
!= NULL
)
4446 ret
->URL
= xmlStrdup(doc
->URL
);
4447 ret
->charset
= doc
->charset
;
4448 ret
->compression
= doc
->compression
;
4449 ret
->standalone
= doc
->standalone
;
4450 if (!recursive
) return(ret
);
4453 ret
->children
= NULL
;
4454 #ifdef LIBXML_TREE_ENABLED
4455 if (doc
->intSubset
!= NULL
) {
4456 ret
->intSubset
= xmlCopyDtd(doc
->intSubset
);
4457 xmlSetTreeDoc((xmlNodePtr
)ret
->intSubset
, ret
);
4458 ret
->intSubset
->parent
= ret
;
4461 if (doc
->oldNs
!= NULL
)
4462 ret
->oldNs
= xmlCopyNamespaceList(doc
->oldNs
);
4463 if (doc
->children
!= NULL
) {
4466 ret
->children
= xmlStaticCopyNodeList(doc
->children
, ret
,
4469 tmp
= ret
->children
;
4470 while (tmp
!= NULL
) {
4471 if (tmp
->next
== NULL
)
4478 #endif /* LIBXML_TREE_ENABLED */
4480 /************************************************************************
4482 * Content access functions *
4484 ************************************************************************/
4490 * Get line number of @node. This requires activation of this option
4491 * before invoking the parser by calling xmlLineNumbersDefault(1)
4493 * Returns the line number if successful, -1 otherwise
4496 xmlGetLineNo(xmlNodePtr node
)
4502 if ((node
->type
== XML_ELEMENT_NODE
) ||
4503 (node
->type
== XML_TEXT_NODE
) ||
4504 (node
->type
== XML_COMMENT_NODE
) ||
4505 (node
->type
== XML_PI_NODE
))
4506 result
= (long) node
->line
;
4507 else if ((node
->prev
!= NULL
) &&
4508 ((node
->prev
->type
== XML_ELEMENT_NODE
) ||
4509 (node
->prev
->type
== XML_TEXT_NODE
) ||
4510 (node
->prev
->type
== XML_COMMENT_NODE
) ||
4511 (node
->prev
->type
== XML_PI_NODE
)))
4512 result
= xmlGetLineNo(node
->prev
);
4513 else if ((node
->parent
!= NULL
) &&
4514 (node
->parent
->type
== XML_ELEMENT_NODE
))
4515 result
= xmlGetLineNo(node
->parent
);
4520 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
4525 * Build a structure based Path for the given node
4527 * Returns the new path or NULL in case of error. The caller must free
4528 * the returned string
4531 xmlGetNodePath(xmlNodePtr node
)
4533 xmlNodePtr cur
, tmp
, next
;
4534 xmlChar
*buffer
= NULL
, *temp
;
4540 int occur
= 0, generic
;
4546 buffer
= (xmlChar
*) xmlMallocAtomic(buf_len
* sizeof(xmlChar
));
4547 if (buffer
== NULL
) {
4548 xmlTreeErrMemory("getting node path");
4551 buf
= (xmlChar
*) xmlMallocAtomic(buf_len
* sizeof(xmlChar
));
4553 xmlTreeErrMemory("getting node path");
4564 if ((cur
->type
== XML_DOCUMENT_NODE
) ||
4565 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
4566 if (buffer
[0] == '/')
4570 } else if (cur
->type
== XML_ELEMENT_NODE
) {
4573 name
= (const char *) cur
->name
;
4575 if (cur
->ns
->prefix
!= NULL
) {
4576 snprintf(nametemp
, sizeof(nametemp
) - 1, "%s:%s",
4577 (char *)cur
->ns
->prefix
, (char *)cur
->name
);
4578 nametemp
[sizeof(nametemp
) - 1] = 0;
4582 * We cannot express named elements in the default
4583 * namespace, so use "*".
4592 * Thumbler index computation
4593 * TODO: the ocurence test seems bogus for namespaced names
4596 while (tmp
!= NULL
) {
4597 if ((tmp
->type
== XML_ELEMENT_NODE
) &&
4599 (xmlStrEqual(cur
->name
, tmp
->name
) &&
4600 ((tmp
->ns
== cur
->ns
) ||
4601 ((tmp
->ns
!= NULL
) && (cur
->ns
!= NULL
) &&
4602 (xmlStrEqual(cur
->ns
->prefix
, tmp
->ns
->prefix
)))))))
4608 while (tmp
!= NULL
&& occur
== 0) {
4609 if ((tmp
->type
== XML_ELEMENT_NODE
) &&
4611 (xmlStrEqual(cur
->name
, tmp
->name
) &&
4612 ((tmp
->ns
== cur
->ns
) ||
4613 ((tmp
->ns
!= NULL
) && (cur
->ns
!= NULL
) &&
4614 (xmlStrEqual(cur
->ns
->prefix
, tmp
->ns
->prefix
)))))))
4622 } else if (cur
->type
== XML_COMMENT_NODE
) {
4628 * Thumbler index computation
4631 while (tmp
!= NULL
) {
4632 if (tmp
->type
== XML_COMMENT_NODE
)
4638 while (tmp
!= NULL
&& occur
== 0) {
4639 if (tmp
->type
== XML_COMMENT_NODE
)
4647 } else if ((cur
->type
== XML_TEXT_NODE
) ||
4648 (cur
->type
== XML_CDATA_SECTION_NODE
)) {
4654 * Thumbler index computation
4657 while (tmp
!= NULL
) {
4658 if ((tmp
->type
== XML_TEXT_NODE
) ||
4659 (tmp
->type
== XML_CDATA_SECTION_NODE
))
4664 * Evaluate if this is the only text- or CDATA-section-node;
4665 * if yes, then we'll get "text()", otherwise "text()[1]".
4669 while (tmp
!= NULL
) {
4670 if ((tmp
->type
== XML_TEXT_NODE
) ||
4671 (tmp
->type
== XML_CDATA_SECTION_NODE
))
4680 } else if (cur
->type
== XML_PI_NODE
) {
4682 snprintf(nametemp
, sizeof(nametemp
) - 1,
4683 "processing-instruction('%s')", (char *)cur
->name
);
4684 nametemp
[sizeof(nametemp
) - 1] = 0;
4690 * Thumbler index computation
4693 while (tmp
!= NULL
) {
4694 if ((tmp
->type
== XML_PI_NODE
) &&
4695 (xmlStrEqual(cur
->name
, tmp
->name
)))
4701 while (tmp
!= NULL
&& occur
== 0) {
4702 if ((tmp
->type
== XML_PI_NODE
) &&
4703 (xmlStrEqual(cur
->name
, tmp
->name
)))
4712 } else if (cur
->type
== XML_ATTRIBUTE_NODE
) {
4714 name
= (const char *) (((xmlAttrPtr
) cur
)->name
);
4716 if (cur
->ns
->prefix
!= NULL
)
4717 snprintf(nametemp
, sizeof(nametemp
) - 1, "%s:%s",
4718 (char *)cur
->ns
->prefix
, (char *)cur
->name
);
4720 snprintf(nametemp
, sizeof(nametemp
) - 1, "%s",
4722 nametemp
[sizeof(nametemp
) - 1] = 0;
4725 next
= ((xmlAttrPtr
) cur
)->parent
;
4731 * Make sure there is enough room
4733 if (xmlStrlen(buffer
) + sizeof(nametemp
) + 20 > buf_len
) {
4735 2 * buf_len
+ xmlStrlen(buffer
) + sizeof(nametemp
) + 20;
4736 temp
= (xmlChar
*) xmlRealloc(buffer
, buf_len
);
4738 xmlTreeErrMemory("getting node path");
4744 temp
= (xmlChar
*) xmlRealloc(buf
, buf_len
);
4746 xmlTreeErrMemory("getting node path");
4754 snprintf((char *) buf
, buf_len
, "%s%s%s",
4755 sep
, name
, (char *) buffer
);
4757 snprintf((char *) buf
, buf_len
, "%s%s[%d]%s",
4758 sep
, name
, occur
, (char *) buffer
);
4759 snprintf((char *) buffer
, buf_len
, "%s", (char *)buf
);
4761 } while (cur
!= NULL
);
4765 #endif /* LIBXML_TREE_ENABLED */
4768 * xmlDocGetRootElement:
4769 * @doc: the document
4771 * Get the root element of the document (doc->children is a list
4772 * containing possibly comments, PIs, etc ...).
4774 * Returns the #xmlNodePtr for the root or NULL
4777 xmlDocGetRootElement(xmlDocPtr doc
) {
4780 if (doc
== NULL
) return(NULL
);
4781 ret
= doc
->children
;
4782 while (ret
!= NULL
) {
4783 if (ret
->type
== XML_ELEMENT_NODE
)
4790 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
4792 * xmlDocSetRootElement:
4793 * @doc: the document
4794 * @root: the new document root element, if root is NULL no action is taken,
4795 * to remove a node from a document use xmlUnlinkNode(root) instead.
4797 * Set the root element of the document (doc->children is a list
4798 * containing possibly comments, PIs, etc ...).
4800 * Returns the old root element if any was found, NULL if root was NULL
4803 xmlDocSetRootElement(xmlDocPtr doc
, xmlNodePtr root
) {
4804 xmlNodePtr old
= NULL
;
4806 if (doc
== NULL
) return(NULL
);
4809 xmlUnlinkNode(root
);
4810 xmlSetTreeDoc(root
, doc
);
4811 root
->parent
= (xmlNodePtr
) doc
;
4812 old
= doc
->children
;
4813 while (old
!= NULL
) {
4814 if (old
->type
== XML_ELEMENT_NODE
)
4819 if (doc
->children
== NULL
) {
4820 doc
->children
= root
;
4823 xmlAddSibling(doc
->children
, root
);
4826 xmlReplaceNode(old
, root
);
4832 #if defined(LIBXML_TREE_ENABLED)
4835 * @cur: the node being changed
4836 * @lang: the language description
4838 * Set the language of a node, i.e. the values of the xml:lang
4842 xmlNodeSetLang(xmlNodePtr cur
, const xmlChar
*lang
) {
4845 if (cur
== NULL
) return;
4848 case XML_CDATA_SECTION_NODE
:
4849 case XML_COMMENT_NODE
:
4850 case XML_DOCUMENT_NODE
:
4851 case XML_DOCUMENT_TYPE_NODE
:
4852 case XML_DOCUMENT_FRAG_NODE
:
4853 case XML_NOTATION_NODE
:
4854 case XML_HTML_DOCUMENT_NODE
:
4856 case XML_ELEMENT_DECL
:
4857 case XML_ATTRIBUTE_DECL
:
4858 case XML_ENTITY_DECL
:
4860 case XML_ENTITY_REF_NODE
:
4861 case XML_ENTITY_NODE
:
4862 case XML_NAMESPACE_DECL
:
4863 #ifdef LIBXML_DOCB_ENABLED
4864 case XML_DOCB_DOCUMENT_NODE
:
4866 case XML_XINCLUDE_START
:
4867 case XML_XINCLUDE_END
:
4869 case XML_ELEMENT_NODE
:
4870 case XML_ATTRIBUTE_NODE
:
4873 ns
= xmlSearchNsByHref(cur
->doc
, cur
, XML_XML_NAMESPACE
);
4876 xmlSetNsProp(cur
, ns
, BAD_CAST
"lang", lang
);
4878 #endif /* LIBXML_TREE_ENABLED */
4882 * @cur: the node being checked
4884 * Searches the language of a node, i.e. the values of the xml:lang
4885 * attribute or the one carried by the nearest ancestor.
4887 * Returns a pointer to the lang value, or NULL if not found
4888 * It's up to the caller to free the memory with xmlFree().
4891 xmlNodeGetLang(xmlNodePtr cur
) {
4894 while (cur
!= NULL
) {
4895 lang
= xmlGetNsProp(cur
, BAD_CAST
"lang", XML_XML_NAMESPACE
);
4904 #ifdef LIBXML_TREE_ENABLED
4906 * xmlNodeSetSpacePreserve:
4907 * @cur: the node being changed
4908 * @val: the xml:space value ("0": default, 1: "preserve")
4910 * Set (or reset) the space preserving behaviour of a node, i.e. the
4911 * value of the xml:space attribute.
4914 xmlNodeSetSpacePreserve(xmlNodePtr cur
, int val
) {
4917 if (cur
== NULL
) return;
4920 case XML_CDATA_SECTION_NODE
:
4921 case XML_COMMENT_NODE
:
4922 case XML_DOCUMENT_NODE
:
4923 case XML_DOCUMENT_TYPE_NODE
:
4924 case XML_DOCUMENT_FRAG_NODE
:
4925 case XML_NOTATION_NODE
:
4926 case XML_HTML_DOCUMENT_NODE
:
4928 case XML_ELEMENT_DECL
:
4929 case XML_ATTRIBUTE_DECL
:
4930 case XML_ENTITY_DECL
:
4932 case XML_ENTITY_REF_NODE
:
4933 case XML_ENTITY_NODE
:
4934 case XML_NAMESPACE_DECL
:
4935 case XML_XINCLUDE_START
:
4936 case XML_XINCLUDE_END
:
4937 #ifdef LIBXML_DOCB_ENABLED
4938 case XML_DOCB_DOCUMENT_NODE
:
4941 case XML_ELEMENT_NODE
:
4942 case XML_ATTRIBUTE_NODE
:
4945 ns
= xmlSearchNsByHref(cur
->doc
, cur
, XML_XML_NAMESPACE
);
4950 xmlSetNsProp(cur
, ns
, BAD_CAST
"space", BAD_CAST
"default");
4953 xmlSetNsProp(cur
, ns
, BAD_CAST
"space", BAD_CAST
"preserve");
4957 #endif /* LIBXML_TREE_ENABLED */
4960 * xmlNodeGetSpacePreserve:
4961 * @cur: the node being checked
4963 * Searches the space preserving behaviour of a node, i.e. the values
4964 * of the xml:space attribute or the one carried by the nearest
4967 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
4970 xmlNodeGetSpacePreserve(xmlNodePtr cur
) {
4973 while (cur
!= NULL
) {
4974 space
= xmlGetNsProp(cur
, BAD_CAST
"space", XML_XML_NAMESPACE
);
4975 if (space
!= NULL
) {
4976 if (xmlStrEqual(space
, BAD_CAST
"preserve")) {
4980 if (xmlStrEqual(space
, BAD_CAST
"default")) {
4991 #ifdef LIBXML_TREE_ENABLED
4994 * @cur: the node being changed
4995 * @name: the new tag name
4997 * Set (or reset) the name of a node.
5000 xmlNodeSetName(xmlNodePtr cur
, const xmlChar
*name
) {
5004 if (cur
== NULL
) return;
5005 if (name
== NULL
) return;
5008 case XML_CDATA_SECTION_NODE
:
5009 case XML_COMMENT_NODE
:
5010 case XML_DOCUMENT_TYPE_NODE
:
5011 case XML_DOCUMENT_FRAG_NODE
:
5012 case XML_NOTATION_NODE
:
5013 case XML_HTML_DOCUMENT_NODE
:
5014 case XML_NAMESPACE_DECL
:
5015 case XML_XINCLUDE_START
:
5016 case XML_XINCLUDE_END
:
5017 #ifdef LIBXML_DOCB_ENABLED
5018 case XML_DOCB_DOCUMENT_NODE
:
5021 case XML_ELEMENT_NODE
:
5022 case XML_ATTRIBUTE_NODE
:
5024 case XML_ENTITY_REF_NODE
:
5025 case XML_ENTITY_NODE
:
5027 case XML_DOCUMENT_NODE
:
5028 case XML_ELEMENT_DECL
:
5029 case XML_ATTRIBUTE_DECL
:
5030 case XML_ENTITY_DECL
:
5039 if ((cur
->name
!= NULL
) && (!xmlDictOwns(dict
, cur
->name
)))
5040 xmlFree((xmlChar
*) cur
->name
);
5041 cur
->name
= xmlDictLookup(dict
, name
, -1);
5043 if (cur
->name
!= NULL
) xmlFree((xmlChar
*) cur
->name
);
5044 cur
->name
= xmlStrdup(name
);
5049 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
5052 * @cur: the node being changed
5053 * @uri: the new base URI
5055 * Set (or reset) the base URI of a node, i.e. the value of the
5056 * xml:base attribute.
5059 xmlNodeSetBase(xmlNodePtr cur
, const xmlChar
* uri
) {
5063 if (cur
== NULL
) return;
5066 case XML_CDATA_SECTION_NODE
:
5067 case XML_COMMENT_NODE
:
5068 case XML_DOCUMENT_TYPE_NODE
:
5069 case XML_DOCUMENT_FRAG_NODE
:
5070 case XML_NOTATION_NODE
:
5072 case XML_ELEMENT_DECL
:
5073 case XML_ATTRIBUTE_DECL
:
5074 case XML_ENTITY_DECL
:
5076 case XML_ENTITY_REF_NODE
:
5077 case XML_ENTITY_NODE
:
5078 case XML_NAMESPACE_DECL
:
5079 case XML_XINCLUDE_START
:
5080 case XML_XINCLUDE_END
:
5082 case XML_ELEMENT_NODE
:
5083 case XML_ATTRIBUTE_NODE
:
5085 case XML_DOCUMENT_NODE
:
5086 #ifdef LIBXML_DOCB_ENABLED
5087 case XML_DOCB_DOCUMENT_NODE
:
5089 case XML_HTML_DOCUMENT_NODE
: {
5090 xmlDocPtr doc
= (xmlDocPtr
) cur
;
5092 if (doc
->URL
!= NULL
)
5093 xmlFree((xmlChar
*) doc
->URL
);
5097 doc
->URL
= xmlPathToURI(uri
);
5102 ns
= xmlSearchNsByHref(cur
->doc
, cur
, XML_XML_NAMESPACE
);
5105 fixed
= xmlPathToURI(uri
);
5106 if (fixed
!= NULL
) {
5107 xmlSetNsProp(cur
, ns
, BAD_CAST
"base", fixed
);
5110 xmlSetNsProp(cur
, ns
, BAD_CAST
"base", uri
);
5113 #endif /* LIBXML_TREE_ENABLED */
5117 * @doc: the document the node pertains to
5118 * @cur: the node being checked
5120 * Searches for the BASE URL. The code should work on both XML
5121 * and HTML document even if base mechanisms are completely different.
5122 * It returns the base as defined in RFC 2396 sections
5123 * 5.1.1. Base URI within Document Content
5125 * 5.1.2. Base URI from the Encapsulating Entity
5126 * However it does not return the document base (5.1.3), use
5127 * doc->URL in this case
5129 * Returns a pointer to the base URL, or NULL if not found
5130 * It's up to the caller to free the memory with xmlFree().
5133 xmlNodeGetBase(xmlDocPtr doc
, xmlNodePtr cur
) {
5134 xmlChar
*oldbase
= NULL
;
5135 xmlChar
*base
, *newbase
;
5137 if ((cur
== NULL
) && (doc
== NULL
))
5139 if (doc
== NULL
) doc
= cur
->doc
;
5140 if ((doc
!= NULL
) && (doc
->type
== XML_HTML_DOCUMENT_NODE
)) {
5141 cur
= doc
->children
;
5142 while ((cur
!= NULL
) && (cur
->name
!= NULL
)) {
5143 if (cur
->type
!= XML_ELEMENT_NODE
) {
5147 if (!xmlStrcasecmp(cur
->name
, BAD_CAST
"html")) {
5148 cur
= cur
->children
;
5151 if (!xmlStrcasecmp(cur
->name
, BAD_CAST
"head")) {
5152 cur
= cur
->children
;
5155 if (!xmlStrcasecmp(cur
->name
, BAD_CAST
"base")) {
5156 return(xmlGetProp(cur
, BAD_CAST
"href"));
5162 while (cur
!= NULL
) {
5163 if (cur
->type
== XML_ENTITY_DECL
) {
5164 xmlEntityPtr ent
= (xmlEntityPtr
) cur
;
5165 return(xmlStrdup(ent
->URI
));
5167 if (cur
->type
== XML_ELEMENT_NODE
) {
5168 base
= xmlGetNsProp(cur
, BAD_CAST
"base", XML_XML_NAMESPACE
);
5170 if (oldbase
!= NULL
) {
5171 newbase
= xmlBuildURI(oldbase
, base
);
5172 if (newbase
!= NULL
) {
5184 if ((!xmlStrncmp(oldbase
, BAD_CAST
"http://", 7)) ||
5185 (!xmlStrncmp(oldbase
, BAD_CAST
"ftp://", 6)) ||
5186 (!xmlStrncmp(oldbase
, BAD_CAST
"urn:", 4)))
5192 if ((doc
!= NULL
) && (doc
->URL
!= NULL
)) {
5193 if (oldbase
== NULL
)
5194 return(xmlStrdup(doc
->URL
));
5195 newbase
= xmlBuildURI(oldbase
, doc
->URL
);
5203 * xmlNodeBufGetContent:
5205 * @cur: the node being read
5207 * Read the value of a node @cur, this can be either the text carried
5208 * directly by this node if it's a TEXT node or the aggregate string
5209 * of the values carried by this node child's (TEXT and ENTITY_REF).
5210 * Entity references are substituted.
5211 * Fills up the buffer @buffer with this value
5213 * Returns 0 in case of success and -1 in case of error.
5216 xmlNodeBufGetContent(xmlBufferPtr buffer
, xmlNodePtr cur
)
5218 if ((cur
== NULL
) || (buffer
== NULL
)) return(-1);
5219 switch (cur
->type
) {
5220 case XML_CDATA_SECTION_NODE
:
5222 xmlBufferCat(buffer
, cur
->content
);
5224 case XML_DOCUMENT_FRAG_NODE
:
5225 case XML_ELEMENT_NODE
:{
5226 xmlNodePtr tmp
= cur
;
5228 while (tmp
!= NULL
) {
5229 switch (tmp
->type
) {
5230 case XML_CDATA_SECTION_NODE
:
5232 if (tmp
->content
!= NULL
)
5233 xmlBufferCat(buffer
, tmp
->content
);
5235 case XML_ENTITY_REF_NODE
:
5236 xmlNodeBufGetContent(buffer
, tmp
);
5244 if (tmp
->children
!= NULL
) {
5245 if (tmp
->children
->type
!= XML_ENTITY_DECL
) {
5246 tmp
= tmp
->children
;
5253 if (tmp
->next
!= NULL
) {
5266 if (tmp
->next
!= NULL
) {
5270 } while (tmp
!= NULL
);
5274 case XML_ATTRIBUTE_NODE
:{
5275 xmlAttrPtr attr
= (xmlAttrPtr
) cur
;
5276 xmlNodePtr tmp
= attr
->children
;
5278 while (tmp
!= NULL
) {
5279 if (tmp
->type
== XML_TEXT_NODE
)
5280 xmlBufferCat(buffer
, tmp
->content
);
5282 xmlNodeBufGetContent(buffer
, tmp
);
5287 case XML_COMMENT_NODE
:
5289 xmlBufferCat(buffer
, cur
->content
);
5291 case XML_ENTITY_REF_NODE
:{
5295 /* lookup entity declaration */
5296 ent
= xmlGetDocEntity(cur
->doc
, cur
->name
);
5300 /* an entity content can be any "well balanced chunk",
5301 * i.e. the result of the content [43] production:
5302 * http://www.w3.org/TR/REC-xml#NT-content
5303 * -> we iterate through child nodes and recursive call
5304 * xmlNodeGetContent() which handles all possible node types */
5305 tmp
= ent
->children
;
5307 xmlNodeBufGetContent(buffer
, tmp
);
5312 case XML_ENTITY_NODE
:
5313 case XML_DOCUMENT_TYPE_NODE
:
5314 case XML_NOTATION_NODE
:
5316 case XML_XINCLUDE_START
:
5317 case XML_XINCLUDE_END
:
5319 case XML_DOCUMENT_NODE
:
5320 #ifdef LIBXML_DOCB_ENABLED
5321 case XML_DOCB_DOCUMENT_NODE
:
5323 case XML_HTML_DOCUMENT_NODE
:
5324 cur
= cur
->children
;
5325 while (cur
!= NULL
) {
5326 if ((cur
->type
== XML_ELEMENT_NODE
) ||
5327 (cur
->type
== XML_TEXT_NODE
) ||
5328 (cur
->type
== XML_CDATA_SECTION_NODE
)) {
5329 xmlNodeBufGetContent(buffer
, cur
);
5334 case XML_NAMESPACE_DECL
:
5335 xmlBufferCat(buffer
, ((xmlNsPtr
) cur
)->href
);
5337 case XML_ELEMENT_DECL
:
5338 case XML_ATTRIBUTE_DECL
:
5339 case XML_ENTITY_DECL
:
5345 * xmlNodeGetContent:
5346 * @cur: the node being read
5348 * Read the value of a node, this can be either the text carried
5349 * directly by this node if it's a TEXT node or the aggregate string
5350 * of the values carried by this node child's (TEXT and ENTITY_REF).
5351 * Entity references are substituted.
5352 * Returns a new #xmlChar * or NULL if no content is available.
5353 * It's up to the caller to free the memory with xmlFree().
5356 xmlNodeGetContent(xmlNodePtr cur
)
5360 switch (cur
->type
) {
5361 case XML_DOCUMENT_FRAG_NODE
:
5362 case XML_ELEMENT_NODE
:{
5363 xmlBufferPtr buffer
;
5366 buffer
= xmlBufferCreateSize(64);
5369 xmlNodeBufGetContent(buffer
, cur
);
5370 ret
= buffer
->content
;
5371 buffer
->content
= NULL
;
5372 xmlBufferFree(buffer
);
5375 case XML_ATTRIBUTE_NODE
:
5376 return(xmlGetPropNodeValueInternal((xmlAttrPtr
) cur
));
5377 case XML_COMMENT_NODE
:
5379 if (cur
->content
!= NULL
)
5380 return (xmlStrdup(cur
->content
));
5382 case XML_ENTITY_REF_NODE
:{
5384 xmlBufferPtr buffer
;
5387 /* lookup entity declaration */
5388 ent
= xmlGetDocEntity(cur
->doc
, cur
->name
);
5392 buffer
= xmlBufferCreate();
5396 xmlNodeBufGetContent(buffer
, cur
);
5398 ret
= buffer
->content
;
5399 buffer
->content
= NULL
;
5400 xmlBufferFree(buffer
);
5403 case XML_ENTITY_NODE
:
5404 case XML_DOCUMENT_TYPE_NODE
:
5405 case XML_NOTATION_NODE
:
5407 case XML_XINCLUDE_START
:
5408 case XML_XINCLUDE_END
:
5410 case XML_DOCUMENT_NODE
:
5411 #ifdef LIBXML_DOCB_ENABLED
5412 case XML_DOCB_DOCUMENT_NODE
:
5414 case XML_HTML_DOCUMENT_NODE
: {
5415 xmlBufferPtr buffer
;
5418 buffer
= xmlBufferCreate();
5422 xmlNodeBufGetContent(buffer
, (xmlNodePtr
) cur
);
5424 ret
= buffer
->content
;
5425 buffer
->content
= NULL
;
5426 xmlBufferFree(buffer
);
5429 case XML_NAMESPACE_DECL
: {
5432 tmp
= xmlStrdup(((xmlNsPtr
) cur
)->href
);
5435 case XML_ELEMENT_DECL
:
5438 case XML_ATTRIBUTE_DECL
:
5441 case XML_ENTITY_DECL
:
5444 case XML_CDATA_SECTION_NODE
:
5446 if (cur
->content
!= NULL
)
5447 return (xmlStrdup(cur
->content
));
5454 * xmlNodeSetContent:
5455 * @cur: the node being modified
5456 * @content: the new value of the content
5458 * Replace the content of a node.
5459 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5460 * references, but XML special chars need to be escaped first by using
5461 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5464 xmlNodeSetContent(xmlNodePtr cur
, const xmlChar
*content
) {
5467 xmlGenericError(xmlGenericErrorContext
,
5468 "xmlNodeSetContent : node == NULL\n");
5472 switch (cur
->type
) {
5473 case XML_DOCUMENT_FRAG_NODE
:
5474 case XML_ELEMENT_NODE
:
5475 case XML_ATTRIBUTE_NODE
:
5476 if (cur
->children
!= NULL
) xmlFreeNodeList(cur
->children
);
5477 cur
->children
= xmlStringGetNodeList(cur
->doc
, content
);
5478 UPDATE_LAST_CHILD_AND_PARENT(cur
)
5481 case XML_CDATA_SECTION_NODE
:
5482 case XML_ENTITY_REF_NODE
:
5483 case XML_ENTITY_NODE
:
5485 case XML_COMMENT_NODE
:
5486 if ((cur
->content
!= NULL
) &&
5487 (cur
->content
!= (xmlChar
*) &(cur
->properties
))) {
5488 if (!((cur
->doc
!= NULL
) && (cur
->doc
->dict
!= NULL
) &&
5489 (xmlDictOwns(cur
->doc
->dict
, cur
->content
))))
5490 xmlFree(cur
->content
);
5492 if (cur
->children
!= NULL
) xmlFreeNodeList(cur
->children
);
5493 cur
->last
= cur
->children
= NULL
;
5494 if (content
!= NULL
) {
5495 cur
->content
= xmlStrdup(content
);
5497 cur
->content
= NULL
;
5498 cur
->properties
= NULL
;
5501 case XML_DOCUMENT_NODE
:
5502 case XML_HTML_DOCUMENT_NODE
:
5503 case XML_DOCUMENT_TYPE_NODE
:
5504 case XML_XINCLUDE_START
:
5505 case XML_XINCLUDE_END
:
5506 #ifdef LIBXML_DOCB_ENABLED
5507 case XML_DOCB_DOCUMENT_NODE
:
5510 case XML_NOTATION_NODE
:
5514 case XML_NAMESPACE_DECL
:
5516 case XML_ELEMENT_DECL
:
5519 case XML_ATTRIBUTE_DECL
:
5522 case XML_ENTITY_DECL
:
5528 #ifdef LIBXML_TREE_ENABLED
5530 * xmlNodeSetContentLen:
5531 * @cur: the node being modified
5532 * @content: the new value of the content
5533 * @len: the size of @content
5535 * Replace the content of a node.
5536 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5537 * references, but XML special chars need to be escaped first by using
5538 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
5541 xmlNodeSetContentLen(xmlNodePtr cur
, const xmlChar
*content
, int len
) {
5544 xmlGenericError(xmlGenericErrorContext
,
5545 "xmlNodeSetContentLen : node == NULL\n");
5549 switch (cur
->type
) {
5550 case XML_DOCUMENT_FRAG_NODE
:
5551 case XML_ELEMENT_NODE
:
5552 case XML_ATTRIBUTE_NODE
:
5553 if (cur
->children
!= NULL
) xmlFreeNodeList(cur
->children
);
5554 cur
->children
= xmlStringLenGetNodeList(cur
->doc
, content
, len
);
5555 UPDATE_LAST_CHILD_AND_PARENT(cur
)
5558 case XML_CDATA_SECTION_NODE
:
5559 case XML_ENTITY_REF_NODE
:
5560 case XML_ENTITY_NODE
:
5562 case XML_COMMENT_NODE
:
5563 case XML_NOTATION_NODE
:
5564 if ((cur
->content
!= NULL
) &&
5565 (cur
->content
!= (xmlChar
*) &(cur
->properties
))) {
5566 if (!((cur
->doc
!= NULL
) && (cur
->doc
->dict
!= NULL
) &&
5567 (xmlDictOwns(cur
->doc
->dict
, cur
->content
))))
5568 xmlFree(cur
->content
);
5570 if (cur
->children
!= NULL
) xmlFreeNodeList(cur
->children
);
5571 cur
->children
= cur
->last
= NULL
;
5572 if (content
!= NULL
) {
5573 cur
->content
= xmlStrndup(content
, len
);
5575 cur
->content
= NULL
;
5576 cur
->properties
= NULL
;
5579 case XML_DOCUMENT_NODE
:
5581 case XML_HTML_DOCUMENT_NODE
:
5582 case XML_DOCUMENT_TYPE_NODE
:
5583 case XML_NAMESPACE_DECL
:
5584 case XML_XINCLUDE_START
:
5585 case XML_XINCLUDE_END
:
5586 #ifdef LIBXML_DOCB_ENABLED
5587 case XML_DOCB_DOCUMENT_NODE
:
5590 case XML_ELEMENT_DECL
:
5593 case XML_ATTRIBUTE_DECL
:
5596 case XML_ENTITY_DECL
:
5601 #endif /* LIBXML_TREE_ENABLED */
5604 * xmlNodeAddContentLen:
5605 * @cur: the node being modified
5606 * @content: extra content
5607 * @len: the size of @content
5609 * Append the extra substring to the node content.
5610 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5611 * raw text, so unescaped XML special chars are allowed, entity
5612 * references are not supported.
5615 xmlNodeAddContentLen(xmlNodePtr cur
, const xmlChar
*content
, int len
) {
5618 xmlGenericError(xmlGenericErrorContext
,
5619 "xmlNodeAddContentLen : node == NULL\n");
5623 if (len
<= 0) return;
5624 switch (cur
->type
) {
5625 case XML_DOCUMENT_FRAG_NODE
:
5626 case XML_ELEMENT_NODE
: {
5627 xmlNodePtr last
, newNode
, tmp
;
5630 newNode
= xmlNewTextLen(content
, len
);
5631 if (newNode
!= NULL
) {
5632 tmp
= xmlAddChild(cur
, newNode
);
5635 if ((last
!= NULL
) && (last
->next
== newNode
)) {
5636 xmlTextMerge(last
, newNode
);
5641 case XML_ATTRIBUTE_NODE
:
5644 case XML_CDATA_SECTION_NODE
:
5645 case XML_ENTITY_REF_NODE
:
5646 case XML_ENTITY_NODE
:
5648 case XML_COMMENT_NODE
:
5649 case XML_NOTATION_NODE
:
5650 if (content
!= NULL
) {
5651 if ((cur
->content
== (xmlChar
*) &(cur
->properties
)) ||
5652 ((cur
->doc
!= NULL
) && (cur
->doc
->dict
!= NULL
) &&
5653 xmlDictOwns(cur
->doc
->dict
, cur
->content
))) {
5654 cur
->content
= xmlStrncatNew(cur
->content
, content
, len
);
5655 cur
->properties
= NULL
;
5659 cur
->content
= xmlStrncat(cur
->content
, content
, len
);
5661 case XML_DOCUMENT_NODE
:
5663 case XML_HTML_DOCUMENT_NODE
:
5664 case XML_DOCUMENT_TYPE_NODE
:
5665 case XML_NAMESPACE_DECL
:
5666 case XML_XINCLUDE_START
:
5667 case XML_XINCLUDE_END
:
5668 #ifdef LIBXML_DOCB_ENABLED
5669 case XML_DOCB_DOCUMENT_NODE
:
5672 case XML_ELEMENT_DECL
:
5673 case XML_ATTRIBUTE_DECL
:
5674 case XML_ENTITY_DECL
:
5680 * xmlNodeAddContent:
5681 * @cur: the node being modified
5682 * @content: extra content
5684 * Append the extra substring to the node content.
5685 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5686 * raw text, so unescaped XML special chars are allowed, entity
5687 * references are not supported.
5690 xmlNodeAddContent(xmlNodePtr cur
, const xmlChar
*content
) {
5695 xmlGenericError(xmlGenericErrorContext
,
5696 "xmlNodeAddContent : node == NULL\n");
5700 if (content
== NULL
) return;
5701 len
= xmlStrlen(content
);
5702 xmlNodeAddContentLen(cur
, content
, len
);
5707 * @first: the first text node
5708 * @second: the second text node being merged
5710 * Merge two text nodes into one
5711 * Returns the first text node augmented
5714 xmlTextMerge(xmlNodePtr first
, xmlNodePtr second
) {
5715 if (first
== NULL
) return(second
);
5716 if (second
== NULL
) return(first
);
5717 if (first
->type
!= XML_TEXT_NODE
) return(first
);
5718 if (second
->type
!= XML_TEXT_NODE
) return(first
);
5719 if (second
->name
!= first
->name
)
5721 xmlNodeAddContent(first
, second
->content
);
5722 xmlUnlinkNode(second
);
5723 xmlFreeNode(second
);
5727 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
5730 * @doc: the document
5731 * @node: the current node
5733 * Search all the namespace applying to a given element.
5734 * Returns an NULL terminated array of all the #xmlNsPtr found
5735 * that need to be freed by the caller or NULL if no
5736 * namespace if defined
5739 xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED
, xmlNodePtr node
)
5742 xmlNsPtr
*ret
= NULL
;
5747 while (node
!= NULL
) {
5748 if (node
->type
== XML_ELEMENT_NODE
) {
5750 while (cur
!= NULL
) {
5753 (xmlNsPtr
*) xmlMalloc((maxns
+ 1) *
5756 xmlTreeErrMemory("getting namespace list");
5761 for (i
= 0; i
< nbns
; i
++) {
5762 if ((cur
->prefix
== ret
[i
]->prefix
) ||
5763 (xmlStrEqual(cur
->prefix
, ret
[i
]->prefix
)))
5767 if (nbns
>= maxns
) {
5769 ret
= (xmlNsPtr
*) xmlRealloc(ret
,
5774 xmlTreeErrMemory("getting namespace list");
5785 node
= node
->parent
;
5789 #endif /* LIBXML_TREE_ENABLED */
5792 * xmlTreeEnsureXMLDecl:
5795 * Ensures that there is an XML namespace declaration on the doc.
5797 * Returns the XML ns-struct or NULL on API and internal errors.
5800 xmlTreeEnsureXMLDecl(xmlDocPtr doc
)
5804 if (doc
->oldNs
!= NULL
)
5805 return (doc
->oldNs
);
5808 ns
= (xmlNsPtr
) xmlMalloc(sizeof(xmlNs
));
5811 "allocating the XML namespace");
5814 memset(ns
, 0, sizeof(xmlNs
));
5815 ns
->type
= XML_LOCAL_NAMESPACE
;
5816 ns
->href
= xmlStrdup(XML_XML_NAMESPACE
);
5817 ns
->prefix
= xmlStrdup((const xmlChar
*)"xml");
5825 * @doc: the document
5826 * @node: the current node
5827 * @nameSpace: the namespace prefix
5829 * Search a Ns registered under a given name space for a document.
5830 * recurse on the parents until it finds the defined namespace
5831 * or return NULL otherwise.
5832 * @nameSpace can be NULL, this is a search for the default namespace.
5833 * We don't allow to cross entities boundaries. If you don't declare
5834 * the namespace within those you will be in troubles !!! A warning
5835 * is generated to cover this case.
5837 * Returns the namespace pointer or NULL.
5840 xmlSearchNs(xmlDocPtr doc
, xmlNodePtr node
, const xmlChar
*nameSpace
) {
5843 xmlNodePtr orig
= node
;
5845 if (node
== NULL
) return(NULL
);
5846 if ((nameSpace
!= NULL
) &&
5847 (xmlStrEqual(nameSpace
, (const xmlChar
*)"xml"))) {
5848 if ((doc
== NULL
) && (node
->type
== XML_ELEMENT_NODE
)) {
5850 * The XML-1.0 namespace is normally held on the root
5851 * element. In this case exceptionally create it on the
5854 cur
= (xmlNsPtr
) xmlMalloc(sizeof(xmlNs
));
5856 xmlTreeErrMemory("searching namespace");
5859 memset(cur
, 0, sizeof(xmlNs
));
5860 cur
->type
= XML_LOCAL_NAMESPACE
;
5861 cur
->href
= xmlStrdup(XML_XML_NAMESPACE
);
5862 cur
->prefix
= xmlStrdup((const xmlChar
*)"xml");
5863 cur
->next
= node
->nsDef
;
5873 * Return the XML namespace declaration held by the doc.
5875 if (doc
->oldNs
== NULL
)
5876 return(xmlTreeEnsureXMLDecl(doc
));
5880 while (node
!= NULL
) {
5881 if ((node
->type
== XML_ENTITY_REF_NODE
) ||
5882 (node
->type
== XML_ENTITY_NODE
) ||
5883 (node
->type
== XML_ENTITY_DECL
))
5885 if (node
->type
== XML_ELEMENT_NODE
) {
5887 while (cur
!= NULL
) {
5888 if ((cur
->prefix
== NULL
) && (nameSpace
== NULL
) &&
5889 (cur
->href
!= NULL
))
5891 if ((cur
->prefix
!= NULL
) && (nameSpace
!= NULL
) &&
5892 (cur
->href
!= NULL
) &&
5893 (xmlStrEqual(cur
->prefix
, nameSpace
)))
5900 if ((cur
->prefix
== NULL
) && (nameSpace
== NULL
) &&
5901 (cur
->href
!= NULL
))
5903 if ((cur
->prefix
!= NULL
) && (nameSpace
!= NULL
) &&
5904 (cur
->href
!= NULL
) &&
5905 (xmlStrEqual(cur
->prefix
, nameSpace
)))
5910 node
= node
->parent
;
5917 * @doc: the document
5918 * @node: the current node
5919 * @ancestor: the ancestor carrying the namespace
5920 * @prefix: the namespace prefix
5922 * Verify that the given namespace held on @ancestor is still in scope
5925 * Returns 1 if true, 0 if false and -1 in case of error.
5928 xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED
, xmlNodePtr node
,
5929 xmlNodePtr ancestor
, const xmlChar
* prefix
)
5933 while ((node
!= NULL
) && (node
!= ancestor
)) {
5934 if ((node
->type
== XML_ENTITY_REF_NODE
) ||
5935 (node
->type
== XML_ENTITY_NODE
) ||
5936 (node
->type
== XML_ENTITY_DECL
))
5938 if (node
->type
== XML_ELEMENT_NODE
) {
5940 while (tst
!= NULL
) {
5941 if ((tst
->prefix
== NULL
)
5942 && (prefix
== NULL
))
5944 if ((tst
->prefix
!= NULL
)
5946 && (xmlStrEqual(tst
->prefix
, prefix
)))
5951 node
= node
->parent
;
5953 if (node
!= ancestor
)
5959 * xmlSearchNsByHref:
5960 * @doc: the document
5961 * @node: the current node
5962 * @href: the namespace value
5964 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5965 * the defined namespace or return NULL otherwise.
5966 * Returns the namespace pointer or NULL.
5969 xmlSearchNsByHref(xmlDocPtr doc
, xmlNodePtr node
, const xmlChar
* href
)
5972 xmlNodePtr orig
= node
;
5975 if ((node
== NULL
) || (href
== NULL
))
5977 if (xmlStrEqual(href
, XML_XML_NAMESPACE
)) {
5979 * Only the document can hold the XML spec namespace.
5981 if ((doc
== NULL
) && (node
->type
== XML_ELEMENT_NODE
)) {
5983 * The XML-1.0 namespace is normally held on the root
5984 * element. In this case exceptionally create it on the
5987 cur
= (xmlNsPtr
) xmlMalloc(sizeof(xmlNs
));
5989 xmlTreeErrMemory("searching namespace");
5992 memset(cur
, 0, sizeof(xmlNs
));
5993 cur
->type
= XML_LOCAL_NAMESPACE
;
5994 cur
->href
= xmlStrdup(XML_XML_NAMESPACE
);
5995 cur
->prefix
= xmlStrdup((const xmlChar
*) "xml");
5996 cur
->next
= node
->nsDef
;
6006 * Return the XML namespace declaration held by the doc.
6008 if (doc
->oldNs
== NULL
)
6009 return(xmlTreeEnsureXMLDecl(doc
));
6013 is_attr
= (node
->type
== XML_ATTRIBUTE_NODE
);
6014 while (node
!= NULL
) {
6015 if ((node
->type
== XML_ENTITY_REF_NODE
) ||
6016 (node
->type
== XML_ENTITY_NODE
) ||
6017 (node
->type
== XML_ENTITY_DECL
))
6019 if (node
->type
== XML_ELEMENT_NODE
) {
6021 while (cur
!= NULL
) {
6022 if ((cur
->href
!= NULL
) && (href
!= NULL
) &&
6023 (xmlStrEqual(cur
->href
, href
))) {
6024 if (((!is_attr
) || (cur
->prefix
!= NULL
)) &&
6025 (xmlNsInScope(doc
, orig
, node
, cur
->prefix
) == 1))
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))
6042 node
= node
->parent
;
6048 * xmlNewReconciliedNs:
6049 * @doc: the document
6050 * @tree: a node expected to hold the new namespace
6051 * @ns: the original namespace
6053 * This function tries to locate a namespace definition in a tree
6054 * ancestors, or create a new namespace definition node similar to
6055 * @ns trying to reuse the same prefix. However if the given prefix is
6056 * null (default namespace) or reused within the subtree defined by
6057 * @tree or on one of its ancestors then a new prefix is generated.
6058 * Returns the (new) namespace definition or NULL in case of error
6061 xmlNewReconciliedNs(xmlDocPtr doc
, xmlNodePtr tree
, xmlNsPtr ns
) {
6068 xmlGenericError(xmlGenericErrorContext
,
6069 "xmlNewReconciliedNs : tree == NULL\n");
6073 if ((ns
== NULL
) || (ns
->type
!= XML_NAMESPACE_DECL
)) {
6075 xmlGenericError(xmlGenericErrorContext
,
6076 "xmlNewReconciliedNs : ns == NULL\n");
6081 * Search an existing namespace definition inherited.
6083 def
= xmlSearchNsByHref(doc
, tree
, ns
->href
);
6088 * Find a close prefix which is not already in use.
6089 * Let's strip namespace prefixes longer than 20 chars !
6091 if (ns
->prefix
== NULL
)
6092 snprintf((char *) prefix
, sizeof(prefix
), "default");
6094 snprintf((char *) prefix
, sizeof(prefix
), "%.20s", (char *)ns
->prefix
);
6096 def
= xmlSearchNs(doc
, tree
, prefix
);
6097 while (def
!= NULL
) {
6098 if (counter
> 1000) return(NULL
);
6099 if (ns
->prefix
== NULL
)
6100 snprintf((char *) prefix
, sizeof(prefix
), "default%d", counter
++);
6102 snprintf((char *) prefix
, sizeof(prefix
), "%.20s%d",
6103 (char *)ns
->prefix
, counter
++);
6104 def
= xmlSearchNs(doc
, tree
, prefix
);
6108 * OK, now we are ready to create a new one.
6110 def
= xmlNewNs(tree
, ns
->href
, prefix
);
6114 #ifdef LIBXML_TREE_ENABLED
6116 * xmlReconciliateNs:
6117 * @doc: the document
6118 * @tree: a node defining the subtree to reconciliate
6120 * This function checks that all the namespaces declared within the given
6121 * tree are properly declared. This is needed for example after Copy or Cut
6122 * and then paste operations. The subtree may still hold pointers to
6123 * namespace declarations outside the subtree or invalid/masked. As much
6124 * as possible the function try to reuse the existing namespaces found in
6125 * the new environment. If not possible the new namespaces are redeclared
6126 * on @tree at the top of the given subtree.
6127 * Returns the number of namespace declarations created or -1 in case of error.
6130 xmlReconciliateNs(xmlDocPtr doc
, xmlNodePtr tree
) {
6131 xmlNsPtr
*oldNs
= NULL
;
6132 xmlNsPtr
*newNs
= NULL
;
6137 xmlNodePtr node
= tree
;
6141 if ((node
== NULL
) || (node
->type
!= XML_ELEMENT_NODE
)) return(-1);
6142 if ((doc
== NULL
) || (doc
->type
!= XML_DOCUMENT_NODE
)) return(-1);
6143 if (node
->doc
!= doc
) return(-1);
6144 while (node
!= NULL
) {
6146 * Reconciliate the node namespace
6148 if (node
->ns
!= NULL
) {
6150 * initialize the cache if needed
6152 if (sizeCache
== 0) {
6154 oldNs
= (xmlNsPtr
*) xmlMalloc(sizeCache
*
6156 if (oldNs
== NULL
) {
6157 xmlTreeErrMemory("fixing namespaces");
6160 newNs
= (xmlNsPtr
*) xmlMalloc(sizeCache
*
6162 if (newNs
== NULL
) {
6163 xmlTreeErrMemory("fixing namespaces");
6168 for (i
= 0;i
< nbCache
;i
++) {
6169 if (oldNs
[i
] == node
->ns
) {
6170 node
->ns
= newNs
[i
];
6176 * OK we need to recreate a new namespace definition
6178 n
= xmlNewReconciliedNs(doc
, tree
, node
->ns
);
6179 if (n
!= NULL
) { /* :-( what if else ??? */
6181 * check if we need to grow the cache buffers.
6183 if (sizeCache
<= nbCache
) {
6185 oldNs
= (xmlNsPtr
*) xmlRealloc(oldNs
, sizeCache
*
6187 if (oldNs
== NULL
) {
6188 xmlTreeErrMemory("fixing namespaces");
6192 newNs
= (xmlNsPtr
*) xmlRealloc(newNs
, sizeCache
*
6194 if (newNs
== NULL
) {
6195 xmlTreeErrMemory("fixing namespaces");
6201 oldNs
[nbCache
++] = node
->ns
;
6207 * now check for namespace hold by attributes on the node.
6209 if (node
->type
== XML_ELEMENT_NODE
) {
6210 attr
= node
->properties
;
6211 while (attr
!= NULL
) {
6212 if (attr
->ns
!= NULL
) {
6214 * initialize the cache if needed
6216 if (sizeCache
== 0) {
6218 oldNs
= (xmlNsPtr
*) xmlMalloc(sizeCache
*
6220 if (oldNs
== NULL
) {
6221 xmlTreeErrMemory("fixing namespaces");
6224 newNs
= (xmlNsPtr
*) xmlMalloc(sizeCache
*
6226 if (newNs
== NULL
) {
6227 xmlTreeErrMemory("fixing namespaces");
6232 for (i
= 0;i
< nbCache
;i
++) {
6233 if (oldNs
[i
] == attr
->ns
) {
6234 attr
->ns
= newNs
[i
];
6240 * OK we need to recreate a new namespace definition
6242 n
= xmlNewReconciliedNs(doc
, tree
, attr
->ns
);
6243 if (n
!= NULL
) { /* :-( what if else ??? */
6245 * check if we need to grow the cache buffers.
6247 if (sizeCache
<= nbCache
) {
6249 oldNs
= (xmlNsPtr
*) xmlRealloc(oldNs
,
6250 sizeCache
* sizeof(xmlNsPtr
));
6251 if (oldNs
== NULL
) {
6252 xmlTreeErrMemory("fixing namespaces");
6256 newNs
= (xmlNsPtr
*) xmlRealloc(newNs
,
6257 sizeCache
* sizeof(xmlNsPtr
));
6258 if (newNs
== NULL
) {
6259 xmlTreeErrMemory("fixing namespaces");
6265 oldNs
[nbCache
++] = attr
->ns
;
6275 * Browse the full subtree, deep first
6277 if ((node
->children
!= NULL
) && (node
->type
!= XML_ENTITY_REF_NODE
)) {
6279 node
= node
->children
;
6280 } else if ((node
!= tree
) && (node
->next
!= NULL
)) {
6283 } else if (node
!= tree
) {
6284 /* go up to parents->next if needed */
6285 while (node
!= tree
) {
6286 if (node
->parent
!= NULL
)
6287 node
= node
->parent
;
6288 if ((node
!= tree
) && (node
->next
!= NULL
)) {
6292 if (node
->parent
== NULL
) {
6297 /* exit condition */
6309 #endif /* LIBXML_TREE_ENABLED */
6312 xmlGetPropNodeInternal(xmlNodePtr node
, const xmlChar
*name
,
6313 const xmlChar
*nsName
, int useDTD
)
6317 if ((node
== NULL
) || (node
->type
!= XML_ELEMENT_NODE
) || (name
== NULL
))
6320 if (node
->properties
!= NULL
) {
6321 prop
= node
->properties
;
6322 if (nsName
== NULL
) {
6324 * We want the attr to be in no namespace.
6327 if ((prop
->ns
== NULL
) && xmlStrEqual(prop
->name
, name
)) {
6331 } while (prop
!= NULL
);
6334 * We want the attr to be in the specified namespace.
6337 if ((prop
->ns
!= NULL
) && xmlStrEqual(prop
->name
, name
) &&
6338 ((prop
->ns
->href
== nsName
) ||
6339 xmlStrEqual(prop
->ns
->href
, nsName
)))
6344 } while (prop
!= NULL
);
6348 #ifdef LIBXML_TREE_ENABLED
6352 * Check if there is a default/fixed attribute declaration in
6353 * the internal or external subset.
6355 if ((node
->doc
!= NULL
) && (node
->doc
->intSubset
!= NULL
)) {
6356 xmlDocPtr doc
= node
->doc
;
6357 xmlAttributePtr attrDecl
= NULL
;
6358 xmlChar
*elemQName
, *tmpstr
= NULL
;
6361 * We need the QName of the element for the DTD-lookup.
6363 if ((node
->ns
!= NULL
) && (node
->ns
->prefix
!= NULL
)) {
6364 tmpstr
= xmlStrdup(node
->ns
->prefix
);
6365 tmpstr
= xmlStrcat(tmpstr
, BAD_CAST
":");
6366 tmpstr
= xmlStrcat(tmpstr
, node
->name
);
6371 elemQName
= (xmlChar
*) node
->name
;
6372 if (nsName
== NULL
) {
6374 * The common and nice case: Attr in no namespace.
6376 attrDecl
= xmlGetDtdQAttrDesc(doc
->intSubset
,
6377 elemQName
, name
, NULL
);
6378 if ((attrDecl
== NULL
) && (doc
->extSubset
!= NULL
)) {
6379 attrDecl
= xmlGetDtdQAttrDesc(doc
->extSubset
,
6380 elemQName
, name
, NULL
);
6383 xmlNsPtr
*nsList
, *cur
;
6386 * The ugly case: Search using the prefixes of in-scope
6387 * ns-decls corresponding to @nsName.
6389 nsList
= xmlGetNsList(node
->doc
, node
);
6390 if (nsList
== NULL
) {
6396 while (*cur
!= NULL
) {
6397 if (xmlStrEqual((*cur
)->href
, nsName
)) {
6398 attrDecl
= xmlGetDtdQAttrDesc(doc
->intSubset
, elemQName
,
6399 name
, (*cur
)->prefix
);
6402 if (doc
->extSubset
!= NULL
) {
6403 attrDecl
= xmlGetDtdQAttrDesc(doc
->extSubset
, elemQName
,
6404 name
, (*cur
)->prefix
);
6416 * Only default/fixed attrs are relevant.
6418 if ((attrDecl
!= NULL
) && (attrDecl
->defaultValue
!= NULL
))
6419 return((xmlAttrPtr
) attrDecl
);
6421 #endif /* LIBXML_TREE_ENABLED */
6426 xmlGetPropNodeValueInternal(xmlAttrPtr prop
)
6430 if (prop
->type
== XML_ATTRIBUTE_NODE
) {
6432 * Note that we return at least the empty string.
6433 * TODO: Do we really always want that?
6435 if (prop
->children
!= NULL
) {
6436 if ((prop
->children
->next
== NULL
) &&
6437 ((prop
->children
->type
== XML_TEXT_NODE
) ||
6438 (prop
->children
->type
== XML_CDATA_SECTION_NODE
)))
6441 * Optimization for the common case: only 1 text node.
6443 return(xmlStrdup(prop
->children
->content
));
6447 ret
= xmlNodeListGetString(prop
->doc
, prop
->children
, 1);
6452 return(xmlStrdup((xmlChar
*)""));
6453 } else if (prop
->type
== XML_ATTRIBUTE_DECL
) {
6454 return(xmlStrdup(((xmlAttributePtr
)prop
)->defaultValue
));
6462 * @name: the attribute name
6464 * Search an attribute associated to a node
6465 * This function also looks in DTD attribute declaration for #FIXED or
6466 * default declaration values unless DTD use has been turned off.
6468 * Returns the attribute or the attribute declaration or NULL if
6469 * neither was found.
6472 xmlHasProp(xmlNodePtr node
, const xmlChar
*name
) {
6476 if ((node
== NULL
) || (node
->type
!= XML_ELEMENT_NODE
) || (name
== NULL
))
6479 * Check on the properties attached to the node
6481 prop
= node
->properties
;
6482 while (prop
!= NULL
) {
6483 if (xmlStrEqual(prop
->name
, name
)) {
6488 if (!xmlCheckDTD
) return(NULL
);
6491 * Check if there is a default declaration in the internal
6492 * or external subsets
6496 xmlAttributePtr attrDecl
;
6497 if (doc
->intSubset
!= NULL
) {
6498 attrDecl
= xmlGetDtdAttrDesc(doc
->intSubset
, node
->name
, name
);
6499 if ((attrDecl
== NULL
) && (doc
->extSubset
!= NULL
))
6500 attrDecl
= xmlGetDtdAttrDesc(doc
->extSubset
, node
->name
, name
);
6501 if ((attrDecl
!= NULL
) && (attrDecl
->defaultValue
!= NULL
))
6502 /* return attribute declaration only if a default value is given
6503 (that includes #FIXED declarations) */
6504 return((xmlAttrPtr
) attrDecl
);
6513 * @name: the attribute name
6514 * @nameSpace: the URI of the namespace
6516 * Search for an attribute associated to a node
6517 * This attribute has to be anchored in the namespace specified.
6518 * This does the entity substitution.
6519 * This function looks in DTD attribute declaration for #FIXED or
6520 * default declaration values unless DTD use has been turned off.
6521 * Note that a namespace of NULL indicates to use the default namespace.
6523 * Returns the attribute or the attribute declaration or NULL
6524 * if neither was found.
6527 xmlHasNsProp(xmlNodePtr node
, const xmlChar
*name
, const xmlChar
*nameSpace
) {
6529 return(xmlGetPropNodeInternal(node
, name
, nameSpace
, xmlCheckDTD
));
6535 * @name: the attribute name
6537 * Search and get the value of an attribute associated to a node
6538 * This does the entity substitution.
6539 * This function looks in DTD attribute declaration for #FIXED or
6540 * default declaration values unless DTD use has been turned off.
6541 * NOTE: this function acts independently of namespaces associated
6542 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6543 * for namespace aware processing.
6545 * Returns the attribute value or NULL if not found.
6546 * It's up to the caller to free the memory with xmlFree().
6549 xmlGetProp(xmlNodePtr node
, const xmlChar
*name
) {
6552 prop
= xmlHasProp(node
, name
);
6555 return(xmlGetPropNodeValueInternal(prop
));
6561 * @name: the attribute name
6563 * Search and get the value of an attribute associated to a node
6564 * This does the entity substitution.
6565 * This function looks in DTD attribute declaration for #FIXED or
6566 * default declaration values unless DTD use has been turned off.
6567 * This function is similar to xmlGetProp except it will accept only
6568 * an attribute in no namespace.
6570 * Returns the attribute value or NULL if not found.
6571 * It's up to the caller to free the memory with xmlFree().
6574 xmlGetNoNsProp(xmlNodePtr node
, const xmlChar
*name
) {
6577 prop
= xmlGetPropNodeInternal(node
, name
, NULL
, xmlCheckDTD
);
6580 return(xmlGetPropNodeValueInternal(prop
));
6586 * @name: the attribute name
6587 * @nameSpace: the URI of the namespace
6589 * Search and get the value of an attribute associated to a node
6590 * This attribute has to be anchored in the namespace specified.
6591 * This does the entity substitution.
6592 * This function looks in DTD attribute declaration for #FIXED or
6593 * default declaration values unless DTD use has been turned off.
6595 * Returns the attribute value or NULL if not found.
6596 * It's up to the caller to free the memory with xmlFree().
6599 xmlGetNsProp(xmlNodePtr node
, const xmlChar
*name
, const xmlChar
*nameSpace
) {
6602 prop
= xmlGetPropNodeInternal(node
, name
, nameSpace
, xmlCheckDTD
);
6605 return(xmlGetPropNodeValueInternal(prop
));
6608 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6612 * @name: the attribute name
6614 * Remove an attribute carried by a node.
6615 * This handles only attributes in no namespace.
6616 * Returns 0 if successful, -1 if not found
6619 xmlUnsetProp(xmlNodePtr node
, const xmlChar
*name
) {
6622 prop
= xmlGetPropNodeInternal(node
, name
, NULL
, 0);
6625 xmlUnlinkNode((xmlNodePtr
) prop
);
6633 * @ns: the namespace definition
6634 * @name: the attribute name
6636 * Remove an attribute carried by a node.
6637 * Returns 0 if successful, -1 if not found
6640 xmlUnsetNsProp(xmlNodePtr node
, xmlNsPtr ns
, const xmlChar
*name
) {
6643 prop
= xmlGetPropNodeInternal(node
, name
, (ns
!= NULL
) ? ns
->href
: NULL
, 0);
6646 xmlUnlinkNode((xmlNodePtr
) prop
);
6652 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
6656 * @name: the attribute name (a QName)
6657 * @value: the attribute value
6659 * Set (or reset) an attribute carried by a node.
6660 * If @name has a prefix, then the corresponding
6661 * namespace-binding will be used, if in scope; it is an
6662 * error it there's no such ns-binding for the prefix in
6664 * Returns the attribute pointer.
6668 xmlSetProp(xmlNodePtr node
, const xmlChar
*name
, const xmlChar
*value
) {
6670 const xmlChar
*nqname
;
6672 if ((node
== NULL
) || (name
== NULL
) || (node
->type
!= XML_ELEMENT_NODE
))
6678 nqname
= xmlSplitQName3(name
, &len
);
6679 if (nqname
!= NULL
) {
6681 xmlChar
*prefix
= xmlStrndup(name
, len
);
6682 ns
= xmlSearchNs(node
->doc
, node
, prefix
);
6686 return(xmlSetNsProp(node
, ns
, nqname
, value
));
6688 return(xmlSetNsProp(node
, NULL
, name
, value
));
6694 * @ns: the namespace definition
6695 * @name: the attribute name
6696 * @value: the attribute value
6698 * Set (or reset) an attribute carried by a node.
6699 * The ns structure must be in scope, this is not checked
6701 * Returns the attribute pointer.
6704 xmlSetNsProp(xmlNodePtr node
, xmlNsPtr ns
, const xmlChar
*name
,
6705 const xmlChar
*value
)
6709 if (ns
&& (ns
->href
== NULL
))
6711 prop
= xmlGetPropNodeInternal(node
, name
, (ns
!= NULL
) ? ns
->href
: NULL
, 0);
6714 * Modify the attribute's value.
6716 if (prop
->atype
== XML_ATTRIBUTE_ID
) {
6717 xmlRemoveID(node
->doc
, prop
);
6718 prop
->atype
= XML_ATTRIBUTE_ID
;
6720 if (prop
->children
!= NULL
)
6721 xmlFreeNodeList(prop
->children
);
6722 prop
->children
= NULL
;
6725 if (value
!= NULL
) {
6728 if(!xmlCheckUTF8(value
)) {
6729 xmlTreeErr(XML_TREE_NOT_UTF8
, (xmlNodePtr
) node
->doc
,
6731 if (node
->doc
!= NULL
)
6732 node
->doc
->encoding
= xmlStrdup(BAD_CAST
"ISO-8859-1");
6734 prop
->children
= xmlNewDocText(node
->doc
, value
);
6736 tmp
= prop
->children
;
6737 while (tmp
!= NULL
) {
6738 tmp
->parent
= (xmlNodePtr
) prop
;
6739 if (tmp
->next
== NULL
)
6744 if (prop
->atype
== XML_ATTRIBUTE_ID
)
6745 xmlAddID(NULL
, node
->doc
, value
, prop
);
6749 * No equal attr found; create a new one.
6751 return(xmlNewPropInternal(node
, ns
, name
, value
, 0));
6754 #endif /* LIBXML_TREE_ENABLED */
6760 * Is this node a Text node ?
6761 * Returns 1 yes, 0 no
6764 xmlNodeIsText(xmlNodePtr node
) {
6765 if (node
== NULL
) return(0);
6767 if (node
->type
== XML_TEXT_NODE
) return(1);
6775 * Checks whether this node is an empty or whitespace only
6776 * (and possibly ignorable) text-node.
6778 * Returns 1 yes, 0 no
6781 xmlIsBlankNode(xmlNodePtr node
) {
6783 if (node
== NULL
) return(0);
6785 if ((node
->type
!= XML_TEXT_NODE
) &&
6786 (node
->type
!= XML_CDATA_SECTION_NODE
))
6788 if (node
->content
== NULL
) return(1);
6789 cur
= node
->content
;
6791 if (!IS_BLANK_CH(*cur
)) return(0);
6801 * @content: the content
6802 * @len: @content length
6804 * Concat the given string at the end of the existing node content
6806 * Returns -1 in case of error, 0 otherwise
6810 xmlTextConcat(xmlNodePtr node
, const xmlChar
*content
, int len
) {
6811 if (node
== NULL
) return(-1);
6813 if ((node
->type
!= XML_TEXT_NODE
) &&
6814 (node
->type
!= XML_CDATA_SECTION_NODE
) &&
6815 (node
->type
!= XML_COMMENT_NODE
) &&
6816 (node
->type
!= XML_PI_NODE
)) {
6818 xmlGenericError(xmlGenericErrorContext
,
6819 "xmlTextConcat: node is not text nor CDATA\n");
6823 /* need to check if content is currently in the dictionary */
6824 if ((node
->content
== (xmlChar
*) &(node
->properties
)) ||
6825 ((node
->doc
!= NULL
) && (node
->doc
->dict
!= NULL
) &&
6826 xmlDictOwns(node
->doc
->dict
, node
->content
))) {
6827 node
->content
= xmlStrncatNew(node
->content
, content
, len
);
6829 node
->content
= xmlStrncat(node
->content
, content
, len
);
6831 node
->properties
= NULL
;
6832 if (node
->content
== NULL
)
6837 /************************************************************************
6839 * Output : to a FILE or in memory *
6841 ************************************************************************/
6846 * routine to create an XML buffer.
6847 * returns the new structure.
6850 xmlBufferCreate(void) {
6853 ret
= (xmlBufferPtr
) xmlMalloc(sizeof(xmlBuffer
));
6855 xmlTreeErrMemory("creating buffer");
6859 ret
->size
= xmlDefaultBufferSize
;
6860 ret
->alloc
= xmlBufferAllocScheme
;
6861 ret
->content
= (xmlChar
*) xmlMallocAtomic(ret
->size
* sizeof(xmlChar
));
6862 if (ret
->content
== NULL
) {
6863 xmlTreeErrMemory("creating buffer");
6867 ret
->content
[0] = 0;
6868 ret
->contentIO
= NULL
;
6873 * xmlBufferCreateSize:
6874 * @size: initial size of buffer
6876 * routine to create an XML buffer.
6877 * returns the new structure.
6880 xmlBufferCreateSize(size_t size
) {
6883 ret
= (xmlBufferPtr
) xmlMalloc(sizeof(xmlBuffer
));
6885 xmlTreeErrMemory("creating buffer");
6889 ret
->alloc
= xmlBufferAllocScheme
;
6890 ret
->size
= (size
? size
+2 : 0); /* +1 for ending null */
6892 ret
->content
= (xmlChar
*) xmlMallocAtomic(ret
->size
* sizeof(xmlChar
));
6893 if (ret
->content
== NULL
) {
6894 xmlTreeErrMemory("creating buffer");
6898 ret
->content
[0] = 0;
6900 ret
->content
= NULL
;
6901 ret
->contentIO
= NULL
;
6906 * xmlBufferCreateStatic:
6907 * @mem: the memory area
6908 * @size: the size in byte
6910 * routine to create an XML buffer from an immutable memory area.
6911 * The area won't be modified nor copied, and is expected to be
6912 * present until the end of the buffer lifetime.
6914 * returns the new structure.
6917 xmlBufferCreateStatic(void *mem
, size_t size
) {
6920 if ((mem
== NULL
) || (size
== 0))
6923 ret
= (xmlBufferPtr
) xmlMalloc(sizeof(xmlBuffer
));
6925 xmlTreeErrMemory("creating buffer");
6930 ret
->alloc
= XML_BUFFER_ALLOC_IMMUTABLE
;
6931 ret
->content
= (xmlChar
*) mem
;
6936 * xmlBufferSetAllocationScheme:
6937 * @buf: the buffer to tune
6938 * @scheme: allocation scheme to use
6940 * Sets the allocation scheme for this buffer
6943 xmlBufferSetAllocationScheme(xmlBufferPtr buf
,
6944 xmlBufferAllocationScheme scheme
) {
6947 xmlGenericError(xmlGenericErrorContext
,
6948 "xmlBufferSetAllocationScheme: buf == NULL\n");
6952 if ((buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) ||
6953 (buf
->alloc
== XML_BUFFER_ALLOC_IO
)) return;
6954 if ((scheme
== XML_BUFFER_ALLOC_DOUBLEIT
) ||
6955 (scheme
== XML_BUFFER_ALLOC_EXACT
) ||
6956 (scheme
== XML_BUFFER_ALLOC_IMMUTABLE
))
6957 buf
->alloc
= scheme
;
6962 * @buf: the buffer to free
6964 * Frees an XML buffer. It frees both the content and the structure which
6968 xmlBufferFree(xmlBufferPtr buf
) {
6971 xmlGenericError(xmlGenericErrorContext
,
6972 "xmlBufferFree: buf == NULL\n");
6977 if ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) &&
6978 (buf
->contentIO
!= NULL
)) {
6979 xmlFree(buf
->contentIO
);
6980 } else if ((buf
->content
!= NULL
) &&
6981 (buf
->alloc
!= XML_BUFFER_ALLOC_IMMUTABLE
)) {
6982 xmlFree(buf
->content
);
6994 xmlBufferEmpty(xmlBufferPtr buf
) {
6995 if (buf
== NULL
) return;
6996 if (buf
->content
== NULL
) return;
6998 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) {
6999 buf
->content
= BAD_CAST
"";
7000 } else if ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) &&
7001 (buf
->contentIO
!= NULL
)) {
7002 size_t start_buf
= buf
->content
- buf
->contentIO
;
7004 buf
->size
+= start_buf
;
7005 buf
->content
= buf
->contentIO
;
7006 buf
->content
[0] = 0;
7008 buf
->content
[0] = 0;
7014 * @buf: the buffer to dump
7015 * @len: the number of xmlChar to remove
7017 * Remove the beginning of an XML buffer.
7019 * Returns the number of #xmlChar removed, or -1 in case of failure.
7022 xmlBufferShrink(xmlBufferPtr buf
, unsigned int len
) {
7023 if (buf
== NULL
) return(-1);
7024 if (len
== 0) return(0);
7025 if (len
> buf
->use
) return(-1);
7028 if ((buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) ||
7029 ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) && (buf
->contentIO
!= NULL
))) {
7031 * we just move the content pointer, but also make sure
7032 * the perceived buffer size has shrinked accordingly
7034 buf
->content
+= len
;
7038 * sometimes though it maybe be better to really shrink
7041 if ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) && (buf
->contentIO
!= NULL
)) {
7042 size_t start_buf
= buf
->content
- buf
->contentIO
;
7043 if (start_buf
>= buf
->size
) {
7044 memmove(buf
->contentIO
, &buf
->content
[0], buf
->use
);
7045 buf
->content
= buf
->contentIO
;
7046 buf
->content
[buf
->use
] = 0;
7047 buf
->size
+= start_buf
;
7051 memmove(buf
->content
, &buf
->content
[len
], buf
->use
);
7052 buf
->content
[buf
->use
] = 0;
7060 * @len: the minimum free size to allocate
7062 * Grow the available space of an XML buffer.
7064 * Returns the new available space or -1 in case of error
7067 xmlBufferGrow(xmlBufferPtr buf
, unsigned int len
) {
7071 if (buf
== NULL
) return(-1);
7073 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return(0);
7074 if (len
+ buf
->use
< buf
->size
) return(0);
7077 * Windows has a BIG problem on realloc timing, so we try to double
7078 * the buffer size (if that's enough) (bug 146697)
7079 * Apparently BSD too, and it's probably best for linux too
7080 * On an embedded system this may be something to change
7083 if (buf
->size
> len
)
7084 size
= buf
->size
* 2;
7086 size
= buf
->use
+ len
+ 100;
7088 size
= buf
->use
+ len
+ 100;
7091 if ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) && (buf
->contentIO
!= NULL
)) {
7092 size_t start_buf
= buf
->content
- buf
->contentIO
;
7094 newbuf
= (xmlChar
*) xmlRealloc(buf
->contentIO
, start_buf
+ size
);
7095 if (newbuf
== NULL
) {
7096 xmlTreeErrMemory("growing buffer");
7099 buf
->contentIO
= newbuf
;
7100 buf
->content
= newbuf
+ start_buf
;
7102 newbuf
= (xmlChar
*) xmlRealloc(buf
->content
, size
);
7103 if (newbuf
== NULL
) {
7104 xmlTreeErrMemory("growing buffer");
7107 buf
->content
= newbuf
;
7110 return(buf
->size
- buf
->use
);
7115 * @file: the file output
7116 * @buf: the buffer to dump
7118 * Dumps an XML buffer to a FILE *.
7119 * Returns the number of #xmlChar written
7122 xmlBufferDump(FILE *file
, xmlBufferPtr buf
) {
7127 xmlGenericError(xmlGenericErrorContext
,
7128 "xmlBufferDump: buf == NULL\n");
7132 if (buf
->content
== NULL
) {
7134 xmlGenericError(xmlGenericErrorContext
,
7135 "xmlBufferDump: buf->content == NULL\n");
7141 ret
= fwrite(buf
->content
, sizeof(xmlChar
), buf
->use
, file
);
7149 * Function to extract the content of a buffer
7151 * Returns the internal content
7155 xmlBufferContent(const xmlBufferPtr buf
)
7160 return buf
->content
;
7167 * Function to get the length of a buffer
7169 * Returns the length of data in the internal content
7173 xmlBufferLength(const xmlBufferPtr buf
)
7183 * @buf: the buffer to resize
7184 * @size: the desired size
7186 * Resize a buffer to accommodate minimum size of @size.
7188 * Returns 0 in case of problems, 1 otherwise
7191 xmlBufferResize(xmlBufferPtr buf
, unsigned int size
)
7193 unsigned int newSize
;
7194 xmlChar
* rebuf
= NULL
;
7200 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return(0);
7202 /* Don't resize if we don't have to */
7203 if (size
< buf
->size
)
7206 /* figure out new size */
7207 switch (buf
->alloc
){
7208 case XML_BUFFER_ALLOC_IO
:
7209 case XML_BUFFER_ALLOC_DOUBLEIT
:
7210 /*take care of empty case*/
7211 newSize
= (buf
->size
? buf
->size
*2 : size
+ 10);
7212 while (size
> newSize
) {
7213 if (newSize
> UINT_MAX
/ 2) {
7214 xmlTreeErrMemory("growing buffer");
7220 case XML_BUFFER_ALLOC_EXACT
:
7228 if ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) && (buf
->contentIO
!= NULL
)) {
7229 start_buf
= buf
->content
- buf
->contentIO
;
7231 if (start_buf
> newSize
) {
7232 /* move data back to start */
7233 memmove(buf
->contentIO
, buf
->content
, buf
->use
);
7234 buf
->content
= buf
->contentIO
;
7235 buf
->content
[buf
->use
] = 0;
7236 buf
->size
+= start_buf
;
7238 rebuf
= (xmlChar
*) xmlRealloc(buf
->contentIO
, start_buf
+ newSize
);
7239 if (rebuf
== NULL
) {
7240 xmlTreeErrMemory("growing buffer");
7243 buf
->contentIO
= rebuf
;
7244 buf
->content
= rebuf
+ start_buf
;
7247 if (buf
->content
== NULL
) {
7248 rebuf
= (xmlChar
*) xmlMallocAtomic(newSize
);
7249 } else if (buf
->size
- buf
->use
< 100) {
7250 rebuf
= (xmlChar
*) xmlRealloc(buf
->content
, newSize
);
7253 * if we are reallocating a buffer far from being full, it's
7254 * better to make a new allocation and copy only the used range
7255 * and free the old one.
7257 rebuf
= (xmlChar
*) xmlMallocAtomic(newSize
);
7258 if (rebuf
!= NULL
) {
7259 memcpy(rebuf
, buf
->content
, buf
->use
);
7260 xmlFree(buf
->content
);
7261 rebuf
[buf
->use
] = 0;
7264 if (rebuf
== NULL
) {
7265 xmlTreeErrMemory("growing buffer");
7268 buf
->content
= rebuf
;
7270 buf
->size
= newSize
;
7277 * @buf: the buffer to dump
7278 * @str: the #xmlChar string
7279 * @len: the number of #xmlChar to add
7281 * Add a string range to an XML buffer. if len == -1, the length of
7282 * str is recomputed.
7284 * Returns 0 successful, a positive error code number otherwise
7285 * and -1 in case of internal or API error.
7288 xmlBufferAdd(xmlBufferPtr buf
, const xmlChar
*str
, int len
) {
7289 unsigned int needSize
;
7291 if ((str
== NULL
) || (buf
== NULL
)) {
7294 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return -1;
7297 xmlGenericError(xmlGenericErrorContext
,
7298 "xmlBufferAdd: len < 0\n");
7302 if (len
== 0) return 0;
7305 len
= xmlStrlen(str
);
7307 if (len
< 0) return -1;
7308 if (len
== 0) return 0;
7310 needSize
= buf
->use
+ len
+ 2;
7311 if (needSize
> buf
->size
){
7312 if (!xmlBufferResize(buf
, needSize
)){
7313 xmlTreeErrMemory("growing buffer");
7314 return XML_ERR_NO_MEMORY
;
7318 memmove(&buf
->content
[buf
->use
], str
, len
*sizeof(xmlChar
));
7320 buf
->content
[buf
->use
] = 0;
7327 * @str: the #xmlChar string
7328 * @len: the number of #xmlChar to add
7330 * Add a string range to the beginning of an XML buffer.
7331 * if len == -1, the length of @str is recomputed.
7333 * Returns 0 successful, a positive error code number otherwise
7334 * and -1 in case of internal or API error.
7337 xmlBufferAddHead(xmlBufferPtr buf
, const xmlChar
*str
, int len
) {
7338 unsigned int needSize
;
7342 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return -1;
7345 xmlGenericError(xmlGenericErrorContext
,
7346 "xmlBufferAddHead: str == NULL\n");
7352 xmlGenericError(xmlGenericErrorContext
,
7353 "xmlBufferAddHead: len < 0\n");
7357 if (len
== 0) return 0;
7360 len
= xmlStrlen(str
);
7362 if (len
<= 0) return -1;
7364 if ((buf
->alloc
== XML_BUFFER_ALLOC_IO
) && (buf
->contentIO
!= NULL
)) {
7365 size_t start_buf
= buf
->content
- buf
->contentIO
;
7367 if (start_buf
> (unsigned int) len
) {
7369 * We can add it in the space previously shrinked
7371 buf
->content
-= len
;
7372 memmove(&buf
->content
[0], str
, len
);
7378 needSize
= buf
->use
+ len
+ 2;
7379 if (needSize
> buf
->size
){
7380 if (!xmlBufferResize(buf
, needSize
)){
7381 xmlTreeErrMemory("growing buffer");
7382 return XML_ERR_NO_MEMORY
;
7386 memmove(&buf
->content
[len
], &buf
->content
[0], buf
->use
);
7387 memmove(&buf
->content
[0], str
, len
);
7389 buf
->content
[buf
->use
] = 0;
7395 * @buf: the buffer to add to
7396 * @str: the #xmlChar string
7398 * Append a zero terminated string to an XML buffer.
7400 * Returns 0 successful, a positive error code number otherwise
7401 * and -1 in case of internal or API error.
7404 xmlBufferCat(xmlBufferPtr buf
, const xmlChar
*str
) {
7407 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return -1;
7408 if (str
== NULL
) return -1;
7409 return xmlBufferAdd(buf
, str
, -1);
7414 * @buf: the buffer to dump
7415 * @str: the C char string
7417 * Append a zero terminated C string to an XML buffer.
7419 * Returns 0 successful, a positive error code number otherwise
7420 * and -1 in case of internal or API error.
7423 xmlBufferCCat(xmlBufferPtr buf
, const char *str
) {
7428 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return -1;
7431 xmlGenericError(xmlGenericErrorContext
,
7432 "xmlBufferCCat: str == NULL\n");
7436 for (cur
= str
;*cur
!= 0;cur
++) {
7437 if (buf
->use
+ 10 >= buf
->size
) {
7438 if (!xmlBufferResize(buf
, buf
->use
+10)){
7439 xmlTreeErrMemory("growing buffer");
7440 return XML_ERR_NO_MEMORY
;
7443 buf
->content
[buf
->use
++] = *cur
;
7445 buf
->content
[buf
->use
] = 0;
7450 * xmlBufferWriteCHAR:
7451 * @buf: the XML buffer
7452 * @string: the string to add
7454 * routine which manages and grows an output buffer. This one adds
7455 * xmlChars at the end of the buffer.
7458 xmlBufferWriteCHAR(xmlBufferPtr buf
, const xmlChar
*string
) {
7461 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return;
7462 xmlBufferCat(buf
, string
);
7466 * xmlBufferWriteChar:
7467 * @buf: the XML buffer output
7468 * @string: the string to add
7470 * routine which manage and grows an output buffer. This one add
7471 * C chars at the end of the array.
7474 xmlBufferWriteChar(xmlBufferPtr buf
, const char *string
) {
7477 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return;
7478 xmlBufferCCat(buf
, string
);
7483 * xmlBufferWriteQuotedString:
7484 * @buf: the XML buffer output
7485 * @string: the string to add
7487 * routine which manage and grows an output buffer. This one writes
7488 * a quoted or double quoted #xmlChar string, checking first if it holds
7489 * quote or double-quotes internally
7492 xmlBufferWriteQuotedString(xmlBufferPtr buf
, const xmlChar
*string
) {
7493 const xmlChar
*cur
, *base
;
7496 if (buf
->alloc
== XML_BUFFER_ALLOC_IMMUTABLE
) return;
7497 if (xmlStrchr(string
, '\"')) {
7498 if (xmlStrchr(string
, '\'')) {
7500 xmlGenericError(xmlGenericErrorContext
,
7501 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7503 xmlBufferCCat(buf
, "\"");
7504 base
= cur
= string
;
7508 xmlBufferAdd(buf
, base
, cur
- base
);
7509 xmlBufferAdd(buf
, BAD_CAST
""", 6);
7518 xmlBufferAdd(buf
, base
, cur
- base
);
7519 xmlBufferCCat(buf
, "\"");
7522 xmlBufferCCat(buf
, "\'");
7523 xmlBufferCat(buf
, string
);
7524 xmlBufferCCat(buf
, "\'");
7527 xmlBufferCCat(buf
, "\"");
7528 xmlBufferCat(buf
, string
);
7529 xmlBufferCCat(buf
, "\"");
7535 * xmlGetDocCompressMode:
7536 * @doc: the document
7538 * get the compression ratio for a document, ZLIB based
7539 * Returns 0 (uncompressed) to 9 (max compression)
7542 xmlGetDocCompressMode (xmlDocPtr doc
) {
7543 if (doc
== NULL
) return(-1);
7544 return(doc
->compression
);
7548 * xmlSetDocCompressMode:
7549 * @doc: the document
7550 * @mode: the compression ratio
7552 * set the compression ratio for a document, ZLIB based
7553 * Correct values: 0 (uncompressed) to 9 (max compression)
7556 xmlSetDocCompressMode (xmlDocPtr doc
, int mode
) {
7557 if (doc
== NULL
) return;
7558 if (mode
< 0) doc
->compression
= 0;
7559 else if (mode
> 9) doc
->compression
= 9;
7560 else doc
->compression
= mode
;
7564 * xmlGetCompressMode:
7566 * get the default compression mode used, ZLIB based.
7567 * Returns 0 (uncompressed) to 9 (max compression)
7570 xmlGetCompressMode(void)
7572 return (xmlCompressMode
);
7576 * xmlSetCompressMode:
7577 * @mode: the compression ratio
7579 * set the default compression mode used, ZLIB based
7580 * Correct values: 0 (uncompressed) to 9 (max compression)
7583 xmlSetCompressMode(int mode
) {
7584 if (mode
< 0) xmlCompressMode
= 0;
7585 else if (mode
> 9) xmlCompressMode
= 9;
7586 else xmlCompressMode
= mode
;
7589 #define XML_TREE_NSMAP_PARENT -1
7590 #define XML_TREE_NSMAP_XML -2
7591 #define XML_TREE_NSMAP_DOC -3
7592 #define XML_TREE_NSMAP_CUSTOM -4
7594 typedef struct xmlNsMapItem
*xmlNsMapItemPtr
;
7595 struct xmlNsMapItem
{
7596 xmlNsMapItemPtr next
;
7597 xmlNsMapItemPtr prev
;
7598 xmlNsPtr oldNs
; /* old ns decl reference */
7599 xmlNsPtr newNs
; /* new ns decl reference */
7600 int shadowDepth
; /* Shadowed at this depth */
7603 * >= 0 == @node's ns-decls
7604 * -1 == @parent's ns-decls
7605 * -2 == the doc->oldNs XML ns-decl
7606 * -3 == the doc->oldNs storage ns-decls
7607 * -4 == ns-decls provided via custom ns-handling
7612 typedef struct xmlNsMap
*xmlNsMapPtr
;
7614 xmlNsMapItemPtr first
;
7615 xmlNsMapItemPtr last
;
7616 xmlNsMapItemPtr pool
;
7619 #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7620 #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7621 #define XML_NSMAP_POP(m, i) \
7623 (m)->last = (i)->prev; \
7624 if ((m)->last == NULL) \
7625 (m)->first = NULL; \
7627 (m)->last->next = NULL; \
7628 (i)->next = (m)->pool; \
7632 * xmlDOMWrapNsMapFree:
7638 xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap
)
7640 xmlNsMapItemPtr cur
, tmp
;
7645 while (cur
!= NULL
) {
7651 while (cur
!= NULL
) {
7660 * xmlDOMWrapNsMapAddItem:
7662 * @oldNs: the old ns-struct
7663 * @newNs: the new ns-struct
7664 * @depth: depth and ns-kind information
7666 * Adds an ns-mapping item.
7668 static xmlNsMapItemPtr
7669 xmlDOMWrapNsMapAddItem(xmlNsMapPtr
*nsmap
, int position
,
7670 xmlNsPtr oldNs
, xmlNsPtr newNs
, int depth
)
7672 xmlNsMapItemPtr ret
;
7677 if ((position
!= -1) && (position
!= 0))
7683 * Create the ns-map.
7685 map
= (xmlNsMapPtr
) xmlMalloc(sizeof(struct xmlNsMap
));
7687 xmlTreeErrMemory("allocating namespace map");
7690 memset(map
, 0, sizeof(struct xmlNsMap
));
7694 if (map
->pool
!= NULL
) {
7696 * Reuse an item from the pool.
7699 map
->pool
= ret
->next
;
7700 memset(ret
, 0, sizeof(struct xmlNsMapItem
));
7703 * Create a new item.
7705 ret
= (xmlNsMapItemPtr
) xmlMalloc(sizeof(struct xmlNsMapItem
));
7707 xmlTreeErrMemory("allocating namespace map item");
7710 memset(ret
, 0, sizeof(struct xmlNsMapItem
));
7713 if (map
->first
== NULL
) {
7719 } else if (position
== -1) {
7723 ret
->prev
= map
->last
;
7724 map
->last
->next
= ret
;
7726 } else if (position
== 0) {
7728 * Set on first position.
7730 map
->first
->prev
= ret
;
7731 ret
->next
= map
->first
;
7738 ret
->shadowDepth
= -1;
7744 * xmlDOMWrapStoreNs:
7746 * @nsName: the namespace name
7747 * @prefix: the prefix
7749 * Creates or reuses an xmlNs struct on doc->oldNs with
7750 * the given prefix and namespace name.
7752 * Returns the aquired ns struct or NULL in case of an API
7753 * or internal error.
7756 xmlDOMWrapStoreNs(xmlDocPtr doc
,
7757 const xmlChar
*nsName
,
7758 const xmlChar
*prefix
)
7764 ns
= xmlTreeEnsureXMLDecl(doc
);
7767 if (ns
->next
!= NULL
) {
7770 while (ns
!= NULL
) {
7771 if (((ns
->prefix
== prefix
) ||
7772 xmlStrEqual(ns
->prefix
, prefix
)) &&
7773 xmlStrEqual(ns
->href
, nsName
)) {
7776 if (ns
->next
== NULL
)
7783 ns
->next
= xmlNewNs(NULL
, nsName
, prefix
);
7790 * xmlDOMWrapNewCtxt:
7792 * Allocates and initializes a new DOM-wrapper context.
7794 * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror.
7797 xmlDOMWrapNewCtxt(void)
7799 xmlDOMWrapCtxtPtr ret
;
7801 ret
= xmlMalloc(sizeof(xmlDOMWrapCtxt
));
7803 xmlTreeErrMemory("allocating DOM-wrapper context");
7806 memset(ret
, 0, sizeof(xmlDOMWrapCtxt
));
7811 * xmlDOMWrapFreeCtxt:
7812 * @ctxt: the DOM-wrapper context
7814 * Frees the DOM-wrapper context.
7817 xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt
)
7821 if (ctxt
->namespaceMap
!= NULL
)
7822 xmlDOMWrapNsMapFree((xmlNsMapPtr
) ctxt
->namespaceMap
);
7824 * TODO: Store the namespace map in the context.
7830 * xmlTreeLookupNsListByPrefix:
7831 * @nsList: a list of ns-structs
7832 * @prefix: the searched prefix
7834 * Searches for a ns-decl with the given prefix in @nsList.
7836 * Returns the ns-decl if found, NULL if not found and on
7840 xmlTreeNSListLookupByPrefix(xmlNsPtr nsList
, const xmlChar
*prefix
)
7848 if ((prefix
== ns
->prefix
) ||
7849 xmlStrEqual(prefix
, ns
->prefix
)) {
7853 } while (ns
!= NULL
);
7860 * xmlDOMWrapNSNormGatherInScopeNs:
7861 * @map: the namespace map
7862 * @node: the node to start with
7864 * Puts in-scope namespaces into the ns-map.
7866 * Returns 0 on success, -1 on API or internal errors.
7869 xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr
*map
,
7877 if ((map
== NULL
) || (*map
!= NULL
))
7880 * Get in-scope ns-decls of @parent.
7883 while ((cur
!= NULL
) && (cur
!= (xmlNodePtr
) cur
->doc
)) {
7884 if (cur
->type
== XML_ELEMENT_NODE
) {
7885 if (cur
->nsDef
!= NULL
) {
7889 if (XML_NSMAP_NOTEMPTY(*map
)) {
7891 * Skip shadowed prefixes.
7893 XML_NSMAP_FOREACH(*map
, mi
) {
7894 if ((ns
->prefix
== mi
->newNs
->prefix
) ||
7895 xmlStrEqual(ns
->prefix
, mi
->newNs
->prefix
)) {
7904 mi
= xmlDOMWrapNsMapAddItem(map
, 0, NULL
,
7905 ns
, XML_TREE_NSMAP_PARENT
);
7909 mi
->shadowDepth
= 0;
7911 } while (ns
!= NULL
);
7920 * XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
7921 * otherwise copy it, when it was in the source-dict.
7923 #define XML_TREE_ADOPT_STR(str) \
7924 if (adoptStr && (str != NULL)) { \
7925 if (destDoc->dict) { \
7926 const xmlChar *old = str; \
7927 str = xmlDictLookup(destDoc->dict, str, -1); \
7928 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
7929 (!xmlDictOwns(sourceDoc->dict, old))) \
7930 xmlFree((char *)old); \
7931 } else if ((sourceDoc) && (sourceDoc->dict) && \
7932 xmlDictOwns(sourceDoc->dict, str)) { \
7933 str = BAD_CAST xmlStrdup(str); \
7938 * XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
7939 * put it in dest-dict or copy it.
7941 #define XML_TREE_ADOPT_STR_2(str) \
7942 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
7943 (sourceDoc->dict != NULL) && \
7944 xmlDictOwns(sourceDoc->dict, cur->content)) { \
7945 if (destDoc->dict) \
7946 cur->content = (xmlChar *) \
7947 xmlDictLookup(destDoc->dict, cur->content, -1); \
7949 cur->content = xmlStrdup(BAD_CAST cur->content); \
7953 * xmlDOMWrapNSNormAddNsMapItem2:
7955 * For internal use. Adds a ns-decl mapping.
7957 * Returns 0 on success, -1 on internal errors.
7960 xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr
**list
, int *size
, int *number
,
7961 xmlNsPtr oldNs
, xmlNsPtr newNs
)
7963 if (*list
== NULL
) {
7964 *list
= (xmlNsPtr
*) xmlMalloc(6 * sizeof(xmlNsPtr
));
7965 if (*list
== NULL
) {
7966 xmlTreeErrMemory("alloc ns map item");
7971 } else if ((*number
) >= (*size
)) {
7973 *list
= (xmlNsPtr
*) xmlRealloc(*list
,
7974 (*size
) * 2 * sizeof(xmlNsPtr
));
7975 if (*list
== NULL
) {
7976 xmlTreeErrMemory("realloc ns map item");
7980 (*list
)[2 * (*number
)] = oldNs
;
7981 (*list
)[2 * (*number
) +1] = newNs
;
7987 * xmlDOMWrapRemoveNode:
7988 * @ctxt: a DOM wrapper context
7990 * @node: the node to be removed.
7991 * @options: set of options, unused at the moment
7993 * Unlinks the given node from its owner.
7994 * This will substitute ns-references to node->nsDef for
7995 * ns-references to doc->oldNs, thus ensuring the removed
7996 * branch to be autark wrt ns-references.
7998 * NOTE: This function was not intensively tested.
8000 * Returns 0 on success, 1 if the node is not supported,
8001 * -1 on API and internal errors.
8004 xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt
, xmlDocPtr doc
,
8005 xmlNodePtr node
, int options ATTRIBUTE_UNUSED
)
8007 xmlNsPtr
*list
= NULL
;
8008 int sizeList
, nbList
, i
, j
;
8011 if ((node
== NULL
) || (doc
== NULL
) || (node
->doc
!= doc
))
8014 /* TODO: 0 or -1 ? */
8015 if (node
->parent
== NULL
)
8018 switch (node
->type
) {
8020 case XML_CDATA_SECTION_NODE
:
8021 case XML_ENTITY_REF_NODE
:
8023 case XML_COMMENT_NODE
:
8024 xmlUnlinkNode(node
);
8026 case XML_ELEMENT_NODE
:
8027 case XML_ATTRIBUTE_NODE
:
8032 xmlUnlinkNode(node
);
8034 * Save out-of-scope ns-references in doc->oldNs.
8037 switch (node
->type
) {
8038 case XML_ELEMENT_NODE
:
8039 if ((ctxt
== NULL
) && (node
->nsDef
!= NULL
)) {
8042 if (xmlDOMWrapNSNormAddNsMapItem2(&list
, &sizeList
,
8043 &nbList
, ns
, ns
) == -1)
8044 goto internal_error
;
8046 } while (ns
!= NULL
);
8048 /* No break on purpose. */
8049 case XML_ATTRIBUTE_NODE
:
8050 if (node
->ns
!= NULL
) {
8055 for (i
= 0, j
= 0; i
< nbList
; i
++, j
+= 2) {
8056 if (node
->ns
== list
[j
]) {
8057 node
->ns
= list
[++j
];
8069 * Add to doc's oldNs.
8071 ns
= xmlDOMWrapStoreNs(doc
, node
->ns
->href
,
8074 goto internal_error
;
8080 if (xmlDOMWrapNSNormAddNsMapItem2(&list
, &sizeList
,
8081 &nbList
, node
->ns
, ns
) == -1)
8082 goto internal_error
;
8086 if ((node
->type
== XML_ELEMENT_NODE
) &&
8087 (node
->properties
!= NULL
)) {
8088 node
= (xmlNodePtr
) node
->properties
;
8096 if ((node
->type
== XML_ELEMENT_NODE
) &&
8097 (node
->children
!= NULL
)) {
8098 node
= node
->children
;
8104 if (node
->next
!= NULL
)
8107 node
= node
->parent
;
8110 } while (node
!= NULL
);
8123 * xmlSearchNsByNamespaceStrict:
8124 * @doc: the document
8125 * @node: the start node
8126 * @nsName: the searched namespace name
8127 * @retNs: the resulting ns-decl
8128 * @prefixed: if the found ns-decl must have a prefix (for attributes)
8130 * Dynamically searches for a ns-declaration which matches
8131 * the given @nsName in the ancestor-or-self axis of @node.
8133 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8134 * and internal errors.
8137 xmlSearchNsByNamespaceStrict(xmlDocPtr doc
, xmlNodePtr node
,
8138 const xmlChar
* nsName
,
8139 xmlNsPtr
*retNs
, int prefixed
)
8141 xmlNodePtr cur
, prev
= NULL
, out
= NULL
;
8142 xmlNsPtr ns
, prevns
;
8144 if ((doc
== NULL
) || (nsName
== NULL
) || (retNs
== NULL
))
8148 if (xmlStrEqual(nsName
, XML_XML_NAMESPACE
)) {
8149 *retNs
= xmlTreeEnsureXMLDecl(doc
);
8156 if (cur
->type
== XML_ELEMENT_NODE
) {
8157 if (cur
->nsDef
!= NULL
) {
8158 for (ns
= cur
->nsDef
; ns
!= NULL
; ns
= ns
->next
) {
8159 if (prefixed
&& (ns
->prefix
== NULL
))
8163 * Check the last level of ns-decls for a
8166 prevns
= prev
->nsDef
;
8168 if ((prevns
->prefix
== ns
->prefix
) ||
8169 ((prevns
->prefix
!= NULL
) &&
8170 (ns
->prefix
!= NULL
) &&
8171 xmlStrEqual(prevns
->prefix
, ns
->prefix
))) {
8177 prevns
= prevns
->next
;
8178 } while (prevns
!= NULL
);
8183 * Ns-name comparison.
8185 if ((nsName
== ns
->href
) ||
8186 xmlStrEqual(nsName
, ns
->href
)) {
8188 * At this point the prefix can only be shadowed,
8189 * if we are the the (at least) 3rd level of
8195 ret
= xmlNsInScope(doc
, node
, prev
, ns
->prefix
);
8199 * TODO: Should we try to find a matching ns-name
8200 * only once? This here keeps on searching.
8201 * I think we should try further since, there might
8202 * be an other matching ns-decl with an unshadowed
8215 } else if ((cur
->type
== XML_ENTITY_NODE
) ||
8216 (cur
->type
== XML_ENTITY_DECL
))
8219 } while ((cur
!= NULL
) && (cur
->doc
!= (xmlDocPtr
) cur
));
8224 * xmlSearchNsByPrefixStrict:
8225 * @doc: the document
8226 * @node: the start node
8227 * @prefix: the searched namespace prefix
8228 * @retNs: the resulting ns-decl
8230 * Dynamically searches for a ns-declaration which matches
8231 * the given @nsName in the ancestor-or-self axis of @node.
8233 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8234 * and internal errors.
8237 xmlSearchNsByPrefixStrict(xmlDocPtr doc
, xmlNodePtr node
,
8238 const xmlChar
* prefix
,
8244 if ((doc
== NULL
) || (node
== NULL
))
8249 if (IS_STR_XML(prefix
)) {
8251 *retNs
= xmlTreeEnsureXMLDecl(doc
);
8259 if (cur
->type
== XML_ELEMENT_NODE
) {
8260 if (cur
->nsDef
!= NULL
) {
8263 if ((prefix
== ns
->prefix
) ||
8264 xmlStrEqual(prefix
, ns
->prefix
))
8267 * Disabled namespaces, e.g. xmlns:abc="".
8269 if (ns
->href
== NULL
)
8276 } while (ns
!= NULL
);
8278 } else if ((cur
->type
== XML_ENTITY_NODE
) ||
8279 (cur
->type
== XML_ENTITY_DECL
))
8282 } while ((cur
!= NULL
) && (cur
->doc
!= (xmlDocPtr
) cur
));
8287 * xmlDOMWrapNSNormDeclareNsForced:
8289 * @elem: the element-node to declare on
8290 * @nsName: the namespace-name of the ns-decl
8291 * @prefix: the preferred prefix of the ns-decl
8292 * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
8294 * Declares a new namespace on @elem. It tries to use the
8295 * given @prefix; if a ns-decl with the given prefix is already existent
8296 * on @elem, it will generate an other prefix.
8298 * Returns 1 if a ns-decl was found, 0 if not and -1 on API
8299 * and internal errors.
8302 xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc
,
8304 const xmlChar
*nsName
,
8305 const xmlChar
*prefix
,
8311 const xmlChar
*pref
;
8314 * Create a ns-decl on @anchor.
8319 * Lookup whether the prefix is unused in elem's ns-decls.
8321 if ((elem
->nsDef
!= NULL
) &&
8322 (xmlTreeNSListLookupByPrefix(elem
->nsDef
, pref
) != NULL
))
8323 goto ns_next_prefix
;
8324 if (checkShadow
&& elem
->parent
&&
8325 ((xmlNodePtr
) elem
->parent
->doc
!= elem
->parent
)) {
8327 * Does it shadow ancestor ns-decls?
8329 if (xmlSearchNsByPrefixStrict(doc
, elem
->parent
, pref
, NULL
) == 1)
8330 goto ns_next_prefix
;
8332 ret
= xmlNewNs(NULL
, nsName
, pref
);
8335 if (elem
->nsDef
== NULL
)
8338 xmlNsPtr ns2
= elem
->nsDef
;
8339 while (ns2
->next
!= NULL
)
8348 if (prefix
== NULL
) {
8349 snprintf((char *) buf
, sizeof(buf
),
8352 snprintf((char *) buf
, sizeof(buf
),
8353 "%.30s_%d", (char *)prefix
, counter
);
8354 pref
= BAD_CAST buf
;
8359 * xmlDOMWrapNSNormAquireNormalizedNs:
8361 * @elem: the element-node to declare namespaces on
8362 * @ns: the ns-struct to use for the search
8363 * @retNs: the found/created ns-struct
8364 * @nsMap: the ns-map
8365 * @depth: the current tree depth
8366 * @ancestorsOnly: search in ancestor ns-decls only
8367 * @prefixed: if the searched ns-decl must have a prefix (for attributes)
8369 * Searches for a matching ns-name in the ns-decls of @nsMap, if not
8370 * found it will either declare it on @elem, or store it in doc->oldNs.
8371 * If a new ns-decl needs to be declared on @elem, it tries to use the
8372 * @ns->prefix for it, if this prefix is already in use on @elem, it will
8373 * change the prefix or the new ns-decl.
8375 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8378 xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc
,
8390 if ((doc
== NULL
) || (ns
== NULL
) || (retNs
== NULL
) ||
8396 * Handle XML namespace.
8398 if (IS_STR_XML(ns
->prefix
)) {
8400 * Insert XML namespace mapping.
8402 *retNs
= xmlTreeEnsureXMLDecl(doc
);
8408 * If the search should be done in ancestors only and no
8409 * @elem (the first ancestor) was specified, then skip the search.
8411 if ((XML_NSMAP_NOTEMPTY(*nsMap
)) &&
8412 (! (ancestorsOnly
&& (elem
== NULL
))))
8415 * Try to find an equal ns-name in in-scope ns-decls.
8417 XML_NSMAP_FOREACH(*nsMap
, mi
) {
8418 if ((mi
->depth
>= XML_TREE_NSMAP_PARENT
) &&
8420 * ancestorsOnly: This should be turned on to gain speed,
8421 * if one knows that the branch itself was already
8422 * ns-wellformed and no stale references existed.
8423 * I.e. it searches in the ancestor axis only.
8425 ((! ancestorsOnly
) || (mi
->depth
== XML_TREE_NSMAP_PARENT
)) &&
8426 /* Skip shadowed prefixes. */
8427 (mi
->shadowDepth
== -1) &&
8428 /* Skip xmlns="" or xmlns:foo="". */
8429 ((mi
->newNs
->href
!= NULL
) &&
8430 (mi
->newNs
->href
[0] != 0)) &&
8431 /* Ensure a prefix if wanted. */
8432 ((! prefixed
) || (mi
->newNs
->prefix
!= NULL
)) &&
8434 ((mi
->newNs
->href
== ns
->href
) ||
8435 xmlStrEqual(mi
->newNs
->href
, ns
->href
))) {
8436 /* Set the mapping. */
8444 * No luck, the namespace is out of scope or shadowed.
8450 * Store ns-decls in "oldNs" of the document-node.
8452 tmpns
= xmlDOMWrapStoreNs(doc
, ns
->href
, ns
->prefix
);
8458 if (xmlDOMWrapNsMapAddItem(nsMap
, -1, ns
,
8459 tmpns
, XML_TREE_NSMAP_DOC
) == NULL
) {
8467 tmpns
= xmlDOMWrapNSNormDeclareNsForced(doc
, elem
, ns
->href
,
8472 if (*nsMap
!= NULL
) {
8474 * Does it shadow ancestor ns-decls?
8476 XML_NSMAP_FOREACH(*nsMap
, mi
) {
8477 if ((mi
->depth
< depth
) &&
8478 (mi
->shadowDepth
== -1) &&
8479 ((ns
->prefix
== mi
->newNs
->prefix
) ||
8480 xmlStrEqual(ns
->prefix
, mi
->newNs
->prefix
))) {
8484 mi
->shadowDepth
= depth
;
8489 if (xmlDOMWrapNsMapAddItem(nsMap
, -1, ns
, tmpns
, depth
) == NULL
) {
8499 XML_DOM_RECONNS_REMOVEREDUND
= 1<<0
8500 } xmlDOMReconcileNSOptions
;
8503 * xmlDOMWrapReconcileNamespaces:
8504 * @ctxt: DOM wrapper context, unused at the moment
8505 * @elem: the element-node
8506 * @options: option flags
8508 * Ensures that ns-references point to ns-decls hold on element-nodes.
8509 * Ensures that the tree is namespace wellformed by creating additional
8510 * ns-decls where needed. Note that, since prefixes of already existent
8511 * ns-decls can be shadowed by this process, it could break QNames in
8512 * attribute values or element content.
8514 * NOTE: This function was not intensively tested.
8516 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8520 xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED
,
8524 int depth
= -1, adoptns
= 0, parnsdone
= 0;
8525 xmlNsPtr ns
, prevns
;
8527 xmlNodePtr cur
, curElem
= NULL
;
8528 xmlNsMapPtr nsMap
= NULL
;
8529 xmlNsMapItemPtr
/* topmi = NULL, */ mi
;
8530 /* @ancestorsOnly should be set by an option flag. */
8531 int ancestorsOnly
= 0;
8532 int optRemoveRedundantNS
=
8533 ((xmlDOMReconcileNSOptions
) options
& XML_DOM_RECONNS_REMOVEREDUND
) ? 1 : 0;
8534 xmlNsPtr
*listRedund
= NULL
;
8535 int sizeRedund
= 0, nbRedund
= 0, ret
, i
, j
;
8537 if ((elem
== NULL
) || (elem
->doc
== NULL
) ||
8538 (elem
->type
!= XML_ELEMENT_NODE
))
8544 switch (cur
->type
) {
8545 case XML_ELEMENT_NODE
:
8550 * Namespace declarations.
8552 if (cur
->nsDef
!= NULL
) {
8555 while (ns
!= NULL
) {
8557 if ((elem
->parent
) &&
8558 ((xmlNodePtr
) elem
->parent
->doc
!= elem
->parent
)) {
8560 * Gather ancestor in-scope ns-decls.
8562 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap
,
8563 elem
->parent
) == -1)
8564 goto internal_error
;
8570 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8572 if (optRemoveRedundantNS
&& XML_NSMAP_NOTEMPTY(nsMap
)) {
8573 XML_NSMAP_FOREACH(nsMap
, mi
) {
8574 if ((mi
->depth
>= XML_TREE_NSMAP_PARENT
) &&
8575 (mi
->shadowDepth
== -1) &&
8576 ((ns
->prefix
== mi
->newNs
->prefix
) ||
8577 xmlStrEqual(ns
->prefix
, mi
->newNs
->prefix
)) &&
8578 ((ns
->href
== mi
->newNs
->href
) ||
8579 xmlStrEqual(ns
->href
, mi
->newNs
->href
)))
8582 * A redundant ns-decl was found.
8583 * Add it to the list of redundant ns-decls.
8585 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund
,
8586 &sizeRedund
, &nbRedund
, ns
, mi
->newNs
) == -1)
8587 goto internal_error
;
8589 * Remove the ns-decl from the element-node.
8592 prevns
->next
= ns
->next
;
8594 cur
->nsDef
= ns
->next
;
8601 * Skip ns-references handling if the referenced
8602 * ns-decl is declared on the same element.
8604 if ((cur
->ns
!= NULL
) && adoptns
&& (cur
->ns
== ns
))
8607 * Does it shadow any ns-decl?
8609 if (XML_NSMAP_NOTEMPTY(nsMap
)) {
8610 XML_NSMAP_FOREACH(nsMap
, mi
) {
8611 if ((mi
->depth
>= XML_TREE_NSMAP_PARENT
) &&
8612 (mi
->shadowDepth
== -1) &&
8613 ((ns
->prefix
== mi
->newNs
->prefix
) ||
8614 xmlStrEqual(ns
->prefix
, mi
->newNs
->prefix
))) {
8616 mi
->shadowDepth
= depth
;
8623 if (xmlDOMWrapNsMapAddItem(&nsMap
, -1, ns
, ns
,
8625 goto internal_error
;
8634 /* No break on purpose. */
8635 case XML_ATTRIBUTE_NODE
:
8636 /* No ns, no fun. */
8637 if (cur
->ns
== NULL
)
8641 if ((elem
->parent
) &&
8642 ((xmlNodePtr
) elem
->parent
->doc
!= elem
->parent
)) {
8643 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap
,
8644 elem
->parent
) == -1)
8645 goto internal_error
;
8650 * Adjust the reference if this was a redundant ns-decl.
8653 for (i
= 0, j
= 0; i
< nbRedund
; i
++, j
+= 2) {
8654 if (cur
->ns
== listRedund
[j
]) {
8655 cur
->ns
= listRedund
[++j
];
8661 * Adopt ns-references.
8663 if (XML_NSMAP_NOTEMPTY(nsMap
)) {
8665 * Search for a mapping.
8667 XML_NSMAP_FOREACH(nsMap
, mi
) {
8668 if ((mi
->shadowDepth
== -1) &&
8669 (cur
->ns
== mi
->oldNs
)) {
8671 cur
->ns
= mi
->newNs
;
8677 * Aquire a normalized ns-decl and add it to the map.
8679 if (xmlDOMWrapNSNormAquireNormalizedNs(doc
, curElem
,
8683 (cur
->type
== XML_ATTRIBUTE_NODE
) ? 1 : 0) == -1)
8684 goto internal_error
;
8688 if ((cur
->type
== XML_ELEMENT_NODE
) &&
8689 (cur
->properties
!= NULL
)) {
8691 * Process attributes.
8693 cur
= (xmlNodePtr
) cur
->properties
;
8701 if ((cur
->type
== XML_ELEMENT_NODE
) &&
8702 (cur
->children
!= NULL
)) {
8704 * Process content of element-nodes only.
8706 cur
= cur
->children
;
8712 if (cur
->type
== XML_ELEMENT_NODE
) {
8713 if (XML_NSMAP_NOTEMPTY(nsMap
)) {
8717 while ((nsMap
->last
!= NULL
) &&
8718 (nsMap
->last
->depth
>= depth
))
8720 XML_NSMAP_POP(nsMap
, mi
)
8725 XML_NSMAP_FOREACH(nsMap
, mi
) {
8726 if (mi
->shadowDepth
>= depth
)
8727 mi
->shadowDepth
= -1;
8732 if (cur
->next
!= NULL
)
8735 if (cur
->type
== XML_ATTRIBUTE_NODE
) {
8742 } while (cur
!= NULL
);
8750 for (i
= 0, j
= 0; i
< nbRedund
; i
++, j
+= 2) {
8751 xmlFreeNs(listRedund
[j
]);
8753 xmlFree(listRedund
);
8756 xmlDOMWrapNsMapFree(nsMap
);
8761 * xmlDOMWrapAdoptBranch:
8762 * @ctxt: the optional context for custom processing
8763 * @sourceDoc: the optional sourceDoc
8764 * @node: the element-node to start with
8765 * @destDoc: the destination doc for adoption
8766 * @destParent: the optional new parent of @node in @destDoc
8767 * @options: option flags
8769 * Ensures that ns-references point to @destDoc: either to
8770 * elements->nsDef entries if @destParent is given, or to
8771 * @destDoc->oldNs otherwise.
8772 * If @destParent is given, it ensures that the tree is namespace
8773 * wellformed by creating additional ns-decls where needed.
8774 * Note that, since prefixes of already existent ns-decls can be
8775 * shadowed by this process, it could break QNames in attribute
8776 * values or element content.
8778 * NOTE: This function was not intensively tested.
8780 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8783 xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt
,
8784 xmlDocPtr sourceDoc
,
8787 xmlNodePtr destParent
,
8788 int options ATTRIBUTE_UNUSED
)
8791 xmlNodePtr cur
, curElem
= NULL
;
8792 xmlNsMapPtr nsMap
= NULL
;
8795 int depth
= -1, adoptStr
= 1;
8796 /* gather @parent's ns-decls. */
8798 /* @ancestorsOnly should be set per option. */
8799 int ancestorsOnly
= 0;
8802 * Optimize string adoption for equal or none dicts.
8804 if ((sourceDoc
!= NULL
) &&
8805 (sourceDoc
->dict
== destDoc
->dict
))
8811 * Get the ns-map from the context if available.
8814 nsMap
= (xmlNsMapPtr
) ctxt
->namespaceMap
;
8816 * Disable search for ns-decls in the parent-axis of the
8817 * desination element, if:
8818 * 1) there's no destination parent
8819 * 2) custom ns-reference handling is used
8821 if ((destParent
== NULL
) ||
8822 (ctxt
&& ctxt
->getNsForNodeFunc
))
8829 while (cur
!= NULL
) {
8831 * Paranoid source-doc sanity check.
8833 if (cur
->doc
!= sourceDoc
) {
8835 * We'll assume XIncluded nodes if the doc differs.
8836 * TODO: Do we need to reconciliate XIncluded nodes?
8837 * This here skips XIncluded nodes and tries to handle
8840 if (cur
->next
== NULL
)
8844 if ((cur
->type
== XML_XINCLUDE_END
) ||
8845 (cur
->doc
== node
->doc
))
8847 } while (cur
->next
!= NULL
);
8849 if (cur
->doc
!= node
->doc
)
8853 switch (cur
->type
) {
8854 case XML_XINCLUDE_START
:
8855 case XML_XINCLUDE_END
:
8860 case XML_ELEMENT_NODE
:
8864 * Namespace declarations.
8865 * - ns->href and ns->prefix are never in the dict, so
8866 * we need not move the values over to the destination dict.
8867 * - Note that for custom handling of ns-references,
8868 * the ns-decls need not be stored in the ns-map,
8869 * since they won't be referenced by node->ns.
8872 ((ctxt
== NULL
) || (ctxt
->getNsForNodeFunc
== NULL
)))
8876 * Gather @parent's in-scope ns-decls.
8878 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap
,
8880 goto internal_error
;
8883 for (ns
= cur
->nsDef
; ns
!= NULL
; ns
= ns
->next
) {
8885 * NOTE: ns->prefix and ns->href are never in the dict.
8886 * XML_TREE_ADOPT_STR(ns->prefix)
8887 * XML_TREE_ADOPT_STR(ns->href)
8890 * Does it shadow any ns-decl?
8892 if (XML_NSMAP_NOTEMPTY(nsMap
)) {
8893 XML_NSMAP_FOREACH(nsMap
, mi
) {
8894 if ((mi
->depth
>= XML_TREE_NSMAP_PARENT
) &&
8895 (mi
->shadowDepth
== -1) &&
8896 ((ns
->prefix
== mi
->newNs
->prefix
) ||
8897 xmlStrEqual(ns
->prefix
,
8898 mi
->newNs
->prefix
))) {
8900 mi
->shadowDepth
= depth
;
8907 if (xmlDOMWrapNsMapAddItem(&nsMap
, -1,
8908 ns
, ns
, depth
) == NULL
)
8909 goto internal_error
;
8912 /* No break on purpose. */
8913 case XML_ATTRIBUTE_NODE
:
8914 /* No namespace, no fun. */
8915 if (cur
->ns
== NULL
)
8919 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap
,
8921 goto internal_error
;
8925 * Adopt ns-references.
8927 if (XML_NSMAP_NOTEMPTY(nsMap
)) {
8929 * Search for a mapping.
8931 XML_NSMAP_FOREACH(nsMap
, mi
) {
8932 if ((mi
->shadowDepth
== -1) &&
8933 (cur
->ns
== mi
->oldNs
)) {
8935 cur
->ns
= mi
->newNs
;
8941 * No matching namespace in scope. We need a new one.
8943 if ((ctxt
) && (ctxt
->getNsForNodeFunc
)) {
8945 * User-defined behaviour.
8947 ns
= ctxt
->getNsForNodeFunc(ctxt
, cur
,
8948 cur
->ns
->href
, cur
->ns
->prefix
);
8950 * Insert mapping if ns is available; it's the users fault
8953 if (xmlDOMWrapNsMapAddItem(&nsMap
, -1,
8954 cur
->ns
, ns
, XML_TREE_NSMAP_CUSTOM
) == NULL
)
8955 goto internal_error
;
8959 * Aquire a normalized ns-decl and add it to the map.
8961 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc
,
8962 /* ns-decls on curElem or on destDoc->oldNs */
8963 destParent
? curElem
: NULL
,
8967 /* ns-decls must be prefixed for attributes. */
8968 (cur
->type
== XML_ATTRIBUTE_NODE
) ? 1 : 0) == -1)
8969 goto internal_error
;
8974 * Further node properties.
8975 * TODO: Is this all?
8977 XML_TREE_ADOPT_STR(cur
->name
)
8978 if (cur
->type
== XML_ELEMENT_NODE
) {
8985 if (cur
->properties
!= NULL
) {
8987 * Process first attribute node.
8989 cur
= (xmlNodePtr
) cur
->properties
;
8996 if ((sourceDoc
!= NULL
) &&
8997 (((xmlAttrPtr
) cur
)->atype
== XML_ATTRIBUTE_ID
))
8999 xmlRemoveID(sourceDoc
, (xmlAttrPtr
) cur
);
9001 ((xmlAttrPtr
) cur
)->atype
= 0;
9002 ((xmlAttrPtr
) cur
)->psvi
= NULL
;
9006 case XML_CDATA_SECTION_NODE
:
9008 * This puts the content in the dest dict, only if
9009 * it was previously in the source dict.
9011 XML_TREE_ADOPT_STR_2(cur
->content
)
9013 case XML_ENTITY_REF_NODE
:
9015 * Remove reference to the entitity-node.
9017 cur
->content
= NULL
;
9018 cur
->children
= NULL
;
9020 if ((destDoc
->intSubset
) || (destDoc
->extSubset
)) {
9023 * Assign new entity-node if available.
9025 ent
= xmlGetDocEntity(destDoc
, cur
->name
);
9027 cur
->content
= ent
->content
;
9028 cur
->children
= (xmlNodePtr
) ent
;
9029 cur
->last
= (xmlNodePtr
) ent
;
9034 XML_TREE_ADOPT_STR(cur
->name
)
9035 XML_TREE_ADOPT_STR_2(cur
->content
)
9037 case XML_COMMENT_NODE
:
9040 goto internal_error
;
9045 if (cur
->children
!= NULL
) {
9046 cur
= cur
->children
;
9053 if ((cur
->type
== XML_ELEMENT_NODE
) ||
9054 (cur
->type
== XML_XINCLUDE_START
) ||
9055 (cur
->type
== XML_XINCLUDE_END
))
9058 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9060 if (XML_NSMAP_NOTEMPTY(nsMap
)) {
9064 while ((nsMap
->last
!= NULL
) &&
9065 (nsMap
->last
->depth
>= depth
))
9067 XML_NSMAP_POP(nsMap
, mi
)
9072 XML_NSMAP_FOREACH(nsMap
, mi
) {
9073 if (mi
->shadowDepth
>= depth
)
9074 mi
->shadowDepth
= -1;
9079 if (cur
->next
!= NULL
)
9081 else if ((cur
->type
== XML_ATTRIBUTE_NODE
) &&
9082 (cur
->parent
->children
!= NULL
))
9084 cur
= cur
->parent
->children
;
9100 if (nsMap
!= NULL
) {
9101 if ((ctxt
) && (ctxt
->namespaceMap
== nsMap
)) {
9103 * Just cleanup the map but don't free.
9107 nsMap
->last
->next
= nsMap
->pool
;
9108 nsMap
->pool
= nsMap
->first
;
9109 nsMap
->first
= NULL
;
9112 xmlDOMWrapNsMapFree(nsMap
);
9118 * xmlDOMWrapCloneNode:
9119 * @ctxt: the optional context for custom processing
9120 * @sourceDoc: the optional sourceDoc
9121 * @node: the node to start with
9122 * @resNode: the clone of the given @node
9123 * @destDoc: the destination doc
9124 * @destParent: the optional new parent of @node in @destDoc
9125 * @deep: descend into child if set
9126 * @options: option flags
9128 * References of out-of scope ns-decls are remapped to point to @destDoc:
9129 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9130 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
9131 * This is the case when you don't know already where the cloned branch
9134 * If @destParent is given, it ensures that the tree is namespace
9135 * wellformed by creating additional ns-decls where needed.
9136 * Note that, since prefixes of already existent ns-decls can be
9137 * shadowed by this process, it could break QNames in attribute
9138 * values or element content.
9140 * 1) What to do with XInclude? Currently this returns an error for XInclude.
9142 * Returns 0 if the operation succeeded,
9143 * 1 if a node of unsupported (or not yet supported) type was given,
9144 * -1 on API/internal errors.
9148 xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt
,
9149 xmlDocPtr sourceDoc
,
9151 xmlNodePtr
*resNode
,
9153 xmlNodePtr destParent
,
9155 int options ATTRIBUTE_UNUSED
)
9158 xmlNodePtr cur
, curElem
= NULL
;
9159 xmlNsMapPtr nsMap
= NULL
;
9163 /* int adoptStr = 1; */
9164 /* gather @parent's ns-decls. */
9168 * TODO: @ancestorsOnly should be set per option.
9171 int ancestorsOnly
= 0;
9172 xmlNodePtr resultClone
= NULL
, clone
= NULL
, parentClone
= NULL
, prevClone
= NULL
;
9173 xmlNsPtr cloneNs
= NULL
, *cloneNsDefSlot
= NULL
;
9174 xmlDictPtr dict
; /* The destination dict */
9176 if ((node
== NULL
) || (resNode
== NULL
) || (destDoc
== NULL
))
9179 * TODO: Initially we support only element-nodes.
9181 if (node
->type
!= XML_ELEMENT_NODE
)
9184 * Check node->doc sanity.
9186 if ((node
->doc
!= NULL
) && (sourceDoc
!= NULL
) &&
9187 (node
->doc
!= sourceDoc
)) {
9189 * Might be an XIncluded node.
9193 if (sourceDoc
== NULL
)
9194 sourceDoc
= node
->doc
;
9195 if (sourceDoc
== NULL
)
9198 dict
= destDoc
->dict
;
9200 * Reuse the namespace map of the context.
9203 nsMap
= (xmlNsMapPtr
) ctxt
->namespaceMap
;
9208 while (cur
!= NULL
) {
9209 if (cur
->doc
!= sourceDoc
) {
9211 * We'll assume XIncluded nodes if the doc differs.
9212 * TODO: Do we need to reconciliate XIncluded nodes?
9213 * TODO: This here returns -1 in this case.
9215 goto internal_error
;
9218 * Create a new node.
9220 switch (cur
->type
) {
9221 case XML_XINCLUDE_START
:
9222 case XML_XINCLUDE_END
:
9224 * TODO: What to do with XInclude?
9226 goto internal_error
;
9228 case XML_ELEMENT_NODE
:
9230 case XML_CDATA_SECTION_NODE
:
9231 case XML_COMMENT_NODE
:
9233 case XML_DOCUMENT_FRAG_NODE
:
9234 case XML_ENTITY_REF_NODE
:
9235 case XML_ENTITY_NODE
:
9237 * Nodes of xmlNode structure.
9239 clone
= (xmlNodePtr
) xmlMalloc(sizeof(xmlNode
));
9240 if (clone
== NULL
) {
9241 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
9242 goto internal_error
;
9244 memset(clone
, 0, sizeof(xmlNode
));
9246 * Set hierachical links.
9248 if (resultClone
!= NULL
) {
9249 clone
->parent
= parentClone
;
9251 prevClone
->next
= clone
;
9252 clone
->prev
= prevClone
;
9254 parentClone
->children
= clone
;
9256 resultClone
= clone
;
9259 case XML_ATTRIBUTE_NODE
:
9261 * Attributes (xmlAttr).
9263 clone
= (xmlNodePtr
) xmlMalloc(sizeof(xmlAttr
));
9264 if (clone
== NULL
) {
9265 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
9266 goto internal_error
;
9268 memset(clone
, 0, sizeof(xmlAttr
));
9270 * Set hierachical links.
9271 * TODO: Change this to add to the end of attributes.
9273 if (resultClone
!= NULL
) {
9274 clone
->parent
= parentClone
;
9276 prevClone
->next
= clone
;
9277 clone
->prev
= prevClone
;
9279 parentClone
->properties
= (xmlAttrPtr
) clone
;
9281 resultClone
= clone
;
9285 * TODO QUESTION: Any other nodes expected?
9287 goto internal_error
;
9290 clone
->type
= cur
->type
;
9291 clone
->doc
= destDoc
;
9294 * Clone the name of the node if any.
9296 if (cur
->name
== xmlStringText
)
9297 clone
->name
= xmlStringText
;
9298 else if (cur
->name
== xmlStringTextNoenc
)
9300 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9301 * in tree.c, it might be set in Libxslt via
9302 * "xsl:disable-output-escaping".
9304 clone
->name
= xmlStringTextNoenc
;
9305 else if (cur
->name
== xmlStringComment
)
9306 clone
->name
= xmlStringComment
;
9307 else if (cur
->name
!= NULL
) {
9308 DICT_CONST_COPY(cur
->name
, clone
->name
);
9311 switch (cur
->type
) {
9312 case XML_XINCLUDE_START
:
9313 case XML_XINCLUDE_END
:
9318 case XML_ELEMENT_NODE
:
9322 * Namespace declarations.
9324 if (cur
->nsDef
!= NULL
) {
9326 if (destParent
&& (ctxt
== NULL
)) {
9328 * Gather @parent's in-scope ns-decls.
9330 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap
,
9332 goto internal_error
;
9337 * Clone namespace declarations.
9339 cloneNsDefSlot
= &(clone
->nsDef
);
9340 for (ns
= cur
->nsDef
; ns
!= NULL
; ns
= ns
->next
) {
9342 * Create a new xmlNs.
9344 cloneNs
= (xmlNsPtr
) xmlMalloc(sizeof(xmlNs
));
9345 if (cloneNs
== NULL
) {
9346 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
9347 "allocating namespace");
9350 memset(cloneNs
, 0, sizeof(xmlNs
));
9351 cloneNs
->type
= XML_LOCAL_NAMESPACE
;
9353 if (ns
->href
!= NULL
)
9354 cloneNs
->href
= xmlStrdup(ns
->href
);
9355 if (ns
->prefix
!= NULL
)
9356 cloneNs
->prefix
= xmlStrdup(ns
->prefix
);
9358 *cloneNsDefSlot
= cloneNs
;
9359 cloneNsDefSlot
= &(cloneNs
->next
);
9362 * Note that for custom handling of ns-references,
9363 * the ns-decls need not be stored in the ns-map,
9364 * since they won't be referenced by node->ns.
9366 if ((ctxt
== NULL
) ||
9367 (ctxt
->getNsForNodeFunc
== NULL
))
9370 * Does it shadow any ns-decl?
9372 if (XML_NSMAP_NOTEMPTY(nsMap
)) {
9373 XML_NSMAP_FOREACH(nsMap
, mi
) {
9374 if ((mi
->depth
>= XML_TREE_NSMAP_PARENT
) &&
9375 (mi
->shadowDepth
== -1) &&
9376 ((ns
->prefix
== mi
->newNs
->prefix
) ||
9377 xmlStrEqual(ns
->prefix
,
9378 mi
->newNs
->prefix
))) {
9380 * Mark as shadowed at the current
9383 mi
->shadowDepth
= depth
;
9390 if (xmlDOMWrapNsMapAddItem(&nsMap
, -1,
9391 ns
, cloneNs
, depth
) == NULL
)
9392 goto internal_error
;
9396 /* cur->ns will be processed further down. */
9398 case XML_ATTRIBUTE_NODE
:
9399 /* IDs will be processed further down. */
9400 /* cur->ns will be processed further down. */
9403 case XML_CDATA_SECTION_NODE
:
9405 * Note that this will also cover the values of attributes.
9407 DICT_COPY(cur
->content
, clone
->content
);
9409 case XML_ENTITY_NODE
:
9410 /* TODO: What to do here? */
9412 case XML_ENTITY_REF_NODE
:
9413 if (sourceDoc
!= destDoc
) {
9414 if ((destDoc
->intSubset
) || (destDoc
->extSubset
)) {
9417 * Different doc: Assign new entity-node if available.
9419 ent
= xmlGetDocEntity(destDoc
, cur
->name
);
9421 clone
->content
= ent
->content
;
9422 clone
->children
= (xmlNodePtr
) ent
;
9423 clone
->last
= (xmlNodePtr
) ent
;
9428 * Same doc: Use the current node's entity declaration
9431 clone
->content
= cur
->content
;
9432 clone
->children
= cur
->children
;
9433 clone
->last
= cur
->last
;
9437 DICT_COPY(cur
->content
, clone
->content
);
9439 case XML_COMMENT_NODE
:
9440 DICT_COPY(cur
->content
, clone
->content
);
9443 goto internal_error
;
9446 if (cur
->ns
== NULL
)
9447 goto end_ns_reference
;
9449 /* handle_ns_reference: */
9451 ** The following will take care of references to ns-decls ********
9452 ** and is intended only for element- and attribute-nodes.
9456 if (destParent
&& (ctxt
== NULL
)) {
9457 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap
, destParent
) == -1)
9458 goto internal_error
;
9463 * Adopt ns-references.
9465 if (XML_NSMAP_NOTEMPTY(nsMap
)) {
9467 * Search for a mapping.
9469 XML_NSMAP_FOREACH(nsMap
, mi
) {
9470 if ((mi
->shadowDepth
== -1) &&
9471 (cur
->ns
== mi
->oldNs
)) {
9473 * This is the nice case: a mapping was found.
9475 clone
->ns
= mi
->newNs
;
9476 goto end_ns_reference
;
9481 * No matching namespace in scope. We need a new one.
9483 if ((ctxt
!= NULL
) && (ctxt
->getNsForNodeFunc
!= NULL
)) {
9485 * User-defined behaviour.
9487 ns
= ctxt
->getNsForNodeFunc(ctxt
, cur
,
9488 cur
->ns
->href
, cur
->ns
->prefix
);
9490 * Add user's mapping.
9492 if (xmlDOMWrapNsMapAddItem(&nsMap
, -1,
9493 cur
->ns
, ns
, XML_TREE_NSMAP_CUSTOM
) == NULL
)
9494 goto internal_error
;
9498 * Aquire a normalized ns-decl and add it to the map.
9500 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc
,
9501 /* ns-decls on curElem or on destDoc->oldNs */
9502 destParent
? curElem
: NULL
,
9505 /* if we need to search only in the ancestor-axis */
9507 /* ns-decls must be prefixed for attributes. */
9508 (cur
->type
== XML_ATTRIBUTE_NODE
) ? 1 : 0) == -1)
9509 goto internal_error
;
9516 * Some post-processing.
9518 * Handle ID attributes.
9520 if ((clone
->type
== XML_ATTRIBUTE_NODE
) &&
9521 (clone
->parent
!= NULL
))
9523 if (xmlIsID(destDoc
, clone
->parent
, (xmlAttrPtr
) clone
)) {
9527 idVal
= xmlNodeListGetString(cur
->doc
, cur
->children
, 1);
9528 if (idVal
!= NULL
) {
9529 if (xmlAddID(NULL
, destDoc
, idVal
, (xmlAttrPtr
) cur
) == NULL
) {
9530 /* TODO: error message. */
9532 goto internal_error
;
9540 ** The following will traverse the tree **************************
9543 * Walk the element's attributes before descending into child-nodes.
9545 if ((cur
->type
== XML_ELEMENT_NODE
) && (cur
->properties
!= NULL
)) {
9547 parentClone
= clone
;
9548 cur
= (xmlNodePtr
) cur
->properties
;
9553 * Descend into child-nodes.
9555 if (cur
->children
!= NULL
) {
9556 if (deep
|| (cur
->type
== XML_ATTRIBUTE_NODE
)) {
9558 parentClone
= clone
;
9559 cur
= cur
->children
;
9566 * At this point we are done with the node, its content
9567 * and an element-nodes's attribute-nodes.
9571 if ((cur
->type
== XML_ELEMENT_NODE
) ||
9572 (cur
->type
== XML_XINCLUDE_START
) ||
9573 (cur
->type
== XML_XINCLUDE_END
)) {
9575 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9577 if (XML_NSMAP_NOTEMPTY(nsMap
)) {
9581 while ((nsMap
->last
!= NULL
) &&
9582 (nsMap
->last
->depth
>= depth
))
9584 XML_NSMAP_POP(nsMap
, mi
)
9589 XML_NSMAP_FOREACH(nsMap
, mi
) {
9590 if (mi
->shadowDepth
>= depth
)
9591 mi
->shadowDepth
= -1;
9596 if (cur
->next
!= NULL
) {
9599 } else if (cur
->type
!= XML_ATTRIBUTE_NODE
) {
9603 if (clone
->parent
!= NULL
)
9604 clone
->parent
->last
= clone
;
9605 clone
= clone
->parent
;
9606 parentClone
= clone
->parent
;
9608 * Process parent --> next;
9613 /* This is for attributes only. */
9614 clone
= clone
->parent
;
9615 parentClone
= clone
->parent
;
9617 * Process parent-element --> children.
9632 if (nsMap
!= NULL
) {
9633 if ((ctxt
) && (ctxt
->namespaceMap
== nsMap
)) {
9635 * Just cleanup the map but don't free.
9639 nsMap
->last
->next
= nsMap
->pool
;
9640 nsMap
->pool
= nsMap
->first
;
9641 nsMap
->first
= NULL
;
9644 xmlDOMWrapNsMapFree(nsMap
);
9647 * TODO: Should we try a cleanup of the cloned node in case of a
9650 *resNode
= resultClone
;
9655 * xmlDOMWrapAdoptAttr:
9656 * @ctxt: the optional context for custom processing
9657 * @sourceDoc: the optional source document of attr
9658 * @attr: the attribute-node to be adopted
9659 * @destDoc: the destination doc for adoption
9660 * @destParent: the optional new parent of @attr in @destDoc
9661 * @options: option flags
9663 * @attr is adopted by @destDoc.
9664 * Ensures that ns-references point to @destDoc: either to
9665 * elements->nsDef entries if @destParent is given, or to
9666 * @destDoc->oldNs otherwise.
9668 * Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9671 xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt
,
9672 xmlDocPtr sourceDoc
,
9675 xmlNodePtr destParent
,
9676 int options ATTRIBUTE_UNUSED
)
9681 if ((attr
== NULL
) || (destDoc
== NULL
))
9684 attr
->doc
= destDoc
;
9685 if (attr
->ns
!= NULL
) {
9689 /* TODO: User defined. */
9691 /* XML Namespace. */
9692 if (IS_STR_XML(attr
->ns
->prefix
)) {
9693 ns
= xmlTreeEnsureXMLDecl(destDoc
);
9694 } else if (destParent
== NULL
) {
9696 * Store in @destDoc->oldNs.
9698 ns
= xmlDOMWrapStoreNs(destDoc
, attr
->ns
->href
, attr
->ns
->prefix
);
9701 * Declare on @destParent.
9703 if (xmlSearchNsByNamespaceStrict(destDoc
, destParent
, attr
->ns
->href
,
9705 goto internal_error
;
9707 ns
= xmlDOMWrapNSNormDeclareNsForced(destDoc
, destParent
,
9708 attr
->ns
->href
, attr
->ns
->prefix
, 1);
9712 goto internal_error
;
9716 XML_TREE_ADOPT_STR(attr
->name
);
9722 if (attr
->children
== NULL
)
9724 cur
= attr
->children
;
9725 while (cur
!= NULL
) {
9727 switch (cur
->type
) {
9729 case XML_CDATA_SECTION_NODE
:
9730 XML_TREE_ADOPT_STR_2(cur
->content
)
9732 case XML_ENTITY_REF_NODE
:
9734 * Remove reference to the entitity-node.
9736 cur
->content
= NULL
;
9737 cur
->children
= NULL
;
9739 if ((destDoc
->intSubset
) || (destDoc
->extSubset
)) {
9742 * Assign new entity-node if available.
9744 ent
= xmlGetDocEntity(destDoc
, cur
->name
);
9746 cur
->content
= ent
->content
;
9747 cur
->children
= (xmlNodePtr
) ent
;
9748 cur
->last
= (xmlNodePtr
) ent
;
9755 if (cur
->children
!= NULL
) {
9756 cur
= cur
->children
;
9760 if (cur
== (xmlNodePtr
) attr
)
9762 if (cur
->next
!= NULL
)
9775 * xmlDOMWrapAdoptNode:
9776 * @ctxt: the optional context for custom processing
9777 * @sourceDoc: the optional sourceDoc
9778 * @node: the node to start with
9779 * @destDoc: the destination doc
9780 * @destParent: the optional new parent of @node in @destDoc
9781 * @options: option flags
9783 * References of out-of scope ns-decls are remapped to point to @destDoc:
9784 * 1) If @destParent is given, then nsDef entries on element-nodes are used
9785 * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
9786 * This is the case when you have an unliked node and just want to move it
9789 * If @destParent is given, it ensures that the tree is namespace
9790 * wellformed by creating additional ns-decls where needed.
9791 * Note that, since prefixes of already existent ns-decls can be
9792 * shadowed by this process, it could break QNames in attribute
9793 * values or element content.
9794 * NOTE: This function was not intensively tested.
9796 * Returns 0 if the operation succeeded,
9797 * 1 if a node of unsupported type was given,
9798 * 2 if a node of not yet supported type was given and
9799 * -1 on API/internal errors.
9802 xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt
,
9803 xmlDocPtr sourceDoc
,
9806 xmlNodePtr destParent
,
9809 if ((node
== NULL
) || (destDoc
== NULL
) ||
9810 ((destParent
!= NULL
) && (destParent
->doc
!= destDoc
)))
9813 * Check node->doc sanity.
9815 if ((node
->doc
!= NULL
) && (sourceDoc
!= NULL
) &&
9816 (node
->doc
!= sourceDoc
)) {
9818 * Might be an XIncluded node.
9822 if (sourceDoc
== NULL
)
9823 sourceDoc
= node
->doc
;
9824 if (sourceDoc
== destDoc
)
9826 switch (node
->type
) {
9827 case XML_ELEMENT_NODE
:
9828 case XML_ATTRIBUTE_NODE
:
9830 case XML_CDATA_SECTION_NODE
:
9831 case XML_ENTITY_REF_NODE
:
9833 case XML_COMMENT_NODE
:
9835 case XML_DOCUMENT_FRAG_NODE
:
9836 /* TODO: Support document-fragment-nodes. */
9842 * Unlink only if @node was not already added to @destParent.
9844 if ((node
->parent
!= NULL
) && (destParent
!= node
->parent
))
9845 xmlUnlinkNode(node
);
9847 if (node
->type
== XML_ELEMENT_NODE
) {
9848 return (xmlDOMWrapAdoptBranch(ctxt
, sourceDoc
, node
,
9849 destDoc
, destParent
, options
));
9850 } else if (node
->type
== XML_ATTRIBUTE_NODE
) {
9851 return (xmlDOMWrapAdoptAttr(ctxt
, sourceDoc
,
9852 (xmlAttrPtr
) node
, destDoc
, destParent
, options
));
9854 xmlNodePtr cur
= node
;
9859 * Optimize string adoption.
9861 if ((sourceDoc
!= NULL
) &&
9862 (sourceDoc
->dict
== destDoc
->dict
))
9864 switch (node
->type
) {
9866 case XML_CDATA_SECTION_NODE
:
9867 XML_TREE_ADOPT_STR_2(node
->content
)
9869 case XML_ENTITY_REF_NODE
:
9871 * Remove reference to the entitity-node.
9873 node
->content
= NULL
;
9874 node
->children
= NULL
;
9876 if ((destDoc
->intSubset
) || (destDoc
->extSubset
)) {
9879 * Assign new entity-node if available.
9881 ent
= xmlGetDocEntity(destDoc
, node
->name
);
9883 node
->content
= ent
->content
;
9884 node
->children
= (xmlNodePtr
) ent
;
9885 node
->last
= (xmlNodePtr
) ent
;
9888 XML_TREE_ADOPT_STR(node
->name
)
9891 XML_TREE_ADOPT_STR(node
->name
)
9892 XML_TREE_ADOPT_STR_2(node
->content
)
9903 #include "elfgcchack.h"