PHP Validation

DOMDocument supports two schema languages for validating a loaded document: XSD, via schemaValidate() (file) and schemaValidateSource() (string), and RelaxNG, via relaxNGValidate() (file) and relaxNGValidateSource() (string). RelaxNG Compact is not supported – only RelaxNG's XML syntax.

The example below loads zoo.xml and validates it against a RelaxNG schema (zoo.rng) and an XSD schema (zoo.xsd). Both calls emit PHP warnings instead of returning a clean boolean result, because the loaded document does not match either schema as given here – illustrating the diagnostics PHP produces on a validation mismatch rather than a silent pass/fail. For working zoo.xml/schema pairs, see the XSD and Relax NG reference pages.

<!DOCTYPE html>
<html><head></head><body><?php

$doc = new DOMDocument();
$doc->load('zoo.xml');
$doc->relaxNGValidate('zoo.rng');
$doc->schemaValidate('zoo.xsd');

?></body></html>

Warning: DOMDocument::relaxNGValidate(): Expecting no namespace for element zoo in C:\Program Files\Zend\Apache2\htdocs\xml\zoo.php on line 5 Warning: DOMDocument::schemaValidate(): Element '{http://www.w3.org/2001/XMLSchema}complexType': The content is not valid. Expected is (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))). in C:\Program Files\Zend\Apache2\htdocs\xml\zoo.php on line 6 Warning: DOMDocument::schemaValidate(): Invalid Schema in C:\Program Files\Zend\Apache2\htdocs\xml\zoo.php on line 6