一、注释元素
1、xsd:annotation
xsd:annotation包含两个子元素xsd:documention和xsd:appinfo,这两个元素是schema中唯一的混合元素类型,可以包含任何子元素和文本。
xsd:documention:提供给开发者人员的注释
xsd:appinfo:提供给计算机应用程序处理的注释
<xs:annotation>
<xs:documentation>
这里是注释!!!!
</xs:documentation>
</xs:annotation>
二、简单类型
1、什么是简单类型
不包含任何子元素和属性
2、内置简单数据类型
- 1、 内置的包含N多种
- 2、anyType相当于所有数据类型的基类,其他的数据类型要么单独存在,或者由其他数据类型派生
3、派生简单数据类型
3.1、限制
- 1、通过设置最大值或最小值,对数值类型限制
<xs:simpleType name="ageType">
<xs:restriction base="xs:integer">
<xs:minExclusive value="0"/>
<xs:maxExclusive value="100"/>
</xs:restriction>
</xs:simpleType>
- 2、通过枚举,将取值空间现在在一组合法的取值
<xs:simpleType name="cityType">
<xs:restriction base="xs:string">
<xs:enumeration value="hubei"/>
<xs:enumeration value="hunan"/>
</xs:restriction>
</xs:simpleType>
- 3、正则表达式,限制字符串的内容
<xs:simpleType name="phoneType">
<xs:restriction base="xs:string">
<xs:pattern value="1[358]\d{9}" />
</xs:restriction>
</xs:simpleType>
- 4、限制文本中字符串长度
<xs:simpleType name="cityType">
<xs:restriction base="xs:string">
<xs:length value="3"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="cityType">
<xs:restriction base="xs:string">
<xs:minLength value="3" />
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
- 5、限制数值位数和小数位数
<xs:simpleType name="ageType">
<xs:restriction base="xs:decimal">
<xs:totalDigits value="5"/>
<xs:fractionDigits value="3"/>
</xs:restriction>
</xs:simpleType>
PS:
totalDigits指最大位数,如果位数小于5也可
fractionDigits指最大小数位数,如果位数小于3也可
3.2、列表
1、若某个元素内容包含多个相同类型的值,则可使用列表。
- 比如成绩包含3个值,则在
<grade>99 98 95</grade>
中要使用列表
- 比如成绩包含3个值,则在
2、
<xs:simpleType name="ageType">
<xs:list itemType="xs:integer"/>
</xs:simpleType>
- 3、进一步限制List内元素
<xs:simpleType name="ageType">
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minExclusive value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>
3.2、合并(union)
1、和列表的区别是,希望在同一个元素内混有不同类型的数据,则可以用合并来实现
- 比如成绩又一门不存在,不存在的成绩以N/A表示,则
<grade>98 99 N/A</grade>
不能使用列表
- 比如成绩又一门不存在,不存在的成绩以N/A表示,则
2、在menberTypes属性中写上所要包含的元素类型,以空格分开
<xs:simpleType name="ageType">
<xs:union memberTypes="xf:unionType1 xf:unionType2"/>
</xs:simpleType>
<xs:simpleType name="unionType1">
<xs:restriction base="xs:integer"/>
</xs:simpleType>
<xs:simpleType name="unionType2">
<xs:restriction base="xs:string">
<xs:enumeration value="N/A"/>
</xs:restriction>
</xs:simpleType>
4、simpleType元素的属性
- 1、全部属性
<simpleType
final = (#all | List of (list | union | restriction))
id = ID
name = NCName>
Content: (annotation?, (restriction | list | union))
</simpleType>
- 2、final 属性的值表示该数据类型不允许进行的操作的列表,比如下面示例中,不允许使用列表对该类型进行派生。但是可以直接使用该类型。
<xsd:simpleType name=“type1” final=“list”>
<xsd:restriction base=“xsd:integer”>
<xsd:minInclusive value=“100”/>
</xsd:restriction>
</xsd:simtleType>
- 3、#all 表示不能对该类型进行任何操作。
三、复杂类型
1、什么是复杂类型
- 子元素和属性至少含一种,文本内容可以有,也可以没有。
2、派生复杂数据类型
2.1、空元素(只包含属性,不包含子元素和文本内容)
- 1、
<xsd:complexType name="ComplexType">
<xsd:attribute name="Att1Name" type="someSimpleType1"/>
<xsd:attribute name="Att2Name" type="someSimpleType2"/>
......
</xsd:complexType>
2.2、只包含子元素,不包含文本内容(可能包含属性)
1、子元素必须要放在容器当中,二不能单独出现在
<complexType>
标签内2、sequence容器:元素必须按照声明的顺序出现
<xs:complexType name="noteType">
<xs:sequence>
<xs:element name="age" type="xf:ageType"/>
<xs:element name="city" type="xf:cityType"/>
<xs:element name="phone" type="xf:phoneType"/>
</xs:sequence>
</xs:complexType>
- 3、all容器:包含的内容不分先后顺序
<xs:complexType name="noteType">
<xs:all>
<xs:element name="age" type="xf:ageType"/>
<xs:element name="city" type="xf:cityType"/>
<xs:element name="phone" type="xf:phoneType"/>
</xs:all>
</xs:complexType>
- 4、choice容器:从所包含的子元素中选择其一
<xs:complexType name="noteType">
<xs:choice>
<xs:element name="age" type="xf:ageType"/>
<xs:element name="city" type="xf:cityType"/>
<xs:element name="phone" type="xf:phoneType"/>
</xs:choice>
</xs:complexType>
2.3、只包含属性和文本内容,不包含子元素
1、引入simpleContent元素:用于表示“文本”、或“文本+属性”
2、xsd:extention(simpleContent中的extension)
- 可以对基础类型进行扩展
- 文本类型由 xsd:extention中的base属性指定
- xsd:extention元素中,只能定义与属性有关的内容(xsd:attribute,xsd:anyAttribute,xsd:attributeGroup)
- extension中定义的属性在拥有此元素类型的元素里不存在,即可以没有att属性
<xs:complexType name="ageType">
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="att" type="xs:integer"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
- 3、xsd:restriction
- 对文本内容进行限制
<xs:complexType name="ageType">
<xs:simpleContent>
<xs:restriction base="xf:ageType2">
<xs:minExclusive value="0"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="ageType2">
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="att"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
2.4、既包含子元素又包含文本(可能包含属性)
1、设置complexType的mixed属性为true
2、
schema文件(部分)
<xsd:element name="letter">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="orderid" type="xsd:positiveInteger"/>
<xsd:element name="shipdate" type="xsd:date"/>
</xsd:sequence>
<xsd:attribute name="letter_id" type="xsd:positiveInteger"/>
</xsd:complexType>
</xsd:element>
xml文件(部分)
<letter letter_id="123">
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2007-07-13</shipdate>.
</letter>
3、复杂数据类型的属性
- 1、全部属性
<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、abstract 属性表示该复杂类型是一个虚类型,只能用作其他类型的父类型。
3、block属性
- 如果不希望使用多态性,可以使用 complexType 的 block 属性(它的取值可能为 #all、extension、restriction)。
- extension 和 restriction,分别表示禁止使用任何通过扩展/限制而派生的子类型的实例来代替声明为父类的元素。
- block 和 final 属性是有区别的,因为即使 block=“#all”,仍然可以派生新的子类型,而 final =“#all” 则表示禁止派生。对于 block 来说,#all 表示禁止使用任何子类型的实例来代替声明为父类的元素。