validate a xml file against a xsd using php

前端 未结 3 483
误落风尘
误落风尘 2021-02-04 02:05

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 m

3条回答
  •  无人及你
    2021-02-04 02:23

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

        $xml = '';
        $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();
        }
    

提交回复
热议问题