的三要素:简单元素,复合元素和属性:
1、简单元素:只含有文本的XML元素。它不可以含有其他元素或属性;
1.1、为复合元素添加简单元素的两种方式:直接添加方式如下:
1 | < xs:element name = "Book" > |
4 | < xs:element name = "title" type = "xs:string" ></ xs:element > |
或者引用的方式:
1 | < xs:element name = "Book" > |
4 | < xs:element ref = "title" ></ xs:element > |
1.2、为简单元素设置默认值和固定值:
1 | < xs:element name = "price" type = "xs:double" default = "22.22" ></ xs:element > |
2 | < xs:element name = "press" type = "xs:string" fixed = "清华大学出版社" ></ xs:element > |
2、属性:简单的元素没有属性,当一个元素包含属性时,则成为复合元素,
2.1、属性的定义非常类似于简单元素:
1 | < xs:attribute name = "lang" type = "xs:string" ></ xs:attribute > |
2.2、为复合元素添加属性的方式和添加简单元素的方式类似:
1 | < xs:attribute name = "lang" type = "xs:string" ></ xs:attribute > |
2 | < xs:element name = "Book" > |
4 | < xs:attribute ref = "lang" ></ xs:attribute > |
或者:
1 | < xs:element name = "Book" > |
3 | < xs:attribute name = "lang" type = "xs:string" ></ xs:attribute > |
2.3、同样可以为属性添加默认值或者固定值;
1 | < xs:attribute name = "lang" type = "xs:string" default = "xie" ></ xs:attribute > |
2.4、任意属性和必须属性:必须属性就是说在写XML文档时,该属性必须含有,不是可有可无的;通过use 属性来指定:
1 | < xs:element name = "Book" > |
3 | < xs:attribute name = "lang" type = "xs:string" use = "required" ></ xs:attribute > |
3、复合元素:是含有其他元素和/或属性的XML元素
复合元素分为四种:空元素、只含有其他元素、只含有文本、含有其他元素和文本;
3.1、空元素:
空元素的定义:
02 | < xs:element name = "product" > |
04 | < xs:attribute name = "proid" type = "xs:positiveInteger" ></ xs:attribute > |
08 | < xs:element name = "product" type = "prodtype" /> |
09 | < xs:complexType name = "prodtype" > |
10 | < xs:attribute name = "prodid" type = "xs:positiveInteger" /> |
如果使用方法二,几个元素可以同时引用相同的复合类型;
3.2、只含有其他元素的复合元素:
2 | < firstname >John</ firstname > |
3 | < lastname >Smith</ lastname > |
schema:
02 | < xs:element name = "person" > |
05 | < xs:element name = "firstname" type = "xs:string" /> |
06 | < xs:element name = "lastname" type = "xs:string" /> |
11 | < xs:element name = "person" type = "persontype" /> |
12 | < xs:complexType name = "persontype" > |
14 | < xs:element name = "firstname" type = "xs:string" /> |
16 | < xs:element name = "lastname" type = "xs:string" /> |
同样如果使用方法二,几个元素可以同时引用相同的复合类型;
3.3、只含有简单文本或属性的复合元素:
1 | < shoesize country = "france" >35</ shoesize > |
这种类型只含有简单内容(文本和属性),因此我们在内容周围添加一个simpleContent元素,当用到简单内容时,你必须在simpleContent元素里定义一个扩展或约束,就像这样:
01 | < xs:element name = "somename" > |
05 | < xs:extension base = "basetype" > |
14 | < xs:element name = "somename" > |
17 | < xs:restriction base = "basetype" > |
tips:用extension/restriction元素扩展或限制元素的基本简单类型;
3.4、混合内容的元素:既含有文本又含有其他元素;
2 | Dear Mr.< name >John Smith</ name >. |
3 | Your order < orderid >1032</ orderid > |
4 | will be shipped on < shipdate >2001-07-13</ shipdate > |
schema:
01 | < xs:element name = "letter" > |
02 | < xs:complexType mixed = "true" > |
04 | < xs:element name = "name" type = "xs:string" /> |
06 | < xs:element name = "orderid" type = "xs:positiveInteger" /> |
07 | < xs:element name = "shipdate" type = "xs:date" /> |
上面为了使字符数据能出现在"letter"子元件之间,mixed属性必须设置为"true"。<xs:sequence>标签指出了已定义的元素(name, orderid 和shipdate)在"letter"元素里必须以指定的顺序出现;