Giter Site home page Giter Site logo

schematron's Introduction

This library is an implementation of the ISO Schematron (with Schematron 1.5 back compatibility). It is done by pure DOM processing and does not require any XSLT sheets nor XSLT PHP extension. It was a requirement for a development.

Usage

Install the Schematron by the Composer or download a release package.

require 'src/Schematron.php';

use Milo\Schematron;

$schematron = new Schematron;
$schematron->load('schema.xml');

$document = new DOMDocument;
$document->load('document.xml');
$result = $schematron->validate($document);

var_dump($result);

Format of the Schematron::validate() result depends on its second argument. E.g. an imaginary results:

# Flat array of failed asserts and successful reports (it is default)
$result = $schematron->validate($document, Schematron::RESULT_SIMPLE);
# array (2)
#    0 => "Person must have surname."
#    1 => "Phone number is required."


# More complex structure
$result = $schematron->validate($document, Schematron::RESULT_COMPLEX);
# array (3)
#    0 => stdClass (2)
#       title => "Pattern 1" (9)
#       rules => array (3)
#          2 => stdClass (2)
#             context => "/"
#             errors => array (2)
#                0 => stdClass (3)
#                |  test => "false()" (7)
#                |  message => "S5 - fail" (9)
#                |  path => "/"
#                1 => stdClass (3)
#                   test => "true()" (6)
#                   message => "S6 - fail" (9)
#                   path => "/"


# Or throws exception of first error occurence
try {
    $result = $schematron->validate($document, Schematron::RESULT_EXCEPTION);
} catch (Milo\SchematronException $e) {
    echo $e->getMessage();  # Person must have surname.
}

A validation phase can be passed by 3rd argument:

$schematron->validate($document, Schematron::RESULT_SIMPLE, 'phase-base-rules');

Schematron performs a schema namespace (ISO or v1.5) autodetection but the namespace can be passed manually:

$schematron = new Schmeatron(Schematron::NS_ISO);

By Schematron::setOptions($options) you can adjust the Schematron behaviour. The $options is a mask of following flags:

# Allows to schema does not contain a <sch:schema> element,
# so <pattern>s stands alone in XML, e.g. in Relax NG schema
Schematron::ALLOW_MISSING_SCHEMA_ELEMENT

# <sch:include> are ignored and do not expand
Schematron::IGNORE_INCLUDE

# <sch:include> are forbidden and loading fails if occures
Schematron::FORBID_INCLUDE

# <sch:rule> with the same @context as any rule before is skipped
# This arises from official Universal Tests (http://www.schematron.com/validators/universalTests.sch)
Schematron::SKIP_DUPLICIT_RULE_CONTEXT

# <sch:schema> needn't to contain <sch::pattern>s
Schematron::ALLOW_EMPTY_SCHEMA

# <sch:pattern> needn't to contain <sch::rule>s
Schematron::ALLOW_EMPTY_PATTERN

# <sch:rule> needn't to contain <sch:assert>s nor <sch:report>s
Schematron::ALLOW_EMPTY_RULE

An <sch:include> processing is affected by setting Schematron::setAllowedInclude($allowed) mask which permits types of include uri and Schematron::setMaxIncludeDepth($depth):

# Remote URLs
Schematron::INCLUDE_URL

# Absolute and relative filesystem paths
Schematron::INCLUDE_ABSOLUTE_PATH
Schematron::INCLUDE_RELATIVE_PATH

# Any URI
Schematron::INCLUDE_ALL

And two basic attributes of loaded schema are accesible over:

$schematron->getSchemaVersion();
$schematron->getSchemaTitle();

Licence

You may use all files under the terms of the New BSD Licence, or the GNU Public Licence (GPL) version 2 or 3, or the MIT Licence.

Tests

The Schematron tests are written for Nette Tester. Two steps are required to run them:

# Download the Tester tool
composer.phar update --dev

# Run the tests
vendor/bin/tester tests

Build Status

schematron's People

Contributors

milo avatar awnage avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.