Re: [PHP] loadXML() and namespace
[email protected] ("Michael A. Peters")
| Newsgroups | php.general |
|---|---|
| Message-ID | <[email protected]> |
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.