纷繁的 XML Schema 技术:基于规则的语言[7]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 taowen2002 的 blog

</sch:schema>

2.2.5. 合起来

solving the same problem with schematron and xslt shows the nature of schematron which is a subset of xslt tailored to xml validation through open rule based schemas.

用 schematron 和 xslt 解决同一个问题揭示了 schematron 的本质,即它是剪裁用于 xml 验证的实现基于开放规则 schema 的 xslt 子集 。

why do i insist that much on the openness of schematron schemas?

为什么我强调 schematron schema 的开放性?

because the default behavior of schematron is to be open, but it is still possible to write closed (or semi closed schemas) with schematron even though it isn't a common practice.

因为 schematron 的缺省行为是开放的,但是也可以用 schematron 编写封闭的(或者是半封闭的)schema,虽然这不是一个常见的行为。

the main trick when doing so is to note that the rules within a schematron pattern are evaluated in lexical order instead of following the rules of priorities as defined by xslt. the default rules which will forbid content not described in any rule need therefore to be located after all the other rules, such as in:

要那样做的主要技巧是记住 schematron pattern 中的规则是以词法顺序执行而不是由 xslt 定义的优先级来决定的。缺省禁止没有在任何规则中描述的内容的规则因而需要放在所有其他规则之后,像这样:

 <?xml version="1.0" encoding="utf-8"?>
<sch:schema xmlns:sch="http://www.ascc.net/xml/schematron">
<sch:title>example schematron schema</sch:title>
<sch:pattern>
<sch:rule context="/library">
<sch:report test="@*">
the library element should not contain attributes.
</sch:report>
</sch:rule>
<sch:rule context="library/book">
<sch:report test="@*[name() != 'id']">
the book element should not include any attribute other than "id".
</sch:report>
<sch:assert test="@*[namespace-uri() = '']">
the book element should not include any attribute other than "id" (namespace).
</sch:assert>
<sch:report test="@id = preceding-sibling::book/@id">
the book id should be unique.
</sch:report>
</sch:rule>
<sch:rule context="*">
<sch:report test="1">
element "<sch:name/>" forbidden under "<sch:name path=".."/>".
</sch:report>
</sch:rule>
<sch:rule context="text()[normalize-space()]">
<sch:report test="1">
text forbidden in "<sch:name path=".."/>" element.
</sch:report>
</sch:rule>
</sch:pattern>

本文关键:XML Schematron Rule XSLT
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top