[xml] Problem in Namespace handling in xmlSetNsProp (?)



hallo, 

i found a problem related to the ancient bugzilla id #55683, which is 
namespace handling for attributes. the bug tracking says it's fixed, 
while it's still broken (i checked the cvs version)

the problem can be reproduced if one tries to insert a new namespaced 
attribute (here bar:foo="bar") to the following node:

<bar:root xmlns="foo" xmlns:bar="foo" foo="hello world"/>

if i now run the following code in a program

---8<----8<----8<----

            ns = xmlSearchNsByHref( node->doc, node, "foo" );
            if ( !ns->prefix ) {
                if ( ns->next && ns->next->prefix ) {
                    ns = ns->next;
                }
                else {
                    ns = NULL;
                }
            }
            xmlSetNsProp( node, ns, "foo", "bar" );

---8<----8<----8<----

i end up with 

<bar:root xmlns="foo" xmlns:bar="foo" bar:attr="test" bar:foo="bar"/>

instead of

<bar:root xmlns="foo" xmlns:bar="foo" bar:attr="test" foo="hello world" bar:foo="bar"/>

taking a closer look to tree.c, i found in xmlSetNsProp() the following code:

---8<----8<----8<----

        /*
         * One need to have
         *   - same attribute names
         *   - and the attribute carrying that namespace
         *         or
         *         no namespace on the attribute and the element carrying it
         */
        if ((xmlStrEqual(prop->name, name)) &&
            (((prop->ns == NULL) && (node->ns != NULL) &&
              (xmlStrEqual(node->ns->href, ns->href))) ||
             ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))))) {

---8<----8<----8<----

as the original bugreport #55683 says, attributes that have no namespace 
themselfes must not inherit the parents namespace. but if one looks to this 
statement, this is exactly what libxml2 does. (as the example shows)

the correct behaviour would be if the following state ment is inserted 
instead:


---8<----8<----8<----

        /*
         * One need to have
         *   - same attribute names
         *   - and the attribute carrying that namespace
         */
        if ((xmlStrEqual(prop->name, name)) &&
            ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href)))) {

---8<----8<----8<----

i tested this code and it gives me the expected results. 

(note: the same problem is with xmlUnsetNsProp())

christian 




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