PHP DOMDocument - createDocumentFragment no funciona con loadHTML
Frecuentes
Visto 3,095 equipos
1
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="http://www.w3.org/1998/Math/MathML">Test string <formula type="inline"><default:math xmlns="http://www.w3.org/1998/Math/MathML"><default:mi>π</default:mi></default:math></formula></p>"
$dom = new \DOMDocument();
@$dom->loadHTML($abstract);
$frag = $dom->createDocumentFragment();
When var dumping the $frag->nodeValue
, I am getting null. Any idea?
1 Respuestas
4
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();
$fragment->appendXml('<p>first</p>second');
$body->appendChild($fragment);
echo $dom->saveHtml();
Salida:
<body><p>first</p>second</body>
Respondido 12 Feb 14, 09:02
Remove \ and
@
from that code and you'll get your answer - Hanky Panky@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. - Milos Cuculovic
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. - ThW
Lo que hice esperar to get? What is the "value" of a childless node? - Jon