xsd(XML Schema Definition)

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

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

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

@H_404_4@二是比DTD丰富和有用,

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

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

@H_404_4@五是支持命名空间。

XSD用途: @H_404_4@

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

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

XSD例子: @H_404_4@

@H_404_4@<?xml version="1.0"?>

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

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

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

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

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

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

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

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

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

@H_404_4@elementFormDefault="qualified">

@H_404_4@...

@H_404_4@...

@H_404_4@</xs:schema> @H_404_4@

@H_404_4@

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

@H_404_4@<?xml version="1.0"?>

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

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

@H_404_4@//指定XSD实例命名空间

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

@H_404_4@//XSD位置

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

@H_404_4@<to>George</to>

@H_404_4@<from>John</from>

@H_404_4@<heading>Reminder</heading>

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

@H_404_4@</note> @H_404_4@

@H_404_4@

@H_404_4@

内建的数据类型

@H_404_4@xs:string

@H_404_4@xs:decimal

@H_404_4@xs:integer

@H_404_4@xs:boolean

@H_404_4@xs:date

@H_404_4@xs:time

@H_404_4@

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

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

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

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

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

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

@H_404_4@<lastname>Smith</lastname>

@H_404_4@<age>28</age>

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

XSD 属性 @H_404_4@

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

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

@H_404_4@

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

@H_404_4@对值的限定

@H_404_4@<xs:element name="age">

@H_404_4@<xs:simpleType>

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

@H_404_4@ <xs:minInclusive value="0"/>

@H_404_4@ <xs:maxInclusive value="120"/>

@H_404_4@ </xs:restriction>

@H_404_4@</xs:simpleType>

@H_404_4@</xs:element>

@H_404_4@

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

@H_404_4@<xs:element name="car">

@H_404_4@<xs:simpleType>

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

@H_404_4@ <xs:enumeration value="Audi"/>

@H_404_4@ <xs:enumeration value="Golf"/>

@H_404_4@ <xs:enumeration value="BMW"/>

@H_404_4@ </xs:restriction>

@H_404_4@</xs:simpleType>

@H_404_4@</xs:element>

@H_404_4@或

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

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

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

@H_404_4@ <xs:enumeration value="Audi"/>

@H_404_4@ <xs:enumeration value="Golf"/>

@H_404_4@ <xs:enumeration value="BMW"/>

@H_404_4@ </xs:restriction>

@H_404_4@</xs:simpleType>

@H_404_4@

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

@H_404_4@<xs:simpleType>

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

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

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

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

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

@H_404_4@ </xs:restriction>

@H_404_4@</xs:simpleType>

@H_404_4@restriction的其他一级子元素

@H_404_4@<xs:length value="8"/>

@H_404_4@<xs:minLength value="5"/>

@H_404_4@<xs:maxLength value="8"/>

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

@H_404_4@<xs:whiteSpace value="collapse"/>

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

@H_404_4@<xs:whiteSpace value="replace"/>

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

@H_404_4@<xs:whiteSpace value="preserve"/> @H_404_4@

@H_404_4@

XSD复合元素: @H_404_4@

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

@H_404_4@<xs:complexType name="personinfo">

@H_404_4@ <xs:sequence>

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

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

@H_404_4@ </xs:sequence>

@H_404_4@</xs:complexType> @H_404_4@

@H_404_4@

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

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

@H_404_4@<xs:complexType name="personinfo">

@H_404_4@ <xs:sequence>

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

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

@H_404_4@ </xs:sequence>

@H_404_4@</xs:complexType>

@H_404_4@<xs:complexType name="fullpersoninfo">

@H_404_4@ <xs:complexContent>

@H_404_4@ <xs:extension base="personinfo">

@H_404_4@ <xs:sequence>

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

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

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

@H_404_4@ </xs:sequence>

@H_404_4@ </xs:extension>

@H_404_4@ </xs:complexContent>

@H_404_4@</xs:complexType>

@H_404_4@

XSD 复合类型指示器

@H_404_4@<xs:complexType>

@H_404_4@ <xs:sequence>

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

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

@H_404_4@ </xs:sequence>

@H_404_4@</xs:complexType>

@H_404_4@Order 指示器:

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

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

@H_404_4@Sequence

@H_404_4@Occurrence 指示器:

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

@H_404_4@minOccurs

@H_404_4@Group 指示器:

@H_404_4@Group name

@H_404_4@attributeGroup name

@H_404_4@

@H_404_4@XML例子:

@H_404_4@<person>

@H_404_4@<full_name>David Smith</full_name>

@H_404_4@<child_name>Jogn</child_name>

@H_404_4@<child_name>mike</child_name>

@H_404_4@<child_name>kyle</child_name>

@H_404_4@<child_name>mary</child_name>

@H_404_4@</person>

@H_404_4@

@H_404_4@Group例子:

@H_404_4@<xs:group name="persongroup">

@H_404_4@ <xs:sequence>

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

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

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

@H_404_4@ </xs:sequence>

@H_404_4@</xs:group>

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

@H_404_4@<xs:complexType name="personinfo">

@H_404_4@ <xs:sequence>

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

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

@H_404_4@ </xs:sequence>

@H_404_4@</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"

猜你在找的XML相关文章