Skip to content

Latest commit

 

History

History
103 lines (80 loc) · 4.5 KB

README.md

File metadata and controls

103 lines (80 loc) · 4.5 KB

eclipxe13/XmlSchemaValidator

Source Code Latest Version Software License Build Status Scrutinizer Coverage Status Total Downloads SensioLabsInsight

This is a library to validate XML files against multiple XSD Schemas according to its own definitions.

The way this works is:

  1. Receive a valid xml string as the content to be evaluated
  2. Scan the file for every schemaLocation
  3. Compose a schema that include all the schemas
  4. Validate the XML against the composed file

Installation

Use composer, so please run

composer require eclipxe/xmlschemavalidator

Basic usage

<?php
$contents = file_get_contents('example.xml');
$validator = new \XmlSchemaValidator\SchemaValidator($contents);
if (! $validator->validate()) {
    echo 'Found error: ' . $validator->getLastError();
}

Advanced usage

<?php
$contents = file_get_contents('example.xml');
$validator = new \XmlSchemaValidator\SchemaValidator($contents);
// change schemas collection to override the schema location of an 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 (\XmlSchemaValidator\SchemaValidatorException $ex) {
    echo 'Found error: ' . $ex->getMessage();
}

About libxml errors

This library depends on PHP libxml and uses internal errors libxml_use_internal_errors to retrieve the errors when creates the DOMDocument or validate against the schema files. Instead of raise an error it creates a LibXmlException with the errors chained. It also restore the value of libxml_use_internal_errors after execution.

Version 1.x is deprecated

Version 1.x is no longer on development. It has a problem of concerns, the same library try to solve two different issues: Validate an XML file and store locally a copy of the XSD files. Version 2.x breaks this problem and give this library only one propose: Validate an XML file against its multiple XSD files, it does not matter where are located.

Also, version 2.x uses PHP 7 with scalar type declarations, so it no longer compatible with PHP 5.6.

Contributing

Contributions are welcome! Please read CONTRIBUTING for details and don't forget to take a look in the TODO and CHANGELOG files.

Copyright and License

The eclipxe13/XmlSchemaValidator library is copyright © Carlos C Soto and licensed for use under the MIT License (MIT). Please see LICENSE for more information.