[xml] problem (bug?) with xmlNewChild()



Hallo Daniel,
hallo list,

I found an interesting issue in xmlNewChild(). I thought, it is possible
to append a new child to documents, elements and document fragments with 
xmlNewChild(). It showed that document fragments are not supported by this 
function, although it would make sense to do so.  I concider this as a bug
(again it is easy to fix).

The way it is fixed, depends on another part of the same function. Here I
am not shure if it is me misunderstanding namespaces or if it is the 
implementation that is wrong: 

If no namespace structure is passed to xmlNewChild() it appears, that the 
namespace of the parent node is silently taken as namespace for the new node
as well. I was quite surprized about this behaviour, since I was expecting a
node with no namespace set. Although the given implementation is unchanged 
at least since 2.4.20, I *think* that's wrong as well.   

Depending on the aspect I mentioned last there are two possible fixes:

/* 1 - my prefered one */
/* tree.c, ~line 2085 */
    if ((parent->type == XML_ELEMENT_NODE) ||
        (parent->type == XML_DOCUMENT_FRAG_NODE)) {
        cur = xmlNewDocNode(parent->doc, ns, name, content);
    }

although in this case a switch statement should be more elegant:

/* 1a */
   switch (parent->type) {
   case XML_ELEMENT_NODE:
   case XML_DOCUMENT_NODE:
   case XML_HTML_DOCUMENT_NODE:
   case XML_DOCUMENT_FRAG_NODE:
      cur = xmlNewDocNode(parent->doc, ns, name, content);
      break;
   default:
      return(NULL);
      break;
   }

/* 2 - minimum alternative */
/* tree.c ~line 2095 */
   } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
      cur = xmlNewDocNode( parent->doc, ns, name, content);
   } else {
/* ... */

Christian



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]