novomirskoy / xml-validator
Requires
- php: ^8.5
- ext-dom: *
- ext-libxml: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- phpunit/phpunit: 13.*
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2026-06-16 13:31:23 UTC
README
XML document validation using XSD schemas
Installation
Install the library using Composer:
composer require novomirskoy/xml-validator
Requirements
The following PHP extensions are required for the library to work:
ext-domext-libxml
Usage
Create an instance of the Validator class and call the validate() method,
passing an XML document as a string and a schema. The schema can be either a string or a path to an XSD file.
Create a schema instance using the named constructors Schema::file()
or Schema::string(), passing the file path or schema string respectively.
The validator does not store the validation result, so it immediately returns a Result object that indicates whether the validation was successful and returns a list of all errors that occurred.
Examples
// Example XML document $xml = <<<XML <?xml version="1.0" encoding="UTF-8"?> <book> <title>Design Patterns</title> <author>Gamma</author> <year>1994</year> </book> XML; // Validation using XSD schema from string $xsdSchema = <<<XSD <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="book"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string"/> <xs:element name="year" type="xs:integer"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> XSD; $result = new Validator()->validate( xml: $xml, schema: Schema::string($xsdSchema), ); if (!$result->isValid()) { foreach ($result->errors as $error) { echo $error, PHP_EOL; } } // Validation using XSD schema from file $result = new Validator()->validate( xml: $xml, schema: Schema::file('/path/to/schema.xsd'), ); if (!$result->isValid()) { foreach ($result->errors as $error) { echo $error, PHP_EOL; } }
Documentation
For the full documentation in Russian, please see docs/README.ru.md.