From: "Michael A. Peters" on
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>
<mrow>
<msup>
<mi>x</mi>
<mn>2</mn>
</msup>
<mo>+</mo>
<mrow>
<mn>4</mn>
<mo></mo>
<mi>x</mi>
</mrow>
<mo>+</mo>
<mn>4</mn>
</mrow>
<mo>=</mo>
<mn>0</mn>
</mrow>
</math>

would get changed to

<defaultmath>
<defaultmrow>
<defaultmrow>
<defaultmsup>
<defaultmi>x</defaultmi>
<defaultmn>2</defaultmn>
</defaultmsup>
<defaultmo>+</defaultmo>
<defaultmrow>
<defaultmn>4</defaultmn>
<defaultmo></defaultmo>
<defaultmi>x</defaultmi>
</defaultmrow>
<defaultmo>+</defaultmo>
<defaultmn>4</defaultmn>
</defaultmrow>
<defaultmo>=</defaultmo>
<defaultmn>0</defaultmn>
</defaultmrow>
</defaultmath>

which of course breaks the page.
So it seems I need to somehow tell loadXML() about the namespace so it
doesn't do that, but I'm not having much luck with the php manual,
DOMDocument documentation seems a little on the "not written" side.

When I create the nodes and add them to the dom via domdocument it works
fine, but the issue is I like to cache the content div as a string and
load it when the page is requested (stuff outside the content div needs
to be dynamic and not cached) but this namespace issue prevents that.

Any tips would be appreciated, caching makes a huge difference.
From: "Michael A. Peters" on
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.