Schema(important!)
1.XML Schema是用一种套预选规定好的XML元素和属性创建的,这些元素和属性定义了XML文档的结构和内容模式。
2.XML Schema是规定XML文档实例的结构和每个元素/属性的数据类型。
SQL-------------->Xpath
数据库----------->XML文档
表结构----------->Schema
例如:
XML:
<书本>
<书名>Java编程思想(第4版)</书名>
<作者>(美)埃克尔 著 陈昊鹏 译</作者>
</书本>
DTD
<!ELEMENT 书本(书名,作者)>
<!ELEMENT 书名(#PCDATA)>
<!ELEMENT 作者(#PCDATA)>
Schema
<element name="书本" type="书本类型" />
<complexType name="书本类型">
<element name="名称" type="string" />
<element name="作者" type="string" />
</complexType>
3.为什么要使用Schema
1>.DTD的局限性
1.DTD遵守XML语法(写XML文档实例时候用一种语法,写DTD时候需要另一种语法)
2.DTD数据类型有限(与数据库类型不一致)
3.DTD不可扩展
4.DTD不支持命名空间(命名冲突)
2>.Schema的新特性
1.Schema基于XML语法
2.Schema可以用能处理XML文档的工具处理
3.Schema大大扩充了数据类型,可以自定义数据类型
4.Schema支持继承Object-Oriented'
5.Schema支持属性组
3>.Schema文档结构
<?xml version="1.0">
<xs:schema xmlns:xs="http://www.w3c.org/2001/XMLSchema" targetNamespace="http://mynamespace/myschema">
<!-- 放入实际的内容 -->
</xsschema>
1.所有的Schema文档使用schema作为其根元素,Schema本身是XML,保存的后缀名为.xsd文件。
2.xs后面的URL是固定的,用于构造schema的元素和数据类型来自http://www.w3c.org/2001/XMLSchema命令空间
3.targetNamespace:本schema定义的元素和数据类型属于targetNamespace="http://mynamespace/myschema"命令空间
4>.Schema的数据类型
1.简单的类型 - 内置的数据类型(built-in data types)
1.基本的数据库类型
1>.Schema的数据类型-基本数据类型
数据类型 描述
string 表示字符串
boolean 布尔型
decimal 代表特定精度的数字
float 表示单精度32位浮点型
double 表示双精度64位浮点型
duration 表示持续时间
dateTime 代表特定时间
time 代表特定时间,但是每天重复的
date 代表日期
hexBinary 代表十六进制数
nayURI 代表一个URI,用来定位文件
NOTATION 代表NOTATION类型
2>.Schema的数据类型-扩展的数据类型
数据类型 描述
ID 用于唯一标识元素
IDERF 参考ID类型的元素或者属性
ENTITY 参考类型
NMTOKEN NMTOKEN类型
NMTOKENS NMTOKEN类型集
long 表示正整数,大小介于-9223372036854775808和9223372036854775807之间
int 表示正整数,大小介于-2147483648和2147483647之间
short 表示正整数,大小介于-32768和32767之间
byte 表示正整数,大小介于-128和127之间
3>.Schema的数据类型-数据类型特性
数据类型 描述
enumberation 在指定的数据集中选择,限定用户的选值
fractionDigits 限定最大的小数位,用于控制精度
length 指定数据的长度
maxExclusive 指定数据的最大值(小于)
maxInclusive 指定数据的最大值(小于等于)
maxLength 指定长度的最大值
minExclusive 指定最小值(大于)
minInclusive 指定最小值(大于等于)
minLength 指定最小长度
Pattern 指定数据的显示规范
4>. Schema元素类型:
类型 说明 特点
schema 根元素 choice
element 元素 list
attribute 属性 union
group 组 unique
attributeGroup 属性组 sequence
simpleType 简单类型 restriction
simpleContent 简单内容
complexType 复杂类型
1.schema元素:
作用:包含已经定义的schema
用法:<xs:schema />
属性:xmlns targetNamespace
2.element元素:
作用:声明一个元素
属性: 实例:
name <xs:element name="cat" type="xs:string" />
type <xs:element name="dog" type="xs:string" />
ref <xs:complexType name="myType">
minOccurs <xs:sequence minOccurs="2" maxOccurs="nubounded">
maxOccurs <xs:element ref="cat"/>
substitutionGroup <xs:element ref="dog"/>
fixed <xs:sequence/>
default <xs:complexType/ >
<xs:element name="pets" type="myType" />
3.group元素:
作用:把一组元素声明组合在一起,以便它们能够一起被复合类型应用
属性:name/ref
示例:
<xs:element name="thing1" type="xs:string" />
<xs:element name="thing2" type="xs:string" />
<xs:attribute name="myAttribute" type="xs:decimal" />
<xs:group name="myGroupOfThings">
<xs:sequence>
<xs:element ref="thing1" />
<xs:element ref="thing2 />
</xs:sequence>
</xs:group>
<xs:complexType name="myComplexType">
<xs:group ref="myGroupOfThings" />
<xs:attribute ref="myAttribute" />
</xs:complexType>
4.attribute元素:
作用:声明一个属性
属性:
name
type
ref
use 作用方式:required 代表name是必填的
示例:
<xs:complexType name="myComplexType">
<xs:attribute name="mybaseattribute" type="xs:string" use="required" />
</xs:complexType>
5.attributeGroup元素
作用:把一个属性声明组合在一起,以便可以被复合类型所使用
属性:name/ref
示例:
<xs:attributeGroup name="myAttributeGroup">
<xs:attribute name="someattribute1" type="xs:integer" />
<xs:attribute name="someattribute2" type="xs:string" />
</xs:attributeGroup>
<xs:complexType name="myElementType">
<xs:attributeGroup ref="myAttributeGroup" />
</xs:complexType>
6.simpleType元素
作用:定义一个简单类型,它决定了元素和属性值的约束和相关信息
属性:name
内容:应用已经存在的简单类型,三种方式:
restrice 限定一个范围
list 从列表中选择
union 包含一个值的结合
1.子元素为:<xs:restrice> 定义一个约束条件
<xs:simpleType name="freezeboilrangelnteger">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0" />
<xs:maxInclusive value="100" />
</xs:restriction>
<xs:simpleType>
2.子元素为:<xs:list> 从一个特定的数据类型的集合中选择定义一个简单类型元素
<xs:simpleType name="listOfDates">
<xs:list itemType="xs:date" />
</xs:simpleType>
3.子元素为:<xs:union> 从一个特定简单数据类型的集合中选择定义一个简单类型元素
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:attribute name="allframesize">
<xs:simpleType>
<xs:union memberTypes="roadbikesize mountainbikesize"/>
</xs:simpleType>
</xs:attribute>
<xs:simpleType name="roadbikesize">
<xs:restriction base="xs:positiveInteger">
<xs:enumeration value="46"/>
<xs:enumeration value="52"/>
<xs:enumeration value="55"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="mountainbikesize">
<xs:restriction base="xs:string">
<xs:enumeration value="small"/>
<xs:enumeration value="medium"/>
<xs:enumeration value="large"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="abcd">
<xs:complexType>
<xs:sequence>
<xs:element name="hhh" type="xs:string"/>
</xs:sequence>
<xs:attribute ref="allframesize" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
7.complexType元素
作用:定义一个复合类型,它决定了一组元素和属性
属性:name
示例:
<xs:complexType name="internationalShoeSize">
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="sizing" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="myShoeSize" type="internationalShoeSize"/>
complexType与simpleType区别(重要):
1.simpleType类型的元素中不能包含元素或者属性。
2.当需要声明一个元素的子元素和/或属性时,用complexType;
3.当需要基于内置的基本数据类型定义一个新的数据类型时,用simpleType。
8.simpleContent元素
作用:应用于complextype,对它的内容进行约束和扩展。
示例:
<xs:element name="shoeSize">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="sizing" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="US"/>
<xs:enumeration value="European"/>
<xs:enumeration value="UK"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
生成XML:
<shoeSize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test8.xsd" sizing="UK">1.2</shoeSize>
9.choice元素
作用:允许唯一的元素从一个组中选择
属性:minOccurs/maxOccurs
示例:
<xs:complexType name="chadState">
<xs:choice minOccurs="1" maxOccurs="1">
<xs:element ref="selected" />
<xs:element ref="unselected" />
<xs:element ref="dimpled" />
<xs:element ref="perforated" />
</xs:choice>
<xs:attribute name="candidate" type="candidateType" />
</xs:complexType>
10.sequence元素
作用:给一组元素一个特定的序列
示例:
<xs:element name="zooAnimals">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="elephant" />
<xs:element name="bear" />
<xs:element name="giraffe" />
</xs:sequence>
</xs:complexType>
11.声明元素
声明元素:<xs:element> 声明属性:<xs:attribute>
3.Schema总结:
1.>总结:Schema
1.Schema是另一种文档类型定义,它遵循XML的语言规范。
2.Schema是可扩展的,支持命名空间。
3.Schema支持更多的数据类型与元素类型。
3.Schema用element声明元素,用attribute声明元素的属性。
4.Schema用simpleType定义简单类型,用complexType定义复杂类型。