How to remove entity declarations from XML in PHP -
i'm trying remove <!entity
definitions xml file without success, thought using following snippet output contain no traces of entity definitions i'm wrong.
how can achieve goal?
i no error message beside domdocument::loadxml(): xmlns: uri &ns_svg; not absolute in entity
a little context:
i'm embedding svg inside another, <!entity
gives me kind of problem, i'm thinking of using libxml_noent
, deleting <!entity
definitions.
the php:
<?php header('content-type: text/plain'); $str = file_get_contents(dirname(__file__) . '/test2.svg'); $document = new domdocument(); $document->loadxml($str); foreach ($document->doctype->entities $entity) { $entity->parentnode->removechild($entity); // thought remove <!entity declaration } echo $document->savexml(); // --> want xml without <!entity ns_svg "http://www.w3.org/2000/svg"> , <!entity ns_xlink "http://www.w3.org/1999/xlink">
the xml:
<?xml version="1.0" encoding="utf-8"?> <!-- generator: adobe illustrator 12.0.1, svg export plug-in . svg version: 6.00 build 51448) --> <!doctype svg public "-//w3c//dtd svg 1.1//en" "http://www.w3.org/graphics/svg/1.1/dtd/svg11.dtd" [ <!entity ns_svg "http://www.w3.org/2000/svg"> <!entity ns_xlink "http://www.w3.org/1999/xlink"> ]> <svg version="1.1" id="bundesschild" sodipodi:version="0.32" xmlns:cc="http://web.resource.org/cc/" xmlns:sodipodi="http://inkscape.sourceforge.net/dtd/sodipodi-0.dtd" inkscape:version="0.43" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:svg="http://www.w3.org/2000/svg" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" sodipodi:docbase="d:\kuvat\wikipedia" sodipodi:docname="flag_of_germany_(state).svg" xmlns="&ns_svg;" xmlns:xlink="&ns_xlink;" width="250" height="275" viewbox="0 0 250 275" overflow="visible" enable-background="new 0 0 250 275" xml:space="preserve"> <path id="schild" fill="#ffce00" stroke="#000000" d="m235.885,2.558c0,0,0,131.825,0,171.735 c0,54.121-50.504,98.265-112.501,98.265c-61.996,0-112.5-44.144-112.5-98.265c0-39.91,0-171.735,0-171.735h235.885z"/> </svg>
the output:
<?xml version="1.0" encoding="utf-8"?> <!-- generator: adobe illustrator 12.0.1, svg export plug-in . svg version: 6.00 build 51448) --> <!doctype svg public "-//w3c//dtd svg 1.1//en" "http://www.w3.org/graphics/svg/1.1/dtd/svg11.dtd" [ <!entity ns_svg "http://www.w3.org/2000/svg"> <!-- why still here???? --> <!entity ns_xlink "http://www.w3.org/1999/xlink"> <!-- why still here???? --> ]> <svg xmlns:cc="http://web.resource.org/cc/" xmlns:sodipodi="http://inkscape.sourceforge.net/dtd/sodipodi-0.dtd" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:svg="http://www.w3.org/2000/svg" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns="&ns_svg;" xmlns:xlink="&ns_xlink;" version="1.1" id="bundesschild" sodipodi:version="0.32" inkscape:version="0.43" sodipodi:docbase="d:\kuvat\wikipedia" sodipodi:docname="flag_of_germany_(state).svg" width="250" height="275" viewbox="0 0 250 275" overflow="visible" enable-background="new 0 0 250 275" xml:space="preserve"> <path id="schild" fill="#ffce00" stroke="#000000" d="m235.885,2.558c0,0,0,131.825,0,171.735 c0,54.121-50.504,98.265-112.501,98.265c-61.996,0-112.5-44.144-112.5-98.265c0-39.91,0-171.735,0-171.735h235.885z"/> </svg>
you can remove whole document type node. entities not seem child nodes of it. before should substitute entities using libxml_noent
:
$document = new domdocument(); $document->loadxml($xml, libxml_noent); $document->removechild($document->doctype); echo $document->savexml();
the way keep document type found, creating new document document type , copy nodes it.
$document = new domdocument(); $document->loadxml($xml, libxml_noent); $existingdocumenttype = $document->doctype; if (null !== $existingdocumenttype) { $implementation = new domimplementation; $newdocumenttype = $implementation->createdocumenttype( $existingdocumenttype->name, $existingdocumenttype->publicid, $existingdocumenttype->systemid ); $newdocument = $implementation->createdocument(null, null, $newdocumenttype); foreach ($document->childnodes $node) { $copy = $newdocument->importnode($node, true); if ($copy) { $newdocument->appendchild($copy); } } $document = $newdocument; } echo $document->savexml();
document type nodes can not imported document, domdcoument::importnode()
return false
it.
if @ standard domdocumenttype::entitites
domnamednodemap
, should have method removenameditem()
. if call php outputs:
warning: domnamednodemap::removenameditem(): not yet implemented in ... on line ...
Comments
Post a Comment