validate a xml file against a xsd using php

Viewed 29534

how to validate a xml file against a xsd? there is domdocument::schemaValidate() but It does not tell where are the errors. is there any class for that? does it have any worth making that parser from scratch? or is it just reinventing he wheel,

3 Answers

This is a complete code snippet for displaying xsd validation errors:

    $xml = '<test/>';
    $xsd = '/path/to/xsd';
    // needed for getting errors
    libxml_use_internal_errors(true);

    $domDocument= new DOMDocument();
    $domDocument->loadXML($xml); 
    if (!$domDocument->schemaValidate($xsd)) {
        $errors = libxml_get_errors();
        foreach ($errors as $error) {
            print_r($error);
        }
        libxml_clear_errors();
    }
Related