How to detect and remove unnecessary xmlns:<something> attributes in PHP DOM?

Slyza

New Member
Say I have a source document like this:\[code\]<element> <subelement xmlns:someprefix="mynamespace"/></element>\[/code\]The \[code\]xmlns:someprefix\[/code\] is obviously not needed here and doesn't do anything since that namespace is not being used anywhere in the document.In PHP, after I've loaded this into a DOM tree with DOMDocument->loadXML(), I'd like to be able to detect that such a namespace declaration exists, and remove it.I know that I can read it with \[code\]hasAttribute()\[/code\] and even remove it with \[code\]removeAttributeNS()\[/code\] (strangely) but only if I know its prefix. It doesn't appear in \[code\]DOMNode->attributes\[/code\] at all, as the thing I'm trying to find is not considered an attribute. I cannot see any way of detecting that it is there without knowing the prefix, other than serialising it back to an XML string and running a regex or something.How can I do it? Any way to query which namespaces (ie xmlns:something) have been declared in an element?
 
Back
Top