1.   Schema机制: 
a)        Simple Type和Complex Type 
由于XML文档被组织成树状结构,因此,我们可以按照节点所处的位置把他们区分为叶子节点和内部节点。Simple Type是Schema引入以表示叶子节点类型的,而Complex Type是用来表示内部节点类型的。因此,对于Simple Type我们关心的是她所允许的值,也就是在Datatypes中所说的Lexical Space,Value Space以及facet。而对于Complex Type,她反映的是文档的结构,所以,对于Complex Type我们关心的是她怎么定义节点之间的关系。 
  
b)        Content Models 
| 
 Content model  | 
 Mixed  | 
 Complex  | 
 Simple  | 
 Empty  |  
| 
 Child elements  | 
 Yes  | 
 Yes  | 
 No  | 
 No  |  
| 
 Child text  | 
 Yes  | 
 No  | 
 Yes  | 
 No  |    
注:由于Complex Type用于定义内部节点,Content Model用于表示该节点所包含子节点的类型。 
Empty Content Complex Type:表示节点不包含任何文本和子节点的Complex Type类型。 
Simple Content Complex Type:表示节点只包含文本的Complex Type类型。 
Complex Content Complex Type:表示节点只包含子节点的Complex Type类型。 
Mixed Content Complex Type:表示节点的子节点当中既包含文本又能包含子节点的Complex Type类型。 
  
c)        定义Complex Type的语法: 
<complexType 
abstract = boolean : false 
block = (#all | List of (extension | restriction)) 
final = (#all | List of (extension | restriction)) 
id = ID 
mixed = boolean : false 
name = NCName 
>  
Content: (annotation?, (simpleContent | complexContent | ((group | all | choice |  
sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))) 
</complexType> 
  
2.   Simple Content Complex Type 
a)        说明:我们不能直接定义Simple Content Complex Type类型,而必须通过对已有的Simple Type或者Simple Content进行扩展或约束得到。 
  
b)        Simple Content的扩展(extension) 
                      i.              说明:对基类型进行扩展,定义额外的属性。 
                   ii.              语法: 
<extension 
base = QName 
id = ID 
>  
Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?)) 
</extension> 
                iii.              例子: 
Schema: 
<xs:element name="title"> 
<xs:complexType> 
<xs:simpleContent> 
<xs:extension base="xs:string"> 
<xs:attribute name="note" type="xs:token"/> 
</xs:extension> 
</xs:simpleContent> 
</xs:complexType> 
</xs:element> 
  
XML: 
<title note="…">…</title> 
  
c)        Simple Content的约束(restriction) 
                      i.              说明:可以对基类型中,已定义的内容和属性进行约束。通过修改属性的use属性,可以禁止该属性的出现。约束后的类型是基类型的子集。 
                   ii.              语法: 
<restriction 
base = QName 
id = ID 
>  
Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive  
| maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength |  
enumeration | whiteSpace | pattern)*)?, ((attribute | attributeGroup)*,  
anyAttribute?)) 
                iii.              例子: 
Schema: 
<xs:element name="title"> 
<xs:complexType> 
<xs:simpleContent> 
<xs:restriction base="xs:string"> 
<xs:maxLength value="5"/> 
</xs:restriction> 
</xs:simpleContent> 
</xs:complexType> 
</xs:element> 
  
XML: 
<title>Hello</title> 
d)        两种方法的比较: 
扩展的方式只能增加新的属性,而不能对基类型当中的文本和属性进行任何的约束和修改。相比之下,约束的方式显得更加灵活,她能够增加对基类型中的文本和属性的约束。 
  
3.   Complex Content Complex Type 
a)        说明:我们能够通过Compositor和Partical的方式,直接定义Complex Content Complex Type类型,也能够通过扩展和约束的方式,对已有的Complex Content Complex Type类型进行扩展。 
  
b)        通过Compositor和Partical定义Complex Content Complex Type 
Compositor和Partical用于定义子节点及其之间的关系。其中Compositor用于说明子节点之间的出现顺序,Partical用于说明都包含了哪些子节点。Schema定义的Compositor包括:sequence,choice,all;Partical包括:element,sequence,choice,any和group。 
                      i.              sequence 
1.        说明:sequence中的Partical必须按顺序出现。 
2.        语法: 
<sequence 
id = ID 
maxOccurs = (nonNegativeInteger | unbounded)  : 1 
minOccurs = nonNegativeInteger : 1 
>  
Content: (annotation?, (element | group | choice | sequence | any)*) 
</sequence> 
3.        例子: 
Schema: 
<xs:element name="author"> 
<xs:complexType> 
<xs:sequence> 
<xs:element name="name" type="xs:string"/> 
<xs:element name="born" type="xs:string"/> 
</xs:sequence> 
<xs:attribute name="id" type="xs:string"/> 
</xs:complexType> 
</xs:element> 
  
XML: 
<author id="…"> 
          <name>…</name> 
          <born>…</born> 
</author> 
  
                   ii.              choice 
1.        说明:choice中的Partical只能出现其中的一个。 
2.        语法: 
<choice 
id = ID 
maxOccurs = (nonNegativeInteger | unbounded)  : 1 
minOccurs = nonNegativeInteger : 1 
>  
Content: (annotation?, (element | group | choice | sequence | any)*) 
</choice> 
3.        例子: 
Schema: 
<xs:element name="author"> 
<xs:complexType> 
<xs:choice> 
<xs:element name="name" type="xs:string"/> 
<xs:element name="born" type="xs:string"/> 
</xs:choice> 
<xs:attribute name="id" type="xs:string"/> 
</xs:complexType> 
</xs:element> 
  
XML: 
<author id="…"> 
          <name>…</name> 
</author> 
  
                iii.              all 
1.        说明:all中的Partical可以按任何顺序组合出现,但all中的Partical的maxOccurs属性只能小于或等于1。 
2.        语法: 
<all 
id = ID 
maxOccurs = 1 : 1 
minOccurs = (0 | 1) : 1 
>  
Content: (annotation?, element*) 
</all> 
3.        例子: 
Schema: 
<xs:element name="author"> 
<xs:complexType> 
<xs:all> 
<xs:element name="name" maxOccurs="1" type="xs:string"/> 
<xs:element name="born" maxOccurs="1" type="xs:string"/> 
</xs:all> 
<xs:attribute name="id" type="xs:string"/> 
</xs:complexType> 
</xs:element> 
  
XML: 
<author id="…"> 
          <born>…</born> 
     <name>…</name> 
</author> 
  
c)        Complex Content的扩展(extention) 
                      i.              说明:对基类型进行扩展,定义额外的元素和属性。 
                   ii.              语法: 
<extension 
base = QName 
id = ID 
>  
Content: (annotation?, ((group | all | choice | sequence)?, ((attribute |  
attributeGroup)*, anyAttribute?))) 
</extension> 
                iii.              例子: 
Schema: 
父类型: 
<xs:complexType name="basePerson"> 
<xs:choice> 
<xs:element name="author" type="xs:string"/> 
<xs:element name="character" type="xs:string"/> 
</xs:choice> 
</xs:complexType> 
子类型: 
<xs:element name="person"> 
<xs:complexType> 
<xs:complexContent> 
<xs:extension base="basePerson"> 
<xs:sequence> 
<xs:element name="editor" type="xs:string"/> 
</xs:sequence> 
</xs:extension> 
</xs:complexContent> 
</xs:complexType> 
</xs:element> 
  
XML: 
<person> 
<author>…</author> 
<editor>…</editor> 
</person> 
  
注:子类型的定义等价于: 
<xs:complexType name="person"> 
<xs:complexContent> 
     <xs:sequence> 
<xs:choice> 
<xs:element name="author" type="xs:string"/> 
<xs:element name="character" type="xs:string"/> 
</xs:choice> 
<xs:sequence> 
<xs:element name="editor" type="xs:string"/> 
</xs:sequence> 
</xs:sequence> 
</xs:complexContent> 
</xs:complexType> 
  
d)        Complex Content的约束(restriction) 
                      i.              说明:可以对基类型中,已定义的内容和属性进行约束。通过修改元素的maxOccurs属性,可以限制元素的出现,通过修改属性的use属性,可以限制属性的出现。约束后的类型是基类型的子集。 
                   ii.              语法: 
<restriction 
base = QName 
id = ID 
>  
Content: (annotation?, (group | all | choice | sequence)?, ((attribute |  
attributeGroup)*, anyAttribute?)) 
</restriction> 
                iii.              例子: 
Schema: 
父类型: 
<xs:complexType name="person"> 
<xs:sequence> 
<xs:element name="name" type="xs:string"/> 
<xs:element name="born" type="xs:string"/> 
</xs:sequence> 
<xs:attribute name="id" type="xs:string"/> 
</xs:complexType> 
  
子类型: 
<xs:element name="author"> 
<xs:complexType> 
<xs:complexContent> 
<xs:restriction base="person"> 
<xs:sequence> 
<xs:element name="name" type="xs:string"/> 
</xs:sequence> 
</xs:restriction> 
</xs:complexContent> 
</xs:complexType> 
</xs:element> 
  
XML: 
<author id="…"> 
<name>…</name> 
</author> 
  
注:通过约束得到新类型时,对于元素部分,我们需要重新书写我们需要的Content内容; 
而对于属性部分,我们只需要书写我们希望改变的部分,如果不需要改变,则可以不书写。 
  
e)        两种方法的比较 
与扩展Simple Content的两种方法相似,extension方法只能在父类型的结尾部分增加新的节点或者属性,而不能对父节点中的节点或属性的类型进行修改。而restriction则可以通过重新书写Content和Attribute的方式,重新定义子类型的各部分内容。另外,这两种方法是非对称的,即通过一次extension和restriction不能得回到原来的类型。对于使用扩展方法,符合扩展类的XML文件内容,不一定符合基类。而对于约束方法,符合扩展类的XML内容,也必须符合基类。 
  
4.   Mixed Content Complex Type 
a)        说明:我们能够通过complexType声明中的mixed属性,定义Mixed Content Complex Type类型。也能够通过扩展和约束的方式,对Mixed Content Complex Type进行扩展。 
  
b)        通过mixed属性定义Mixed Content Complex Type 
                      i.              例子: 
Schema: 
<xs:element name="title"> 
<xs:complexType mixed="true"> 
<xs:choice> 
<xs:element name="em" type="xs:token"/> 
<xs:element name="a" type="xs:string"/> 
</xs:choice> 
<xs:attribute name="lang" type="xs:string"/> 
</xs:complexType> 
</xs:element> 
           
XML: 
<title lang="…"> 
     … 
     <em> 
     … 
</title> 
        
c)        Mixed Content的扩展(extention) 
                      i.              例子: 
Schema: 
<xs:element name="title"> 
<xs:complexType mixed="true"> 
<xs:complexContent mixed="true"> 
<xs:extension base="…"> 
<xs:choice> 
<xs:element name="strong" type="xs:string"/> 
</xs:choice> 
</xs:extension> 
</xs:complexContent> 
</xs:complexType> 
</xs:element> 
  
d)        Mixed Content的约束(restriction) 
                      i.              例子: 
Schema: 
<xs:element name="title"> 
<xs:complexType mixed="true"> 
<xs:complexContent mixed="true"> 
<xs:restriction base="…"> 
<xs:choice> 
<xs:element name="a" type="xs:string"/> 
</xs:choice> 
<xs:attribute name="lang"/> 
</xs:restriction> 
</xs:complexContent> 
</xs:complexType> 
</xs:element> 
  
e)        Complex Content Complex Type和Mixed Content Complex Type之间的相互扩展 
Complex -> Mixed(E)    forbidden 
Mixed -> Complex(E)    forbidden 
Complex -> Mixed(R)    forbidden 
Mixed -> Complex(R)    ok 
  
5.   Empty Content Complex Type 
a)        说明:我们可以通过两种方式来定义Empty Content,一种是通过禁止Simple Content Complex Type中的文本内容出现,另一种是通过禁止Complex Content Complex Type中的元素出现。 
  
b)        通过Simple Content定义Empty Content 
                      i.              例子: 
Schema: 
<xs:element name="br"> 
<xs:complexType> 
<xs:simpleContent> 
<xs:extension base="xs:string"> 
<xs:enumeration value=""/> 
</xs:extension> 
</xs:simpleContent> 
</xs:complexType> 
</xs:element> 
  
c)        通过Complex Content定义Empty Content 
                      i.              例子: 
Schema: 
<xs:element name="br"> 
<xs:complexType/> 
</xs:element> 
  
d)        对基于Simple Content的Empty Content进行扩展和约束 
由于是基于Simple Content的,所以在扩展时,我们可以增加额外的属性,而在约束时可以约束文本的内容。 
  
e)        对基于Complex Content的Empty Content进行扩展和约束 
由于是基于Complex Content的,所以在扩展时,我们可以增加额外的属性和元素,而在 
约束时可以约束属性和元素的内容。 
  
6.   XML文档结构抽象:
  
 
  注:XML文档的结构从抽象来说,就是E(元素节点),A(属性节点),T(文本节点)之间的一个组合关系。而根据这些节点的特性要求,他们的关系只能表现为以下几种:属性定义(A);空元素节点(E());元素节点包含属性节点(E(A));元素节点包含文本节点(E(T));元素节点包含元素节点(E(E));元素节点包含元素节点和文本节点(E(A,T));元素节点包含元素节点和属性节点(E(E,A));元素节点包含元素节点和文本节点(E(E,T));元素节点包含元素节点,属性节点和文本节点(E(E,A,T))。而DTD和Schema的作用就是定义这9中组合关系的规则。 
  
7.   属性定义(A) 
a)        语法: 
<attribute 
default = string 
fixed = string 
form = (qualified | unqualified) 
id = ID 
name = NCName 
ref = QName 
type = QName 
use = (optional | prohibited | required) : optional 
>  
Content: (annotation?, simpleType?) 
</attribute> 
  
b)        例子: 
<attribute name="test" type="xs:string"/> 
  
8.   空元素节点(E()) 
a)        语法:使用Empty Content Complex Type 
b)        例子: 
<xs:element name="test"> 
<xs:complexType/> 
</xs:element> 
  
9.   元素节点包含属性节点(E(A)) 
a)        语法:使用Empty Content Complex Type 
b)        例子: 
<xs:element name="test"> 
<xs:complexType> 
     <attribute name="…" type="…"/> 
              </xs:complexType> 
</xs:element> 
  
10.元素节点包含文本节点(E(T)) 
a)        语法:使用Simple Content Complex Type 
b)        例子: 
<xs:element name="title"> 
<xs:complexType> 
<xs:simpleContent> 
<xs:extension base="xs:string"/> 
</xs:simpleContent> 
</xs:complexType> 
</xs:element> 
  
11.元素节点包含元素节点(E(E)) 
a)        语法:使用Complex Content Complex Type 
b)        例子: 
<xs:complexType name="person"> 
<xs:complexContent> 
<xs:sequence> 
<xs:choice> 
<xs:element name="author" type="xs:string"/> 
<xs:element name="character" type="xs:string"/> 
</xs:choice> 
</xs:sequence> 
</xs:complexContent> 
         </xs:complexType> 
  
12.元素节点包含元素节点和文本节点(E(A,T)) 
a)        语法:使用Simple Content Complex Type 
b)        例子: 
<xs:element name="title"> 
<xs:complexType> 
<xs:simpleContent> 
<xs:extension base="xs:string"> 
<xs:attribute name="note" type="xs:token"/> 
</xs:extension> 
</xs:simpleContent> 
</xs:complexType> 
</xs:element> 
  
13.元素节点包含元素节点和属性节点(E(E,A)) 
a)        语法:使用Complex Content Complex Type 
b)        例子: 
<xs:element name="author"> 
<xs:complexType> 
<xs:sequence> 
<xs:element name="name" type="xs:string"/> 
<xs:element name="born" type="xs:string"/> 
</xs:sequence> 
<xs:attribute name="id" type="xs:string"/> 
</xs:complexType> 
</xs:element> 
  
14.元素节点包含元素节点和文本节点(E(E,T)) 
a)        语法:使用Mixed Content Complex Type 
b)        例子: 
<xs:element name="title"> 
<xs:complexType mixed="true"> 
<xs:choice> 
<xs:element name="em" type="xs:token"/> 
<xs:element name="a" type="xs:string"/> 
</xs:choice> 
</xs:complexType> 
</xs:element> 
  
15.元素节点包含元素节点,属性节点和文本节点(E(E,A,T)) 
a)        语法:使用Mixed Content Complex Type 
b)        例子: 
<xs:element name="title"> 
<xs:complexType mixed="true"> 
<xs:choice> 
<xs:element name="em" type="xs:token"/> 
<xs:element name="a" type="xs:string"/> 
</xs:choice> 
<xs:attribute name="lang" type="xs:string"/> 
</xs:complexType> 
</xs:element>  
 
  |