@H_
301_0@XSD(XML Schema Definition)是DTD(Document Type Definition)替代者的原因,
@H_
301_0@
一是据将来的条件可扩展,
@H_
301_0@
二是比DTD丰富和有用,
@H_
301_0@
三是用XML书写,使用XML语法,结构比DTD简单的多,
@H_
301_0@
四是
支持数据类型,更容易地定义数据约束(data facets),验证数据
@H_
301_0@
五是
支持命名空间。
@H_
301_0@XSD用途:
@H_
301_0@
文档设计者可以通过XSD指定一个XML文档所允许的结构和
内容,并可据此检查一个XML文档是否是有效的。
@H_
301_0@
XML Schema本身是一个XML文档,它符合XML语法结构。可以用通用的XML解析器解析它。
@H_
301_0@XSD例子:
@H_
301_0@
<?xml version="1.0"?>
@H_
301_0@
//<schema>是每一个XSD的根元素
@H_
301_0@
//
显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。
@H_
301_0@
//同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs:
@H_
301_0@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
@H_
301_0@
//被此 schema 定义的元素 (note,to,from,heading,body) 来自命名空间: "http://www.w3school.com.cn"。
@H_
301_0@
targetNamespace="http://www.w3school.com.cn"
@H_
301_0@
//默认的命名空间是 "http://www.w3school.com.cn"。
@H_
301_0@
xmlns="http://www.w3school.com.cn"
@H_
301_0@
//任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定
@H_
301_0@
elementFormDefault="qualified">
@H_
301_0@
...
@H_
301_0@
...
@H_
301_0@
</xs:schema>
@H_
301_0@
@H_
301_0@XML引用XSD例子://也可以只保留<note>,使用时通过JAXB来指定XSD
@H_
301_0@
<?xml version="1.0"?>
@H_
301_0@
//指定了默认命名空间的声明
@H_
301_0@
<note xmlns="http://www.w3school.com.cn"
@H_
301_0@
//指定XSD实例命名空间
@H_
301_0@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@H_
301_0@
//XSD位置
@H_
301_0@
xsi:schemaLocation="http://www.w3school.com.cn note.xsd">
@H_
301_0@
<to>George</to>
@H_
301_0@
<from>John</from>
@H_
301_0@
<heading>Reminder</heading>
@H_
301_0@
<body>Don't forget the meeting!</body>
@H_
301_0@
</note>
@H_
301_0@
@H_
301_0@
@H_
301_0@内建的数据类型
@H_
301_0@
xs:string
@H_
301_0@
xs:decimal
@H_
301_0@
xs:integer
@H_
301_0@
xs:boolean
@H_
301_0@
xs:date
@H_
301_0@
xs:time
@H_
301_0@
@H_
301_0@XSD 简易元素:只包含文本的元素。它不会包含任何其他的元素或
属性。
@H_
301_0@
<xs:element name="lastname" type="xs:string"/>
@H_
301_0@
<xs:element name="age" type="xs:integer"/>
@H_
301_0@
<xs:element name="dateborn" type="xs:date"/>
@H_
301_0@
<xs:element name="color" type="xs:string" default="red"/>
@H_
301_0@
<xs:element name="color" type="xs:string" fixed="red"/>
@H_
301_0@
<lastname>Smith</lastname>
@H_
301_0@
<age>28</age>
@H_
301_0@
<dateborn>1980-03-27</dateborn>
@H_
301_0@XSD
属性
@H_
301_0@
<xs:attribute name="lang" type="xs:string"/>
@H_
301_0@
<lastname lang="EN">Smith</lastname>
@H_
301_0@
@H_
301_0@XSD 限定(restriction)用于为 XML 元素或者
属性定义可接受的值。对 XML 元素的限定被称为 facet。
@H_
301_0@
对值的限定
@H_
301_0@
<xs:element name="age">
@H_
301_0@
<xs:simpleType>
@H_
301_0@
<xs:restriction base="xs:integer">
@H_
301_0@
<xs:minInclusive value="0"/>
@H_
301_0@
<xs:maxInclusive value="120"/>
@H_
301_0@
</xs:restriction>
@H_
301_0@
</xs:simpleType>
@H_
301_0@
</xs:element>
@H_
301_0@
@H_
301_0@
对一组值的限定:枚举约束(enumeration constraint)。
@H_
301_0@
<xs:element name="car">
@H_
301_0@
<xs:simpleType>
@H_
301_0@
<xs:restriction base="xs:string">
@H_
301_0@
<xs:enumeration value="Audi"/>
@H_
301_0@
<xs:enumeration value="Golf"/>
@H_
301_0@
<xs:enumeration value="BMW"/>
@H_
301_0@
</xs:restriction>
@H_
301_0@
</xs:simpleType>
@H_
301_0@
</xs:element>
@H_
301_0@
或
@H_
301_0@
<xs:element name="car" type="carType"/>
@H_
301_0@
<xs:simpleType name="carType">
//carType可以被共享使用。
@H_
301_0@
<xs:restriction base="xs:string">
@H_
301_0@
<xs:enumeration value="Audi"/>
@H_
301_0@
<xs:enumeration value="Golf"/>
@H_
301_0@
<xs:enumeration value="BMW"/>
@H_
301_0@
</xs:restriction>
@H_
301_0@
</xs:simpleType>
@H_
301_0@
@H_
301_0@
对一系列值的限定:正则/模式约束(pattern constraint)。
@H_
301_0@
<xs:simpleType>
@H_
301_0@
<xs:restriction base="xs:string">
@H_
301_0@
<xs:pattern value="[a-z]"/>
@H_
301_0@
//<xs:pattern value="[A-Z][A-Z][A-Z]"/>3个字母
@H_
301_0@
// <xs:pattern value="([a-z])*"/>
@H_
301_0@
//<xs:pattern value="male|female"/>
@H_
301_0@
</xs:restriction>
@H_
301_0@
</xs:simpleType>
@H_
301_0@
restriction的其他一级子元素
@H_
301_0@
<xs:length value="8"/>
@H_
301_0@
<xs:minLength value="5"/>
@H_
301_0@
<xs:maxLength value="8"/>
@H_
301_0@
//将移除所有空白字符(换行、回车、空格以及制表符会被替换为空格,开头和结尾的空格会被移除,而多个连续的空格会被缩减为一个单一的空格)
@H_
301_0@
<xs:whiteSpace value="collapse"/>
@H_
301_0@
//移除所有空白字符(换行、回车、空格以及制表符):
@H_
301_0@
<xs:whiteSpace value="replace"/>
@H_
301_0@
//"XML处理器"不会移除任何空白字符--------"XML处理器",browser-HTML处理器------------
@H_
301_0@
<xs:whiteSpace value="preserve"/>
@H_
301_0@
@H_
301_0@XSD复合元素:
@H_
301_0@
<xs:element name="employee" type="personinfo"/>
@H_
301_0@
<xs:complexType name="personinfo">
@H_
301_0@
<xs:sequence>
@H_
301_0@
<xs:element name="firstname" type="xs:string"/>
@H_
301_0@
<xs:element name="lastname" type="xs:string"/>
@H_
301_0@
</xs:sequence>
@H_
301_0@
</xs:complexType>
@H_
301_0@
@H_
301_0@
在已有的复合元素之上以某个复合元素为基础,然后
添加一些元素:
@H_
301_0@
<xs:element name="employee" type="fullpersoninfo"/>
@H_
301_0@
<xs:complexType name="personinfo">
@H_
301_0@
<xs:sequence>
@H_
301_0@
<xs:element name="firstname" type="xs:string"/>
@H_
301_0@
<xs:element name="lastname" type="xs:string"/>
@H_
301_0@
</xs:sequence>
@H_
301_0@
</xs:complexType>
@H_
301_0@
<xs:complexType name="fullpersoninfo">
@H_
301_0@
<xs:complexContent>
@H_
301_0@
<xs:extension base="personinfo">
@H_
301_0@
<xs:sequence>
@H_
301_0@
<xs:element name="address" type="xs:string"/>
@H_
301_0@
<xs:element name="city" type="xs:string"/>
@H_
301_0@
<xs:element name="country" type="xs:string"/>
@H_
301_0@
</xs:sequence>
@H_
301_0@
</xs:extension>
@H_
301_0@
</xs:complexContent>
@H_
301_0@
</xs:complexType>
@H_
301_0@
@H_
301_0@XSD 复合类型指示器
@H_
301_0@
<xs:complexType>
@H_
301_0@
<xs:sequence>
@H_
301_0@
<xs:element name="full_name" type="xs:string"/>
@H_
301_0@
<xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/>
@H_
301_0@
</xs:sequence>
@H_
301_0@
</xs:complexType>
@H_
301_0@
Order 指示器:
@H_
301_0@
All
//子元素可以按照任意顺序出现,且每个子元素必须只出现一次
@H_
301_0@
Choice
//只能出现一个子元素
@H_
301_0@
Sequence
@H_
301_0@
Occurrence 指示器:
@H_
301_0@
maxOccurs
//某个元素可出现的最大
次数
@H_
301_0@
minOccurs
@H_
301_0@
Group 指示器:
@H_
301_0@
Group name
@H_
301_0@
attributeGroup name
@H_
301_0@
@H_
301_0@
XML例子:
@H_
301_0@
<person>
@H_
301_0@
<full_name>David Smith</full_name>
@H_
301_0@
<child_name>Jogn</child_name>
@H_
301_0@
<child_name>mike</child_name>
@H_
301_0@
<child_name>kyle</child_name>
@H_
301_0@
<child_name>mary</child_name>
@H_
301_0@
</person>
@H_
301_0@
@H_
301_0@
Group例子:
@H_
301_0@
<xs:group name="persongroup">
@H_
301_0@
<xs:sequence>
@H_
301_0@
<xs:element name="firstname" type="xs:string"/>
@H_
301_0@
<xs:element name="lastname" type="xs:string"/>
@H_
301_0@
<xs:element name="birthday" type="xs:date"/>
@H_
301_0@
</xs:sequence>
@H_
301_0@
</xs:group>
@H_
301_0@
<xs:element name="person" type="personinfo"/>
@H_
301_0@
<xs:complexType name="personinfo">
@H_
301_0@
<xs:sequence>
@H_
301_0@
<xs:group ref="persongroup"/>
//与复合元素
功能类似<xs:extension base="personinfo">
@H_
301_0@
<xs:element name="country" type="xs:string"/>
@H_
301_0@
</xs:sequence>
@H_
301_0@
</xs:complexType>
@H_
301_0@
@H_
301_0@XML Schema
@H_
301_0@ 描述 XML 文档的结构。DTD 替代者。同DTD一样,主要是为了约束xml格式,验证是否非法,规范读写xml。
@H_
301_0@ 2001年成为 W3C 标准。也称作 XML Schema 定义(XML Schema Definition,XSD)。
@H_
301_0@ 由 XML 编写。使用 XML 语法。不必学习新的语言
@H_
301_0@ 可使用 XML 解析器来解析 Schema
文件
@H_
301_0@ 可通过 XML DOM 来处理 Schema
@H_
301_0@ 可通过 XSLT 来转换 Schema
@H_
301_0@ web.xml,spring.xml采用schema
@H_
301_0@spring头
文件schema示例解释
@H_
301_0@ <beans xmlns="http://www.springframework.org/schema/beans"
@H_
301_0@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
@H_
301_0@ xsi:schemaLocation="
@H_
301_0@ http://www.springframework.org/schema/beans
@H_
301_0@ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
@H_
301_0@ http://www.springframework.org/schema/aop
@H_
301_0@ http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
@H_
301_0@ xmlns:声明默认的命名空间
@H_
301_0@ xmlns="http://www.springframework.org/schema/beans"
@H_
301_0@ xmlns:aop="http://www.springframework.org/schema/aop"
@H_
301_0@ xmlns:xsi:声明XML Schema实例命名空间,并将xsi前缀与该命名空间绑定,这样模式处理器就可以识别xsi:schemaLocation
属性。XML Schema实例命名空间的前缀通常使用xsi。
@H_
301_0@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@H_
301_0@ xsi:schemaLocation:关联:命名空间与模式位值
@H_
301_0@ xsi:schemaLocation="
@H_
301_0@ http://www.springframework.org/schema/beans
@H_
301_0@ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
@H_
301_0@ http://www.springframework.org/schema/aop
@H_
301_0@ http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"