Schema之简单元素、复合元素和属性

的三要素:简单元素,复合元素和属性:

1、简单元素:只含有文本的XML元素。它不可以含有其他元素或属性;

   1.1、为复合元素添加简单元素的两种方式:直接添加方式如下:

 

1 <xs:element  name ="Book">
2     <xs:complexType >
3       <xs:sequence  >
4         <xs:element  name ="title"  type ="xs:string"></xs:element>
5       </xs:sequence>
6     </xs:complexType>
7   </xs:element>

或者引用的方式:

 

 

1 <xs:element  name ="Book">
2     <xs:complexType >
3       <xs:sequence  >
4         <xs:element  ref ="title" ></xs:element>
5       </xs:sequence>
6     </xs:complexType>
7   </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">
3     <xs:complexType >
4       <xs:attribute  ref ="lang" ></xs:attribute>
5     </xs:complexType>
6   </xs:element>

或者:

 

 

1 <xs:element  name ="Book">
2    <xs:complexType >
3      <xs:attribute  name ="lang"  type ="xs:string"></xs:attribute>
4    </xs:complexType>
5  </xs:element>

2.3、同样可以为属性添加默认值或者固定值;

 

 

1 <xs:attribute  name ="lang" type ="xs:string"  default ="xie"></xs:attribute>

2.4、任意属性和必须属性:必须属性就是说在写XML文档时,该属性必须含有,不是可有可无的;通过use 属性来指定:

 

 

1 <xs:element  name ="Book">
2     <xs:complexType >
3       <xs:attribute  name ="lang" type ="xs:string" use ="required"></xs:attribute>
4     </xs:complexType>
5   </xs:element>

3、复合元素:是含有其他元素和/或属性的XML元素

复合元素分为四种:空元素、只含有其他元素、只含有文本、含有其他元素和文本;

   3.1、空元素:

 
 
1<product pid="1345"/>
空元素的定义:
 
 
01方法一:
02<xs:element  name ="product">
03    <xs:complexType >
04      <xs:attribute  name ="proid"  type ="xs:positiveInteger"></xs:attribute>
05    </xs:complexType>
06  </xs:element>
07方法二:
08 <xs:element  name ="product"  type ="prodtype"/>
09  <xs:complexType  name ="prodtype">
10    <xs:attribute  name ="prodid"  type ="xs:positiveInteger"/>
11  </xs:complexType>
如果使用方法二,几个元素可以同时引用相同的复合类型;
3.2、只含有其他元素的复合元素:
 
 
1<person>
2<firstname>John</firstname>
3<lastname>Smith</lastname>
4</person>

schema:

01 方法一:
02 <xs:element  name ="person">
03     <xs:complexType  >
04       <xs:sequence  >
05         <xs:element  name ="firstname" type ="xs:string"/>
06         <xs:element name ="lastname"  type ="xs:string"/>
07       </xs:sequence>
08     </xs:complexType>
09   </xs:element>
10 方法二:
11 <xs:element name="person" type="persontype"/>
12 <xs:complexType name="persontype">
13   <xs:sequence>
14     <xs:element name="firstname" type="xs:string"/>
15  
16     <xs:element name="lastname" type="xs:string"/>
17   </xs:sequence>
18 </xs:complexType>
同样如果使用方法二,几个元素可以同时引用相同的复合类型;

    3.3、只含有简单文本或属性的复合元素:

 

1 <shoesize country="france">35</shoesize>

这种类型只含有简单内容(文本和属性),因此我们在内容周围添加一个simpleContent元素,当用到简单内容时,你必须在simpleContent元素里定义一个扩展或约束,就像这样:

01 <xs:element name="somename">
02  
03   <xs:complexType>
04     <xs:simpleContent>
05       <xs:extension base="basetype">
06         ....
07         ....
08       </xs:extension>    
09     </xs:simpleContent>
10   </xs:complexType>
11  
12 </xs:element>
13 OR
14 <xs:element name="somename">
15   <xs:complexType>
16     <xs:simpleContent>
17       <xs:restriction base="basetype">
18  
19         ....
20         ....
21       </xs:restriction>    
22     </xs:simpleContent>
23   </xs:complexType>
24 </xs:element>

tips:用extension/restriction元素扩展或限制元素的基本简单类型;

 

     3.4、混合内容的元素:既含有文本又含有其他元素;

 

1 <letter>
2 Dear Mr.<name>John Smith</name>.
3 Your order <orderid>1032</orderid>
4 will be shipped on <shipdate>2001-07-13</shipdate>
5 </letter>

schema:

 

 

01 <xs:element name="letter">
02   <xs:complexType mixed="true">
03     <xs:sequence>
04       <xs:element name="name" type="xs:string"/>
05  
06       <xs:element name="orderid" type="xs:positiveInteger"/>
07       <xs:element name="shipdate" type="xs:date"/>
08     </xs:sequence>
09  
10   </xs:complexType>
11 </xs:element>

上面为了使字符数据能出现在"letter"子元件之间,mixed属性必须设置为"true"。<xs:sequence>标签指出了已定义的元素(name, orderid 和shipdate)在"letter"元素里必须以指定的顺序出现;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值