Subject: | provide a way to clean up namespace declarations |
One can easily find oneself with unnecessary namespace declarations
(which can be a problem when validating against a DTD). E.g. (with
XML::LibXML 1.70 acting as front-end to libxml2 2.7.7):
#! /usr/local/bin/perl
use XML::LibXML;
my $doc = XML::LibXML::Document->new;
my $elt = $doc->createElement("foo");
$doc->setDocumentElement($elt);
$elt->setAttributeNS("file:///dev/null","x:useless","0");
$elt->removeAttribute("x:useless");
print $doc->toString;
produces the following output:
<?xml version="1.0"?>
<foo xmlns:x="file:///dev/null"/>
I would like to remove the declaration of the x namespace prefix after
verification that it serves no purpose. It seems possible to remove it
by force with $elt->setAttributeNS(XML_XMLNS_NS, "xmlns:x", undef)
(although, strangely, $elt->removeAttributeNS(XML_XMLNS_NS, "xmlns:x")
does not work; I don't know why the difference), but this will break the
tree if the x prefix was, in fact, in use: so I'd like a way of either
checking whether it is, or of pruning only unnecessary prefixes.
Would it be possible to have something of the sort? (Of course, the
function in question might have to walk through the entire tree or
subtree. That is not a problem, since one would typically want to clean
up namespaces only once, at the very end.)
I believe in libxml2 this could be done by running
xmlDOMWrapReconcileNamespaces() after attempting to remove namespace
declarations, so maybe what I want is a wrapper for that. But I'm not sure.