纷繁的 XML Schema 技术:基于语法的语言[1]

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

本文简介:选择自 taowen2002 的 blog

3. 基于语法的语言 (relax ng)

we have seen that a schema can be described as a set of rules formalized using a language such as schematron of xslt (other languages such as prolog are probably good candidates too). the fact that this is possible isn't a proof that it's easy and people have developed other classes of more specific schema languages describing the structure of the documents rather than the rules to apply to validate them.

我们已经可以看到 schema 能够描述为一套使用像脱胎于 xslt 的 schematron (其他像 prolog 这样的语言也是很好的候选者)这样的形式化的规则。这是可能的事实并不是一个简单的证明,而且人们还开发了其他更专门的 schema 语言来描述文档的结构而不是验证它们的规则。

relax ng is the main example of such languages qualified of "grammar based" since they describe documents in the manner of a bnf adapted to describing xml trees.

relax ng 是这样的 "基于语法的" 语言中的主要代表,因为它们以适合描述 xml 树的 bnf 的方法描述文档。

although its syntax is very different from xpath, relax ng is all about named patterns allowed in the structure.

虽然它的语法不同于 xpath,但是 relax ng 完全是与在结构中允许的命名 pattern 有关。

3.1. 入门

the description of our simplified library could be:

我们的简化库可以描述为这样:

 <?xml version="1.0" encoding="utf-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
<start>
<element name="library">
<zeroormore>
<element name="book">
<attribute name="id"/>
</element>
</zeroormore>
</element>
</start>
</grammar>

this schema reads almost as plain english and describes: 'a grammar starting with a document element named "library" containing zero or more elements named "book" with an attribute named "id"' and his equivalent to our xslt closed schema accepting only "/library", "/library/book" and "/library/book/@id" --except that the restriction on ids being unique is not captured (yet) in our relax ng schema.

这个 schema 读起来几乎和英语一样,而且描述了:'一个语法,它由名字为 "library" 的文档元素开始,包含零个或者多个名字为 "book" ,有一个名字为 "id" 的属性的元素',而且它相当于我们只接受 "/library", "/library/book" 和 "/library/book/@id" 的 xslt 封闭 schema —— 除了在我们的 relax ng schema 中还没有添加 id 必须是唯一的限制。

3.2. 非 xml 语法

the xml syntax of this schema is still quite verbose and james clark has proposed an equivalent yet more concise non xml syntax. using this syntax, our schema would become:

这个 schema 的 xml 语法仍然非常冗长而且 james clark 以及提议了一种 xml 语法的对等物,而且更精确。使用这种语法,我们的 schema 变成:

 grammar {
start = element library{
element book {attribute id {text}} *
}

本文关键:XML Schema RELAX NG
 

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

go top