PHP code example of milo / schematron

1. Go to this page and download the library: Download milo/schematron 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/ */

    

milo / schematron example snippets




use Milo\Schematron;

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

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

var_dump($result);

# 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 s (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.
}

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

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

# 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: 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

# Remote URLs
Schematron::INCLUDE_URL

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

# Any URI
Schematron::INCLUDE_ALL

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