xsd(XML Schema Definition)

XSD(XML Schema Definition)是DTD(Document Type Definition)替代者的原因,

一是据将来的条件可扩展,

二是比DTD丰富和有用,

三是用XML书写,使用XML语法,结构比DTD简单的多,

四是支持数据类型,更容易地定义数据约束(data facets),验证数据 

五是支持命名空间。

XSD用途:

文档设计者可以通过XSD指定一个XML文档所允许的结构和内容,并可据此检查一个XML文档是否是有效的。

XML Schema本身是一个XML文档,它符合XML语法结构。可以用通用的XML解析器解析它。

XSD例子:

<?xml version="1.0"?>

//<schema>是每一个XSD的根元素

//显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。

//同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

//被此 schema 定义的元素 (note, to, from, heading, body) 来自命名空间: "http://www.w3school.com.cn"。

targetNamespace="http://www.w3school.com.cn"

//默认的命名空间是 "http://www.w3school.com.cn"。

xmlns="http://www.w3school.com.cn"

//任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定

elementFormDefault="qualified">

...

...

</xs:schema>

XML引用XSD例子://也可以只保留<note>,使用时通过JAXB来指定XSD

<?xml version="1.0"?>

//指定了默认命名空间的声明

<note xmlns="http://www.w3school.com.cn"

//指定XSD实例命名空间

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

//XSD位置

xsi:schemaLocation="http://www.w3school.com.cn note.xsd">

<to>George</to>

<from>John</from>

<heading>Reminder</heading>

<body>Don't forget the meeting!</body>

</note>

内建的数据类型

xs:string 

xs:decimal 

xs:integer 

xs:boolean 

xs:date 

xs:time 

XSD 简易元素:只包含文本的元素。它不会包含任何其他的元素或属性。

<xs:element name="lastname" type="xs:string"/>

<xs:element name="age" type="xs:integer"/>

<xs:element name="dateborn" type="xs:date"/> 

<xs:element name="color" type="xs:string" default="red"/>

<xs:element name="color" type="xs:string" fixed="red"/>

<lastname>Smith</lastname>

<age>28</age>

<dateborn>1980-03-27</dateborn>

XSD 属性

<xs:attribute name="lang" type="xs:string"/>

<lastname lang="EN">Smith</lastname>

XSD 限定(restriction)用于为 XML 元素或者属性定义可接受的值。对 XML 元素的限定被称为 facet。

对值的限定

<xs:element name="age">

<xs:simpleType>

 <xs:restriction base="xs:integer">

   <xs:minInclusive value="0"/>

   <xs:maxInclusive value="120"/>

 </xs:restriction>

</xs:simpleType>

</xs:element> 

对一组值的限定:枚举约束(enumeration constraint)。

<xs:element name="car">

<xs:simpleType>

 <xs:restriction base="xs:string">

   <xs:enumeration value="Audi"/>

   <xs:enumeration value="Golf"/>

   <xs:enumeration value="BMW"/>

 </xs:restriction>

</xs:simpleType>

</xs:element> 

<xs:element name="car" type="carType"/>

<xs:simpleType name="carType"> //carType可以被共享使用。

 <xs:restriction base="xs:string">

   <xs:enumeration value="Audi"/>

   <xs:enumeration value="Golf"/>

   <xs:enumeration value="BMW"/>

 </xs:restriction>

</xs:simpleType>

对一系列值的限定:正则/模式约束(pattern constraint)。

<xs:simpleType>

 <xs:restriction base="xs:string">

   <xs:pattern value="[a-z]"/>

   //<xs:pattern value="[A-Z][A-Z][A-Z]"/>3个字母

   // <xs:pattern value="([a-z])*"/>

   //<xs:pattern value="male|female"/>

 </xs:restriction>

</xs:simpleType>

restriction的其他一级子元素

<xs:length value="8"/>

<xs:minLength value="5"/>

<xs:maxLength value="8"/>

//将移除所有空白字符(换行、回车、空格以及制表符会被替换为空格,开头和结尾的空格会被移除,而多个连续的空格会被缩减为一个单一的空格)

<xs:whiteSpace value="collapse"/>

//移除所有空白字符(换行、回车、空格以及制表符):

<xs:whiteSpace value="replace"/>

//"XML处理器"不会移除任何空白字符--------"XML处理器",browser-HTML处理器------------

<xs:whiteSpace value="preserve"/>

XSD复合元素:

<xs:element name="employee" type="personinfo"/>

<xs:complexType name="personinfo">

 <xs:sequence>

   <xs:element name="firstname" type="xs:string"/>

   <xs:element name="lastname" type="xs:string"/>

 </xs:sequence>

</xs:complexType>

在已有的复合元素之上以某个复合元素为基础,然后添加一些元素:

<xs:element name="employee" type="fullpersoninfo"/>

<xs:complexType name="personinfo">

 <xs:sequence>

   <xs:element name="firstname" type="xs:string"/>

   <xs:element name="lastname" type="xs:string"/>

 </xs:sequence>

</xs:complexType>

<xs:complexType name="fullpersoninfo">

 <xs:complexContent>

   <xs:extension base="personinfo">

     <xs:sequence>

       <xs:element name="address" type="xs:string"/>

       <xs:element name="city" type="xs:string"/>

       <xs:element name="country" type="xs:string"/>

     </xs:sequence>

   </xs:extension>

 </xs:complexContent>

</xs:complexType>

XSD 复合类型指示器

<xs:complexType>

   <xs:sequence>

     <xs:element name="full_name" type="xs:string"/>

     <xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/>

   </xs:sequence>

  </xs:complexType>

Order 指示器:

All //子元素可以按照任意顺序出现,且每个子元素必须只出现一次

Choice //只能出现一个子元素 

Sequence 

Occurrence 指示器:

maxOccurs //某个元素可出现的最大次数

minOccurs 

Group 指示器:

Group name 

attributeGroup name 

XML例子:

<person>

<full_name>David Smith</full_name>

<child_name>Jogn</child_name>

<child_name>mike</child_name>

<child_name>kyle</child_name>

<child_name>mary</child_name>

</person>

Group例子:

<xs:group name="persongroup">

 <xs:sequence>

   <xs:element name="firstname" type="xs:string"/>

   <xs:element name="lastname" type="xs:string"/>

   <xs:element name="birthday" type="xs:date"/>

 </xs:sequence>

</xs:group>

<xs:element name="person" type="personinfo"/>

<xs:complexType name="personinfo">

 <xs:sequence>

   <xs:group ref="persongroup"/> //与复合元素功能类似<xs:extension base="personinfo">

   <xs:element name="country" type="xs:string"/>

 </xs:sequence>

</xs:complexType>

XML Schema 

        描述 XML 文档的结构。DTD 替代者。同DTD一样,主要是为了约束xml格式,验证是否非法,规范读写xml。

        2001年成为 W3C 标准。也称作 XML Schema 定义(XML Schema Definition,XSD)。

        由 XML 编写。使用 XML 语法。不必学习新的语言 

                可使用 XML 解析器来解析 Schema 文件 

                可通过 XML DOM 来处理 Schema 

                可通过 XSLT 来转换 Schema

        web.xml,spring.xml采用schema

spring头文件schema示例解释        

        <beans xmlns="http://www.springframework.org/schema/beans" 

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"

                xsi:schemaLocation="        

                http://www.springframework.org/schema/beans 

                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        

                http://www.springframework.org/schema/aop 

                http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">        

        xmlns:声明默认的命名空间

                xmlns="http://www.springframework.org/schema/beans" 

                xmlns:aop="http://www.springframework.org/schema/aop"

        xmlns:xsi:声明XML Schema实例命名空间,并将xsi前缀与该命名空间绑定,这样模式处理器就可以识别xsi:schemaLocation属性。XML Schema实例命名空间的前缀通常使用xsi。

                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

        xsi:schemaLocation:关联:命名空间与模式位值

                xsi:schemaLocation="        

                http://www.springframework.org/schema/beans 

                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        

                http://www.springframework.org/schema/aop 

                http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" 

猜你喜欢

转载自luckywnj.iteye.com/blog/1716588