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

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

本文简介:选择自 taowen2002 的 blog

</library>

an application managing the library described by this document, or even a xslt stylesheet designed to display it would probably be very confused if the name or content of the elements are not what they expect and a the main feature of a xml schema language is to provide a formal way to describe what is expected to protect the applications from these risks of errors.

一个管理本文档描述的库的程序,或者甚至是一个 xslt 样式表被设计出来显示它。如果元素的名字或者内容不是它所期望的,它可能被完全地困住了。而 xml schema 语言的一个主要作用就是提供一种正规的办法来描述所期望的是什么并保护程序免于发生错误的风险。

2. 基于规则的语言 (xslt & schematron)

the most basic way to implement this firewall is to give a set of rules which need to followed by the instance documents.

最基本的实现这种防火墙的办法是给出一组需要附带一些例子文档的规则。

this is the approach followed by rule based xml schema languages which main representative is schematron. before presenting schematron itself, we will have a look on how xslt may be used as a xml schema language since this is a good exercise to understand the basics of those schema languages.

这就是基于规则的 xml schema 语言所采用的办法,其中的代表者就是 schematron。在介绍 schematron 之前,我们将先看看 xslt 可以怎么用作 xml schema 语言,因为这对于理解这些 schema 语言的基本知识来说是一个很好的锻炼。

2.1. xslt 用作基于规则的 xml schema 语言

we can use "classical" programming languages to write a rule based xml schema either general purpose using a xml api or xml specific such as xslt or xquery.

我们可以使用 "传统的" 编程语言来编写基于规则的 xml schema 也能通用地使用 xml api 或者特殊的 xml 例如 xslt 或者xquery。

to illustrate this point, let's take the following very simple snippet of our example:

为了阐明这个观点,让我们来看看下面这个我们例子中非常简单的代码片断:

 <?xml version="1.0"?>
<library>
<book id="_0836217462"/>
<book id="_0805033106"/>
</library>

why so simple? because we will see that even if it is true that we can use xslt as a rule based xml schema language, this is quite verbose and i don't want spend all the time allocated to this tutorial to develop our schema!

为什么如此简单?因为即使xslt可以用作基于规则的 xml schema 语言,那也是非常冗长的我不想把所有分配给这个教程的时间用来开发我们的 schema!

to write this schema, we have basically two options which are the same than we have when we configure a firewall: the closed one where all what is not allowed is forbidden and the open one where all what is not forbidden is allowed and we will implement both schemas.

编写这个 schema,我基本上有两个选择来配置防火墙:封闭的,所有不允许的都被禁止;以及开放的,所有不禁止的都被允许。我们将两种 schema 都实现一下。

the first conclusion from this simple example is that xml applications tend to forbid much more than they allow: closed schemas are often easier to write than open schemas.

从这个简单的例子中得出的第一个推论就是 xml 程序倾向于禁止比它们允许的更多东西:封闭的 schema 比开放的 schema 一般更容易写一些。

on the other hand, it's easier to define user friendly error messages in a open schema since the context in which something is forbidden is always determined.

另一方面,在开放 schema 中定义用户友好的错误信息更加容易,因为被禁止的东西的上下文总是确定的。

2.1.1. 开放的 xslt schema

to implement an open schema with xslt, we will start defining a default template which will accept anything:

为了用 xslt 实现开放的 schema,我们将从定义个一个缺省的允许所有东西的模板开始:

 <xsl:template match="*|@*|text()">
<xsl:apply-templates select="*|@*|text()"/>

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

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

go top