PHP DOMDocument - createDocumentFragment no funciona con loadHTML

I have a string that contains HTML and I would like to insert this HTML in a DOMElement.

For that, I did:

$abstract = "<p xmlns:default="">Test string <formula type="inline"><default:math xmlns=""><default:mi>&pi;</default:mi></default:math></formula></p>"

$dom = new \DOMDocument();
$frag = $dom->createDocumentFragment();

When var dumping the $frag->nodeValue, I am getting null. Any idea?

preguntado el 12 de febrero de 14 a las 08:02

Remove \ and @ from that code and you'll get your answer -

@Hanky웃Panky, no, this will not resolve the problem: the \ is for Symfony framework, to be able to find the DomDocument class, the @ is to hide warnings, because I have some non common tags that should be loaded. -

The \ is for the top level php namespace. It is optional if you're are outside a namespace (in the top level namespace). The @ blocks errors, but the Math-ML part is broken anyway. -

Lo que hice esperar to get? What is the "value" of a childless node? -

1 Respuestas

I am not sure what you expect, you creating a new fragment and you add no content. Even if you do it would not work because the document fragment is no node, it is an helper construct to add a XML fragment to a document.

Aquí hay un ejemplo:

$dom = new \DOMDocument();
$body = $dom->appendChild($dom->createElement('body'));

$fragment = $dom->createDocumentFragment();


echo $dom->saveHtml();



Respondido 12 Feb 14, 09:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.