定制 milo/schematron 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

milo/schematron

最新稳定版本:v1.0.1

Composer 安装命令:

composer require milo/schematron

包简介

Implementation of ISO Schematron with Schematron 1.5 back compatibility. Library does not need any XSLT.

README 文档

README

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

统计信息

  • 总下载量: 359.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 18
  • 点击次数: 1
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 3
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2013-07-22