Re: [PHP-XML-DEV] DomDocument, insert HTML Element

[email protected] ("Rob Richards") Fri, 12 Nov 2004 06:32:46 -0500
Newsgroups php.xml.dev
Message-ID <[email protected]>
----- Original Message ----- 
From: Martel Valgoerad

> What would be the right place to post? The reason I'm asking is because I
> would like to subscribe and I don't know where (but I would rather skip
the
> high volume general lists since I'm looking for PHP 5 XML/XSLT support
only).

No specific PHP xml/xslt list that I know of. Questions should go to
php-general I assume.

> I'm using PHP 5.0.2, btw. I found the DocumentFragment in the DOM Refrence
on
> w3c (and I was trying to use it) but the DomFragment is undocumented on
the
> PHP docs: http://pl.php.net/manual/en/ref.dom.php

There are no implemented methods for DOMFragment in any of the released
versions of PHP (thus no docs yet).

> Is there any place where I can find the documentation for DocumentFragment
or
> even attributes of DomDocument like resolveExternals or
substituteEntities?

You can check some old outdated docs - these are pre manual docs so dont
trust everything you read - at http://www.ctindustries.net/dom/.
Here you'll find the properties for the objects (Havent figured out how to
document the properties correctly in the manual).

> I'm using XML + XSLT as a templating engine. In this particular case I'm
> dealing with form user input which accepts HTML (and then is stored in a
> database). What approach would you recommend? Of course, if it's not too
bold
> to ask given this thread is highly off-topic.

You can always store the user submitted data in a CDATA node - this pretty
much is free form text.

If you need the submitted data to parse correctly into a tree (like using
the appendXML method), for PHP 5.0.x you can try the following hack:

$doc = new DomDocument('1.0', 'UTF-8');
$doc->resolveExternals = true;
$doc->substituteEntities = true;

$art = $doc->createElement('article');
$art->setAttribute('id', 2335);

$HTML = 'Some kind of <a href="http://cos.com">HTML</a>';
$body = $doc->createElement('body');

$htmldoc = DOMDocument::loadXML("<doc>$HTML</doc>");
$htmlroot = $htmldoc->documentElement;
if ($htmlroot->hasChildNodes()) {
 foreach($htmlroot->childNodes as $node) {
  $body->appendChild($doc->importNode($node, TRUE));
 }
}

$art->appendChild($body);

$doc->appendChild($art);