# Schematron

**Schematron** 是一种基于规则的 XML 模式语言，它通过针对选定的上下文节点求值 XPath（用于选取 XML 文档各部分的一种查询语言）断言来验证文档，而不是将文档与语法进行对照检查。它被标准化为 ISO/IEC 19757-3，即文档模式定义语言（DSDL）系列的第 3 部分，该标准确立了 Schematron 模式的要求，并规定了 XML 文档何时匹配模式所声明的 pattern。<sup>[1](https://www.iso.org/cms/live/live/en/sites/isoorg/contents/data/standard/08/56/85625.html?browse=tc)</sup> 该语言由 Rick Jelliffe 在台湾中央研究院计算中心设计和实现。<sup>[2](https://www.researchgate.net/publication/2949288_Constraint_Specification_Languages_comparing_XCSL_Schematron_and_XML-Schemas)</sup>

| 关键事实 | 详情 |
|---|---|
| 标准 | ISO/IEC 19757-3，DSDL 第 3 部分：使用 Schematron 进行基于规则的验证；第 4 版于 2025 年 9 月发布，共 51 页，由 ISO/IEC JTC 1/SC 34 制定<sup>[1](https://www.iso.org/cms/live/live/en/sites/isoorg/contents/data/standard/08/56/85625.html?browse=tc)</sup> |
| 起源 | 由 Rick Jelliffe 在台湾中央研究院计算中心设计和实现<sup>[2](https://www.researchgate.net/publication/2949288_Constraint_Specification_Languages_comparing_XCSL_Schematron_and_XML-Schemas)</sup> |
| 方法 | 基于规则，而非基于语法：通过 XPath 找到上下文节点，然后针对每个节点测试进一步的 XPath 表达式<sup>[3](https://schematron.com/document/2755.html)</sup> |
| 核心元素 | pattern、rule、assert、report、phase 和 diagnostic；assert 在其测试为假时触发，report 在其测试为真时触发<sup>[4](https://www.xfront.com/schematron/Usage-and-Features.html)</sup><sup> • </sup><sup>[5](https://jats.taylorandfrancis.com/jats-guide/tools/schematron/svrl/)</sup> |
| 输出 | Schematron 验证报告语言（SVRL），一种在 ISO/IEC 19757-3 中描述的 XML 报告格式<sup>[5](https://jats.taylorandfrancis.com/jats-guide/tools/schematron/svrl/)</sup> |
| 实现 | schematron.com 的概述称其在 XSLT 之上实现起来极其简单，另有 Python 和 Perl 的实现<sup>[3](https://schematron.com/document/2755.html)</sup> |

## 验证的工作方式

Schematron 处理器遵循一个简单的顺序。它首先使用 XPath 路径条件在文档中找到有用的上下文节点，通常是元素；然后针对每个上下文节点求值进一步的 XPath 表达式；最后报告哪些断言失败、哪些报告成功，从而为用户提供有针对性的信息。<sup>[3](https://schematron.com/document/2755.html)</sup>

两类测试元素承载约束。assert 元素在其 XPath 测试求值为假时输出一条消息，断言某个条件成立；report 元素在其测试求值为真时输出一条消息，标记值得注意或禁止出现的情况。较旧的模式假定 assert 用于错误、report 用于提示信息，但当前的做法是选择使测试更易于编写的那种方向，并用 role 属性标记消息的严重程度，例如 error、warning 或 info。<sup>[5](https://jats.taylorandfrancis.com/jats-guide/tools/schematron/svrl/)</sup>

## 模式的结构

Schematron 模式将相关规则分组为 pattern。理想情况下，每个 pattern 覆盖一个自包含的策略领域，如标识符、财务合计或出版元数据；保持 pattern 较小，则更容易分别启用、检查和报告，而将所有规则打包进一个 pattern 会模糊各项规则的归属，并使构建配置文件变得困难。<sup>[4](https://www.xfront.com/schematron/Usage-and-Features.html)</sup> 每个 rule 选择一个上下文，其断言和报告相对于这些节点求值 XPath。首选狭窄、稳定的上下文，因为非常宽泛的后代搜索可能很慢，并可能在意外位置重复触发同一策略。<sup>[4](https://www.xfront.com/schematron/Usage-and-Features.html)</sup>

**阶段。** phase 为一个验证场景激活一组选定的 pattern。因此，同一源模式可以支持基本接收配置文件、更严格的出版配置文件和迁移审计，而无需复制规则。阶段标识符被视为公共配置，从业者会测试每个必需的 pattern 在预期阶段中处于活动状态。<sup>[4](https://www.xfront.com/schematron/Usage-and-Features.html)</sup> 标准本身不提供对阶段或阶段内 pattern 进行排序的机制。<sup>[6](https://schematron.com/document/2760.html)</sup>

**诊断与标识符。** 可重用的 diagnostic 元素可以将详细信息附加到多个断言上。保持断言和诊断标识符的稳定性有助于本地化、分析和自动路由；此类标识符应指向策略而非消息措辞，因为消息可能会被改进，而不需要每个消费系统修改其分类方式。<sup>[4](https://www.xfront.com/schematron/Usage-and-Features.html)</sup> 在 SVRL 输出中，失败的断言显示为 svrl:failed-assert 元素，成功的报告显示为 svrl:successful-report 元素，每个元素都带有 test、role 和 id 属性，以及一个 location 属性，给出文档中产生消息的确切位置的 XPath。<sup>[5](https://jats.taylorandfrancis.com/jats-guide/tools/schematron/svrl/)</sup>

**抽象模式。** 抽象 pattern 和 rule，以及外部定义，是随标准化一同加入的，它们让多个规则共享一种形态：例如，一个针对一般表格的抽象 pattern 可以绑定到具体的 XPath 表达式，用于那些恰好以不同名称具有表格结构的元素。抽象构造减少了重复，但过多的间接层会使活动的 XPath 难以检查。<sup>[6](https://schematron.com/document/2760.html)</sup><sup> • </sup><sup>[4](https://www.xfront.com/schematron/Usage-and-Features.html)</sup>

## 与基于语法的模式语言的关系

Schematron 在基本概念上不同于 DTD 和 XML Schema：它不是基于语法，而是基于在解析后的文档中查找树模式。这使它能够表达在基于语法的语言中不便表达的结构，例如不同上下文中元素之间的关系、数据模型上的不变式，以及将文档中任何位置的信息联系在一起的约束。<sup>[3](https://schematron.com/document/2755.html)</sup><sup> • </sup><sup>[2](https://www.researchgate.net/publication/2949288_Constraint_Specification_Languages_comparing_XCSL_Schematron_and_XML-Schemas)</sup> XML Schema 处理数据类型和数据格式，但仅靠语法语言无法规定许多非结构性约束，如依赖关系和一致性。<sup>[2](https://www.researchgate.net/publication/2949288_Constraint_Specification_Languages_comparing_XCSL_Schematron_and_XML-Schemas)</sup>

这两种方法是互补的。Schematron 与基于语法的结构验证语言协同工作，包括 DTD、XML Schema、RELAX 和 TREX，并且 Schematron 模式可以嵌入 XML Schema 的 appinfo 元素内或 RELAX NG 模式内。它所属的标准系列 DSDL 旨在让多种聚焦良好的 XML 验证语言协同工作。<sup>[3](https://schematron.com/document/2755.html)</sup>

## 实现与工具

Schematron 构建在标准 XPath 和 XSLT 工具之上，这使代码库保持小巧；schematron.com 的概述称其在 XSLT 之上实现和定制极其简单，另有 Python 和 Perl 的实现。<sup>[3](https://schematron.com/document/2755.html)</sup> 根据标准，编译验证样式表是一个三阶段过程，因为必须解析抽象 pattern 和 rule 以及外部定义；然后编译后的样式表应用于 XML 文档以生成 SVRL 报告。<sup>[7](https://archive.xmlprague.cz/2019/files/xmlprague-2019-proceedings.pdf)</sup> 由于 SVRL 是 XML，它具有机器可读性，可以转换为 HTML 等其他格式，因此 Schematron 很容易集成到现有的 XML 工具链和流水线中。<sup>[5](https://jats.taylorandfrancis.com/jats-guide/tools/schematron/svrl/)</sup><sup> • </sup><sup>[3](https://schematron.com/document/2755.html)</sup>

该语言很小。schematron.com 的概述统计 ISO Schematron 中只有 6 个基本元素，其他元素主要支持验证器的用户界面；<sup>[3](https://schematron.com/document/2755.html)</sup> 2015 年 XML Prague 的一篇论文统计总共有 21 个元素和少量属性。<sup>[8](https://archive.xmlprague.cz/2015/files/xmlprague-2015-proceedings.pdf)</sup> 实现还在标准之外添加了实用检查：Rust 的 schematron 程序包会检查模式中合法但几乎肯定错误的构造，例如同一 pattern 中较早规则遮蔽了后续规则，或带命名空间的模式中未加前缀的元素名，它将其识别为 Schematron 模式静默无作为的两种方式。<sup>[9](https://docs.rs/crate/schematron/0.3.1)</sup>

## 应用

Schematron 用于在出版、数据交换和标准工作中对 XML 文档执行业务规则。在学术出版中，一位使用 JATS（期刊文章标签套件）的出版商构建了一个模块化的 Schematron 套件，分层在 DTD 之上，因为规则的数量、复杂性以及期刊间的差异使得不宜将规则放入 DTD 或模式本身。每条业务或样式规则都位于自己的文件中，驱动文件通过 include 机制引入适当的测试，因此通用规则只写一次、集中维护，并纳入多个工作流，每个出版物只包含与其相关的测试。该套件还可以在文档生命周期的多个时间点，按各阶段的不同要求运行。<sup>[10](https://www.ncbi.nlm.nih.gov/books/NBK279902/)</sup>

在数据交换标准中，Schematron 既实现业务规则，即为部分用户使包容性模式更具限制性，也实现命名与设计规则（NDR），即控制模式定义语言本身的使用方式，以保持扩展的互操作性。由于 Schematron 支持关于任意模式的断言，它可以执行在基于语法的模式语言中难以表达的约束，而其模块化的基于规则的语法及作者提供的诊断消息使其成为实现指南的常见选择。<sup>[11](https://balisage.net/Proceedings/vol3/print/Lubell01/BalisageVol3-Lubell01.html)</sup>

## 标准化状态

当前版本 ISO/IEC 19757-3:2025（第 4 版）由 [ISO/IEC JTC 1](https://www.edgepedia.org/zh/iso-iec-jtc-1)/SC 34 于 2025 年 9 月发布，共 51 页，定价 204 瑞士法郎。它取代了已撤销的 ISO/IEC 19757-3:2020，该标准每 5 年审查一次；其当前阶段列为“待修订的国际标准”（90.92）。修订项目于 2022 年 9 月获批，2024 年经过工作草案和委员会草案阶段，并于 2024 年 8 月获批注册为 DIS。<sup>[1](https://www.iso.org/cms/live/live/en/sites/isoorg/contents/data/standard/08/56/85625.html?browse=tc)</sup> 该标准还在一个资料性附录中描述了 SVRL，并包含资料性查询语言绑定，例如 2016 年版中的 XPath 2 绑定。<sup>[5](https://jats.taylorandfrancis.com/jats-guide/tools/schematron/svrl/)</sup><sup> • </sup><sup>[12](https://www.evs.ee/en/StandardDownload/DownloadPreview?language=EnglishLanguage&productId=64904)</sup>

## References

1. [ISO/IEC 19757-3:2025 - Information technology - Document Schema Definition Languages (DSDL) - Part 3: Rule-based validation using Schematron, ISO](https://www.iso.org/cms/live/live/en/sites/isoorg/contents/data/standard/08/56/85625.html?browse=tc)
2. [Jacinto, Librelotto, Ramalho, Henriques. Constraint Specification Languages: comparing XCSL, Schematron and XML-Schemas, ResearchGate](https://www.researchgate.net/publication/2949288_Constraint_Specification_Languages_comparing_XCSL_Schematron_and_XML-Schemas)
3. [An Overview of Schematron, schematron.com](https://schematron.com/document/2755.html)
4. [Schematron Usage, Phases and Diagnostics, xFront](https://www.xfront.com/schematron/Usage-and-Features.html)
5. [Schematron Validation Reports, JATS Guide, Taylor & Francis](https://jats.taylorandfrancis.com/jats-guide/tools/schematron/svrl/)
6. [WhyIsSchematronDifferent?, schematron.com](https://schematron.com/document/2760.html)
7. [XML Prague 2019 Proceedings](https://archive.xmlprague.cz/2019/files/xmlprague-2019-proceedings.pdf)
8. [XML Prague 2015 Proceedings](https://archive.xmlprague.cz/2015/files/xmlprague-2015-proceedings.pdf)
9. [schematron 0.3.1, Docs.rs](https://docs.rs/crate/schematron/0.3.1)
10. [Superimposing Business Rules on JATS, JATS-Con Proceedings 2015, NCBI Bookshelf](https://www.ncbi.nlm.nih.gov/books/NBK279902/)
11. [Documenting and Implementing Guidelines with Schematron, Balisage Proceedings](https://balisage.net/Proceedings/vol3/print/Lubell01/BalisageVol3-Lubell01.html)
12. [ISO/IEC 19757-3:2016 preview, EVS](https://www.evs.ee/en/StandardDownload/DownloadPreview?language=EnglishLanguage&productId=64904)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Data formats and serialization

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/schematron

English original: https://www.edgepedia.org/schematron
