Frankly, for me it was a difficult question, because I myself did not think about it, although the answer is quite simple:
According to the Level 1 recommendation of the DOM, the Attr
interface inherits the Node
interface, that is, you can attach nodes to the attribute. And EntityReference
allowed in Attr
children.
In XML, where an attribute value can contain entity references, Attr node child nodes provide a view in which object references are not extended. These child nodes can be either Text nodes or EntityReference nodes. Because the attribute type may not be known, there are no tokenized attribute values.
Here is a working example in PHP:
<?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(); ?>
that leads to
C:\>\php\5.3.8\php.exe entities.php <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE root [ <!ENTITY my_entity "some content"> ]> <root attr="prefix &my_entity; suffix">&my_entity;</root>
source share