Skip Menu |

This queue is for tickets about the XML-LibXML CPAN distribution.

Report information
The Basics
Id: 55000
Status: resolved
Priority: 0/
Queue: XML-LibXML

People
Owner: Nobody in particular
Requestors: duerig [...] flux.utah.edu
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Bug Report: setAttributeNS
Date: Thu, 25 Feb 2010 12:31:07 -0700 (MST)
To: bug-XML-LibXML [...] rt.cpan.org
From: Jonathon Duerig <duerig [...] flux.utah.edu>
If an element contains both a default namespace declaration and a second namespace declaration, adding an attribute using the default namespace declaration will cause that attribute to have the other prefix. OS Version: FreeBSD 6.3-RELEASE Perl Version: v5.8.8 LibXML Version: 1.70 Consider the following example script: === BEGIN MINIMAL CASE === #!/usr/bin/perl -w use strict; use XML::LibXML; my $xml_string = <<'XML'; <?xml version="1.0" encoding="UTF-8"?> <element xmlns="uri" xmlns:wrong="other"> </element> XML my $parser = XML::LibXML->new; my $doc = $parser->parse_string($xml_string); my $root = $doc->documentElement(); $root->setAttributeNS("uri", "prefix:attribute", "text"); print $doc->toString(1); === END MINIMAL CASE === This will emit the following text: <?xml version="1.0" encoding="UTF-8"?> <element xmlns="uri" xmlns:wrong="other" wrong:attribute="text"> </element> Notice the 'wrong:attribute', when the uri in setAttributeNS() was for the default namespace. The call should have either added a new declaration (xmlns:prefix="uri") along with the attribute (prefix:attribute="text") or omitted the prefix altogether because the attribute was associated with an element in the default namespace. --- Broad audience or deep message: Pick one.
I can confirm this bug. But the solution should be to always add a new NS declaration because the default namespace doesn't apply for attributes. See the attachment for a proposed fix.
Subject: libxml-rt-55000.diff
Index: LibXML.xs =================================================================== --- LibXML.xs (revision 822) +++ LibXML.xs (working copy) @@ -6035,12 +6035,18 @@ } } else if ( !ns->prefix ) { - if ( ns->next && ns->next->prefix ) { - ns = ns->next; + if ( prefix && xmlStrlen( prefix ) ) { + ns = xmlSearchNs(self->doc, self, prefix); + + if ( ns ) { + if ( !xmlStrEqual(ns->href, nsURI) ) { + ns = NULL; + } + } + else { + ns = xmlNewNs(self, nsURI , prefix ); + } } - else if ( prefix && xmlStrlen( prefix ) ) { - ns = xmlNewNs(self, nsURI , prefix ); - } else { ns = NULL; }
Fixed in the repository. I had to modify the patch to pass some tests. I will upload a new version soon.