使用 schematron 进行 xsd 验证
我正在尝试将 schematron 验证添加到我的 xsd 中。 这是我的新 xsd :
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
xmlns:sch="http://www.ascc.net/xml/schematron"
elementFormDefault="qualified" >
<xs:element name="books">
<xs:complextype>
<xs:sequence> ;P
<xs:element name="book" type="bookType" maxoccurs="unbounded">
<xs:annotation>
<xs:appinfo>
<sch:pattern id="onLoanTests" xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:rule context="book">
<sch:report test="@on-loan and not(@return-date)">
Every book that is on loan must have a return date
</sch:report>
</sch:rule>
</sch:pattern>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complextype>
</xs:element>
<xs:complextype name="bookType">
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="author" type="xs:string" />
<xs:element name="publication-date" type="xs:string" />
</xs:sequence>
<xs:attribute name="publisher" type="xs:string" use="required" />
<xs:attribute name="on-loan" type="xs:string" use="required" />
<xs:attribute name="return-date" type="xs:string" use="optional" />
</xs:complextype>
</xs:schema>
这是我的测试 xml :
<books>
<book publisher="ddd" on-loan="sdsd">
<title>idan title</title>
<author>idan author</author>
<publication-date>idan date</publication-date>
</book>
</books>
使用我提供的 xml,我没有收到验证错误。
我假设我会收到消息“每本借出的书都必须有归还日期”并且 xml 无效。建议为什么?
更新 我确实设法通过在 oXygen xml 编辑器中使用 schematron 验证来使其工作。 但是,我应该如何在我的代码中使用? 我需要安装一些特殊的东西吗?链接到另一个库?
更新2 显然,此处在“处理”部分中详细介绍了所有所需的步骤。
I'm trying to add schematron validation to my xsd.
This is my new xsd :
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
xmlns:sch="http://www.ascc.net/xml/schematron"
elementFormDefault="qualified" >
<xs:element name="books">
<xs:complextype>
<xs:sequence> ;P
<xs:element name="book" type="bookType" maxoccurs="unbounded">
<xs:annotation>
<xs:appinfo>
<sch:pattern id="onLoanTests" xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:rule context="book">
<sch:report test="@on-loan and not(@return-date)">
Every book that is on loan must have a return date
</sch:report>
</sch:rule>
</sch:pattern>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complextype>
</xs:element>
<xs:complextype name="bookType">
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="author" type="xs:string" />
<xs:element name="publication-date" type="xs:string" />
</xs:sequence>
<xs:attribute name="publisher" type="xs:string" use="required" />
<xs:attribute name="on-loan" type="xs:string" use="required" />
<xs:attribute name="return-date" type="xs:string" use="optional" />
</xs:complextype>
</xs:schema>
This is my test xml :
<books>
<book publisher="ddd" on-loan="sdsd">
<title>idan title</title>
<author>idan author</author>
<publication-date>idan date</publication-date>
</book>
</books>
Using the xml I provided I don't get validation error.
I assumed I will get the message "Every book that is on loan must have a return date" And that the xml won't be valid. Suggestions as to why ?
Update
I did manage to make it work by using the schematron validation in oXygen xml editor.
However, how am I suppose to use in my code ?
Do I need to install something special ? link to another library ?
Update2
Apparently here in the "Processing" section, all the needed steps are detailed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的第二次更新可能是对该主题的最佳参考。 XSD 本身不允许您使用一种机制来验证模式以及模式本身。
xsd:appinfo
元素确实允许您嵌入不同模式语言的验证信息,但它是专门供应用程序使用的(因此得名)。这意味着您需要做一些事情来启用它。您引用的论文提供了最佳方法,可归结为:
来自 XSD 的规则
实施自
schematron.com 转换
架构到 XSLT
根据 XSD
通过处理对抗模式
2 中创建的 XSLT。
根据您的环境,您可能需要考虑查看 XProc 实现(葫芦 或 calumet) 来实现该管道。
Your second update is probably the best reference to the subject. XSD itself does not allow you a mechanism for validating against a schematron as well as the schema itself. The
xsd:appinfo
element does allow you to be embed validation information for a different schema language but it is specifically for application use (hence the name).That means you need to do something to enable it. The paper you referenced gives the best approach which boils down to:
rules from your XSD
implementation from
schematron.com to convert the
schema to XSLT
against the XSD
against the schematron by processing
the XSLT created in 2.
Depending on your environment you might want to consider looking at an XProc implementation (calabash or calumet) to achieve that pipeline.