Re: [PHP] loadXML() and namespace

2010-02-17 Thread Michael A. Peters

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.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] loadXML() and namespace

2010-02-15 Thread Michael A. Peters
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
mix/mi
mn2/mn
  /msup
  mo+/mo
  mrow
mn4/mn
mo/mo
mix/mi
  /mrow
  mo+/mo
  mn4/mn
/mrow
mo=/mo
mn0/mn
  /mrow
/math

would get changed to

defaultmath
  defaultmrow
defaultmrow
  defaultmsup
defaultmix/defaultmi
defaultmn2/defaultmn
  /defaultmsup
  defaultmo+/defaultmo
  defaultmrow
defaultmn4/defaultmn
defaultmo/defaultmo
defaultmix/defaultmi
  /defaultmrow
  defaultmo+/defaultmo
  defaultmn4/defaultmn
/defaultmrow
defaultmo=/defaultmo
defaultmn0/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.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php