xsd(XML Schema Definition)

前端之家收集整理的这篇文章主要介绍了xsd(XML Schema Definition)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@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"

猜你在找的XML相关文章