How can I find out the namespace of an element in PHP DOM?

#686578

New Member
This sounds like a pretty easy question to answer but I haven't been able to get it to work. I'm running PHP 5.2.6.I have a DOM element (the root element) which, when I go to $element->saveXML(), it outputs an xmlns attribute:\[code\]<?xml version="1.0" encoding="UTF-8" standalone="yes"?><html xmlns="http://www.w3.org/1999/xhtml" lang="en">...\[/code\]However, I cannot find any way programmatically within PHP to see that namespace. I want to be able to check whether it exists and what it's set to.Checking \[code\]$document->documentElement->namespaceURI\[/code\] would be the obvious answer but that is empty (I've never actually been able to get that to be non-empty). What is generating that xmlns value in the output and how can I read it?The only practical way I've been able to do this so far is a complete hack - by saving it as XML to a string using saveXML() then reading through that using regular expressions.Edit:This may be a peculiarity of loading XML in using loadHTML() rather than loadXML() and then printing it out using saveXML(). When you do that, it appears that for some reason saveXML adds an xmlns attribute even though there is no way to detect that this xmlns value is part of the document using DOM methods. Which I guess means that if I had a way of detecting whether the document passed in had been loaded in using loadHTML() then I could solve this a different way.
 
Back
Top