milo/schematron

ISO Schematron 的实现,支持 Schematron 1.5 的向后兼容性。该库不需要任何 XSLT。

v1.0.1 2014-04-07 10:32 UTC

This package is auto-updated.

Last update: 2024-09-04 20:26:57 UTC


README

这个库是对 ISO Schematron 的实现(支持 Schematron 1.5 的向后兼容性)。它通过纯 DOM 处理实现,不需要任何 XSLT 表单或 XSLT PHP 扩展。这是开发的一个要求。

用法

通过 Composer 安装 Schematron 或下载发布包。

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);

Schematron::validate() 结果的格式取决于其第二个参数。例如,假设的结果

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

可以通过第三个参数传递验证阶段

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

Schematron 会自动检测模式名称空间(ISO 或 v1.5),但可以手动传递名称空间

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

通过 Schematron::setOptions($options) 你可以调整 Schematron 的行为。$options 是以下标志的掩码

# 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

<sch:include> 处理受 Schematron::setAllowedInclude($allowed) 掩码的影响,该掩码允许包含 uri 的类型和 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

并且可以通过以下方式访问加载模式的基本属性

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

许可证

你可以根据新 BSD 许可证、GNU 公共许可证(GPL)版本 2 或 3 或 MIT 许可证的条款使用所有文件。

测试

Schematron 测试是为 Nette Tester 编写的。运行它们需要两个步骤

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

# Run the tests
vendor/bin/tester tests

Build Status