XML学习笔记(五)Schema语法之简单类型

Preface:本文是W3Schools上《Schema指南》的学习笔记。其中大部分内容是对指南的翻译总结。由于原文的例子更详尽生动,如果各位想阅读原文可以到这个网址http://www.w3schools.com/schema/default.asp。同时,W3Schools提供了测试,大家可以测试一下自己的理解程度。

        首先简单的说一下,Schema中的简单类型(SimpleType)有三种:SimpleElement(简单元素)、Attribute(属性)和Restrictions(约束)。下面逐一介绍这几种类型。
 

一、XSD SimpleElement

        所谓SimpleElement是指不包含任何其他元素和属性,只包含Text(元素间的内容)的元素。这里Text指代的不单单是文本(string),准确地说应该是数据,可以是Schema内置的数据类型的数据,也可以是我们自己创建的数据类型的数据。

定义一个SimpleElement

< xs:element  name ="xxx"  type ="yyy" >
在这里再次提醒语法,XML是大小写敏感的,属性的值要用双引号括起来。“xxx”是我们要定义的Element的名称。“yyy”是这个Element的数据类型。XML Schema预定义的内置数据类型有如下一些:
“xs:string ”、“xs:decimal”、“xs:integer”、“xs:boolean”、“xs:date”、“xs:time”。
看一个例子,以下是一些在XML中出现的SimpleElement
< lastname > Refsnes </ lastname >
< age > 36 </ age >
< dateborn > 1970-03-27 </ dateborn >  
那么在Schema中应该作如下定义,注意Element名称和数据类型的对应。
< xs:element  name ="lastname"  type ="xs:string" />
< xs:element  name ="age"  type ="xs:integer" />
< xs:element  name ="dateborn"  type ="xs:date" />  

Element的默认值和固定值

默认值是当Elment中没有指定一个值时默认提供的值,使用default属性给出。
< xs:element  name ="color"  type ="xs:string"  default ="red" />  
固定值同样是自动给出的,但是XML的用户不能再为Element指定值,使用fixed属性给出。
< xs:element  name ="color"  type ="xs:string"  fixed ="red" />

 

二、XSD Attribute

        所有的Attribute都是作为简单类型的。一个SimpleElement是不能有属性的。如果一个Element包含有Attriute我们就认为它看作是一个复杂类型(Complex Type)。虽然Attribute不会单独的出现,但我们仍将它作为一种SimpleType在这里介绍

定义一个Attribute

< xs:attribute  name ="xxx"  type ="yyy" />  
“xxx”是Attribute的名称。“yyy”是Attribute的数据类型。可以使用XML Schema定义的内置数据类型,如:“xs:string ”、“xs:decimal”、“xs:integer”、“xs:boolean”、“xs:date”、“xs:time”。
例如有如下一个Element,包含一个Attribute
< lastname  lang ="EN" > Smith </ lastname >
那么定义lang属性的Schema语句应该如下:
< xs:attribute  name ="lang"  type ="xs:string" />  

Attribute的默认值和固定值

同样可以使用default属性和fixed属性为Attrbute指定默认值和固定值。
默认值
< xs:attribute  name ="lang"  type ="xs:string"  default ="EN" />  
固定值
< xs:attribute  name ="lang"  type ="xs:string"  fixed ="EN" />  

可选和必需属性

属性默认是可选的(即可以不填)可以使用use属性来指定属性是必需的,如下:
< xs:attribute  name ="lang"  type ="xs:string"  use ="required" />

 

三、XSD Restrictions(约束)

        Restrictions是用来限制(或者说定义)Element或Attribute可接受值的。而对于Element的Restrictions通常又称为Facets。
下面通过一些常见的约束例子来说明Restrictions的用法和语法。

数值型范围限制

< xs:element  name ="age" >< xs:simpleType >
< xs:restriction  base ="xs:integer" >
< xs:minInclusive  value ="0" />
< xs:maxInclusive  value ="120" />
</ xs:restriction >
</ xs:simpleType ></ xs:element >  
该约束定义age元素的值是整形而且值要在0到120之间。

枚举限制

< xs:element  name ="car" >
< xs:simpleType >
< xs:restriction  base ="xs:string" >
< xs:enumeration  value ="Audi" />
< xs:enumeration  value ="Golf" />
< xs:enumeration  value ="BMW" />
</ xs:restriction >
</ xs:simpleType >
</ xs:element >  
这里约束了car的值是string,而且只能为“Audi”、“Golf”和“BMW”中的一个。
可以使用另一种写法:
< xs:element  name ="car"  type ="carType" />
< xs:simpleType  name ="carType" >
< xs:restriction  base ="xs:string" >
< xs:enumeration  value ="Audi" />
< xs:enumeration  value ="Golf" />
< xs:enumeration  value ="BMW" />
</ xs:restriction >
</ xs:simpleType >  
这种写法的好处是,上面的Restriction不是定义在Element中的,可以被其他的Element很方便的调用。

使用正则表达式(RegularExpression)约束

< xs:element  name ="letter" >
< xs:simpleType >
< xs:restriction  base ="xs:string" >
< xs:pattern  value ="[a-z]" />
</ xs:restriction >
</ xs:simpleType >
</ xs:element >
这里<xs:pattern>的value属性的值是一个正则表达式正则表达式的语法则不再本文介绍的范围。使用RegularExpression你可以规定任何格式的string约束。

空格字符(Whitespace Characters)约束

< xs:element  name ="address" >
< xs:simpleType >
< xs:restriction  base ="xs:string" >
< xs:whiteSpace  value ="preserve" />
</ xs:restriction >
</ xs:simpleType >
</ xs:element >  
以上例子对address中的所有空格字符进行保留。关键是value="preserve"。XML语法本来就是保留空格的。
当值为“replace”时
XML processer会用空间来代替所有的空格字符。
当值为“collapse”时
会将连续的空格合并成一个。

长度约束

< xs:element  name ="password" >
< xs:simpleType >
< xs:restriction  base ="xs:string" >
< xs:length  value ="8" />
</ xs:restriction >
</ xs:simpleType >
</ xs:element >  
以上例子限定了password元素的长度为8。当然也可以使用 <xs:minLength value="?"/>和<xs:maxLength value="?"/>来限定最长最短值。
 

更多的有关约束标签的参考,请查阅以下的网址:
http://www.w3schools.com/schema/schema_elements_ref.asp


本文为个人原创,转载请注明出自:http://jackma.cnblogs.com/ 
Author:JackMa

转载于:https://www.cnblogs.com/JackMa/archive/2008/02/23/1078629.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值