note 106662 deleted from domdocument.createentityreference by danbrown
| From: | danbrown@php.net | Date: | Tue, 29 Nov 2011 15:45:02 +0000 |
| Subject: | note 106662 deleted from domdocument.createentityreference by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184793@lists.php.net to get a copy of this message | ||
Note Submitter: sean dot cole at verizon dot net
----
I had some issues getting this to work, mostly because the documentation is so lacking. Here is the
help I got from stackoverflow, thanks newtover. If you're interested in the source, you can
check it out with this partial link (questions/8233394/xml-entity-in-setattribute-addattribute). I
would add the full but the requirements for submitting this suggests I don't add URL's
hopefully this is acceptable.
<?php
// a valid XML should contain used entities declarations to be valid,
// but DOM recs do not contain means to generate DTD;
// in PHP you can use XMLWriter for the purpose though
$dtd = <<<DTD
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE root [
<!ENTITY my_entity "some content">
]>
<root/>
DTD;
$xml = new DOMDocument();
$xml->formatOutput = true;
$xml->loadXML($dtd);
$root = $xml->documentElement;
$entity = $xml->createEntityReference( 'my_entity' );
$an_attr = $xml->createAttribute( 'attr' );
$an_attr->appendChild( $xml->createTextNode('prefix ') );
$an_attr->appendChild( $entity );
$an_attr->appendChild( $xml->createTextNode(' suffix') );
$root->setAttributeNode( $an_attr );
// clone the entity to use it more than once
$root->appendChild( $entity->cloneNode() );
print $xml->saveXML();
?>