Problem with namespaces in XML-LibXML-1.63
by Steve Hay other posts by this author
Aug 13 2007 8:38AM messages near this date
view in the new Beta List Site
|
Re: Problem with namespaces in XML-LibXML-1.63
& XSLT I've just upgraded from 1.58_01 to 1.63 and I've been bitten by a change
of behaviour that was introduced in 1.62, namely, that
$elt-> cloneNode(0) now copies namespace information.
Thus, the following program:
use XML::LibXML;
use XML::LibXML::XPathContext;
my $parser = XML::LibXML-> new();
my $olddoc = $parser-> parse_string(<<'EOX');
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.foo.org"/>
EOX
my $ctx = XML::LibXML::XPathContext-> new();
$ctx-> registerNs('foo', 'http://www.foo.org');
$ctx-> setContextNode($olddoc);
my($oldelt) = $ctx-> findnodes('/foo:root');
my $newelt = $oldelt-> cloneNode(0);
my $newdoc = XML::LibXML::Document-> createDocument('1.0', 'UTF-8');
$newdoc-> setDocumentElement($newelt);
print $newdoc-> toString();
now prints:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.foo.org"/>
whereas it used to print:
<?xml version="1.0" encoding="UTF-8"?>
<root/>
I see in Node.pod that this is an intentional change for a good reason
(and suitably documented too) so I'm not complaining about it, but I do
need some way of getting the old behaviour, or something equivalent to
it, back.
I'm copying an element from within a document that has an xmlns
declaration in its document element, and making a new document out of
it. The new document must not have any xmlns declaration when it is
output, otherwise it'll break lots of XSL code further down the line.
I had been using cloneNode(0) recursively (and copying any attributes
present "manually") to achieve the desired behaviour, but as shown above
I now find that cloneNode(0) copies the namespace too.
I tried using the new setNamespaceDeclPrefix() on the copied element to
try to clear its namespace:
...
my $newelt = $oldelt-> cloneNode(0);
$newelt-> setNamespaceDeclPrefix('foo', undef);
...
but that gives me an error:
setNamespaceDeclPrefix: prefix '(null)' is in use at test.pl line 13.
Why does that cause an error? Is there some way to remove the xmlns
using setNamespaceDeclPrefix() or setNamespaceDeclURI()?
_______________________________________________
Perl-XML mailing list
Perl-XML@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Steve Hay
Petr Pajas
|