Today I ran into a small problem, validating import XML data against a DTD(Document Type Definition), and discovered a neat way to solve this without PHP4's own means, as the PHP 4 version on the running system wasn't supporting the DOM XML Functions.
Xmllint, a command line XML tool, parses provided XML files and can be used to verify that these files are well formed and are satisfying an associated DTD/XML Schema.
The following method body shows an example call of the xmllint tool for validating a given XML file against a DTD. The location of the DTD file is wrapped inside a DOCTYPE definition of the import file and will be automatically resolved by xmllint.
<?php
...
function isValidImportFile($filename) {
if($filename == '' || !$this->isSupposedFiletype($filename, 'xml')) {
return false;
}
$xmllintCall = "xmllint --valid --noout {$this->_xmlDirectory}{$filename}";
exec(escapeshellcmd($xmllintCall), $xmllintOutput, $xmllintFeedback);
if($xmllintFeedback == '0') {
return true;
} else {
// Maybe log xmllint feedback here for further evaluation.
return false;
}
}
...
In addition xmllint is also very helpfull when writing custom DTDs or XML Schema, as you get an immediate reply regarding the correctness of the definition rules and their 'impact' on the associated XML files.
[http://raphaelstolt.blogspot.com/2007/04/validating-xml-files-within-php-4.html]
[http://infohost.nmt.edu/tcc/help/xml/lint.html]