note 62519 deleted from ref.xml by didou
| From: | didou@php.net | Date: | Sun, 23 Apr 2006 22:58:26 +0000 |
| Subject: | note 62519 deleted from ref.xml by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-109247@lists.php.net to get a copy of this message | ||
Note Submitter: senyahnoj
----
Validate an XML document against its DTD using PHP & xmllint
<?
# .e.g. validate this against foo.dtd
$xml = "<?xml version=\"1.0\" ?>
<!DOCTYPE foo SYSTEM \"http://foo/foo.dtd\">
<foo>
<bar>foobar</bar>
<!--etc -->
</foo>";
function validate_xml($xml)
{
$path = "/tmp/"; # path for tmp xml file
$xmllint = "/usr/bin/xmllint"; # path to xmllint
/*
command-line options for xmllint:
--htmlout html output
--nowrap html header and footer removed (I prefer my own)
--valid validate against specified DTD
--noout no stdout (it's the stderr we want - we'll convert the stderr to stdout
later on!)
*/
$opts = "--htmlout --nowrap --valid --noout";
# write to a temporary file
$filename = $path . uniqid("x" . ".xml");
if(!$h = fopen($filename,'w'))
{ die("Could not open file for writing"); }
if(!fwrite($h,$xml))
{ die("Could not write to file"); }
fclose($h);
# need to convert stderr to stdout (the 2>&1 bit)
exec("$xmllint $opts $filename 2>&1",$errors);
# delete temporary file
unlink($filename);
# print validation errors if there are any
if(count($errors) > 0)
{
foreach($errors as $v)
{ echo $v; }
}
}
validate_xml($xml);
?>
Ref: http://linuxcommand.org/man_pages/xmllint1.html