Re: loadXML() and namespace
| From: | Michael A. Peters | Date: | Wed, 17 Feb 2010 20:57:41 +0000 |
| Subject: | Re: loadXML() and namespace | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-302164@lists.php.net to get a copy of this message | ||
Michael A. Peters wrote:
It seems that if I use loadXML($string) and the $string has a namespace defined in it, domdocument is nuking the namespace and changing the nodenames from whatever to defaultwhatever. Example - <math xmlns="http://www.w3.org/1998/Math/MathML"> <mrow> *snip*
</mrow> </math> would get changed to <defaultmath> <defaultmrow> *snip*
</defaultmrow> </defaultmath> which of course breaks the page.It seems to be related to the "xml" island concept of (x)html 5 where for compatibility with IE and MathType etc. an xmlns is declared as an attribute but a prefix isn't declared (IE no m:math). To solve it, on the buffered string that gets imported I run: function nsIsland($string) { $search[] = "/<math\s([^xmlns]*)xmlns=[^\s>]*/"; $replace[] = "<math $1"; return preg_replace($search,$replace,$string); } (I assume I may have to add a search/replace for svg) - then after import I look for math nodes and add the xmlns attribute back to them, and it seems to work. A bit hackish, I'm guessing a future version of DOMDocument may take the html5 "xml island" into consideration, but this works now.