Reputation: 105
I have external XML and DTD files. The XML file unfortunately doesn't have a declaration to point to its corresponding DTD file. Is it possible to still validate against it using PHP? Any help or a link to a resource would be greatly appreciated. Thanks.
Upvotes: 1
Views: 728
Reputation: 2774
Duplicate of XML validation against given DTD in PHP
<?php
$xml = <<<END
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE foo SYSTEM "foo.dtd">
<foo>
<bar>baz</bar>
</foo>
END;
$root = 'foo';
$old = new DOMDocument;
$old->loadXML($xml);
$creator = new DOMImplementation;
$doctype = $creator->createDocumentType($root, null, 'bar.dtd');
$new = $creator->createDocument(null, null, $doctype);
$new->encoding = "utf-8";
$oldNode = $old->getElementsByTagName($root)->item(0);
$newNode = $new->importNode($oldNode, true);
$new->appendChild($newNode);
$new->validate();
?>
Upvotes: 3