note 62519 added to ref.xml
| From: | senyahnoj at osu1 dot php dot net | Date: | Thu, 02 Mar 2006 13:35:30 +0000 |
| Subject: | note 62519 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-104701@lists.php.net to get a copy of this message | ||
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
----
Server IP: 84.18.202.180
Probable Submitter: 80.229.2.35
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- http://master.php.net/note/edit/62519
Del: integrated -- http://master.php.net/note/delete/62519/integrated
Del: useless -- http://master.php.net/note/delete/62519/useless
Del: bad code -- http://master.php.net/note/delete/62519/bad+code
Del: spam -- http://master.php.net/note/delete/62519/spam
Del: non-english -- http://master.php.net/note/delete/62519/non-english
Del: in docs -- http://master.php.net/note/delete/62519/in+docs
Del: other reasons-- http://master.php.net/note/delete/62519
Reject -- http://master.php.net/note/reject/62519
Search -- http://master.php.net/manage/user-notes.php