|
Posted by Janwillem Borleffs on 12/31/05 19:07
Lόpher Cypher wrote:
> I am wondering if there are any scripts for PHP4 out there to verify
> an XML file against XSD.
>
AFAIK, you can only do this with an external program, e.g. xmllint
(available as part of the libxml2 distribution and as a standalone
executable for Windows; just Google for 'xmllint').
Together with the domxml extension, you could something as followed (not
fully tested):
<?php
class ValidatedDomDocument extends DomDocument {
function schemaValidate($xsd_file) {
if (!file_exists($xsd_file)) {
return false;
}
$xml = $this->dump_mem();
$descriptorspec = array(array("pipe", "r"));
$process = proc_open(
"G:/xmllint/xmllint.exe --schema $xsd_file -",
$descriptorspec,
$pipes
);
if (is_resource($process)) {
fwrite($pipes[0], $xml);
fclose($pipes[0]);
return proc_close($process) == 0;
}
return false;
}
}
$doc = new ValidatedDomDocument('<yourxml />');
print $doc->schemaValidate('yourschema.xsd') ? 'valid' : 'invalid';
?>
HTH;
JW
Navigation:
[Reply to this message]
|