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:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
...
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 $document->documentElement->namespaceURI 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.
Like edorian already showed, getting the namespace works fine when the Markup is loaded with
loadXML. But you are right that this wont work for Markup loaded withloadHTML:will produce empty results. But you can use XPath
and for body
or with context node
My uneducated assumption is that internally, a HTML Document is different from a real Document. Internally libxml uses a different module to parse HTML and the DOMDocument itself will be of a different nodeType, as you can simply verify by doing
with 13 being a
XML_HTML_DOCUMENT_NODE.