XML Schema全接触 (这里主要介绍W3C的Schema标准语法)

<p>来源:<a href="http://bbs.w3china.org/dispbbs.asp?BoardID=23&ID=27215&ReplyID=22178&skin=1">http://bbs.w3china.org/dispbbs.asp?BoardID=23&ID=27215&ReplyID=22178&skin=1</a></p>
<p> </p>
<p>XML Schema规范
</p>
<p> </p>
<p>2001年5月2日,XML Schema 规范成为了W3C的正式推荐标准。经过三年多的发展,XML 的Schema标准终于修成了正果。这样,XML、XML Schema和Namespace都成为了W3C的正式标准,这是一个值得庆贺的历史性时刻,意味着XML语法的规范已经奠定了扎实的基础,XML的广泛发展和应用也即将成为现实。这里将介绍XML定义语言、XML Schema全貌和XML Schema如何校验XML文档。</p>
<p>XML定义语言:DTD,XDR和XSD</p>
<p>我们知道XML文档有结构良好和有效性两种约束。格式良好适合于所有的XML文档,即满足XML标准中对于格式的规定。而当XML文档满足一定的语义约束则称该XML文档为有效的XML文档。目前常用的XML定义语言有DTD,XDR和XSD。</p>
<p>文档类型定义(DTD)</p>
<p>文档类型定义用不同于XML的独立语法来规定了XML文档中各种元素集合的内容模式。该语言直接沿袭了定义SGML语言的方法,这样做的好处是如下:</p>
<p>DTD使得XML文档保持一致<br>DTD可以共享<br>DTD提供了对XML语汇的形式化和完整的定义<br>每个XML文档有单个的DTD来限制</p>
<p>Schema</p>
<p>类似DTD,Schema可以规定一套特定文档的结构或模型。使用Schema语言来描述文档结构有下面以下好处:<br>Schema使用的是XML语法<br>Schema可以用XML解析器来解析<br>Schema允许全局性元素(在整个XML文档中元素用相同方式来使用)和局部性元素(元素在特定的上下文中有不同的含义)<br>Schema提供丰富的数据类型(如整型、布尔型、日期类型等);而且一个元素中的数据类型可以进行规定,甚至可以根据需要自定义数据类型</p>
<p>XDR</p>
<p>XDR的全称是XML-Data Reduced Language,是早先由XML-Data草案派生出来的一个定义XML语言的分支。由于微软最先提出了XML-Data,并且经过修订后在其软件中应用,所以XDR也被广泛使用。目前支持XDR Schema的产品有:</p>
<p>Microsoft Biztalk Server<br>Microsoft SQL Server 2000<br>Microsoft Office 2000<br>Microsoft IE5.0和后续版本<br>XDR也得到了Extensibility的 XML Authority编辑工具的支持。</p>
<p>XSD Schema</p>
<p>最为正式的XML Schema语言是由W3C指定的XML Schema规范,简称为XSD(XML Schema Definition)。XSD也提供了数据类型的支持和结构定义的方法。</p>
<p>2001年5月2日,XML Schema 规范成为了W3C的正式推荐标准。这就意味着经过三年多的发展,XML 的Schema标准终于修成了正果。这样,XML、XML Schema和Namespace都成为了W3C的正式,这是一个值得庆贺的历史性时刻,意味着XML语法的规范已经奠定了扎实的基础。XML的广泛发展和应用也即将成为现实。下面我们就将重点来介绍最新的XML Schema标准。</p>
<p>我们用实际的例子来介绍XML Schema的用法。比如,有这样的XML实例文档:</p>
<p><?xml version="1.0" encoding="GB18030"?><br><studentlst><br><student><br><name>至尊宝</name><br><genda>男</genda><br><sid>001</sid><br><birthday>1576-3-2</birthday><br></student><br><student><br><name>白晶晶</name><br><genda>女</genda><br><sid>002</sid><br><birthday>1578-4-25</birthday><br></student><br></studentlst></p>
<p>但是其中name和genda、sid等文本元素的数据类型都是统一的字符类型,而事实上我们一般要求对它们有更为严格的限制。比如,要求name仍然为字符类型,而genda为可选的枚举类型,只能取男或女,sid要求是三位的整数类型,并且要求birthday为日期类型。name和birthday的定义比较简单:</p>
<p><element name="name" type="string" minOccurs="1" maxOccurs="1" /><br><element name="birthday" type="date" minOccurs="1" maxOccurs="1"/></p>
<p>其中string和date类型都是Schema中自带的基本数据类型(Primary Data Type)。minOccurs和maxOccurs是最少和最多出现次数,这里是表示有而且只出现一次。这是最简单的元素声明。在XML Schema中提供两种数据类型:一是我们刚刚接触的基本数据类型或者称为内建数据类型;另外一种是扩展的数据类型,既在基本数据类型基础上用户自己扩展的数据类型。Schema中没有规定性别的类型,也没有直接规定三位整数的数据类型,但是性别是有男、女两种选择的枚举类型;而sid的每位都是非负整数,于是我们可以先定义两类扩展数据类型,然后来限制genda和sid元素。</p>
<p><simpleType name="GendaType"><br><restriction base="string"><br><enumeration value="男"/><br><enumeration value="女"/><br></restriction><br></simpleType><br><simpleType name="SIDType"><br><restriction base="string"><br><length value="3"/><br><pattern value="/d{3}"/><br></restriction><br></simpleType></p>
<p>在定义扩展数据类型时是描述了一个simpleType元素,name属性是该数据类型的名称,数据类型由restriction子元素进行约束,该元素中的base属性是基类型,即它要扩展的基本数据类型。在枚举类型中在restriction的子元素中罗列初可选的数值;而在第二种情况时,通过对各方面(facet)进行规定来定义数据类型,比如length规定该数据类型总共有多少位数,而pattern则通过正则表达式来规定出现的方式。这里的意思是连续出现三位的整数。</p>
<p>用上面定义的数据类型来约束genda和sid的方法和name和birthday元素的声明相同:</p>
<p><element name="genda" type="GendaType" minOccurs="1" maxOccurs="1"/><br><element name="sid" type="SIDType" minOccurs="1" maxOccurs="1"/></p>
<p>有了最基本的四个文本内容的元素,如何定义作为其父元素的student元素呢?由于student元素是由子元素组成的,在Schema中称它位复杂类型的元素。而且其子元素是顺序组成的序列,因此这样声明student元素:</p>
<p><element name="student"><br><complexType><br><sequence><br><element name="name" type="string" minOccurs="1" maxOccurs="1"/><br><element name="genda" type="GendaType" minOccurs="1" maxOccurs="1"/><br><element name="sid" type="SIDType" minOccurs="1" maxOccurs="1"/><br><element name="birthday" type="date" minOccurs="1" maxOccurs="1"/><br></sequence><br></complexType><br></element></p>
<p>另外,studentlst元素包含了student元素,并且出现的方式是一个或者多个,或者可以不出现,在Schema中出现的方式可以用miOccurs和maxOccurs来表达:</p>
<p><element name="studentlst"><br><complexType><br><sequence><br><element name="student" minOccurs="0" maxOccurs="unbounded"><br><complexType><br><sequence><br><element name="name" type="string" minOccurs="1" maxOccurs="1"/><br><element name="genda" type="GendaType" minOccurs="1" maxOccurs="1"/><br><element name="sid" type="SIDType" minOccurs="1" maxOccurs="1"/><br><element name="birthday" type="date" minOccurs="1" maxOccurs="1"/><br><sequence><br><complexType><br><element><br><sequence><br><complexType><br><element></p>
<p>最后我们将这些元素和数据类型的声明都包含在schema根元素中:</p>
<p><?xml version="1.0" encoding="GB18030"?><br><schema xmlns="<a class="contentlink" href="http://www.w3.org/2001/XMLSchema%22" target="_blank">http://www.w3.org/2001/XMLSchema"</a> xmlns:sl="<a class="contentlink" href="http://www.xml.org.cn/namespaces/StudentList%22" target="_blank">http://www.xml.org.cn/namespaces/StudentList"</a> targetNamespace="<a class="contentlink" href="http://www.xml.org.cn/namespaces/StudentList%22%3E" target="_blank">http://www.xml.org.cn/namespaces/StudentList"></a><br><element name="studentlst"><br><complexType><br><sequence><br><element name="student" minOccurs="0" maxOccurs="unbounded"><br><complexType><br><sequence><br><element name="name" type="string" minOccurs="1" maxOccurs="1"/><br><element name="genda" type="sl:GendaType" minOccurs="1" maxOccurs="1"/><br><element name="sid" type="sl:SIDType" minOccurs="1" maxOccurs="1"/><br><element name="birthday" type="date" minOccurs="1" maxOccurs="1"/><br></sequence><br></complexType><br></element><br></sequence><br></complexType><br></element><br><simpleType name="GendaType"><br><restriction base="string"><br><enumeration value="男"/><br><enumeration value="女"/><br></restriction><br></simpleType><br><simpleType name="SIDType"><br><restriction base="string"><br><length value="3"/><br><pattern value="{3}"/><br></restriction><br></simpleType><br></schema></p>
<p>用XSD校验XML</p>
<p>由了XML Schema,你可以用来校验XML文档的语义和结构。在MSXML 4.0技术预览版本已经提供了用XSD Schema来校验XML文档的功能。在校验文档时,将schema添加到XMLSchemaCache对象中,设置其 object, set the schemas property of a DOMDocument对象的schemas属性引用XMLSchemaCache对象中的schema。在将XML文档载入到DOMDocument对象中时将自动执行校验操作。我们不妨用例子来说明如何在Visual Basic中通过编程实现XML文档校验。其中包括:</p>
<p>books.xsd<br>用来校验books.xml文件的Schema<br>books.xml<br>该文件将被载入并且和books.xsd对照校验<br>Visual Basic校验代码<br>创建一个XMLSchemaCache对象,将schema添加给它,然后设置schemas property of the DOMDocument对象的shemas属性。在开始的时候你要进行如下操作:<br>打开Visual Basic 6.0,选择Standard EXE新项目<br>在Project菜单中选择References.<br>在Available References列表中选择Microsoft XML,v4.0<br>给Form1添加一个Command button<br>存储该项目<br>books.xml</p>
<p>在XML编辑器甚至一般的文本编辑器中输入以下XML代码,并且存为books.xml:</p>
<p><?xml version="1.0" encoding= "GB18030"?><br><x:catalog xmlns:x="urn:books"><br><book id="bk101"><br><author>Gambardella, Matthew</author><br><title>XML Developer's Guide</title><br><genre>Computer</genre><br><price>44.95</price><br><publish_date>2000-10-01</< font>publish_date><br><description>An in-depth look at creating applications with XML.</description><br>< title>2000-10-01</< font>title><br></book><br></x:catalog></p>
<p>books.xsd</p>
<p>下面是本例中使用的books.xsd schema。</p>
<p><xsd:schema xmlns:xsd="<a class="contentlink" href="http://www.w3.org/2001/XMLSchema%22%3E" target="_blank">http://www.w3.org/2001/XMLSchema"></a><br><xsd:element name="catalog" type="CatalogData"/><br><xsd:complexType name="CatalogData"><br><xsd:sequence><br><xsd:element name="book" type="bookdata" minOccurs="0" maxOccurs="unbounded"/><br></xsd:sequence><br></xsd:complexType><br><xsd:complexType name="bookdata"><br><xsd:sequence><br><xsd:element name="author" type="xsd:string"/><br><xsd:element name="title" type="xsd:string"/><br><xsd:element name="genre" type="xsd:string"/><br><xsd:element name="price" type="xsd:float"/><br><xsd:element name="publish_date" type="xsd:date"/><br><xsd:element name="description" type="xsd:string"/><br></xsd:sequence><br><xsd:attribute name="id" type="xsd:string"/><br></xsd:complexType><br></xsd:schema></p>
<p>XML Schema和名域</p>
<p>Schema是一些规则的集合(也称为语法或者语汇),其中包括了类型定义(简单和复杂类型)以及元素和属性声明。由于XML中可能存在不同的语汇来描述不同的元素和属性,因此需要使用名域(namespace)和前缀来避免元素和属性声明之间的模糊性。当你使用来自多个名域的schema时,分清元素和属性名称是最基础性的工作。</p>
<p>一个名域通常有一串字符串来相互区别,如 "urn:www.microsoft.com", "<a class="contentlink" href="http://www.xml.org.cn%22/" target="_blank">http://www.xml.org.cn"</a>, "<a class="contentlink" href="http://www.w3c.org/2001/XMLSchema%22" target="_blank">http://www.w3c.org/2001/XMLSchema"</a>以及 "uuid:1234567890"等。</p>
<p>XML Schema的序言</p>
<p>XML schema的开头时是一些导言,之后才是正式的声明。在schema 元素的导言中可能包含三个可选的属性。</p>
<p>例如,下面的语法使用的schema元素引用了三个最常使用的名域:</p>
<p>xmlns="<a class="contentlink" href="http://www.w3c.org/2001/XMLSchema%22" target="_blank">http://www.w3c.org/2001/XMLSchema"</a> xmlns:xsd="<a class="contentlink" href="http://www.w3c.org/2001/XMLSchema-datatypes%22" target="_blank">http://www.w3c.org/2001/XMLSchema-datatypes"</a> xmlns:xsi="<a class="contentlink" href="http://www.w3c.org/2001/XMLSchema-instances%22" target="_blank">http://www.w3c.org/2001/XMLSchema-instances"</a><br>version"1.0"></p>
<p><br>前两个属性用XML名域来标识W3C中的两个XML schema规范。第一个 xmlns属性包含了基本的XML schema元素,比如element, attribute, complexType, group, simpleType等。第二个xmlns属性定义了标准的XML schema属性类型例如string, float, integer, 等。</p>
<p>缺省名域</p>
<p>对于任何一个XML Schema定义文档(XSD)都有一个最顶层的schema (XSD)元素。而且该schema (XSD)元素定义必须包含下面的名域:</p>
<p><a class="contentlink" href="http://www.w3.org/2001/XMLSchema" target="_blank">http://www.w3.org/2001/XMLSchema</a></p>
<p>作为名域的标识符(在声明中作为元素或属性的前缀),你也可以不使用xsd或xsi。</p>
<p>我们分别来观察XSD和XML实例文档中相关的名域。比如前面介绍student.xsd的序言是这样的:</p>
<p>< schema xmlns="<a class="contentlink" href="http://www.w3.org/2001/XMLSchema%22" target="_blank">http://www.w3.org/2001/XMLSchema"</a> xmlns:sl="<a class="contentlink" href="http://www.xml.org.cn/namespaces/StudentList%22" target="_blank">http://www.xml.org.cn/namespaces/StudentList"</a> targetNamespace="<a class="contentlink" href="http://www.xml.org.cn/namespaces/StudentList%22%3E" target="_blank">http://www.xml.org.cn/namespaces/StudentList"></a></p>
<p>这里的targetNamespace属性表示了该shema所对应的名域的URI。也就是说在引用该Schema的其它文档中要声明名域,其URI应该是targetNamespace的属性值。例如在这里因为要用到student.xsd自己定义的扩展数据类型,所以也声明的名域xmlns:sl="<a class="contentlink" href="http://www.xml.org.cn/namespaces/StudentList%22" target="_blank">http://www.xml.org.cn/namespaces/StudentList"</a>。</p>
<p>我们再来看由该schema规定的XML文档的开头将是什么样子:</p>
<p><studentlst xmlns="<a class="contentlink" href="http://www.xml.org.cn/namespaces/StudentList%22" target="_blank">http://www.xml.org.cn/namespaces/StudentList"</a><br>xmlns:xsi="<a class="contentlink" href="http://www.w3.org/2001/XMLSchema-instance%22" target="_blank">http://www.w3.org/2001/XMLSchema-instance"</a><br>xsi:schemaLocation="<a class="contentlink" href="http://www.xml.org.cn/namespaces/StudentList" target="_blank">http://www.xml.org.cn/namespaces/StudentList</a> student.xsd"></p>
<p>其中缺省名域声明xmlns="<a class="contentlink" href="http://www.xml.org.cn/namespaces/StudentList%22" target="_blank">http://www.xml.org.cn/namespaces/StudentList"</a>就是和刚刚声明的XML Schema的名域相结合来规定该XML文档。<br>xmlns:xsi="<a class="contentlink" href="http://www.w3.org/2001/XMLSchema-instance%22" target="_blank">http://www.w3.org/2001/XMLSchema-instance"</a> 是任何XML实例文档固有的名域,当然按照前面所说的名域名称xsi是可以自己规定的。而xsi:schemaLocation="<a class="contentlink" href="http://www.xml.org.cn/namespaces/StudentList" target="_blank">http://www.xml.org.cn/namespaces/StudentList</a> student.xsd"则规定了该名域所对象的schema的位置,即在相同路径的student.xsd文件。</p>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值