1、复杂类型和简单类型之间最根本的区别就是:复杂类型的内容中可以包含其他元素,也可以带有属性(Attribute),但简单类型既不能包含子元素,也不能带有任何属性,但限制条件或扩展条件还是可以有的。
一个复杂类型例子:
<xsd:complexType name="CNAddress" >
<xsd:sequence>
<xsd:element name="name"type="xsd:string"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city"type="xsd:string"/>
<xsd:element name="zip"type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>
</xsd:complexType>
一个简单类型例子:
<xsd:simpleType name="myInteger">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="10000"/>
<xsd:maxInclusive value="99999"/>
</xsd:restriction>
</xsd:simpleType>
2、element存在约束:element可以通过其minOccurs和maxOccurs两个属性来约束元素实例存在的个数,这两个属性的缺省值都是1,表示默认情况下此元素在XML实例文档中必须出现一次。
例如:
<element name="responseDate" minOccurs="1" maxOccurs="1" type="timeInstant"/>
3、attribute存在约束:元素属性也可以通过attribute的use属性来约束出现一次或根本不出现;use属性的取值可以是required,optional,prohibited三个值,缺省(默认)值是optional.
4、 element和attribute都有一个default和fixed属性,不能同时存在。针对 element来说,只有当element实例为空时才采用此 default值,而attribute是当实例不提供此attribute时才采用此default值,因此对attribute而言,只有其use值是optional时default值才有意义,而且对element和attribute来说fixed和default两个属性不能同时存在,否则会出现错误。
fixed属性要求element或attribute如果出现的话,就一定要是fixed属性里指定的值。
5、全局元素和全局属性。直接定义在schema元素下,即schema元素的顶级子元素的element和attribute都是全局的,称之为全局元素和全局属性,你在其他类型定义中可以直接引用。
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:documentation xml:lang="en">
Purchase order schema for Example.com.
Copyright 2000 Example.com. All rights reserved.
</xsd:documentation>
</xsd:annotation>
<xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
<xsd:element name="comment" type="xsd:string"/>
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:element name="shipTo" type="USAddress"/>
<xsd:element name="billTo" type="USAddress"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items" type="Items"/>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
<xsd:complexType name="USAddress">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"
fixed="US"/>
</xsd:complexType>
<xsd:complexType name="Items">
<xsd:sequence>
<xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="productName" type="xsd:string"/>
<xsd:element name="quantity">
<xsd:simpleType>
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxExclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="USPrice" type="xsd:decimal"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="partNum" type="SKU" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<!-- Stock Keeping Unit, a code for identifying products -->
<xsd:simpleType name="SKU">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{3}-[A-Z]{2}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
从上面这个例子可以看出,purchaseOrder和comment是全局元素,可以被其他element用ref属性引用,purchaseOrderType,USAddress,Items和SKU是全局属性,可以被其他element用type属性引用。
6、派生新类型有两种方式:第一种就是直接从其他类型中扩展(继承)而来,另外一种就是通过对已有类型进行限定性约束而来。
如:以下有三种通过限定性约束定义的新类型:
通过值范围限定:
<xsd:simpleType name="myInteger">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="10000"/>
<xsd:maxInclusive value="99999"/>
</xsd:restriction>
</xsd:simpleType>
使用模式匹配限定:
<xsd:simpleType name="SKU">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{3}-[A-Z]{2}"/>
</xsd:restriction>
</xsd:simpleType>
使用枚举方式限定:
<xsd:simpleType name="CNCity">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="BeiJing"/>
<xsd:enumeration value="NanChang"/>
<xsd:enumeration value="ShangHai"/>
</xsd:restriction>
</xsd:simpleType>
7、原子类型(不可分割的类型,象string,integer等系统内建的类型)、列表类型、联合类型合起来统一称为简单类型。在Schema中有NMTOKENS、IDREFS、ENTITIES三种内建的列表类型,你也可以从已有的简单类型来创建list(列表)类型,但你不能从已有的list类型和复杂类型来创建列表(list)类型。
如:
<xsd:simpleType name="listOfMyIntType">
<xsd:list itemType="myInteger"/>
</xsd:simpleType>
在XML实例文档中列表类型的值是通过空格来进行分隔的,如果声明了一个listOfMyIntType元素,其值可能是:
<listOfMyInt>20003 15037 95977 95945</listOfMyInt>
8、约束List类型。有几个方面的元素可以应用于list类型来进行约束,它们是:length、minLength、maxLength和enumeration,如:
<xsd:simpleType name="USStateList">
<xsd:list itemType="USState"/>
</xsd:simpleType>
<xsd:simpleType name="SixUSStates">
<xsd:restriction base="USStateList">
<xsd:length value="6"/>
</xsd:restriction>
</xsd:simpleType>
注:针对列表类型要千万注意成员是string类型的,因为string类型中的空格和列表类型的分割符空格会造成部分混淆。
9、对元素的定义可以采用通过指定其type属性为已定义的属性的方式(基本类型或自定义的全局属性),也可一采用匿名定义类型的方式,如:
采用类型定义:
<xsd:element name=”comment” type=”xsd:string”>
采用匿名定义:
<xsd:element name=”quantity”>
<xsd:simpleType>
<xsd:restriction base=”xsd:positiveInteger”>
<xsd:maxExclusive value=”100” />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
10、union(联合)类型表示在XML实例文档中的元素实例符合union类型定义的成员类型中的一种就可以了(合法),这一点和C++中的联合类型有类似的概念,如:
<xsd:simpleType name="addrUnion">
<xsd:union memberTypes="xsd:string integer"/>
</xsd:simpleType>
11、复杂类型一般可以分为三类:
第一类是包含字符内容和属性但不包含子元素,针对第一类可以通过simpleContent来实现;
第二类是包含属性和子元素但不包含字符数据(字符数据包含在子元素中),通过complexContent来做到;
第三类是即包含属性和字符内容又包含子元素的,只需要将complexType的属性mixed设为true就可以了;
具体的例子如下:
第一种类型(从一个简单类型扩展而来,增加了属性):
<xsd:element name="internationalPrice">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="currency" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
第二种类型(有一个element和两个attribute构成):
<xsd:element name="internationalPrice">
<xsd:complexType>
<xsd:complexContent>
<xsd:element name=”Country” ?type=”xsd:string” />
<xsd:attribute name="currency" type="xsd:string"/>
<xsd:attribute name="value"?type="xsd:decimal"/>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
注意:在这里由于默认情况下缺省是complexContent,所以在这里简略的写法是:
<xsd:element name="internationalPrice">
<xsd:complexType>
<xsd:element name=”Country”type=”xsd:string” />
<xsd:attribute name="currency" type="xsd:string"/>
<xsd:attribute name="value"?type="xsd:decimal"/>
</xsd:complexType>
</xsd:element>
第三种类型:
<xsd:element name="letterBody">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="salutation">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="quantity"type="xsd:positiveInteger"/>
<xsd:element name="productName" type="xsd:string"/>
<xsd:element name="shipDate"type="xsd:date" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
第三种类型的实例可能如下:
<letterBody>
<salutation>Dear Mr.
<name>Robert Smith</name>.
</salutation>
Your order of
<quantity>1</quantity>
<productName>Baby Monitor</productName>
shipped from our warehouse on
<shipDate>1999-05-21</shipDate>
</letterBody>
12、根据11的描述那么要定义一个空内容的元素,也就是说定义一个只包含属性的元素,只要在complexContent中不包含任何子元素,就可以了,如(注:complexContent在complexType外面被省略了):
<xsd:element name="internationalPrice">
<xsd:complexType>
<xsd:attribute name="currency" type="xsd:string"/>
<xsd:attribute name="value"type="xsd:decimal"/>
</xsd:complexType>
</xsd:element>
13、anyType是所有Schema类型的基类型,和Java中的Object类似。因此,以下定义:
<xsd:element name="anything" type="xsd:anyType"/>
可以写成:
<xsd:element name="anything"/>
没有评论:
发表评论