<!-- Schema 组成部分
命名空间的定义 【xs:】
根元素名字和类型的定义
子元素名字和类型的定义 并说明和根元素的关系
-->
<!-- Schema 文档格式如下 文件后缀名是 xsd -->
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http/www.w3.org/2001/XMLSchema">
<!-- 文档中子元素名字和类型定义 -->
</xs:schema>
<!-- Schema 在 XML 中的引用 格式:-->
<根元素 xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"></根元素>
【数据类型
【简单数据类型
【内置数据类型
【Primitive 原始数据类型 引用时需加上 xs:
string 表示字符串
boolean 表示布尔值 真或假
decimal 表示十进制的数字用来准确定义一个值
float 表示单精度32位浮点数
double 表示双精度64位浮点数
timeDuration 表示持续时间
dateTime 代表特定时间
time 代表特定的时间 每天重复
date 代表日期
anyURI 代表一个RUI 用来定位文件
recurringDuration 表示在一个特定的间隔之后重现的持续时间
【Derived 派生数据类型 引用时需加上 xs:
integer 符号表示的十进制数 由 decimal 导出
long 表示abs(pow(2,9)/2) 范围的有符号的数字
nonNegativeInteger 表示大于或等于 0 的整数 由 integer 导出
positiveInteger 表示一个大于 0 的整数 由 nonNegativeInteger 导出
int 表示最小值为 -231 ~ 230 的一个数
time 表示每天时间重现的一个实例 由 recurringDuration 导出
date 表示从特定一天午夜开始 在下一天午夜结束的一个时间段
由 timeDuration 导出
【用户自定义的数据类型 通过 simpleType 元素 定义
常用的子元素:
enumeration 自指定的数据集中选择 限定用户的选值
fractionDigits 限定最大的小数位 用于控制精度
length 指定数据的长度
maxExclusive 指定数据的最大致(小于)
maxInclusive 指定数据的最大值(小于等于)
maxLength 指定长度的最大值
minExclusive 指定最小值(大于)
minInclusive 指定最小值(大于等于)
minLength 指定长度的最小值
Pattern 指定数据的显示规范
用户自定义的简单数据类型格式:
<xs:simpleType name="自定义数据类型的名称">
<xs:restriction base="所基于的内置数据类型的名称">
自定义数据类型的内容模式
</xs:restriction>
</xs:simpleType>
restriction 可使用的关键字及其含义:
enumeration 在指定的数据集中选择 限定用户的选值
fractionDigits 限定最大的小数位 用于控制精度
length 指定数据的长度
maxExclusive 指定数据的最大值(小于
maxInclusive 指定数据的最大值(小于等于)
maxLength 指定长度的最大值
minExclusive 指定最小值(大于)
minInclusive 指定最小值(大于等于)
minLength 指定长度的最小长度
Pattern 指定数据的显示规范
案例:
<xs:simpleType name="phoneno">
<xs:restriction base="xs:string">
<xs:length vlaue="8"/>
<xs:pattern vlaue="\d{4}-\d{3}"/>
</xs:restriction>
</xs:simpleType>
【复杂数据类型 通过 complexType 定义
语法声明:
<xs:complexType name="数据类型的名称">
内容模型定义(包括了元素和属性的声明)
</xs:complexType>
使用 attribute 元素来声明 xml 文档中的属性
格式:<xs:attribute name="属性名" default="默认值" fixed="固定值" use="optional | required" />
use 指定属性是强制还是可选的 optional(可选的) required(强制的)
【sequence 元素: 指定组中的子元素按照一定的顺序出现
格式:<xs:sequence></xs:sequence>
【choice 元素: 同一时刻只能使用组中子元素的其中之一
格式:<choice id="ID" maxOccurs="大于等于 0 的这个数 | unbounded" minOccurs="大于等于 0 的整数"></choice>
【group 元素:通过使用通用名将一组元素组合到一起
格式:<xs:group maxOccurs="大于等于 0 的整数 | undounded" minOccurs="大于等于 0 的整数" name="组名" ref="组名"></group>
maxOccurs 最大出现次数 undounded 为任意次数
minOccurs 最小出现次数 0 为可选
name 用来为组元素分配一个名字
ref 用来在复合元素中引用一个组
案例:<xs:group name="custname">
<xs:sequence>
<xs:element name="FIRSTNAME" type="xs:string" />
<xs:element name="LASTNAME" type="xs:string" />
</xs:sequence>
</xs:group>
<xs:complexType name="custtype">
<xs:sequence>
<xs:group ref="custname">
<xs:element name="Order" type="Address" />
</xs:sequence>
<xs:complexType>
【all 元素:组中的子元素按照任意顺序出现
all 元素的子元素在默认情况下是必须出现的 而且每次最多显示一次
格式:<xs:all maxOccurs="大于0的整数" minOccurs="0|1"></all>
案例: <xs:complexType name="Address">
<xs:all minOccurs="1">
<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:all>
</xs:complexType>
【attributeGroup 元素:把一组属性声明组合在一起
以便定衣服在类型是使用
格式:<xs:attributeGroup name="group-name" ref="group-name">
属性 1 的声明
属性 2 的声明
。。。。。。
属性 n 的声明
</xs:attributeGroup>
案例:<xs:element name="EMPLOYEE" type="emtype" />
<xs:complexType name="emtype">
<xs:group ref="custname" />
<xs:attributeGroup ref="depesig" />
</xs:complexType>
<xs:attributeGroup name="depesig">
<xs:attribute name="ID" type="xs:string" />
<xs:attribute name="SEX" type="xs:string" />
</xs:attributeGroup>
【include 元素用来包含或引用定位在一个明确地址的外部模式
格式:<include id="ID" schemaLocation="filename" />
schemaLocation 指定模式文件的路径
【import 元素:和 include 完成同样的功能 但 Import
允许访问来自多个不同目标名称空间模式的组件
格式:<import id="ID" namespace="namespace" schemaLocation="filename" />
案例:
<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="元素名称" type="数据类型名" default="默认值" minOccurs="nonNegativeInteger" maxOccurs="nonNegativeInteger | unbounded"/>
minOccurs 可以出现的最少次数
等于 0 该元素为可选
大于 0 至少要出现的次数
该属性是可选的
单独出现只能是 0 或 1
maxOccurs 可以出现的最多次数
为 undounded 为任意次数
该属性是可选的
单独出现只能是 1 或 多次
minOccurs 和 maxOccurs 默认值都是 1
【复杂元素声明 与 简单元素声明 类似
不同的是 type 属性值必须是一个复杂数据类型
案例:
<xs:element name="Order" type="Address" />
<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>
名为 Order 元素 必须包含四个子元素
且必须是 street、city、state、zip
来自《XML编程与应用教程(第2版)》自行整理的笔记