xs:complexType

Download Report

Transcript xs:complexType

第4章 XML模式定义-- XSD
什么是Schema
SQL
XPath
数据库
XML文档数据
表结构
Schema
XML Schema是用一套预先规定的XML元素和属性
创建的,这些元素和属性定义了XML文档的结构和
内容模式。
XML Schema规定XML文档实例的结构和每个元素/
属性的数据类型。
为何需要Schema
 DTD 的局限性
– DTD不遵守XML语法(写XML文档实例时候用一
种语法,写DTD的时候用另外一种语法)
– DTD数据类型有限(与数据库数据类型不一致)
– DTD不可扩展
– DTD不支持命名空间(命名冲突)
 Schema的新特性
– Schema基于XML语法
– Schema可以用能处理XML文档的工具处理
– Schema大大扩充了数据类型,可以自定义数据类
型
– Schema支持元素的继承
– Schema支持属性组
4.1 Schema的文档结构
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://mynamespace/myschema"
elementFormDefault="unqualified">
<!--放入实际内容 -->
</xs:schema>
所有Schema文档使用schema作为其根元素
用于构造schema的元素和数据类型来自http://www.w3.org/2001/XMLSchema命名空间
本schema定义的元素和数据类型属于http://mynamespace/myschema命名空间
目标命名空间不一定遵循本Schema(若是qualified则必须遵循)
4.2 Schema的数据类型
• 简单类型
– 内置的数据类型(built-in data types)
• 基本的数据类型
• 扩展的数据类型
– 用户自定义数据类型(通过simpleType
定义)
• 复杂类型(通过complexType定义)
用户自定义的简单数据类
型的语法
<xs:simpleType name=“自定义数据类型的
名称”>
<xs:restriction base=“所基于的内置
数据类型的名称”>
自定义数据类型的内容模式
</xs:restriction>
</xs:simpleType>
Restriction中可以使用的关键字及含义
特性
描述
enumeration
在指定的数据集中选择,限定用户的选值
fractionDigits
限定最大的小数位,用于控制精度
length
指定数据的长度
maxExclusive
指定数据的最大值(小于)
maxInclusive
指定数据的最大值(小于等于)
maxLength
指定长度的最大值
minExclusive
指定最小值(大于)
minInclusive
指定最小值(大于等于)
minLength
指定最小长度
Pattern
指定数据的显示规范
复杂数据类型的声明语法
<xs:complexType name=“数据类型的名称”>
内容模型定义(包括子元素和属性的声明)
</xs:complexType>
complexType元素使用示例
<xs:complexType name="Address" >
<xs:sequence>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="zip" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
<xs:element name=“QDAddress " type=" Address "/>
complexType与simpleType区别
• simpleType类型的元素中不能包含元素
或者属性。
• 当需要声明一个元素的子元素和/或属
性时,用complexType;
• 当需要基于内置的基本数据类型定义一
个新的数据类型时,用simpleType。
4.3 模式文件中的元素声明
• element元素作用:声明一个元素
• 属性:
– name
– type
– ref
<xs:element name="cat" type="xs:string"/>
– minOccurs
<xs:element name="dog" type="xs:string"/>
– maxOccurs
<xs:element name="pets">
– fixed
<xs:complexType>
– default
<xs:choice minOccurs="0"
• 示例:
maxOccurs="unbounded">
<xs:element ref="cat"/>
<xs:element ref="dog"/>
</xs:choice>
</xs:complexType>
</xs:element>
声明元素的方法
1
使用内置的数据类型
<xs:element name="cat" type="xs:string"/>
<xs:element name="dog" type="xs:string"/>
<xs:element name="redDog" type="xs:string" substitutionGroup="dog" />
<xs:element name="brownDog" type="xs:string" substitutionGroup ="dog"
/>
声明元素的方法(续)
2
使用用户定义的simpleType
<xs:simpleType name="shapes">
<xs:restriction base="xs:string">
<xs:enumeration value="triangle"/>
<xs:enumeration value="rectangle"/>
<xs:enumeration value="square"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="geometry" type="shapes"/>
<xs:element name="geometry">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="triangle"/>
<xs:enumeration value="rectangle"/>
<xs:enumeration value="square"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
声明元素的方法(续)
3
使用complexType
<xs:element name="pets">
<xs:complexType>
<xs:choice minOccurs="0"
maxOccurs="unbounded">
<xs:element name="cat" type="xs:string"/>
<xs:element name="dog" type="xs:string"/>
</xs:choice>
</xs:complexType>
</xs:element>
4.4 模式文件中的属性声明
<xs:attribute name="name" type="type"
use="XXX" >
简单类型
复杂类型
required、
optional、
prohibited
示例:
<xs:complexType name="myComplexType">
<xs:attribute name="mybaseattribute" type="xs:string"
use="required"/>
</xs:complexType>
4.5 在XML模式中创建元
素和属性组
• sequence:指定组中的子元素按照一定顺序
进行出现
• group:用通用名组合成组
• choice:同一个时刻只能使用组中子元素的其
中之一
• all:组中的子元素在父元素内可以按照任意顺
序出现
• attributeGroup:创建属性组。
sequence元素
• 作用:给一组元素一个特定的序列
• 示例:
<xs:element name="zooAnimals">
<xs:complexType>
<xs:sequence>
<xs:element name="elephant" type="xs:string" />
<xs:element name="bear" type="xs:string" />
<xs:element name=“panda" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
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>
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>
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>
4.6 模式重用
模式的一个关键特征是支持高度重
用性。在一个模式中声明的组件能够被
另一个模式重用。
• Include元素
• Import元素
Include元素
• Include元素用来包含或引用定位在一个明确地
址的外部模式。语法:
<include id=“ID” schemaLocation=“filename”/>
• Include元素包含2个属性:
id—用来指定元素的ID。ID必须是唯一的,但
ID属性是可选择的。
schemaLocation—指定模式文件的物理地址。
目标命名空间
• Include元素允许引用任何在同一目标命名空间
的上下文中被定义的外部模式。使用schema元素的
targetNamespace属性来声明目标命名空间。
• <schema
xmlns=“http://www.w3.org/2001/XMLSchema”
targetNamespace=“www.cybershop.com/purchase”>
……..
</schema>
Import元素
• Import和include完成同样功能,但import允许访问来自多
个不同目标命名空间的外部模式。
• 语法:
<import id=“ID” namespace=“namespace”
schemaLocation=“filename”/>
• Import 元素包含3个属性:
id—用来指定元素的ID。ID必须是唯一的,但ID属性是可
选择的。
namespace—指定被引入模式所属名称空间URI。
schemaLocation—指定模式文件的物理地址。
4.7 本章小结
•
•
•
•
•
•
Schema的文档结构
XSD的数据类型
XML Schema中元素的声明语法
XML Schema中属性的声明语法
在XML模式中创建元素和属性组
模式文档的重用