Skip Menu |

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

Report information
The Basics
Id: 56309
Status: open
Priority: 0/
Queue: XML-LibXML

People
Owner: Nobody in particular
Requestors: david+bugs [...] madore.org
Cc:
AdminCc:

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



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.
Hi David, sorry for the late response to your bug. On Tue Apr 06 01:12:22 2010, david+bugs@madore.org wrote: Show quoted text
> 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. Yes, that would be a useful feature assuming it is easily doable. Right now the priority is to fix XML::LibXML bugs and not to implement new features, but if you can provide a patch (with tests/etc. - see the "HACKING.txt" document), then I will seriously consider to apply it.
On 2010-04-06T06:12:22+01:00, david+bugs@madore.org wrote: Show quoted text
> One can easily find oneself with unnecessary namespace declarations > (which can be a problem when validating against a DTD).
One person's idea of "unnecessary" might be different from another person's idea. For example, is the "b" prefix necessary here? <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b="urn:xmlns:25hoursaday-com:bookstore"> <xsl:template match="b:bookstore"> <book-titles> <xsl:apply-templates select="b:book/b:title"/> </book-titles> </xsl:template> <xsl:template match="b:title"> <xsl:copy-of select="." /> </xsl:template> </xsl:stylesheet> David, are you aware of toStringEC14N in XML::LibXML::Node? This performs exclusive XML canonicalisation, which (amongst other things) removes unnecessary namespace prefixes. (It allows you to pass an arrayref of additional prefixes which need to be preserved - for example, "b" in the above example.)