PHP code example of eclipxe / xmlschemavalidator

1. Go to this page and download the library: Download eclipxe/xmlschemavalidator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eclipxe / xmlschemavalidator example snippets



declare(strict_types=1);

use Eclipxe\XmlSchemaValidator\SchemaValidator;

$contents = file_get_contents('example.xml');
$validator = SchemaValidator::createFromString($contents);
if (! $validator->validate()) {
    echo 'Found error: ' . $validator->getLastError();
}


declare(strict_types=1);

use Eclipxe\XmlSchemaValidator\SchemaValidator;
use Eclipxe\XmlSchemaValidator\Exceptions\ValidationFailException;

// create SchemaValidator using a DOMDocument
$document = new DOMDocument();
$document->load('example.xml');
$validator = new SchemaValidator($document);

// change schemas collection to override the schema location of a specific namespace
$schemas = $validator->buildSchemas();
$schemas->create('http://example.org/schemas/x1', './local-schemas/x1.xsd');

// validateWithSchemas does not return boolean, it throws an exception
try {
    $validator->validateWithSchemas($schemas);
} catch (ValidationFailException $ex) {
    echo 'Found error: ' . $ex->getMessage();
}